| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 # TODO(iannucci): merge this with the test command, doesn't need to be top | 65 # TODO(iannucci): merge this with the test command, doesn't need to be top |
| 66 # level. | 66 # level. |
| 67 helpstr = 'Check recipes for stylistic and hygenic issues.' | 67 helpstr = 'Check recipes for stylistic and hygenic issues.' |
| 68 lint_p = parser.add_parser( | 68 lint_p = parser.add_parser( |
| 69 'lint', help=helpstr, description=helpstr) | 69 'lint', help=helpstr, description=helpstr) |
| 70 lint_p.add_argument( | 70 lint_p.add_argument( |
| 71 '--whitelist', '-w', action='append', default=[], | 71 '--whitelist', '-w', action='append', default=[], |
| 72 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. ' |
| 73 'Use multiple times to add multiple patterns,') | 73 'Use multiple times to add multiple patterns,') |
| 74 | 74 |
| 75 lint_p.set_defaults(command='lint', func=main) | 75 lint_p.set_defaults(func=main) |
| 76 | 76 |
| 77 | 77 |
| 78 def main(package_deps, args): | 78 def main(package_deps, args): |
| 79 from . import loader | 79 from . import loader |
| 80 | 80 |
| 81 universe = loader.RecipeUniverse(package_deps, args.package) | 81 universe = loader.RecipeUniverse(package_deps, args.package) |
| 82 universe_view = loader.UniverseView(universe, package_deps.root_package) | 82 universe_view = loader.UniverseView(universe, package_deps.root_package) |
| 83 | 83 |
| 84 whitelist = map(re.compile, MODULES_WHITELIST + args.whitelist) | 84 whitelist = map(re.compile, MODULES_WHITELIST + args.whitelist) |
| 85 | 85 |
| 86 errors = [] | 86 errors = [] |
| 87 for recipe_path, recipe_name in universe_view.loop_over_recipes(): | 87 for recipe_path, recipe_name in universe_view.loop_over_recipes(): |
| 88 errors.extend( | 88 errors.extend( |
| 89 ImportsTest(recipe_path, recipe_name, whitelist, universe_view)) | 89 ImportsTest(recipe_path, recipe_name, whitelist, universe_view)) |
| 90 | 90 |
| 91 if errors: | 91 if errors: |
| 92 raise TestFailure('\n'.join(map(str, errors))) | 92 raise TestFailure('\n'.join(map(str, errors))) |
| OLD | NEW |