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