| OLD | NEW |
| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 dirs.append(self.expect_dir) | 62 dirs.append(self.expect_dir) |
| 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):', | |
| 73 ] + [' %s' % l for l in self.RunStepsLines] + [ | |
| 74 '', | 72 '', |
| 75 'def GenTests(api):', | 73 ])) |
| 76 ] + [' %s' % l for l in self.GenTestsLines])) | 74 if self.RunStepsLines: |
| 75 f.write('\n'.join([ |
| 76 'def RunSteps(api):', |
| 77 ] + [' %s' % l for l in self.RunStepsLines] + [''])) |
| 78 if self.GenTestsLines: |
| 79 f.write('\n'.join([ |
| 80 '', |
| 81 'def GenTests(api):', |
| 82 ] + [' %s' % l for l in self.GenTestsLines])) |
| 77 for test_name, test_contents in self.expectations.iteritems(): | 83 for test_name, test_contents in self.expectations.iteritems(): |
| 78 name = ''.join('_' if c in '<>:"\\/|?*\0' else c for c in test_name) | 84 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: | 85 with open(os.path.join(self.expect_dir, '%s.json' % name), 'w') as f: |
| 80 json.dump(test_contents, f) | 86 json.dump(test_contents, f) |
| 81 | 87 |
| 82 | 88 |
| 83 class RecipeModuleWriter(object): | 89 class RecipeModuleWriter(object): |
| 84 """Helper to write a recipe module for tests.""" | 90 """Helper to write a recipe module for tests.""" |
| 85 | 91 |
| 86 def __init__(self, root_dir, name): | 92 def __init__(self, root_dir, name): |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 | 400 |
| 395 def test_test_expectation_pass(self): | 401 def test_test_expectation_pass(self): |
| 396 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') | 402 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') |
| 397 rw.DEPS = ['recipe_engine/step'] | 403 rw.DEPS = ['recipe_engine/step'] |
| 398 rw.RunStepsLines = ['api.step("test", ["echo", "bar"])'] | 404 rw.RunStepsLines = ['api.step("test", ["echo", "bar"])'] |
| 399 rw.add_expectation('basic', [{'cmd': ['echo', 'bar'], 'name': 'test'}]) | 405 rw.add_expectation('basic', [{'cmd': ['echo', 'bar'], 'name': 'test'}]) |
| 400 rw.write() | 406 rw.write() |
| 401 self._run_recipes('test', 'run', '--json', self.json_path) | 407 self._run_recipes('test', 'run', '--json', self.json_path) |
| 402 self.assertEqual(self.json_generator.get(), self.json_contents) | 408 self.assertEqual(self.json_generator.get(), self.json_contents) |
| 403 | 409 |
| 410 def test_test_missing_runsteps(self): |
| 411 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') |
| 412 rw.RunStepsLines = None |
| 413 rw.add_expectation('basic') |
| 414 rw.write() |
| 415 with self.assertRaises(subprocess.CalledProcessError) as cm: |
| 416 self._run_recipes('test', 'run', '--json', self.json_path) |
| 417 self.assertIn('Missing or misspelled RunSteps function', |
| 418 cm.exception.output) |
| 419 |
| 420 def test_test_missing_gentests(self): |
| 421 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') |
| 422 rw.RunStepsLines = ['pass'] |
| 423 rw.GenTestsLines = None |
| 424 rw.add_expectation('basic') |
| 425 rw.write() |
| 426 with self.assertRaises(subprocess.CalledProcessError) as cm: |
| 427 self._run_recipes('test', 'run', '--json', self.json_path) |
| 428 self.assertIn('Missing or misspelled GenTests function', |
| 429 cm.exception.output) |
| 430 |
| 404 def test_test_recipe_not_covered(self): | 431 def test_test_recipe_not_covered(self): |
| 405 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') | 432 rw = RecipeWriter(os.path.join(self._root_dir, 'recipes'), 'foo') |
| 406 rw.RunStepsLines = ['if False:', ' pass'] | 433 rw.RunStepsLines = ['if False:', ' pass'] |
| 407 rw.add_expectation('basic') | 434 rw.add_expectation('basic') |
| 408 rw.write() | 435 rw.write() |
| 409 with self.assertRaises(subprocess.CalledProcessError) as cm: | 436 with self.assertRaises(subprocess.CalledProcessError) as cm: |
| 410 self._run_recipes('test', 'run', '--json', self.json_path) | 437 self._run_recipes('test', 'run', '--json', self.json_path) |
| 411 self.assertIn('FATAL: Insufficient coverage', cm.exception.output) | 438 self.assertIn('FATAL: Insufficient coverage', cm.exception.output) |
| 412 self.assertNotIn('CHECK(FAIL)', cm.exception.output) | 439 self.assertNotIn('CHECK(FAIL)', cm.exception.output) |
| 413 self.assertNotIn('foo.basic failed', cm.exception.output) | 440 self.assertNotIn('foo.basic failed', cm.exception.output) |
| (...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1212 .check_failure('bar_check', 'bar_file', 2, 'bar_func', ['bar_args']) | 1239 .check_failure('bar_check', 'bar_file', 2, 'bar_func', ['bar_args']) |
| 1213 .internal_failure('bar_internal') | 1240 .internal_failure('bar_internal') |
| 1214 .uncovered_module('bar_module') | 1241 .uncovered_module('bar_module') |
| 1215 .unused_expectation('bar_expectation') | 1242 .unused_expectation('bar_expectation') |
| 1216 .get(), | 1243 .get(), |
| 1217 self.json_contents) | 1244 self.json_contents) |
| 1218 | 1245 |
| 1219 | 1246 |
| 1220 if __name__ == '__main__': | 1247 if __name__ == '__main__': |
| 1221 sys.exit(unittest.main()) | 1248 sys.exit(unittest.main()) |
| OLD | NEW |