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

Side by Side Diff: expect_tests/handle_train.py

Issue 412773002: Copy expect_tests from build (Closed) Base URL: https://chromium.googlesource.com/infra/testing/expect_tests@master
Patch Set: Fancy ast walker Created 6 years, 5 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 | « expect_tests/handle_test.py ('k') | expect_tests/main.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 collections
6 import os
7 import sys
8 import time
9
10 from expect_tests.type_definitions import Handler, MultiTest
11 from expect_tests.serialize import (
12 WriteNewData, DiffData, NonExistant, GetCurrentData)
13
14
15 DirSeen = collections.namedtuple('DirSeen', 'dir')
16 ForcedWriteAction = collections.namedtuple('ForcedWriteAction', 'test')
17 DiffWriteAction = collections.namedtuple('DiffWriteAction', 'test')
18 SchemaDiffWriteAction = collections.namedtuple('SchemaDiffWriteAction', 'test')
19 MissingWriteAction = collections.namedtuple('MissingWriteAction', 'test')
20 NoAction = collections.namedtuple('NoAction', 'test')
21
22
23 class TrainHandler(Handler):
24 """Write test expectations to disk."""
25 @classmethod
26 def add_options(cls, parser):
27 parser.add_argument(
28 '--force', action='store_true', help=(
29 'Immediately write expectations to disk instead of determining if '
30 'they contain a diff from the current expectations.'
31 ))
32
33 @classmethod
34 def gen_stage_loop(cls, _opts, tests, put_next_stage, put_result_stage):
35 dirs_seen = {None}
36 for test in tests:
37 if isinstance(test, MultiTest):
38 subtests = test.tests
39 else:
40 subtests = [test]
41 for subtest in subtests:
42 if subtest.expect_dir not in dirs_seen:
43 try:
44 os.makedirs(subtest.expect_dir)
45 except OSError:
46 pass
47 put_result_stage(DirSeen(subtest.expect_dir))
48 dirs_seen.add(subtest.expect_dir)
49 put_next_stage(test)
50
51 @classmethod
52 def run_stage_loop(cls, opts, tests_results, put_next_stage):
53 for test, result, _ in tests_results:
54 if opts.force:
55 WriteNewData(test, result.data)
56 put_next_stage(ForcedWriteAction(test))
57 continue
58
59 try:
60 current, same_schema = GetCurrentData(test)
61 except ValueError:
62 current = NonExistant
63 same_schema = False
64 diff = DiffData(current, result.data)
65 if diff is not None or not same_schema:
66 WriteNewData(test, result.data)
67 if current is NonExistant:
68 put_next_stage(MissingWriteAction(test))
69 elif diff:
70 put_next_stage(DiffWriteAction(test))
71 else:
72 put_next_stage(SchemaDiffWriteAction(test))
73 else:
74 put_next_stage(NoAction(test))
75
76 class ResultStageHandler(Handler.ResultStageHandler):
77 def __init__(self, opts):
78 super(TrainHandler.ResultStageHandler, self).__init__(opts)
79 self.dirs_seen = set()
80 self.files_expected = collections.defaultdict(set)
81 self.start = time.time()
82 self.num_tests = 0
83 self.verbose_actions = []
84 self.normal_actions = []
85
86 def _record_expected(self, test, indicator):
87 self.num_tests += 1
88 if not self.opts.quiet:
89 sys.stdout.write(indicator)
90 sys.stdout.flush()
91 if test.expect_path() is not None:
92 head, tail = os.path.split(test.expect_path())
93 self.files_expected[head].add(tail)
94
95 def _record_write(self, test, indicator, why):
96 self._record_expected(test, indicator)
97 if test.expect_path() is not None:
98 name = test.expect_path() if self.opts.verbose else test.name
99 self.normal_actions.append('Wrote %s: %s' % (name, why))
100
101 def handle_DirSeen(self, dirseen):
102 self.dirs_seen.add(dirseen.dir)
103
104 def handle_NoAction(self, result):
105 self._record_expected(result.test, '.')
106 self.verbose_actions.append('%s did not change' % result.test.name)
107
108 def handle_ForcedWriteAction(self, result):
109 self._record_write(result.test, 'F', 'forced')
110
111 def handle_DiffWriteAction(self, result):
112 self._record_write(result.test, 'D', 'diff')
113
114 def handle_SchemaDiffWriteAction(self, result):
115 self._record_write(result.test, 'S', 'schema changed')
116
117 def handle_MissingWriteAction(self, result):
118 self._record_write(result.test, 'M', 'missing')
119
120 def handle_TestError(self, result):
121 self._record_expected(result.test, 'E')
122 self.normal_actions.append('%s failed: %s' %
123 (result.test.name, result.message))
124
125 def finalize(self, aborted):
126 super(TrainHandler.ResultStageHandler, self).finalize(aborted)
127
128 if not aborted and not self.opts.test_glob:
129 for d in self.dirs_seen:
130 expected = self.files_expected[d]
131 for f in os.listdir(d):
132 # Skip OWNERS files and files beginning with a '.' (like '.svn')
133 if f == 'OWNERS' or f[0] == '.':
134 continue
135 if f not in expected:
136 path = os.path.join(d, f)
137 os.unlink(path)
138 if self.opts.verbose:
139 print 'Removed unexpected file', path
140
141 if not self.opts.quiet:
142 print
143
144 if self.normal_actions:
145 print '\n'.join(self.normal_actions)
146 if self.opts.verbose:
147 if self.verbose_actions:
148 print '\n'.join(self.verbose_actions)
149
150 trained = sum(len(x) for x in self.files_expected.itervalues())
151 print '-' * 70
152 print 'Trained %d tests in %0.3fs (ran %d tests)' % (
153 trained, time.time() - self.start, self.num_tests)
OLDNEW
« no previous file with comments | « expect_tests/handle_test.py ('k') | expect_tests/main.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698