| 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 27 matching lines...) Expand all Loading... |
| 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): |
| 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 loader = recipe_loader.ModuleLoader() |
| 49 recipe = recipe_loader.load_recipe(recipe_name, loader) |
| 49 for attr in dir(recipe): | 50 for attr in dir(recipe): |
| 50 val = getattr(recipe, attr) | 51 val = getattr(recipe, attr) |
| 51 if isinstance(val, types.ModuleType): | 52 if isinstance(val, types.ModuleType): |
| 52 module_name = val.__name__ | 53 module_name = val.__name__ |
| 53 for pattern in MODULES_WHITELIST: | 54 for pattern in MODULES_WHITELIST: |
| 54 if pattern.match(val.__name__): | 55 if pattern.match(val.__name__): |
| 55 break | 56 break |
| 56 else: | 57 else: |
| 57 yield ('In %s:\n' | 58 yield ('In %s:\n' |
| 58 ' Non-whitelisted import of %s' % | 59 ' Non-whitelisted import of %s' % |
| 59 (recipe_path, module_name)) | 60 (recipe_path, module_name)) |
| 60 | 61 |
| 61 | 62 |
| 62 def MainTest(): | 63 def MainTest(): |
| 63 errors = [] | 64 errors = [] |
| 64 for recipe_path, recipe_name in recipe_loader.loop_over_recipes(): | 65 for recipe_path, recipe_name in recipe_loader.loop_over_recipes(): |
| 65 errors.extend(ImportsTest(recipe_path, recipe_name)) | 66 errors.extend(ImportsTest(recipe_path, recipe_name)) |
| 66 | 67 |
| 67 if errors: | 68 if errors: |
| 68 raise TestFailure('\n'.join(map(str, errors))) | 69 raise TestFailure('\n'.join(map(str, errors))) |
| 69 | 70 |
| 70 | 71 |
| 71 if __name__ == '__main__': | 72 if __name__ == '__main__': |
| 72 MainTest() | 73 MainTest() |
| OLD | NEW |