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

Side by Side Diff: scripts/slave/unittests/expect_tests/handle_train.py

Issue 220353003: Replace recipes_test.py with a new parallel expectation-based test runner. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: refactor Created 6 years, 8 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 | Annotate | Revision Log
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 time
6 import os
7 import collections
8
9 from .types import Handler
10 from .serialize import WriteNewData, DiffData, NonExistant, GetCurrentData
11
12 DirSeen = collections.namedtuple('DirSeen', 'dir')
13 WriteAction = collections.namedtuple('WriteAction', 'test why')
14 NoAction = collections.namedtuple('NoAction', 'test')
15
16 class TrainHandler(Handler):
17 """Write test expectations to disk."""
18 @classmethod
19 def add_options(cls, parser):
20 parser.add_argument(
21 '--force', action='store_true', help=(
22 'Immediately write expectations to disk instead of determining if '
23 'they contain a diff from the current expectations.'
24 ))
25
26 @classmethod
27 def gen_stage_loop(cls, _opts, tests, put_next_stage, put_result_stage):
28 dirs_seen = set()
29 for test in tests:
30 if test.expect_dir not in dirs_seen:
31 try:
32 os.makedirs(test.expect_dir)
33 except OSError:
34 pass
35 put_result_stage(DirSeen(test.expect_dir))
36 dirs_seen.add(test.expect_dir)
37 put_next_stage(test)
38
39 @classmethod
40 def run_stage_loop(cls, opts, tests_results, put_next_stage):
41 for test, result in tests_results:
42 if opts.force:
43 WriteNewData(test, result.data)
44 put_next_stage(WriteAction(test, 'forced'))
45 return
46
47 current, same_schema = GetCurrentData(test)
48 diff = DiffData(current, result.data)
49 if diff is not None or not same_schema:
50 WriteNewData(test, result.data)
51 if current is NonExistant:
52 why = 'missing'
53 elif diff:
54 why = 'diff'
55 else:
56 why = 'schema changed'
57 put_next_stage(WriteAction(test, why))
58 else:
59 put_next_stage(NoAction(test))
60
61 class ResultStageHandler(Handler.ResultStageHandler):
62 def __init__(self, opts):
63 super(TrainHandler.ResultStageHandler, self).__init__(opts)
64 self.dirs_seen = set()
65 self.files_expected = collections.defaultdict(set)
66 self.start = time.time()
67 self.num_tests = 0
68
69 def _record_expected(self, test):
70 head, tail = os.path.split(test.expect_path())
71 self.files_expected[head].add(tail)
72
73 # Handlers
74 def DirSeen(self, dirseen):
75 self.dirs_seen.add(dirseen.dir)
76
77 def NoAction(self, result):
78 self._record_expected(result.test)
79 if self.opts.verbose:
80 print '%s did not change' % result.test.name
81
82 def WriteAction(self, result):
83 self._record_expected(result.test)
84 if not self.opts.quiet:
85 test = result.test
86 name = test.expect_path() if self.opts.verbose else test.name
87 print 'Wrote %s: %s' % (name, result.why)
88
89 def finalize(self, aborted):
90 if not aborted and not self.opts.test_glob:
91 for d in self.dirs_seen:
92 expected = self.files_expected[d]
93 for f in os.listdir(d):
94 if f == 'OWNERS':
95 continue
96 if f not in expected:
97 path = os.path.join(d, f)
98 os.unlink(path)
99 if self.opts.verbose:
100 print 'Removed unexpected file', path
101 if not self.opts.quiet:
102 num_tests = sum(len(x) for x in self.files_expected.itervalues())
103 print 'Trained %d tests in %0.3fs' % (
104 num_tests, time.time() - self.start)
105
106
107
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698