-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresult.py
More file actions
74 lines (63 loc) · 2.38 KB
/
Copy pathresult.py
File metadata and controls
74 lines (63 loc) · 2.38 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
from tmc.hmac_writer import write_hmac
from unittest.runner import TextTestResult
from .points import _parse_points, _name_test
from copy import deepcopy
import atexit
import gc
import json
import traceback
module_secret = None
# Copy of the results for the Python editor
results = []
class TMCResult(TextTestResult):
def __init__(self, stream, descriptions, verbosity):
self.__results = []
global module_secret
secret = module_secret
del module_secret
that = self
def write_output():
nonlocal that
nonlocal secret
output = json.dumps(that.__results)
if secret is not None:
write_hmac(secret, output)
with open(".tmc_test_results.json", "w", encoding="utf-8") as text_file:
text_file.write(output)
atexit.register(write_output)
super(TMCResult, self).__init__(stream, descriptions, verbosity)
def startTest(self, test):
# It is easy to use a lot of memory in a unit test and if we don't force garbage collection here, we can go over the memory limit in the sandbox
gc.collect()
super(TMCResult, self).startTest(test)
def stopTest(self, test):
super(TMCResult, self).stopTest(test)
# It is easy to use a lot of memory in a unit test and if we don't force garbage collection here, we can go over the memory limit in the sandbox
gc.collect()
def addSuccess(self, test):
super(TMCResult, self).addSuccess(test)
self.addResult(test, 'passed')
def addFailure(self, test, err):
super(TMCResult, self).addFailure(test, err)
self.addResult(test, 'failed', err)
def addError(self, test, err):
super(TMCResult, self).addError(test, err)
self.addResult(test, 'errored', err)
def addResult(self, test, status, err=None):
global results
points = _parse_points(test)
message = ""
backtrace = []
if err is not None:
message = str(err[1])
backtrace = traceback.format_tb(err[2])
details = {
'name': _name_test(test),
'status': status,
'message': message,
'passed': status == 'passed',
'points': points,
'backtrace': backtrace
}
self.__results.append(details)
results = deepcopy(self.__results)