blob: 19f7f550bcb2ed55cab9bf7545d5ee2c629e4364 [file] [log] [blame]
[email protected]2fac3752011-11-27 20:56:511# Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]93b2d7372009-11-17 18:54:532# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
[email protected]5a0114502011-11-29 13:01:245"""Presubmit script for changes affecting chrome/
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
tfarina78bb92f42015-01-31 00:20:488for more details about the presubmit API built into depot_tools.
[email protected]5a0114502011-11-29 13:01:249"""
10
11import re
[email protected]93b2d7372009-11-17 18:54:5312
13INCLUDE_CPP_FILES_ONLY = (
tfarina6aed9ae2015-03-08 03:54:0714 r'.*\.(cc|h)$',
[email protected]93b2d7372009-11-17 18:54:5315)
16
Lei Zhang29278b42017-11-10 22:34:1717INCLUDE_SOURCE_FILES_ONLY = (
18 r'.*\.(c|cc|cpp|h|m|mm)$',
19)
20
[email protected]93b2d7372009-11-17 18:54:5321EXCLUDE = (
22 # Objective C confuses everything.
23 r'.*cocoa.*',
24 r'.*_mac\.(cc|h)$',
25 r'.*_mac_.*',
26 # All the messages files do weird multiple include trickery
[email protected]3139e9f2013-10-10 21:29:4727 r'.*_messages.*\.h$',
[email protected]93b2d7372009-11-17 18:54:5328 r'render_messages.h$',
29 # Autogenerated window resources files are off limits
30 r'.*resource.h$',
[email protected]93b2d7372009-11-17 18:54:5331 # Header trickery
32 r'.*-inl\.h$',
[email protected]93b2d7372009-11-17 18:54:5333 # Has safe printf usage that cpplint complains about
34 r'safe_browsing_util\.cc$',
[email protected]93b2d7372009-11-17 18:54:5335)
36
[email protected]5a0114502011-11-29 13:01:2437def _CheckChangeLintsClean(input_api, output_api):
38 """Makes sure that the chrome/ code is cpplint clean."""
[email protected]93b2d7372009-11-17 18:54:5339 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)
[email protected]5a0114502011-11-29 13:01:2442 return input_api.canned_checks.CheckChangeLintsClean(
43 input_api, output_api, sources)
44
Lei Zhang29278b42017-11-10 22:34:1745
[email protected]5a0114502011-11-29 13:01:2446def _CheckNoContentUnitTestsInChrome(input_api, output_api):
47 """Makes sure that no unit tests from content/ are included in unit_tests."""
48 problems = []
49 for f in input_api.AffectedFiles():
Lei Zhang421a7ac2017-11-13 07:16:2750 if not f.LocalPath().endswith('BUILD.gn'):
[email protected]5a0114502011-11-29 13:01:2451 continue
52
53 for line_num, line in f.ChangedContents():
54 m = re.search(r"'(.*\/content\/.*unittest.*)'", line)
55 if m:
56 problems.append(m.group(1))
57
58 if not problems:
59 return []
60 return [output_api.PresubmitPromptWarning(
61 'Unit tests located in content/ should be added to the ' +
Lei Zhang421a7ac2017-11-13 07:16:2762 'content_unittests target.',
[email protected]5a0114502011-11-29 13:01:2463 items=problems)]
64
Lei Zhang29278b42017-11-10 22:34:1765
66def _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\)')
70 results = []
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))
74
75 return results
76
77
78def _CheckNoOSIOSMacrosInChrome(input_api, output_api):
79 """Check for OS_IOS which isn't used in chrome/."""
80 ios_macros = []
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))
86
87 if not ios_macros:
88 return []
89
90 return [output_api.PresubmitError(
91 'OS_IOS is not used in chrome/ but found in:\n', ios_macros)]
92
93
[email protected]5a0114502011-11-29 13:01:2494def _CommonChecks(input_api, output_api):
95 """Checks common to both upload and commit."""
96 results = []
97 results.extend(_CheckNoContentUnitTestsInChrome(input_api, output_api))
Lei Zhang29278b42017-11-10 22:34:1798 results.extend(_CheckNoOSIOSMacrosInChrome(input_api, output_api))
[email protected]5a0114502011-11-29 13:01:2499 return results
100
Lei Zhang29278b42017-11-10 22:34:17101
[email protected]5a0114502011-11-29 13:01:24102def CheckChangeOnUpload(input_api, output_api):
103 results = []
104 results.extend(_CommonChecks(input_api, output_api))
105 results.extend(_CheckChangeLintsClean(input_api, output_api))
106 return results
107
Lei Zhang29278b42017-11-10 22:34:17108
[email protected]5a0114502011-11-29 13:01:24109def CheckChangeOnCommit(input_api, output_api):
110 results = []
111 results.extend(_CommonChecks(input_api, output_api))
[email protected]93b2d7372009-11-17 18:54:53112 return results