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

Side by Side Diff: unittests/test_test.py

Issue 2845133002: Add friendly error message when GenTests is missing or misspelled (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
« recipe_engine/test.py ('K') | « 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 for d in dirs: 63 for d in dirs:
64 if not os.path.exists(d): 64 if not os.path.exists(d):
65 os.makedirs(d) 65 os.makedirs(d)
66 with open(os.path.join(self.recipes_dir, '%s.py' % self.name), 'w') as f: 66 with open(os.path.join(self.recipes_dir, '%s.py' % self.name), 'w') as f:
67 f.write('\n'.join([ 67 f.write('\n'.join([
68 'from recipe_engine import post_process', 68 'from recipe_engine import post_process',
69 '', 69 '',
70 'DEPS = %r' % self.DEPS, 70 'DEPS = %r' % self.DEPS,
71 '', 71 '',
72 'def RunSteps(api):', 72 'def RunSteps(api):',
73 ] + [' %s' % l for l in self.RunStepsLines] + [ 73 ] + [' %s' % l for l in self.RunStepsLines] + ['']))
74 '', 74 if self.GenTestsLines:
75 'def GenTests(api):', 75 f.write('\n'.join([
76 ] + [' %s' % l for l in self.GenTestsLines])) 76 '',
77 'def GenTests(api):',
78 ] + [' %s' % l for l in self.GenTestsLines]))
77 for test_name, test_contents in self.expectations.iteritems(): 79 for test_name, test_contents in self.expectations.iteritems():
78 name = ''.join('_' if c in '<>:"\\/|?*\0' else c for c in test_name) 80 name = ''.join('_' if c in '<>:"\\/|?*\0' else c for c in test_name)
79 with open(os.path.join(self.expect_dir, '%s.json' % name), 'w') as f: 81 with open(os.path.join(self.expect_dir, '%s.json' % name), 'w') as f:
80 json.dump(test_contents, f) 82 json.dump(test_contents, f)
81 83
82 84
83 class RecipeModuleWriter(object): 85 class RecipeModuleWriter(object):
84 """Helper to write a recipe module for tests.""" 86 """Helper to write a recipe module for tests."""
85 87
86 def __init__(self, root_dir, name): 88 def __init__(self, root_dir, name):
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 396
395 def test_test_expectation_pass(self): 397 def test_test_expectation_pass(self):
396 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') 398 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo')
397 rw.DEPS = ['recipe_engine/step'] 399 rw.DEPS = ['recipe_engine/step']
398 rw.RunStepsLines = ['api.step("test", ["echo", "bar"])'] 400 rw.RunStepsLines = ['api.step("test", ["echo", "bar"])']
399 rw.add_expectation('basic', [{'cmd': ['echo', 'bar'], 'name': 'test'}]) 401 rw.add_expectation('basic', [{'cmd': ['echo', 'bar'], 'name': 'test'}])
400 rw.write() 402 rw.write()
401 self._run_recipes('test', 'run', '--json', self.json_path) 403 self._run_recipes('test', 'run', '--json', self.json_path)
402 self.assertEqual(self.json_generator.get(), self.json_contents) 404 self.assertEqual(self.json_generator.get(), self.json_contents)
403 405
406 def test_test_missing_gentests(self):
407 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo')
408 rw.RunStepsLines = ['pass']
409 rw.GenTestsLines = None
410 rw.add_expectation('basic')
411 rw.write()
412 with self.assertRaises(subprocess.CalledProcessError) as cm:
413 self._run_recipes('test', 'run', '--json', self.json_path)
414 self.assertIn('Missing or misspelled GenTests function',
415 cm.exception.output)
416
404 def test_test_recipe_not_covered(self): 417 def test_test_recipe_not_covered(self):
405 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') 418 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo')
406 rw.RunStepsLines = ['if False:', ' pass'] 419 rw.RunStepsLines = ['if False:', ' pass']
407 rw.add_expectation('basic') 420 rw.add_expectation('basic')
408 rw.write() 421 rw.write()
409 with self.assertRaises(subprocess.CalledProcessError) as cm: 422 with self.assertRaises(subprocess.CalledProcessError) as cm:
410 self._run_recipes('test', 'run', '--json', self.json_path) 423 self._run_recipes('test', 'run', '--json', self.json_path)
411 self.assertIn('FATAL: Insufficient coverage', cm.exception.output) 424 self.assertIn('FATAL: Insufficient coverage', cm.exception.output)
412 self.assertNotIn('CHECK(FAIL)', cm.exception.output) 425 self.assertNotIn('CHECK(FAIL)', cm.exception.output)
413 self.assertNotIn('foo.basic failed', cm.exception.output) 426 self.assertNotIn('foo.basic failed', cm.exception.output)
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 .check_failure('bar_check', 'bar_file', 2, 'bar_func', ['bar_args']) 1185 .check_failure('bar_check', 'bar_file', 2, 'bar_func', ['bar_args'])
1173 .internal_failure('bar_internal') 1186 .internal_failure('bar_internal')
1174 .uncovered_module('bar_module') 1187 .uncovered_module('bar_module')
1175 .unused_expectation('bar_expectation') 1188 .unused_expectation('bar_expectation')
1176 .get(), 1189 .get(),
1177 self.json_contents) 1190 self.json_contents)
1178 1191
1179 1192
1180 if __name__ == '__main__': 1193 if __name__ == '__main__':
1181 sys.exit(unittest.main()) 1194 sys.exit(unittest.main())
OLDNEW
« recipe_engine/test.py ('K') | « recipe_engine/test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698