1 #!/usr/bin/env vpython3
2 # Copyright 2017 The Chromium Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
14 import common_merge_script_tests
16 THIS_DIR = os.path.dirname(__file__)
18 import standard_isolated_script_merge
21 TWO_COMPLETED_SHARDS = {
24 u'state': u'COMPLETED',
27 u'state': u'COMPLETED',
32 class StandardIsolatedScriptMergeTest(unittest.TestCase):
34 self.temp_dir = tempfile.mkdtemp()
38 # pylint: disable=super-with-arguments
40 shutil.rmtree(self.temp_dir)
41 super(StandardIsolatedScriptMergeTest, self).tearDown()
42 # pylint: enable=super-with-arguments
44 def _write_temp_file(self, path, content):
45 abs_path = os.path.join(self.temp_dir, path.replace('/', os.sep))
46 if not os.path.exists(os.path.dirname(abs_path)):
47 os.makedirs(os.path.dirname(abs_path))
48 with open(abs_path, 'w') as f:
49 if isinstance(content, dict):
52 assert isinstance(content, str)
56 def _stage(self, summary, files):
57 self.summary = self._write_temp_file('summary.json', summary)
58 for path, content in files.items():
59 abs_path = self._write_temp_file(path, content)
60 self.test_files.append(abs_path)
62 class OutputTest(StandardIsolatedScriptMergeTest):
63 def test_success_and_failure(self):
64 self._stage(TWO_COMPLETED_SHARDS,
68 'successes': ['fizz', 'baz'],
72 'successes': ['buzz', 'bar'],
73 'failures': ['failing_test_one']
77 output_json_file = os.path.join(self.temp_dir, 'output.json')
78 standard_isolated_script_merge.StandardIsolatedScriptMerge(
79 output_json_file, self.summary, self.test_files)
81 with open(output_json_file, 'r') as f:
82 results = json.load(f)
83 self.assertEquals(results['successes'], ['fizz', 'baz', 'buzz', 'bar'])
84 self.assertEquals(results['failures'], ['failing_test_one'])
85 self.assertTrue(results['valid'])
87 def test_missing_shard(self):
88 self._stage(TWO_COMPLETED_SHARDS,
92 'successes': ['fizz', 'baz'],
95 output_json_file = os.path.join(self.temp_dir, 'output.json')
96 standard_isolated_script_merge.StandardIsolatedScriptMerge(
97 output_json_file, self.summary, self.test_files)
99 with open(output_json_file, 'r') as f:
100 results = json.load(f)
101 self.assertEquals(results['successes'], ['fizz', 'baz'])
102 self.assertEquals(results['failures'], [])
103 self.assertTrue(results['valid'])
104 self.assertEquals(results['global_tags'], ['UNRELIABLE_RESULTS'])
105 self.assertEquals(results['missing_shards'], [1])
107 class InputParsingTest(StandardIsolatedScriptMergeTest):
108 # pylint: disable=super-with-arguments
110 super(InputParsingTest, self).setUp()
112 self.merge_test_results_args = []
113 def mock_merge_test_results(results_list):
114 self.merge_test_results_args.append(results_list)
123 'standard_isolated_script_merge.results_merger.merge_test_results',
124 side_effect=mock_merge_test_results)
126 self.addCleanup(m.stop)
127 # pylint: enable=super-with-arguments
129 def test_simple(self):
130 self._stage(TWO_COMPLETED_SHARDS,
134 'result0': ['bar', 'baz'],
138 'result1': {'foo': 'bar'}
142 output_json_file = os.path.join(self.temp_dir, 'output.json')
143 exit_code = standard_isolated_script_merge.StandardIsolatedScriptMerge(
144 output_json_file, self.summary, self.test_files)
146 self.assertEquals(0, exit_code)
162 self.merge_test_results_args)
164 def test_no_jsons(self):
170 output_json_file = os.path.join(self.temp_dir, 'output.json')
171 exit_code = standard_isolated_script_merge.StandardIsolatedScriptMerge(
172 output_json_file, self.summary, json_files)
174 self.assertEquals(0, exit_code)
175 self.assertEquals([[]], self.merge_test_results_args)
178 class CommandLineTest(common_merge_script_tests.CommandLineTest):
180 # pylint: disable=super-with-arguments
181 def __init__(self, methodName='runTest'):
182 super(CommandLineTest, self).__init__(
183 methodName, standard_isolated_script_merge)
184 # pylint: enable=super-with-arguments
187 if __name__ == '__main__':