| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # If this presubmit check fails or misbehaves, please complain to | 5 # If this presubmit check fails or misbehaves, please complain to |
| 6 # mnissler@chromium.org, pastarmovj@chromium.org or joaodasilva@chromium.org. | 6 # mnissler@chromium.org, pastarmovj@chromium.org or joaodasilva@chromium.org. |
| 7 | 7 |
| 8 import itertools |
| 9 import sys |
| 8 | 10 |
| 9 def _CheckPolicyTemplatesSyntax(input_api, output_api): | 11 def _CheckPolicyTemplatesSyntax(input_api, output_api): |
| 10 filepath = input_api.os_path.join(input_api.PresubmitLocalPath(), | 12 filepath = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 11 'policy_templates.json') | 13 'policy_templates.json') |
| 12 if any(f.AbsoluteLocalPath() == filepath | 14 if any(f.AbsoluteLocalPath() == filepath |
| 13 for f in input_api.AffectedFiles()): | 15 for f in input_api.AffectedFiles()): |
| 14 import sys | |
| 15 old_sys_path = sys.path | 16 old_sys_path = sys.path |
| 16 try: | 17 try: |
| 17 sys.path = [input_api.PresubmitLocalPath()] + sys.path | 18 sys.path = [input_api.PresubmitLocalPath()] + sys.path |
| 18 # Optimization: only load this when it's needed. | 19 # Optimization: only load this when it's needed. |
| 19 import syntax_check_policy_template_json | 20 import syntax_check_policy_template_json |
| 20 checker = syntax_check_policy_template_json.PolicyTemplateChecker() | 21 checker = syntax_check_policy_template_json.PolicyTemplateChecker() |
| 21 if checker.Run([], filepath) > 0: | 22 if checker.Run([], filepath) > 0: |
| 22 return [output_api.PresubmitError('Syntax error(s) in file:', | 23 return [output_api.PresubmitError('Syntax error(s) in file:', |
| 23 [filepath])] | 24 [filepath])] |
| 24 finally: | 25 finally: |
| 25 sys.path = old_sys_path | 26 sys.path = old_sys_path |
| 26 return [] | 27 return [] |
| 27 | 28 |
| 28 | 29 |
| 29 def _CheckPolicyPyauto(input_api, output_api): | 30 def _CheckPolicyTestCases(input_api, output_api): |
| 30 os_path = input_api.os_path | 31 os_path = input_api.os_path |
| 31 pardir = os_path.pardir | |
| 32 local_path = input_api.PresubmitLocalPath() | 32 local_path = input_api.PresubmitLocalPath() |
| 33 template_path = os_path.join(local_path, 'policy_templates.json') | 33 template_path = os_path.join(local_path, 'policy_templates.json') |
| 34 pyauto_path = os_path.normpath(os_path.join( | |
| 35 local_path, pardir, pardir, 'test', 'functional')) | |
| 36 affected_files = input_api.AffectedFiles() | 34 affected_files = input_api.AffectedFiles() |
| 37 if not any(f.AbsoluteLocalPath() == template_path for f in affected_files): | 35 if not any(f.AbsoluteLocalPath() == template_path for f in affected_files): |
| 38 return [] | 36 return [] |
| 39 | 37 |
| 40 # Read list of policies in the template. eval() is used instead of a JSON | 38 # Read list of policies in the template. eval() is used instead of a JSON |
| 41 # parser because policy_templates.json is not quite JSON, and uses some | 39 # parser because policy_templates.json is not quite JSON, and uses some |
| 42 # python features such as #-comments and '''strings'''. policy_templates.json | 40 # python features such as #-comments and '''strings'''. policy_templates.json |
| 43 # is actually maintained as a python dictionary. | 41 # is actually maintained as a python dictionary. |
| 44 with open(template_path) as f: | 42 with open(template_path) as f: |
| 45 template_data = eval(f.read(), {}) | 43 template_data = eval(f.read(), {}) |
| 46 policies = ( policy['name'] | 44 policies = ( policy['name'] |
| 47 for policy in template_data['policy_definitions'] | 45 for policy in template_data['policy_definitions'] |
| 48 if policy['type'] != 'group' ) | 46 if policy['type'] != 'group' ) |
| 49 groups = ( policy['policies'] | 47 groups = ( policy['policies'] |
| 50 for policy in template_data['policy_definitions'] | 48 for policy in template_data['policy_definitions'] |
| 51 if policy['type'] == 'group' ) | 49 if policy['type'] == 'group' ) |
| 52 subpolicies = ( policy['name'] for group in groups for policy in group ) | 50 subpolicies = ( policy['name'] for group in groups for policy in group ) |
| 53 import itertools | |
| 54 template_policies = frozenset(itertools.chain(policies, subpolicies)) | 51 template_policies = frozenset(itertools.chain(policies, subpolicies)) |
| 55 | 52 |
| 56 # Read list of policies in the pyauto test. | 53 # Read list of policies in chrome/test/data/policy/policy_test_cases.json. |
| 57 import sys | 54 root = input_api.change.RepositoryRoot() |
| 58 prev_sys_path = sys.path | 55 policy_test_cases_file = os_path.join( |
| 59 try: | 56 root, 'chrome', 'test', 'data', 'policy', 'policy_test_cases.json') |
| 60 sys.path = [pyauto_path] + sys.path | 57 test_names = input_api.json.load(open(policy_test_cases_file)).keys() |
| 61 from policy_test_cases import PolicyPrefsTestCases | 58 tested_policies = frozenset( |
| 62 pyauto_policies = frozenset(PolicyPrefsTestCases.policies.keys()) | 59 [name for name in test_names if name[:2] != '--']) |
| 63 finally: | |
| 64 sys.path = prev_sys_path | |
| 65 | 60 |
| 66 # Finally check if any policies are missing. | 61 # Finally check if any policies are missing. |
| 67 missing = template_policies - pyauto_policies | 62 missing = template_policies - tested_policies |
| 68 extra = pyauto_policies - template_policies | 63 extra = tested_policies - template_policies |
| 69 error_missing = ('Policy \'%s\' was added to policy_templates.json but not ' | 64 error_missing = ('Policy \'%s\' was added to policy_templates.json but not ' |
| 70 'to src/chrome/test/functional/policy_test_cases.py. ' | 65 'to src/chrome/test/data/policy/policy_test_cases.json. ' |
| 71 'Please update both files.') | 66 'Please update both files.') |
| 72 error_extra = ('Policy \'%s\' is tested by ' | 67 error_extra = ('Policy \'%s\' is tested by ' |
| 73 'src/chrome/test/functional/policy_test_cases.py but is not ' | 68 'src/chrome/test/data/policy/policy_test_cases.json but is not' |
| 74 'defined in policy_templates.json. Please update both files.') | 69 ' defined in policy_templates.json. Please update both files.') |
| 75 results = [] | 70 results = [] |
| 76 for policy in missing: | 71 for policy in missing: |
| 77 results.append(output_api.PresubmitError(error_missing % policy)) | 72 results.append(output_api.PresubmitError(error_missing % policy)) |
| 78 for policy in extra: | 73 for policy in extra: |
| 79 results.append(output_api.PresubmitError(error_extra % policy)) | 74 results.append(output_api.PresubmitError(error_extra % policy)) |
| 80 return results | 75 return results |
| 81 | 76 |
| 82 | 77 |
| 83 def _CommonChecks(input_api, output_api): | 78 def _CommonChecks(input_api, output_api): |
| 84 results = [] | 79 results = [] |
| 85 results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api)) | 80 results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api)) |
| 86 results.extend(_CheckPolicyPyauto(input_api, output_api)) | 81 results.extend(_CheckPolicyTestCases(input_api, output_api)) |
| 87 return results | 82 return results |
| 88 | 83 |
| 89 | 84 |
| 90 def CheckChangeOnUpload(input_api, output_api): | 85 def CheckChangeOnUpload(input_api, output_api): |
| 91 return _CommonChecks(input_api, output_api) | 86 return _CommonChecks(input_api, output_api) |
| 92 | 87 |
| 93 | 88 |
| 94 def CheckChangeOnCommit(input_api, output_api): | 89 def CheckChangeOnCommit(input_api, output_api): |
| 95 return _CommonChecks(input_api, output_api) | 90 return _CommonChecks(input_api, output_api) |
| OLD | NEW |