Repository navigation
Expand file tree
/
Copy pathbase.py
More file actions
213 lines (175 loc) · 6.26 KB
/
Copy pathbase.py
File metadata and controls
213 lines (175 loc) · 6.26 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import re
import sublime
import sublime_plugin
import threading
from collections import ChainMap, deque
from fnmatch import fnmatch
from glob import glob
DEFAULT_SETTINGS = {
# input
"tests_dir": "tests",
"pattern": "test*.py",
# runner
"async": False, # deprecated
"deferred": True,
"condition_timeout": 4000,
"failfast": False,
# output
"output": None,
"verbosity": 2,
"warnings": "default",
"capture_console": False,
# reloader
"reload_package_on_testing": True,
# coverage
"coverage": False,
"coverage_on_worker_thread": False, # experimental
"generate_html_report": False,
"generate_xml_report": False,
}
DONE_MESSAGE = "UnitTesting: Done.\n"
def have_plugin_host_33():
try:
return have_plugin_host_33.result
except Exception:
plugin_host = os.path.join(
os.path.dirname(sublime.executable_path()),
(
"plugin_host-3.3.exe"
if sublime.platform() == "windows"
else "plugin_host-3.3"
),
)
settings = sublime.load_settings("Preferences.sublime-settings")
have_plugin_host_33.result = os.path.isfile(plugin_host) and not settings.get(
"disable_plugin_host_3.3", False
)
return have_plugin_host_33.result
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 OutputPanel:
def __init__(
self,
window,
name,
file_regex="",
line_regex="",
base_dir=None,
word_wrap=False,
line_numbers=False,
gutter=False,
scroll_past_end=False,
):
self.name = name
self.window = window
self.output_view = window.create_output_panel(name)
# default to the current file directory
if not base_dir:
view = window.active_view()
if view:
file_name = view.file_name()
if file_name:
base_dir = os.path.dirname(file_name)
settings = self.output_view.settings()
settings.set("result_file_regex", file_regex)
settings.set("result_line_regex", line_regex)
settings.set("result_base_dir", base_dir)
settings.set("word_wrap", word_wrap)
settings.set("line_numbers", line_numbers)
settings.set("gutter", gutter)
settings.set("scroll_past_end", scroll_past_end)
# make sure to apply settings
self.output_view = window.create_output_panel(name)
self.output_view.assign_syntax("unit-testing-test-result.sublime-syntax")
self.output_view.set_read_only(True)
self.closed = False
self.text_queue_lock = threading.Lock()
self.text_queue = deque()
def write(self, s):
with self.text_queue_lock:
self.text_queue.append(s)
def writeln(self, s):
self.write(s + "\n")
def _write(self):
text = ""
with self.text_queue_lock:
while self.text_queue:
text += self.text_queue.popleft()
self.output_view.run_command("append", {"characters": text, "force": True})
self.output_view.show(self.output_view.size())
def flush(self):
self._write()
def show(self):
self.window.run_command("show_panel", {"panel": "output." + self.name})
def close(self):
self.flush()
self.closed = True
class StdioSplitter:
def __init__(self, io, stream):
self.io = io
self.stream = stream
def write(self, data):
self.io.write(data)
self.stream.write(data)
def writeln(self, s):
self.write(s + "\n")
def flush(self):
self.io.flush()
self.stream.flush()
class BaseUnittestingCommand(sublime_plugin.WindowCommand):
def current_package_name(self):
view = self.window.active_view()
if view and view.file_name():
file_path = relative_to_spp(view.file_name())
if file_path:
return file_path.split(os.sep)[1]
folders = self.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 current_test_file(self, pattern):
view = self.window.active_view()
if view:
current_file = os.path.basename(view.file_name() or "")
if current_file and fnmatch(current_file, pattern):
self.window.settings().set("UnitTesting.last_test_file", current_file)
return current_file
return self.window.settings().get("UnitTesting.last_test_file")
def load_stream(self, package, settings):
output = settings["output"]
if not output or output == "<panel>":
output_panel = OutputPanel(
self.window, "UnitTesting", file_regex=r'File "([^"]*)", line (\d+)'
)
output_panel.show()
return output_panel
if not os.path.isabs(output):
output = os.path.join(sublime.packages_path(), package, output)
os.makedirs(os.path.dirname(output), exist_ok=True)
return open(output, "w", encoding="utf-8")
def load_unittesting_settings(self, package, options):
file_name = os.path.join(sublime.packages_path(), package, "unittesting.json")
try:
with open(file_name, "r", encoding="utf-8") as fp:
json_data = sublime.decode_value(fp.read())
if not isinstance(json_data, dict):
raise ValueError("unittesting.json content must be an object!")
except FileNotFoundError:
json_data = {}
except Exception as e:
json_data = {}
print("ERROR: Unable to load 'unittesting.json'\n ", str(e))
return ChainMap(options, json_data, DEFAULT_SETTINGS)