Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: PRESUBMIT.py

Issue 7582002: Add check to help prevent test-only function usage in production code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 = input_api.re.compile(source_extensions)
52 file_exclusion_pattern = input_api.re.compile(
53 r'(_test_support|_(unit|browser|ui|perf)test)%s' % 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 problems = []
62 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
63 local_path = f.LocalPath()
64 if (file_inclusion_pattern.search(local_path) and
65 not file_exclusion_pattern.search(local_path)):
M-A Ruel 2011/08/05 18:27:41 It should use a source filter instead and you need
Jói 2011/08/08 23:11:00 Done.
66 lines = input_api.ReadFile(f).split('\n')
M-A Ruel 2011/08/05 18:27:41 splitlines()
Jói 2011/08/08 23:11:00 Done.
67 line_number = 0
68 for line in lines:
69 if (inclusion_pattern.search(line) and
70 not exclusion_pattern.search(line)):
71 problems.append(
72 '%s:%d\n %s' % (local_path, line_number, line.strip()))
73 line_number += 1
74
75 if problems:
76 return [output_api.PresubmitPromptWarning(
77 'You might be calling functions intended only for testing from\n' +
M-A Ruel 2011/08/05 18:27:41 no need for +
Jói 2011/08/08 23:11:00 Done.
78 'production code. Please verify that the following usages are OK,\n' +
79 'and email joi@chromium.org if this presubmit check needs to be\n' +
80 'improved because of frequent false positives:',
81 problems)]
82 else:
83 return []
84
85
41 def _CommonChecks(input_api, output_api): 86 def _CommonChecks(input_api, output_api):
42 """Checks common to both upload and commit.""" 87 """Checks common to both upload and commit."""
43 results = [] 88 results = []
44 results.extend(input_api.canned_checks.PanProjectChecks( 89 results.extend(input_api.canned_checks.PanProjectChecks(
45 input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) 90 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
46 results.extend(_CheckNoInterfacesInBase(input_api, output_api)) 91 results.extend(_CheckNoInterfacesInBase(input_api, output_api))
47 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) 92 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
93 results.extend(
94 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
48 return results 95 return results
49 96
50 97
51 def _CheckSubversionConfig(input_api, output_api): 98 def _CheckSubversionConfig(input_api, output_api):
52 """Verifies the subversion config file is correctly setup. 99 """Verifies the subversion config file is correctly setup.
53 100
54 Checks that autoprops are enabled, returns an error otherwise. 101 Checks that autoprops are enabled, returns an error otherwise.
55 """ 102 """
56 join = input_api.os_path.join 103 join = input_api.os_path.join
57 if input_api.platform == 'win32': 104 if input_api.platform == 'win32':
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 results.extend(input_api.canned_checks.CheckChangeHasBugField( 193 results.extend(input_api.canned_checks.CheckChangeHasBugField(
147 input_api, output_api)) 194 input_api, output_api))
148 results.extend(input_api.canned_checks.CheckChangeHasTestField( 195 results.extend(input_api.canned_checks.CheckChangeHasTestField(
149 input_api, output_api)) 196 input_api, output_api))
150 results.extend(_CheckSubversionConfig(input_api, output_api)) 197 results.extend(_CheckSubversionConfig(input_api, output_api))
151 return results 198 return results
152 199
153 200
154 def GetPreferredTrySlaves(): 201 def GetPreferredTrySlaves():
155 return ['win', 'linux', 'mac'] 202 return ['win', 'linux', 'mac']
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698