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

Side by Side Diff: unittests/test_test.py

Issue 2850543002: Fix recipes.py test train when on-disk expectation files are invalid (Closed)
Patch Set: Created 3 years, 7 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 | « recipe_engine/test.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2017 The LUCI Authors. All rights reserved. 2 # Copyright 2017 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 3 # Use of this source code is governed under the Apache License, Version 2.0
4 # that can be found in the LICENSE file. 4 # that can be found in the LICENSE file.
5 5
6 import json 6 import json
7 import os 7 import os
8 import shutil 8 import shutil
9 import subprocess 9 import subprocess
10 import sys 10 import sys
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 self._run_recipes('test', 'train', '--json', self.json_path) 738 self._run_recipes('test', 'train', '--json', self.json_path)
739 expect_path = os.path.join(rw.expect_dir, 'basic.json') 739 expect_path = os.path.join(rw.expect_dir, 'basic.json')
740 with open(expect_path) as f: 740 with open(expect_path) as f:
741 expect_contents = json.load(f) 741 expect_contents = json.load(f)
742 self.assertEqual( 742 self.assertEqual(
743 [{u'cmd': [u'echo', u'bar'], u'name': u'test'}, 743 [{u'cmd': [u'echo', u'bar'], u'name': u'test'},
744 {u'status_code': 0, u'recipe_result': None, u'name': u'$result'}], 744 {u'status_code': 0, u'recipe_result': None, u'name': u'$result'}],
745 expect_contents) 745 expect_contents)
746 self.assertEqual(self.json_generator.get(), self.json_contents) 746 self.assertEqual(self.json_generator.get(), self.json_contents)
747 747
748 def test_train_invaid_json(self):
749 # 1. Initial state: recipe expectations are passing.
750 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo')
751 rw.RunStepsLines = ['pass']
752 rw.add_expectation('basic')
753 rw.write()
754 self._run_recipes('test', 'run', '--json', self.json_path)
755 self.assertEqual(self.json_generator.get(), self.json_contents)
756
757 # 2. Change the expectation and verify tests would fail.
758 expect_path = os.path.join(rw.expect_dir, 'basic.json')
759 with open(expect_path, 'w') as f:
760 f.write('\n'.join([
761 'not valid JSON',
762 '<<<<<',
763 'merge conflict',
764 '>>>>>',
765 ]))
766 with self.assertRaises(subprocess.CalledProcessError) as cm:
767 self._run_recipes('test', 'run', '--json', self.json_path)
768 self.assertIn('foo.basic failed', cm.exception.output)
769 self.assertEqual(
770 self.json_generator
771 .internal_failure('foo.basic')
772 .invalid()
773 .unused_expectation('recipes/foo.expected')
774 .unused_expectation('recipes/foo.expected/basic.json')
775 .coverage_failure('recipes/foo.py', [6])
776 .get(),
777 self.json_contents)
778
779 # 3. Make sure training the recipe succeeds and produces correct results.
780 self._run_recipes('test', 'train', '--json', self.json_path)
781 with open(expect_path) as f:
782 expect_contents = json.load(f)
783 self.assertEqual(
784 [{u'status_code': 0, u'recipe_result': None, u'name': u'$result'}],
785 expect_contents)
786 self.assertEqual(self.json_generator.get(), self.json_contents)
787
748 def test_train_checks_coverage(self): 788 def test_train_checks_coverage(self):
749 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') 789 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo')
750 rw.RunStepsLines = ['if False:', ' pass'] 790 rw.RunStepsLines = ['if False:', ' pass']
751 rw.add_expectation('basic') 791 rw.add_expectation('basic')
752 rw.write() 792 rw.write()
753 with self.assertRaises(subprocess.CalledProcessError) as cm: 793 with self.assertRaises(subprocess.CalledProcessError) as cm:
754 self._run_recipes('test', 'train', '--json', self.json_path) 794 self._run_recipes('test', 'train', '--json', self.json_path)
755 self.assertIn('FATAL: Insufficient coverage', cm.exception.output) 795 self.assertIn('FATAL: Insufficient coverage', cm.exception.output)
756 self.assertNotIn('CHECK(FAIL)', cm.exception.output) 796 self.assertNotIn('CHECK(FAIL)', cm.exception.output)
757 self.assertNotIn('foo.basic failed', cm.exception.output) 797 self.assertNotIn('foo.basic failed', cm.exception.output)
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 .check_failure('bar_check', 'bar_file', 2, 'bar_func', ['bar_args']) 1212 .check_failure('bar_check', 'bar_file', 2, 'bar_func', ['bar_args'])
1173 .internal_failure('bar_internal') 1213 .internal_failure('bar_internal')
1174 .uncovered_module('bar_module') 1214 .uncovered_module('bar_module')
1175 .unused_expectation('bar_expectation') 1215 .unused_expectation('bar_expectation')
1176 .get(), 1216 .get(),
1177 self.json_contents) 1217 self.json_contents)
1178 1218
1179 1219
1180 if __name__ == '__main__': 1220 if __name__ == '__main__':
1181 sys.exit(unittest.main()) 1221 sys.exit(unittest.main())
OLDNEW
« no previous file with comments | « recipe_engine/test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698