blob: ba84e744d22c7c64f2cdd5561e3b7f5793f0f2e2 [file] [log] [blame]
andrew@webrtc.org2442de12012-01-23 17:45:41 +00001# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
2#
3# Use of this source code is governed by a BSD-style license
4# that can be found in the LICENSE file in the root of the source
5# tree. An additional intellectual property rights grant can be found
6# in the file PATENTS. All contributing project authors may
7# be found in the AUTHORS file in the root of the source tree.
niklase@google.comda159d62011-05-30 11:51:34 +00008
kjellander@webrtc.org85759802013-10-22 16:47:40 +00009import re
10
11
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000012def _CheckNoIOStreamInHeaders(input_api, output_api):
13 """Checks to make sure no .h files include <iostream>."""
14 files = []
15 pattern = input_api.re.compile(r'^#include\s*<iostream>',
16 input_api.re.MULTILINE)
17 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
18 if not f.LocalPath().endswith('.h'):
19 continue
20 contents = input_api.ReadFile(f)
21 if pattern.search(contents):
22 files.append(f)
23
24 if len(files):
25 return [ output_api.PresubmitError(
26 'Do not #include <iostream> in header files, since it inserts static ' +
27 'initialization into every file including the header. Instead, ' +
28 '#include <ostream>. See http://crbug.com/94794',
29 files) ]
30 return []
31
32def _CheckNoFRIEND_TEST(input_api, output_api):
33 """Make sure that gtest's FRIEND_TEST() macro is not used, the
34 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
35 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
36 problems = []
37
38 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
39 for f in input_api.AffectedFiles(file_filter=file_filter):
40 for line_num, line in f.ChangedContents():
41 if 'FRIEND_TEST(' in line:
42 problems.append(' %s:%d' % (f.LocalPath(), line_num))
43
44 if not problems:
45 return []
46 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
47 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
48 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
49
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000050def _CheckApprovedFilesLintClean(input_api, output_api,
51 source_file_filter=None):
52 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000053 This check is based on _CheckChangeLintsClean in
54 depot_tools/presubmit_canned_checks.py but has less filters and only checks
55 added files."""
56 result = []
57
58 # Initialize cpplint.
59 import cpplint
60 # Access to a protected member _XX of a client class
61 # pylint: disable=W0212
62 cpplint._cpplint_state.ResetErrorCounts()
63
64 # Justifications for each filter:
65 #
66 # - build/header_guard : WebRTC coding style says they should be prefixed
67 # with WEBRTC_, which is not possible to configure in
68 # cpplint.py.
69 cpplint._SetFilters('-build/header_guard')
70
71 # Use the strictest verbosity level for cpplint.py (level 1) which is the
72 # default when running cpplint.py from command line.
73 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000074 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000075 verbosity_level = 1
76 files = []
77 for f in input_api.AffectedSourceFiles(source_file_filter):
78 # Note that moved/renamed files also count as added for svn.
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +000079 if (f.Action() == 'A'):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000080 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000081
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000082 for file_name in files:
83 cpplint.ProcessFile(file_name, verbosity_level)
84
85 if cpplint._cpplint_state.error_count > 0:
86 if input_api.is_committing:
87 # TODO(kjellander): Change back to PresubmitError below when we're
88 # confident with the lint settings.
89 res_type = output_api.PresubmitPromptWarning
90 else:
91 res_type = output_api.PresubmitPromptWarning
92 result = [res_type('Changelist failed cpplint.py check.')]
93
94 return result
95
henrike@webrtc.org056176b2014-02-19 23:18:19 +000096def _CheckTalkOrWebrtcOnly(input_api, output_api):
97 base_folders = set(["webrtc", "talk"])
98 base_folders_in_cl = set()
99
100 for f in input_api.AffectedFiles():
101 full_path = f.LocalPath()
102 base_folders_in_cl.add(full_path[:full_path.find('/')])
103
104 results = []
105 if base_folders.issubset(base_folders_in_cl):
106 error_type = output_api.PresubmitError
107 results.append(error_type(
108 'It is not allowed to check in files to ' + ', '.join(base_folders) +
109 ' in the same cl',
110 []))
111 return results
112
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000113def _CommonChecks(input_api, output_api):
114 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000115 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:34 +0000116 results = []
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000117 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
118 black_list=(r'^.*gviz_api\.py$',
119 r'^.*gaeunit\.py$',
fischman@webrtc.org33584f92013-07-25 16:43:30 +0000120 # Embedded shell-script fakes out pylint.
121 r'^talk/site_scons/site_tools/talk_linux.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000122 r'^third_party/.*\.py$',
123 r'^testing/.*\.py$',
124 r'^tools/gyp/.*\.py$',
125 r'^tools/perf_expectations/.*\.py$',
phoglund@webrtc.org6d07ad92013-05-14 09:42:39 +0000126 r'^tools/protoc_wrapper/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000127 r'^tools/python/.*\.py$',
128 r'^tools/python_charts/data/.*\.py$',
kjellander@webrtc.org33654222013-08-22 07:57:00 +0000129 r'^tools/refactoring/.*\.py$',
kjellander@webrtc.orgf9bdbe32013-12-11 13:37:12 +0000130 r'^tools/swarming_client/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000131 # TODO(phoglund): should arguably be checked.
132 r'^tools/valgrind-webrtc/.*\.py$',
133 r'^tools/valgrind/.*\.py$',
134 # TODO(phoglund): should arguably be checked.
135 r'^webrtc/build/.*\.py$',
136 r'^build/.*\.py$',
fischman@webrtc.org10adbef2014-03-01 02:09:36 +0000137 r'^out.*/.*\.py$',),
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000138 disabled_warnings=['F0401', # Failed to import x
139 'E0611', # No package y in x
140 'W0232', # Class has no __init__ method
141 ]))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000142 results.extend(input_api.canned_checks.CheckLongLines(
pbos@webrtc.orgf2e7bc62013-04-08 15:46:07 +0000143 input_api, output_api, maxlen=80))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000144 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
145 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000146 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
147 input_api, output_api))
148 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
149 input_api, output_api))
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000150 results.extend(_CheckApprovedFilesLintClean(input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000151 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
152 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
henrike@webrtc.org056176b2014-02-19 23:18:19 +0000153 results.extend(_CheckTalkOrWebrtcOnly(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000154 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000155
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000156def CheckChangeOnUpload(input_api, output_api):
157 results = []
158 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000159 return results
160
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000161def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000162 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000163 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000164 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000165 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
166 input_api, output_api))
167 results.extend(input_api.canned_checks.CheckChangeHasDescription(
168 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000169 results.extend(input_api.canned_checks.CheckChangeHasBugField(
170 input_api, output_api))
171 results.extend(input_api.canned_checks.CheckChangeHasTestField(
172 input_api, output_api))
kjellander@webrtc.org12cb88c2014-02-13 11:53:43 +0000173 results.extend(input_api.canned_checks.CheckTreeIsOpen(
174 input_api, output_api,
175 json_url='http://webrtc-status.appspot.com/current?format=json'))
niklase@google.com1198db92011-06-09 07:07:24 +0000176 return results
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000177
178# pylint: disable=W0613
179def GetPreferredTrySlaves(project, change):
180 files = change.LocalPaths()
181
kjellander@webrtc.orgcf2b3ac2013-12-20 21:20:42 +0000182 android_bots = [
183 'android',
kjellander@webrtc.org2a260d92014-01-27 16:08:43 +0000184 'android_apk',
185 'android_apk_rel',
kjellander@webrtc.orgcf2b3ac2013-12-20 21:20:42 +0000186 'android_rel',
187 'android_clang',
188 ]
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000189 ios_bots = [
190 'ios',
191 'ios_rel',
192 ]
193 linux_bots = [
194 'linux',
195 'linux_asan',
kjellander@webrtc.org570bc3d2014-01-22 19:00:01 +0000196 'linux_baremetal',
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000197 'linux_memcheck',
198 'linux_rel',
199 'linux_tsan',
200 ]
201 mac_bots = [
202 'mac',
203 'mac_asan',
kjellander@webrtc.org570bc3d2014-01-22 19:00:01 +0000204 'mac_baremetal',
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000205 'mac_rel',
206 'mac_x64_rel',
207 ]
208 win_bots = [
209 'win',
kjellander@webrtc.org570bc3d2014-01-22 19:00:01 +0000210 'win_baremetal',
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000211 'win_rel',
212 'win_x64_rel',
213 ]
214
215 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
216 return []
217
218 if all(re.search('[/_]ios[/_.]', f) for f in files):
219 return ios_bots
220 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
221 return mac_bots
222 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
223 return win_bots
224
kjellander@webrtc.orgcf2b3ac2013-12-20 21:20:42 +0000225 return android_bots + ios_bots + linux_bots + mac_bots + win_bots