| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 | 33 |
| 34 class ImportViolationError(Exception): | 34 class ImportViolationError(Exception): |
| 35 pass | 35 pass |
| 36 | 36 |
| 37 | 37 |
| 38 class TestFailure(Exception): | 38 class TestFailure(Exception): |
| 39 pass | 39 pass |
| 40 | 40 |
| 41 | 41 |
| 42 def ImportsTest(recipe_path, recipe_name): | 42 def ImportsTest(recipe_path, recipe_name, universe): |
| 43 """Tests that recipe_name only uses allowed imports. | 43 """Tests that recipe_name only uses allowed imports. |
| 44 | 44 |
| 45 Returns a list of errors, or an empty list if there are no errors (duh). | 45 Returns a list of errors, or an empty list if there are no errors (duh). |
| 46 """ | 46 """ |
| 47 | 47 |
| 48 recipe = recipe_loader.load_recipe(recipe_name) | 48 recipe = universe.load_recipe(recipe_name) |
| 49 for attr in dir(recipe): | 49 for attr in dir(recipe): |
| 50 val = getattr(recipe, attr) | 50 val = getattr(recipe, attr) |
| 51 if isinstance(val, types.ModuleType): | 51 if isinstance(val, types.ModuleType): |
| 52 module_name = val.__name__ | 52 module_name = val.__name__ |
| 53 for pattern in MODULES_WHITELIST: | 53 for pattern in MODULES_WHITELIST: |
| 54 if pattern.match(val.__name__): | 54 if pattern.match(val.__name__): |
| 55 break | 55 break |
| 56 else: | 56 else: |
| 57 yield ('In %s:\n' | 57 yield ('In %s:\n' |
| 58 ' Non-whitelisted import of %s' % | 58 ' Non-whitelisted import of %s' % |
| 59 (recipe_path, module_name)) | 59 (recipe_path, module_name)) |
| 60 | 60 |
| 61 | 61 |
| 62 def MainTest(): | 62 def MainTest(): |
| 63 universe = recipe_loader.RecipeUniverse() |
| 64 |
| 63 errors = [] | 65 errors = [] |
| 64 for recipe_path, recipe_name in recipe_loader.loop_over_recipes(): | 66 for recipe_path, recipe_name in recipe_loader.loop_over_recipes(): |
| 65 errors.extend(ImportsTest(recipe_path, recipe_name)) | 67 errors.extend(ImportsTest(recipe_path, recipe_name, universe)) |
| 66 | 68 |
| 67 if errors: | 69 if errors: |
| 68 raise TestFailure('\n'.join(map(str, errors))) | 70 raise TestFailure('\n'.join(map(str, errors))) |
| 69 | 71 |
| 70 | 72 |
| 71 if __name__ == '__main__': | 73 if __name__ == '__main__': |
| 72 MainTest() | 74 MainTest() |
| OLD | NEW |