-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmixin.py
159 lines (131 loc) · 4.89 KB
/
mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from collections import ChainMap
import os
import sys
import re
import socket
from glob import glob
from .utils import reload_package
from .utils import OutputPanel
from .utils import JsonFile
from .utils import ProgressBar
import sublime
DEFAULT_SETTINGS = {
"tests_dir": "tests",
"pattern": "test*.py",
"async": False,
"deferred": False,
"legacy_runner": False, # shall not used anymore
"verbosity": 2,
"output": None,
"reload_package_on_testing": True,
"show_reload_progress": True,
"start_coverage_after_reload": False,
"generate_html_report": False,
"capture_console": False,
"failfast": False
}
def casedpath(path):
# path on Windows may not be properly cased
r = glob(re.sub(r'([^:/\\])(?=[/\\]|$)', r'[\1]', path))
return r and r[0] or path
def relative_to_spp(path):
spp = sublime.packages_path()
spp_real = casedpath(os.path.realpath(spp))
for p in [path, casedpath(os.path.realpath(path))]:
for sp in [spp, spp_real]:
if p.startswith(sp + os.sep):
return p[len(sp):]
return None
class UnitTestingMixin(object):
def package_python_version(self, package):
if sublime.version() < '4000':
return "3.3"
try:
version = sublime.load_resource("Packages/{}/.python-version".format(package))
except FileNotFoundError:
version = "3.3"
return version
@property
def current_package_name(self):
window = sublime.active_window()
view = window.active_view()
if view and view.file_name():
file_path = relative_to_spp(view.file_name())
if file_path and file_path.endswith(".py"):
return file_path.split(os.sep)[1]
folders = window.folders()
if folders and len(folders) > 0:
first_folder = relative_to_spp(folders[0])
if first_folder:
return os.path.basename(first_folder)
return None
def input_parser(self, package):
m = re.match(r'([^:]+):(.+)', package)
if m:
return m.groups()
else:
return (package, None)
def prompt_package(self, callback):
package = self.current_package_name
if not package:
package = ""
view = sublime.active_window().show_input_panel(
'Package:', package, callback, None, None)
view.run_command("select_all")
def load_unittesting_settings(self, package, options):
jfile = os.path.join(sublime.packages_path(), package, "unittesting.json")
package_settings = JsonFile(jfile).load() if os.path.exists(jfile) else {}
return ChainMap({}, options, package_settings, DEFAULT_SETTINGS)
def default_output(self, package):
outputdir = os.path.join(
sublime.packages_path(), 'User', 'UnitTesting', "tests_output")
if not os.path.isdir(outputdir):
os.makedirs(outputdir)
outfile = os.path.join(outputdir, package)
return outfile
def load_stream(self, package, settings):
tcp_port = settings.get("tcp_port")
if tcp_port is None:
output_panel = OutputPanel(
'UnitTesting', file_regex=r'File "([^"]*)", line (\d+)')
output_panel.show()
stream = output_panel
else:
conn = socket.create_connection(('localhost', tcp_port))
stream = conn.makefile('w')
return stream
def reload_package(self, package, dummy=False, show_reload_progress=False):
if show_reload_progress:
progress_bar = ProgressBar("Reloading %s" % package)
progress_bar.start()
try:
reload_package(package, dummy=dummy, verbose=True)
finally:
progress_bar.stop()
sublime.status_message("{} reloaded.".format(package))
else:
reload_package(package, dummy=dummy, verbose=False)
def remove_test_modules(self, package, tests_dir):
tests_dir = os.path.join(sublime.packages_path(), package, tests_dir)
real_tests_dir = os.path.realpath(tests_dir)
modules = {}
# make a copy of sys.modules
for mname in sys.modules:
modules[mname] = sys.modules[mname]
for mname in modules:
try:
mpath = sys.modules[mname].__path__._path[0]
except AttributeError:
try:
mpath = os.path.dirname(sys.modules[mname].__file__)
except Exception:
continue
except Exception:
continue
if os.path.realpath(mpath).startswith(real_tests_dir):
del sys.modules[mname]
# remove tests dir in sys.path
if tests_dir in sys.path:
sys.path.remove(tests_dir)
elif real_tests_dir in sys.path:
sys.path.remove(real_tests_dir)