-
Notifications
You must be signed in to change notification settings - Fork 6
/
pytest_exec.py
331 lines (256 loc) · 9.91 KB
/
pytest_exec.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sublime
from collections import defaultdict
import functools
import os
import re
import sys
from .matchers import Matchers
from Default import exec
MYPY = False
if MYPY:
from typing import Callable
Filename = str
Line = int
Text = str
TB_MODE = re.compile(r"tb[= ](.*?)\s")
SUBLIME4 = int(sublime.version()) >= 4000
def get_trace_back_mode(cmd):
# type: (str) -> str
"""Parses cmd and returns a trace back mode"""
match = TB_MODE.search(' '.join(cmd))
return match.group(1) if match else 'auto'
def broadcast(event, message=None):
sublime.active_window().run_command(event, message)
def get_report_file():
path = os.path.join(sublime.cache_path(), 'PyTest')
if not os.path.isdir(path):
os.makedirs(path)
return os.path.join(path, 'last-run.xml')
class _PytestExecCommand(exec.ExecCommand):
def run(self, **kw):
mode = self._tb_mode = get_trace_back_mode(kw['cmd'])
# For the line mode, we don't get useful reports at all.
# Note that if the user already wants a xml-report, he has bad luck,
# bc the last `--junit-xml` wins.
if mode != 'line':
kw['cmd'] += [
'--junit-xml={}'.format(get_report_file()),
'-o', 'junit_family=legacy'
]
broadcast('pytest_start', {
'mode': mode,
'cmd': kw['cmd']
})
super().run(**kw)
if self.proc is None:
broadcast("pytest_exec_failed")
self.show_errors_inline = False
# output_view cannot be dumped through broadcast,
# so we go the ugly mile
from . import PyTest
PyTest.State['pytest_view'] = self.output_view
def _on_finish(self, proc):
view = self.output_view
output = get_whole_text(view).strip()
last_line = output[output.rfind('\n'):]
summary = ''
matches = re.finditer(r' ([\d]+) ', last_line)
if matches:
test_count = sum(int(m.group(1)) for m in matches)
try:
failure_summary = (
"%s. "
% re.search(r'\d+ failed', last_line).group(0) # type: ignore[union-attr]
)
except AttributeError:
failure_summary = ''
summary = "Ran %s tests. %s" % (test_count, failure_summary)
summary += last_line.replace('=', '')
exit_code = proc.exit_code()
failures = exit_code is not None and exit_code > 0
if failures:
broadcast("pytest_will_fail")
broadcast('pytest_finished', {
"summary": summary,
"failures": failures
})
# This is a 'best' guess. Maybe we should parse the output for the
# `rootdir` pytest uses.
base_dir = view.settings().get('result_base_dir')
# For the 'line' go with output regex parsing bc the reporter
# isn't useful at all.
if self._tb_mode == 'line':
sublime.set_timeout_async(
functools.partial(
parse_output, output, base_dir, Matchers[self._tb_mode]))
else:
sublime.set_timeout_async(
functools.partial(
parse_result, base_dir, Matchers[self._tb_mode]))
def _on_data(self, characters):
# actually only relevant with instafail
if characters.find('\n') >= 0:
# broadcast('pytest_remember_errors', {
# "errors": parse_output(
# self.output_view, Matchers[self._tb_mode]),
# })
pass
else:
if characters in 'FX':
broadcast("pytest_will_fail")
broadcast('pytest_still_running')
if SUBLIME4:
class PytestExecCommand(_PytestExecCommand):
def on_finished(self, proc):
super().on_finished(proc)
self._on_finish(proc)
def write(self, characters):
super().write(characters)
self._on_data(characters)
else:
class PytestExecCommand(_PytestExecCommand): # type: ignore[no-redef]
def finish(self, proc):
super().finish(proc)
self._on_finish(proc)
def service_text_queue(self):
self.text_queue_lock.acquire()
is_empty = False
try:
if len(self.text_queue) == 0:
# this can happen if a new build was started, which will clear
# the text_queue
return
characters = self.text_queue.popleft()
is_empty = (len(self.text_queue) == 0)
finally:
self.text_queue_lock.release()
self.output_view.run_command(
'append',
{'characters': characters, 'force': True, 'scroll_to_end': False})
self._on_data(characters)
if not is_empty:
sublime.set_timeout(self.service_text_queue, 1)
def get_whole_text(view):
# type: (sublime.View) -> str
reg = sublime.Region(0, view.size())
return view.substr(reg)
def parse_result(base_dir, parse_traceback):
from lxml import etree
from . import matchers
fullname = functools.partial(os.path.join, base_dir)
def resolve_path(fpath):
return os.path.join(base_dir, relative_filename(base_dir, fpath))
tree = etree.parse(get_report_file())
testcases = tree.xpath('//testcase[failure or error]')
all_tracebacks = []
for tc in testcases:
tracebacks = []
rel_file = relative_filename(base_dir, tc.attrib['file'])
file = fullname(rel_file)
testcase = get_testcase(tc, file, rel_file)
failure = tc.find('failure')
if failure is not None:
if 'XPASS' in failure.attrib['message']:
synthetic_traceback = {
'file': file,
'line': int(tc.attrib['line']) + 1,
'text': failure.attrib['message']
}
tracebacks.append(synthetic_traceback)
else:
f_tracebacks = parse_traceback(
failure.text, resolve_path, testcase)
end = f_tracebacks[-1]
culprit = (matchers.get_culprit(end['text']) or
failure.attrib['message'])
# For long tracebacks, we place the culprit right at the top
# which should be the failing test
if culprit and len(f_tracebacks) > 1:
head = f_tracebacks[0]
prefix = 'E ' if not culprit.startswith('E ') else ''
head['text'] = prefix + culprit + '\n' + head['text']
tracebacks.extend(f_tracebacks)
error = tc.find('error')
if error is not None:
e_tracebacks = parse_traceback(error.text, resolve_path, testcase)
end = e_tracebacks[-1]
culprit = matchers.get_culprit(end['text'])
if culprit and len(e_tracebacks) > 1:
head = e_tracebacks[0]
head['text'] = 'E ' + culprit + '\n' + head['text']
# For errors in the fixtures ("at teardown" etc.), we place a
# synthetic marker at the failing test
synthetic_traceback = {
'file': file,
'line': int(tc.attrib.get('line', 0)) + 1,
'text': error.attrib['message'] + ':\n' + (culprit or error.text)
}
tracebacks.append(synthetic_traceback)
tracebacks.extend(e_tracebacks)
system_out = tc.find('system-out')
if system_out is not None:
head = tracebacks[0]
head['text'] = (
head['text'] + '\n------ Output ------\n' + system_out.text)
all_tracebacks.extend(tracebacks)
errs_by_file = defaultdict(list)
for tbck in all_tracebacks:
errs_by_file[tbck['file']].append(tbck)
broadcast('pytest_remember_errors', {
"errors": errs_by_file,
})
def get_testcase(testcase, file, rel_file):
name = testcase.attrib['name']
classname = testcase.attrib['classname']
root, _ = os.path.splitext(rel_file)
dotted_filepath = root.replace(os.path.sep, '.')
if classname == dotted_filepath:
return file + "::" + name
elif classname.startswith(dotted_filepath):
classname = classname[len(dotted_filepath) + 1:]
classname = classname.replace('.', '::').replace('()', '')
return file + '::' + classname + '::' + name
return ''
def parse_output(text, base_dir, get_matches):
# type: (str, str, Callable) -> None
fullname = functools.partial(os.path.join, base_dir)
matches = get_matches(text, fullname)
errs_by_file = defaultdict(list)
for tbck in matches:
errs_by_file[tbck['file']].append(tbck)
broadcast('pytest_remember_errors', {
"errors": errs_by_file,
})
def relative_filename(base, file):
# older py.test always returned a relative path
if not os.path.isabs(file):
return file
if os.path.commonprefix([base, file]) == base:
return os.path.relpath(file, base)
real_base = resolve_path(base)
if os.path.commonprefix([real_base, file]) == real_base:
return os.path.relpath(file, real_base)
# wrong base, still return something
print('wrong base:', base)
print('filename:', file)
return file
if (
sys.platform == "win32"
and sys.version_info < (3, 8)
and sys.getwindowsversion()[:2] >= (6, 0)
):
try:
from nt import _getfinalpathname
except ImportError:
resolve_path = os.path.realpath
else:
def resolve_path(path):
# type: (str) -> str
rpath = _getfinalpathname(path)
if rpath.startswith("\\\\?\\"):
rpath = rpath[4:]
if rpath.startswith("UNC\\"):
rpath = "\\" + rpath[3:]
return rpath
else:
resolve_path = os.path.realpath