OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import sys | |
6 import time | |
7 import collections | |
8 | |
9 from cStringIO import StringIO | |
10 | |
11 from .types import Handler | |
12 from .serialize import GetCurrentData, DiffData, NonExistant | |
13 | |
14 | |
15 Missing = collections.namedtuple('Missing', 'test') | |
16 Fail = collections.namedtuple('Fail', 'test diff') | |
17 Pass = collections.namedtuple('Pass', 'test') | |
18 | |
19 | |
20 class TestHandler(Handler): | |
21 """Run the tests.""" | |
22 @classmethod | |
23 def run_stage_loop(cls, _opts, results, put_next_stage): | |
24 for test, result in results: | |
25 current, _ = GetCurrentData(test) | |
26 if current is NonExistant: | |
27 put_next_stage(Missing(test)) | |
28 else: | |
29 diff = DiffData(current, result.data) | |
30 if not diff: | |
31 put_next_stage(Pass(test)) | |
32 else: | |
33 put_next_stage(Fail(test, diff)) | |
34 | |
35 class ResultStageHandler(Handler.ResultStageHandler): | |
36 def __init__(self, *args): | |
37 super(TestHandler.ResultStageHandler, self).__init__(*args) | |
38 self.err_out = StringIO() | |
39 self.start = time.time() | |
40 self.errors = collections.defaultdict(int) | |
41 self.num_tests = 0 | |
42 | |
43 def _emit(self, short, test, verbose): | |
agable
2014/04/03 00:38:00
This is a really weird input argument order.
| |
44 if self.opts.verbose: | |
45 print >> sys.stdout, '%s ... %s' % (test.name if test else '????', | |
46 verbose) | |
47 else: | |
48 sys.stdout.write(short) | |
49 sys.stdout.flush() | |
50 | |
51 def _add_result(self, msg_lines, test, header, category): | |
52 print >> self.err_out | |
53 print >> self.err_out, '=' * 70 | |
54 if test is not None: | |
55 print >> self.err_out, '%s: %s (%s)' % ( | |
56 header, test.name, test.expect_path()) | |
57 print >> self.err_out, '-' * 70 | |
58 if msg_lines: | |
59 print >> self.err_out, '\n'.join(msg_lines) | |
60 self.errors[category] += 1 | |
61 self.num_tests += 1 | |
62 | |
63 # Handlers | |
64 def Pass(self, p): | |
65 if not self.opts.quiet: | |
66 self._emit('.', p.test, 'ok') | |
67 self.num_tests += 1 | |
68 | |
69 def Fail(self, fail): | |
70 self._emit('F', fail.test, 'FAIL') | |
71 self._add_result(fail.diff, fail.test, 'FAIL', 'failures') | |
72 return False | |
73 | |
74 def TestError(self, test_error): | |
75 self._emit('E', test_error.test, 'ERROR') | |
76 self._add_result([test_error.message], test_error.test, 'ERROR', 'errors') | |
77 return False | |
78 | |
79 def UnknownError(self, error): | |
80 self._emit('U', None, 'UNKNOWN ERROR') | |
81 self._add_result([error.message], None, 'UNKNOWN ERROR', 'unknown_errors') | |
82 return False | |
83 | |
84 def Missing(self, missing): | |
85 self._emit('M', missing.test, 'MISSING') | |
86 self._add_result([], missing.test, 'MISSING', 'missing') | |
87 return False | |
88 | |
89 def finalize(self, aborted): | |
90 # TODO(iannucci): print summary stats (and timing info?) | |
91 buf = self.err_out.getvalue() | |
92 if buf: | |
93 print | |
94 print buf | |
95 if not self.opts.quiet: | |
96 print | |
97 print '-' * 70 | |
98 print 'Ran %d tests in %0.3fs' % ( | |
99 self.num_tests, time.time() - self.start) | |
100 print | |
101 if aborted: | |
102 print 'ABORTED' | |
103 elif self.errors: | |
104 print 'FAILED (%s)' % (', '.join('%s=%d' % i | |
105 for i in self.errors.iteritems())) | |
106 elif not self.opts.quiet: | |
107 print 'OK' | |
108 | |
OLD | NEW |