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
3 # found in the LICENSE file.
5 """Presubmit script for changes affecting chrome/
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools.
13 INCLUDE_CPP_FILES_ONLY = (
17 INCLUDE_SOURCE_FILES_ONLY = (
18 r'.*\.(c|cc|cpp|h|m|mm)$',
22 # Objective C confuses everything.
26 # All the messages files do weird multiple include trickery
28 r'render_messages.h$',
29 # Autogenerated window resources files are off limits
33 # Has safe printf usage that cpplint complains about
34 r'safe_browsing_util\.cc$',
37 def _CheckChangeLintsClean(input_api, output_api):
38 """Makes sure that the chrome/ code is cpplint clean."""
39 black_list = input_api.DEFAULT_BLACK_LIST + EXCLUDE
40 sources = lambda x: input_api.FilterSourceFile(
41 x, white_list=INCLUDE_CPP_FILES_ONLY, black_list=black_list)
42 return input_api.canned_checks.CheckChangeLintsClean(
43 input_api, output_api, sources)
46 def _CheckNoContentUnitTestsInChrome(input_api, output_api):
47 """Makes sure that no unit tests from content/ are included in unit_tests."""
49 for f in input_api.AffectedFiles():
50 if not f.LocalPath().endswith('BUILD.gn'):
53 for line_num, line in f.ChangedContents():
54 m = re.search(r"'(.*\/content\/.*unittest.*)'", line)
56 problems.append(m.group(1))
60 return [output_api.PresubmitPromptWarning(
61 'Unit tests located in content/ should be added to the ' +
62 'content_unittests target.',
66 def _CheckNoOSIOSMacrosInChromeFile(input_api, f):
67 """Check for OS_IOS in a given file in chrome/."""
68 preprocessor_statement = input_api.re.compile(r'^\s*#')
69 ios_macro = input_api.re.compile(r'defined\(OS_IOS\)')
71 for lnum, line in f.ChangedContents():
72 if preprocessor_statement.search(line) and ios_macro.search(line):
73 results.append(' %s:%d' % (f.LocalPath(), lnum))
78 def _CheckNoOSIOSMacrosInChrome(input_api, output_api):
79 """Check for OS_IOS which isn't used in chrome/."""
81 def SourceFilter(affected_file):
82 return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY,
83 input_api.DEFAULT_BLACK_LIST)
84 for f in input_api.AffectedSourceFiles(SourceFilter):
85 ios_macros.extend(_CheckNoOSIOSMacrosInChromeFile(input_api, f))
90 return [output_api.PresubmitError(
91 'OS_IOS is not used in chrome/ but found in:\n', ios_macros)]
94 def _CommonChecks(input_api, output_api):
95 """Checks common to both upload and commit."""
97 results.extend(_CheckNoContentUnitTestsInChrome(input_api, output_api))
98 results.extend(_CheckNoOSIOSMacrosInChrome(input_api, output_api))
102 def CheckChangeOnUpload(input_api, output_api):
104 results.extend(_CommonChecks(input_api, output_api))
105 results.extend(_CheckChangeLintsClean(input_api, output_api))
109 def CheckChangeOnCommit(input_api, output_api):
111 results.extend(_CommonChecks(input_api, output_api))