| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The LUCI Authors. All rights reserved. | 2 # Copyright 2015 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 """Tests that recipes are on their best behavior. | 6 """Tests that recipes are on their best behavior. |
| 7 | 7 |
| 8 Checks that recipes only import modules from a whitelist. Imports are | 8 Checks that recipes only import modules from a whitelist. Imports are |
| 9 generally not safe in recipes if they depend on the platform, since | 9 generally not safe in recipes if they depend on the platform, since |
| 10 e.g. you can run a recipe simulation for a Windows recipe on Linux. | 10 e.g. you can run a recipe simulation for a Windows recipe on Linux. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 for pattern in whitelist: | 55 for pattern in whitelist: |
| 56 if pattern.match(val.__name__): | 56 if pattern.match(val.__name__): |
| 57 break | 57 break |
| 58 else: | 58 else: |
| 59 yield ('In %s:\n' | 59 yield ('In %s:\n' |
| 60 ' Non-whitelisted import of %s' % | 60 ' Non-whitelisted import of %s' % |
| 61 (recipe_path, module_name)) | 61 (recipe_path, module_name)) |
| 62 | 62 |
| 63 | 63 |
| 64 def add_subparser(parser): | 64 def add_subparser(parser): |
| 65 # TODO(iannucci): merge this with the test command, doesn't need to be top |
| 66 # level. |
| 67 helpstr = 'Check recipes for stylistic and hygenic issues.' |
| 65 lint_p = parser.add_parser( | 68 lint_p = parser.add_parser( |
| 66 'lint', | 69 'lint', help=helpstr, description=helpstr) |
| 67 description='Check recipes for stylistic and hygenic issues') | |
| 68 lint_p.add_argument( | 70 lint_p.add_argument( |
| 69 '--whitelist', '-w', action='append', default=[], | 71 '--whitelist', '-w', action='append', default=[], |
| 70 help='A regexp matching module names to add to the default whitelist. ' | 72 help='A regexp matching module names to add to the default whitelist. ' |
| 71 'Use multiple times to add multiple patterns,') | 73 'Use multiple times to add multiple patterns,') |
| 72 | 74 |
| 73 lint_p.set_defaults(command='lint', func=main) | 75 lint_p.set_defaults(command='lint', func=main) |
| 74 | 76 |
| 75 | 77 |
| 76 def main(package_deps, args): | 78 def main(package_deps, args): |
| 77 from . import loader | 79 from . import loader |
| 78 | 80 |
| 79 universe = loader.RecipeUniverse(package_deps, args.package) | 81 universe = loader.RecipeUniverse(package_deps, args.package) |
| 80 universe_view = loader.UniverseView(universe, package_deps.root_package) | 82 universe_view = loader.UniverseView(universe, package_deps.root_package) |
| 81 | 83 |
| 82 whitelist = map(re.compile, MODULES_WHITELIST + args.whitelist) | 84 whitelist = map(re.compile, MODULES_WHITELIST + args.whitelist) |
| 83 | 85 |
| 84 errors = [] | 86 errors = [] |
| 85 for recipe_path, recipe_name in universe_view.loop_over_recipes(): | 87 for recipe_path, recipe_name in universe_view.loop_over_recipes(): |
| 86 errors.extend( | 88 errors.extend( |
| 87 ImportsTest(recipe_path, recipe_name, whitelist, universe_view)) | 89 ImportsTest(recipe_path, recipe_name, whitelist, universe_view)) |
| 88 | 90 |
| 89 if errors: | 91 if errors: |
| 90 raise TestFailure('\n'.join(map(str, errors))) | 92 raise TestFailure('\n'.join(map(str, errors))) |
| OLD | NEW |