OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 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 """Top-level presubmit script for Chromium. | 5 """Top-level presubmit script for Chromium. |
6 | 6 |
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
8 for more details about the presubmit API built into gcl. | 8 for more details about the presubmit API built into gcl. |
9 """ | 9 """ |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 if len(files): | 32 if len(files): |
33 return [ output_api.PresubmitError( | 33 return [ output_api.PresubmitError( |
34 'Objective-C interfaces or categories are forbidden in libbase. ' + | 34 'Objective-C interfaces or categories are forbidden in libbase. ' + |
35 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' + | 35 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' + |
36 'browse_thread/thread/efb28c10435987fd', | 36 'browse_thread/thread/efb28c10435987fd', |
37 files) ] | 37 files) ] |
38 return [] | 38 return [] |
39 | 39 |
40 | 40 |
| 41 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
| 42 """Attempts to prevent use of functions intended only for testing in |
| 43 non-testing code. For now this is just a best-effort implementation |
| 44 that ignores header files and may have some false positives. A |
| 45 better implementation would probably need a proper C++ parser. |
| 46 """ |
| 47 # We only scan .cc files and the like, as the declaration of |
| 48 # for-testing functions in header files are hard to distinguish from |
| 49 # calls to such functions without a proper C++ parser. |
| 50 source_extensions = r'\.(cc|cpp|cxx|mm)$' |
| 51 file_inclusion_pattern = r'.+%s' % source_extensions |
| 52 file_exclusion_pattern = (r'.+(_test_support|_(unit|browser|ui|perf)test)%s' % |
| 53 source_extensions) |
| 54 |
| 55 base_function_pattern = r'ForTest(ing)?|for_test(ing)?' |
| 56 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern) |
| 57 exclusion_pattern = input_api.re.compile( |
| 58 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % ( |
| 59 base_function_pattern, base_function_pattern)) |
| 60 |
| 61 def FilterFile(affected_file): |
| 62 black_list = ((file_exclusion_pattern, ) + _EXCLUDED_PATHS + |
| 63 input_api.DEFAULT_BLACK_LIST) |
| 64 return input_api.FilterSourceFile( |
| 65 affected_file, |
| 66 white_list=(file_inclusion_pattern, ), |
| 67 black_list=black_list) |
| 68 |
| 69 problems = [] |
| 70 for f in input_api.AffectedSourceFiles(FilterFile): |
| 71 local_path = f.LocalPath() |
| 72 lines = input_api.ReadFile(f).splitlines() |
| 73 line_number = 0 |
| 74 for line in lines: |
| 75 if (inclusion_pattern.search(line) and |
| 76 not exclusion_pattern.search(line)): |
| 77 problems.append( |
| 78 '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 79 line_number += 1 |
| 80 |
| 81 if problems: |
| 82 return [output_api.PresubmitPromptWarning( |
| 83 'You might be calling functions intended only for testing from\n' |
| 84 'production code. Please verify that the following usages are OK,\n' |
| 85 'and email joi@chromium.org if you are seeing false positives:', |
| 86 problems)] |
| 87 else: |
| 88 return [] |
| 89 |
| 90 |
41 def _CommonChecks(input_api, output_api): | 91 def _CommonChecks(input_api, output_api): |
42 """Checks common to both upload and commit.""" | 92 """Checks common to both upload and commit.""" |
43 results = [] | 93 results = [] |
44 results.extend(input_api.canned_checks.PanProjectChecks( | 94 results.extend(input_api.canned_checks.PanProjectChecks( |
45 input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) | 95 input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
46 results.extend(_CheckNoInterfacesInBase(input_api, output_api)) | 96 results.extend(_CheckNoInterfacesInBase(input_api, output_api)) |
47 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) | 97 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
| 98 results.extend( |
| 99 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
48 return results | 100 return results |
49 | 101 |
50 | 102 |
51 def _CheckSubversionConfig(input_api, output_api): | 103 def _CheckSubversionConfig(input_api, output_api): |
52 """Verifies the subversion config file is correctly setup. | 104 """Verifies the subversion config file is correctly setup. |
53 | 105 |
54 Checks that autoprops are enabled, returns an error otherwise. | 106 Checks that autoprops are enabled, returns an error otherwise. |
55 """ | 107 """ |
56 join = input_api.os_path.join | 108 join = input_api.os_path.join |
57 if input_api.platform == 'win32': | 109 if input_api.platform == 'win32': |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 results.extend(input_api.canned_checks.CheckChangeHasBugField( | 198 results.extend(input_api.canned_checks.CheckChangeHasBugField( |
147 input_api, output_api)) | 199 input_api, output_api)) |
148 results.extend(input_api.canned_checks.CheckChangeHasTestField( | 200 results.extend(input_api.canned_checks.CheckChangeHasTestField( |
149 input_api, output_api)) | 201 input_api, output_api)) |
150 results.extend(_CheckSubversionConfig(input_api, output_api)) | 202 results.extend(_CheckSubversionConfig(input_api, output_api)) |
151 return results | 203 return results |
152 | 204 |
153 | 205 |
154 def GetPreferredTrySlaves(): | 206 def GetPreferredTrySlaves(): |
155 return ['win', 'linux', 'mac'] | 207 return ['win', 'linux', 'mac'] |
OLD | NEW |