Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: tools/testrunner/local/progress.py

Issue 1171943002: [test] Refactoring - Use subject/observer pattern for progress indicators. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Review Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/testrunner/local/execution.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2012 the V8 project authors. All rights reserved. 1 # Copyright 2012 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without 2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are 3 # modification, are permitted provided that the following conditions are
4 # met: 4 # met:
5 # 5 #
6 # * Redistributions of source code must retain the above copyright 6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer. 7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above 8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following 9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided 10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution. 11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its 12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived 13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission. 14 # from this software without specific prior written permission.
15 # 15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 28
29 from functools import wraps
29 import json 30 import json
30 import os 31 import os
31 import sys 32 import sys
32 import time 33 import time
33 34
34 from . import junit_output 35 from . import junit_output
35 36
36 37
37 ABS_PATH_PREFIX = os.getcwd() + os.sep 38 ABS_PATH_PREFIX = os.getcwd() + os.sep
38 39
39 40
40 def EscapeCommand(command): 41 def EscapeCommand(command):
41 parts = [] 42 parts = []
42 for part in command: 43 for part in command:
43 if ' ' in part: 44 if ' ' in part:
44 # Escape spaces. We may need to escape more characters for this 45 # Escape spaces. We may need to escape more characters for this
45 # to work properly. 46 # to work properly.
46 parts.append('"%s"' % part) 47 parts.append('"%s"' % part)
47 else: 48 else:
48 parts.append(part) 49 parts.append(part)
49 return " ".join(parts) 50 return " ".join(parts)
50 51
51 52
52 class ProgressIndicator(object): 53 class ProgressIndicator(object):
53 54
54 def __init__(self): 55 def __init__(self):
55 self.runner = None 56 self.runner = None
56 57
58 def SetRunner(self, runner):
59 self.runner = runner
60
57 def Starting(self): 61 def Starting(self):
58 pass 62 pass
59 63
60 def Done(self): 64 def Done(self):
61 pass 65 pass
62 66
63 def AboutToRun(self, test): 67 def AboutToRun(self, test):
64 pass 68 pass
65 69
66 def HasRun(self, test, has_unexpected_output): 70 def HasRun(self, test, has_unexpected_output):
67 pass 71 pass
68 72
69 def Heartbeat(self): 73 def Heartbeat(self):
70 pass 74 pass
71 75
72 def PrintFailureHeader(self, test): 76 def PrintFailureHeader(self, test):
73 if test.suite.IsNegativeTest(test): 77 if test.suite.IsNegativeTest(test):
74 negative_marker = '[negative] ' 78 negative_marker = '[negative] '
75 else: 79 else:
76 negative_marker = '' 80 negative_marker = ''
77 print "=== %(label)s %(negative)s===" % { 81 print "=== %(label)s %(negative)s===" % {
78 'label': test.GetLabel(), 82 'label': test.GetLabel(),
79 'negative': negative_marker 83 'negative': negative_marker
80 } 84 }
81 85
82 86
87 class IndicatorNotifier(object):
88 """Holds a list of progress indicators and notifies them all on events."""
89 def __init__(self):
90 self.indicators = []
91
92 def Register(self, indicator):
93 self.indicators.append(indicator)
94
95
96 # Forge all generic event-dispatching methods in IndicatorNotifier, which are
97 # part of the ProgressIndicator interface.
98 for func_name in ProgressIndicator.__dict__:
99 func = getattr(ProgressIndicator, func_name)
100 if callable(func) and not func.__name__.startswith('_'):
101 def wrap_functor(f):
102 @wraps(f)
103 def functor(self, *args, **kwargs):
104 """Generic event dispatcher."""
105 for indicator in self.indicators:
106 getattr(indicator, f.__name__)(*args, **kwargs)
107 return functor
108 setattr(IndicatorNotifier, func_name, wrap_functor(func))
109
110
83 class SimpleProgressIndicator(ProgressIndicator): 111 class SimpleProgressIndicator(ProgressIndicator):
84 """Abstract base class for {Verbose,Dots}ProgressIndicator""" 112 """Abstract base class for {Verbose,Dots}ProgressIndicator"""
85 113
86 def Starting(self): 114 def Starting(self):
87 print 'Running %i tests' % self.runner.total 115 print 'Running %i tests' % self.runner.total
88 116
89 def Done(self): 117 def Done(self):
90 print 118 print
91 for failed in self.runner.failed: 119 for failed in self.runner.failed:
92 self.PrintFailureHeader(failed) 120 self.PrintFailureHeader(failed)
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 'stderr': '%s', 272 'stderr': '%s',
245 } 273 }
246 super(MonochromeProgressIndicator, self).__init__(templates) 274 super(MonochromeProgressIndicator, self).__init__(templates)
247 275
248 def ClearLine(self, last_line_length): 276 def ClearLine(self, last_line_length):
249 print ("\r" + (" " * last_line_length) + "\r"), 277 print ("\r" + (" " * last_line_length) + "\r"),
250 278
251 279
252 class JUnitTestProgressIndicator(ProgressIndicator): 280 class JUnitTestProgressIndicator(ProgressIndicator):
253 281
254 def __init__(self, progress_indicator, junitout, junittestsuite): 282 def __init__(self, junitout, junittestsuite):
255 self.progress_indicator = progress_indicator
256 self.outputter = junit_output.JUnitTestOutput(junittestsuite) 283 self.outputter = junit_output.JUnitTestOutput(junittestsuite)
257 if junitout: 284 if junitout:
258 self.outfile = open(junitout, "w") 285 self.outfile = open(junitout, "w")
259 else: 286 else:
260 self.outfile = sys.stdout 287 self.outfile = sys.stdout
261 288
262 def Starting(self):
263 self.progress_indicator.runner = self.runner
264 self.progress_indicator.Starting()
265
266 def Done(self): 289 def Done(self):
267 self.progress_indicator.Done()
268 self.outputter.FinishAndWrite(self.outfile) 290 self.outputter.FinishAndWrite(self.outfile)
269 if self.outfile != sys.stdout: 291 if self.outfile != sys.stdout:
270 self.outfile.close() 292 self.outfile.close()
271 293
272 def AboutToRun(self, test):
273 self.progress_indicator.AboutToRun(test)
274
275 def HasRun(self, test, has_unexpected_output): 294 def HasRun(self, test, has_unexpected_output):
276 self.progress_indicator.HasRun(test, has_unexpected_output)
277 fail_text = "" 295 fail_text = ""
278 if has_unexpected_output: 296 if has_unexpected_output:
279 stdout = test.output.stdout.strip() 297 stdout = test.output.stdout.strip()
280 if len(stdout): 298 if len(stdout):
281 fail_text += "stdout:\n%s\n" % stdout 299 fail_text += "stdout:\n%s\n" % stdout
282 stderr = test.output.stderr.strip() 300 stderr = test.output.stderr.strip()
283 if len(stderr): 301 if len(stderr):
284 fail_text += "stderr:\n%s\n" % stderr 302 fail_text += "stderr:\n%s\n" % stderr
285 fail_text += "Command: %s" % EscapeCommand(self.runner.GetCommand(test)) 303 fail_text += "Command: %s" % EscapeCommand(self.runner.GetCommand(test))
286 if test.output.HasCrashed(): 304 if test.output.HasCrashed():
287 fail_text += "exit code: %d\n--- CRASHED ---" % test.output.exit_code 305 fail_text += "exit code: %d\n--- CRASHED ---" % test.output.exit_code
288 if test.output.HasTimedOut(): 306 if test.output.HasTimedOut():
289 fail_text += "--- TIMEOUT ---" 307 fail_text += "--- TIMEOUT ---"
290 self.outputter.HasRunTest( 308 self.outputter.HasRunTest(
291 [test.GetLabel()] + self.runner.context.mode_flags + test.flags, 309 [test.GetLabel()] + self.runner.context.mode_flags + test.flags,
292 test.duration, 310 test.duration,
293 fail_text) 311 fail_text)
294 312
295 def Heartbeat(self):
296 self.progress_indicator.Heartbeat()
297 313
298 class JsonTestProgressIndicator(ProgressIndicator): 314 class JsonTestProgressIndicator(ProgressIndicator):
299 315
300 def __init__(self, progress_indicator, json_test_results, arch, mode): 316 def __init__(self, json_test_results, arch, mode):
301 self.progress_indicator = progress_indicator
302 self.json_test_results = json_test_results 317 self.json_test_results = json_test_results
303 self.arch = arch 318 self.arch = arch
304 self.mode = mode 319 self.mode = mode
305 self.results = [] 320 self.results = []
306 self.tests = [] 321 self.tests = []
307 322
308 def Starting(self):
309 self.progress_indicator.runner = self.runner
310 self.progress_indicator.Starting()
311
312 def Done(self): 323 def Done(self):
313 self.progress_indicator.Done()
314 complete_results = [] 324 complete_results = []
315 if os.path.exists(self.json_test_results): 325 if os.path.exists(self.json_test_results):
316 with open(self.json_test_results, "r") as f: 326 with open(self.json_test_results, "r") as f:
317 # Buildbot might start out with an empty file. 327 # Buildbot might start out with an empty file.
318 complete_results = json.loads(f.read() or "[]") 328 complete_results = json.loads(f.read() or "[]")
319 329
320 # Sort tests by duration. 330 # Sort tests by duration.
321 timed_tests = [t for t in self.tests if t.duration is not None] 331 timed_tests = [t for t in self.tests if t.duration is not None]
322 timed_tests.sort(lambda a, b: cmp(b.duration, a.duration)) 332 timed_tests.sort(lambda a, b: cmp(b.duration, a.duration))
323 slowest_tests = [ 333 slowest_tests = [
324 { 334 {
325 "name": test.GetLabel(), 335 "name": test.GetLabel(),
326 "flags": test.flags, 336 "flags": test.flags,
327 "command": EscapeCommand(self.runner.GetCommand(test)).replace( 337 "command": EscapeCommand(self.runner.GetCommand(test)).replace(
328 ABS_PATH_PREFIX, ""), 338 ABS_PATH_PREFIX, ""),
329 "duration": test.duration, 339 "duration": test.duration,
330 } for test in timed_tests[:20] 340 } for test in timed_tests[:20]
331 ] 341 ]
332 342
333 complete_results.append({ 343 complete_results.append({
334 "arch": self.arch, 344 "arch": self.arch,
335 "mode": self.mode, 345 "mode": self.mode,
336 "results": self.results, 346 "results": self.results,
337 "slowest_tests": slowest_tests, 347 "slowest_tests": slowest_tests,
338 }) 348 })
339 349
340 with open(self.json_test_results, "w") as f: 350 with open(self.json_test_results, "w") as f:
341 f.write(json.dumps(complete_results)) 351 f.write(json.dumps(complete_results))
342 352
343 def AboutToRun(self, test):
344 self.progress_indicator.AboutToRun(test)
345
346 def HasRun(self, test, has_unexpected_output): 353 def HasRun(self, test, has_unexpected_output):
347 self.progress_indicator.HasRun(test, has_unexpected_output)
348 # Buffer all tests for sorting the durations in the end. 354 # Buffer all tests for sorting the durations in the end.
349 self.tests.append(test) 355 self.tests.append(test)
350 if not has_unexpected_output: 356 if not has_unexpected_output:
351 # Omit tests that run as expected. Passing tests of reruns after failures 357 # Omit tests that run as expected. Passing tests of reruns after failures
352 # will have unexpected_output to be reported here has well. 358 # will have unexpected_output to be reported here has well.
353 return 359 return
354 360
355 self.results.append({ 361 self.results.append({
356 "name": test.GetLabel(), 362 "name": test.GetLabel(),
357 "flags": test.flags, 363 "flags": test.flags,
358 "command": EscapeCommand(self.runner.GetCommand(test)).replace( 364 "command": EscapeCommand(self.runner.GetCommand(test)).replace(
359 ABS_PATH_PREFIX, ""), 365 ABS_PATH_PREFIX, ""),
360 "run": test.run, 366 "run": test.run,
361 "stdout": test.output.stdout, 367 "stdout": test.output.stdout,
362 "stderr": test.output.stderr, 368 "stderr": test.output.stderr,
363 "exit_code": test.output.exit_code, 369 "exit_code": test.output.exit_code,
364 "result": test.suite.GetOutcome(test), 370 "result": test.suite.GetOutcome(test),
365 "expected": list(test.outcomes or ["PASS"]), 371 "expected": list(test.outcomes or ["PASS"]),
366 "duration": test.duration, 372 "duration": test.duration,
367 }) 373 })
368 374
369 def Heartbeat(self):
370 self.progress_indicator.Heartbeat()
371
372 375
373 PROGRESS_INDICATORS = { 376 PROGRESS_INDICATORS = {
374 'verbose': VerboseProgressIndicator, 377 'verbose': VerboseProgressIndicator,
375 'dots': DotsProgressIndicator, 378 'dots': DotsProgressIndicator,
376 'color': ColorProgressIndicator, 379 'color': ColorProgressIndicator,
377 'mono': MonochromeProgressIndicator 380 'mono': MonochromeProgressIndicator
378 } 381 }
OLDNEW
« no previous file with comments | « tools/testrunner/local/execution.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698