-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeepTesting.py
203 lines (178 loc) · 7.73 KB
/
KeepTesting.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
import sys, sublime, sublime_plugin, time, datetime, os, threading, subprocess, re, multiprocessing, string
exe = __import__('exec')
class KeepTestingCommand(sublime_plugin.EventListener):
test_output_pattern = re.compile('.*\[TestEventLogger\]\s+(.*)')
test_status_pattern = re.compile('.*\[TestEventLogger\] ([A-Za-z0-9\._]+) > ([A-Za-z0-9_]+) ([A-Z]+).*')
error_message_start_pattern = re.compile('.*\[TestEventLogger\]\s+java\.lang\.AssertionError:.*')
error_message_trace_pattern = re.compile('.*\[TestEventLogger\]\s+at\s+.*')
compile_error_pattern = re.compile('.*\[ERROR\]\s+\[system.err\]\s+(.*)')
line_number_pattern = re.compile('.*(:[0-9]+)\)')
test_runs = []
running_tests = []
lock_path = None
project_path = None
test_results = multiprocessing.Manager().dict()
done = False
stop = False
def run_tests(self):
KeepTestingCommand.test_runs.append(self)
self.done = False
self.project_path = sublime.active_window().folders()[0]
self.lock_path = self.project_path + "/.keepTestingLock"
if self.is_running() == False:
self.start_tests()
self.check_results()
def stop_all(self):
for t in KeepTestingCommand.test_runs:
t.stop = True
def check_results(self):
if self.stop:
return
if self.done and self.is_running():
return
test_count = 0
test_ok = 0
test_fail = 0
test_skip = 0
if (len(self.test_results.keys()) != 0):
progressbar = "["
error_message = ""
keys = self.test_results.keys()
test_count = 0
test_ok = 0
test_fail = 0
for k in keys:
if k == "compile":
continue
test_count += 1
result = self.test_results[k][0]
error = self.test_results[k][1]
line_number = self.test_results[k][2]
if (result == 'PASSED'):
test_ok += 1
progressbar += '-'
elif (result == 'FAILED'):
test_fail += 1
progressbar += 'X'
error_message = k.split("|")[1]
if (len(error) > 0):
error_message += line_number + " - " + error
elif (result == 'SKIPPED'):
progressbar += 'S'
test_skip += 1
elif (result == 'STARTED'):
progressbar += 'o'
else:
print "Unknown result for " + k + ": " + result
progressbar += '?'
progressbar += "] "
if 'compile' in self.test_results:
progressbar = "Compile error: " + str(self.test_results['compile'][1])
test_fail = 1
elif test_fail > 0:
progressbar += " " + error_message
else:
progressbar += str(test_ok) + "/" + str(test_count - test_skip) + " passed"
if (test_skip > 0):
progressbar += " (" + str(test_skip) + " skipped)"
sublime.status_message(progressbar)
if self.is_running() or test_fail > 0:
sublime.set_timeout(self.check_results, 50)
def stopped_tests(self):
if self.lock_path is None:
print "Error: lock_path not set"
return
if self.is_running():
#print "stopping tests"
os.unlink(self.lock_path)
self.done = True
def start_tests(self):
if self.lock_path is None:
print "Error: lock_path not set"
return
# print "starting tests"
sublime.status_message("Running tests...")
with open (self.lock_path, 'w') as f: f.write('')
self.test_results = multiprocessing.Manager().dict()
threading.Thread(target=self.worker, args=(self.test_results,)).start()
def is_running(self):
if self.lock_path is None:
print "Error: lock_path not set"
return false
return os.path.isfile(self.lock_path)
def worker(self, result):
p = None
try:
p = subprocess.Popen(['/usr/local/bin/gradle', '--daemon', '--debug', 'clean', 'test'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=self.project_path)
out = ''
success = False
failed_tests = None
capturing_error_message = False
error_message = ""
current_key = None
last_key = None
current_test_class = None
while(True):
if self.stop:
break
retcode = p.poll()
line = p.stdout.readline()
out += line
m = KeepTestingCommand.test_status_pattern.match(line)
if m:
current_test_class = m.group(1)
current_key = m.group(1) + "|" + m.group(2)
result[current_key] = [m.group(3),'','']
error_message = ''
m = KeepTestingCommand.compile_error_pattern.match(line)
if m and "Note:" not in m.group(1):
compile_error = string.replace(m.group(1), self.project_path, '')
compile_error = string.replace(compile_error, '/src/main/java/', '')
compile_error = string.replace(compile_error, '/', '.')
result['compile'] = ['COMPILERERROR',compile_error,'']
break
elif (KeepTestingCommand.error_message_start_pattern.match(line)):
capturing_error_message = True
elif (KeepTestingCommand.error_message_trace_pattern.match(line)):
capturing_error_message = False
result[current_key] = [result[current_key][0], error_message, result[current_key][2]]
if current_test_class in line:
m = KeepTestingCommand.line_number_pattern.match(line)
if m:
result[current_key] = [result[current_key][0], result[current_key][1], m.group(1)]
elif capturing_error_message:
print line
m = KeepTestingCommand.test_output_pattern.match(line)
if m:
error_message += " " + m.group(1)
if (retcode is not None):
break
self.stopped_tests()
except:
print "[KeepTesting] [Error] : " + "Unexpected error, killing process. ", sys.exc_info()[0]
self.stop_all()
if p is not None:
p.kill()
self.stopped_tests()
return
def log_debug(self, message):
print "[KeepTesting] [Debug] : " + str(message)
def on_post_save(self, view):
activated = sublime.load_settings("Preferences.sublime-settings").get("keep_testing", False)
if activated is False:
return
if (len(sublime.active_window().folders()) == 0):
self.log_debug("No folder open")
self.stop_all()
return
if (os.path.isfile(sublime.active_window().folders()[0] + "/build.gradle") == False):
self.log_debug("Current folder doesn't have build.gradle")
self.stop_all()
return
if '..' in os.path.relpath(view.file_name(), sublime.active_window().folders()[0]):
self.log_debug("Current file isn't inside current folder")
self.stop_all()
return
self.log_debug("Running tests")
cmd = KeepTestingCommand()
sublime.set_timeout(cmd.run_tests, 1)