forked from chromium/chromium
-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathPRESUBMIT.py
280 lines (233 loc) · 10.4 KB
/
PRESUBMIT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Presubmit script for ios.
See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into depot_tools.
"""
import os
NULLABILITY_PATTERN = r'(nonnull|nullable|_Nullable|_Nonnull)'
TODO_PATTERN = r'TO[D]O\(([^\)]*)\)'
BUG_PATTERN = r'^(crbug\.com|b)/\d+$'
DEPRECATED_BUG_PATTERN = r'^b/\d+$'
INCLUDE_PATTERN = r'^#include'
PIPE_IN_COMMENT_PATTERN = r'//.*[^|]\|(?!\|)'
IOS_PACKAGE_PATTERN = r'^ios'
BOXED_BOOL_PATTERN = r'@\((YES|NO)\)'
def IsSubListOf(needle, hay):
"""Returns whether there is a slice of |hay| equal to |needle|."""
for i, line in enumerate(hay):
if line == needle[0]:
if needle == hay[i:i + len(needle)]:
return True
return False
def _CheckNullabilityAnnotations(input_api, output_api):
""" Checks whether there are nullability annotations in ios code.
They are accepted in ios/web_view/public since it tries to mimic
the platform library but not anywhere else.
"""
nullability_regex = input_api.re.compile(NULLABILITY_PATTERN)
errors = []
for f in input_api.AffectedFiles():
if f.LocalPath().startswith('ios/web_view/public/'):
# ios/web_view/public tries to mimic an existing API that
# might have nullability in it and that is acceptable.
continue
for line_num, line in f.ChangedContents():
if nullability_regex.search(line):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if not errors:
return []
plural_suffix = '' if len(errors) == 1 else 's'
warning_message = ('Found Nullability annotation%(plural)s. '
'Prefer DCHECKs in ios code to check for nullness:' % {
'plural': plural_suffix
})
return [output_api.PresubmitPromptWarning(warning_message, items=errors)]
def _CheckBugInToDo(input_api, output_api):
""" Checks whether TODOs in ios code are identified by a bug number."""
errors = []
warnings = []
for f in input_api.AffectedFiles():
for line_num, line in f.ChangedContents():
if _HasToDoWithNoBug(input_api, line):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if _HasToDoWithDeprecatedBug(input_api, line):
warnings.append('%s:%s' % (f.LocalPath(), line_num))
if not errors and not warnings:
return []
output = []
if errors:
singular_article = 'a ' if len(errors) == 1 else ''
plural_suffix = '' if len(errors) == 1 else 's'
error_message = '\n'.join([
'Found TO'
'DO%(plural)s without %(a)sbug number%(plural)s (expected format '
'is \"TO'
'DO(crbug.com/######)\"):' % {
'plural': plural_suffix,
'a' : singular_article
}
] + errors) + '\n'
output.append(output_api.PresubmitError(error_message))
if warnings:
singular_article = 'a ' if len(warnings) == 1 else ''
plural_suffix = '' if len(warnings) == 1 else 's'
warning_message = '\n'.join([
'Found TO'
'DO%(plural)s with %(a)sdeprecated bug link%(plural)s (found '
'"b/#####\", expected format is \"crbug.com/######"):' % {
'plural': plural_suffix,
'a' : singular_article
}
] + warnings) + '\n'
output.append(output_api.PresubmitPromptWarning(warning_message))
return output
def _CheckHasNoIncludeDirectives(input_api, output_api):
""" Checks that #include preprocessor directives are not present."""
errors = []
for f in input_api.AffectedFiles():
if not _IsInIosPackage(input_api, f.LocalPath()):
continue
_, ext = os.path.splitext(f.LocalPath())
if ext != '.mm':
continue
for line_num, line in f.ChangedContents():
if _HasIncludeDirective(input_api, line):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if not errors:
return []
singular_plural = 'it' if len(errors) == 1 else 'them'
plural_suffix = '' if len(errors) == 1 else 's'
error_message = '\n'.join([
'Found usage of `#include` preprocessor directive%(plural)s! Please, '
'replace %(singular_plural)s with `#import` preprocessor '
'directive%(plural)s instead. '
'Consider replacing all existing `#include` with `#import` (if any) in '
'this file for the code clean up. See '
'https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main'
'/styleguide/objective-c/objective-c.md'
'#import-and-include-in-the-directory for more details. '
'\n\nAffected file%(plural)s:' % {
'plural': plural_suffix,
'singular_plural': singular_plural
}
] + errors) + '\n'
return [output_api.PresubmitError(error_message)]
def _CheckHasNoPipeInComment(input_api, output_api):
""" Checks that comments don't contain pipes."""
pipe_regex = input_api.re.compile(PIPE_IN_COMMENT_PATTERN)
errors = []
for f in input_api.AffectedFiles():
if not _IsInIosPackage(input_api, f.LocalPath()):
continue
for line_num, line in f.ChangedContents():
if pipe_regex.search(line):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if not errors:
return []
warning_message = '\n'.join([
'Please use backticks "`" instead of pipes "|" if you need to quote'
' variable names and symbols in comments.\n'
'Found potential uses of pipes in:'
] + errors) + '\n'
return [output_api.PresubmitPromptWarning(warning_message)]
def _CheckCanImproveTestUsingExpectNSEQ(input_api, output_api):
""" Checks that test files use EXPECT_NSEQ when possible."""
errors = []
# Substrings that should not be used together with EXPECT_TRUE or
# EXPECT_FALSE in tests.
wrong_patterns = ["isEqualToString:", "isEqualToData:", "isEqualToArray:"]
for f in input_api.AffectedFiles():
if not '_unittest.' in f.LocalPath():
continue
for line_num, line in f.ChangedContents():
if line.startswith(("EXPECT_TRUE", "EXPECT_FALSE")):
# Condition is in one line.
if any(x in line for x in wrong_patterns):
errors.append('%s:%s' % (f.LocalPath(), line_num))
# Condition is split on multiple lines.
elif not line.endswith(";"):
# Check this is not the last line.
if line_num < len(f.NewContents()):
next_line = f.NewContents()[line_num]
if any(x in next_line for x in wrong_patterns):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if not errors:
return []
plural_suffix = '' if len(errors) == 1 else 's'
warning_message = '\n'.join([
'Found possible improvement in unittest. Prefer using'
' EXPECT_NSEQ() or EXPECT_NSNE() when possible.'
'\n\nAffected file%(plural)s:' % {
'plural': plural_suffix,
}
] + errors) + '\n'
return [output_api.PresubmitPromptWarning(warning_message)]
def _IsInIosPackage(input_api, path):
""" Returns True if path is within ios package"""
ios_package_regex = input_api.re.compile(IOS_PACKAGE_PATTERN)
return ios_package_regex.search(path)
def _HasIncludeDirective(input_api, line):
""" Returns True if #include is found in the line"""
include_regex = input_api.re.compile(INCLUDE_PATTERN)
return include_regex.search(line)
def _HasToDoWithNoBug(input_api, line):
""" Returns True if TODO is not identified by a bug number."""
todo_regex = input_api.re.compile(TODO_PATTERN)
bug_regex = input_api.re.compile(BUG_PATTERN)
todo_match = todo_regex.search(line)
if not todo_match:
return False
return not bug_regex.match(todo_match.group(1))
def _HasToDoWithDeprecatedBug(input_api, line):
""" Returns True if TODO is identified by a deprecated bug number format."""
todo_regex = input_api.re.compile(TODO_PATTERN)
deprecated_bug_regex = input_api.re.compile(DEPRECATED_BUG_PATTERN)
todo_match = todo_regex.search(line)
if not todo_match:
return False
return deprecated_bug_regex.match(todo_match.group(1))
def _CheckHasNoBoxedBOOL(input_api, output_api):
""" Checks that there are no @(YES) or @(NO)."""
boxed_BOOL_regex = input_api.re.compile(BOXED_BOOL_PATTERN)
errors = []
for f in input_api.AffectedFiles():
for line_num, line in f.ChangedContents():
if boxed_BOOL_regex.search(line):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if not errors:
return []
plural_suffix = '' if len(errors) == 1 else 's'
warning_message = ('Found boxed BOOL%(plural)s. '
'Prefer @YES or @NO in ios code:' % {
'plural': plural_suffix
})
return [output_api.PresubmitPromptWarning(warning_message, items=errors)]
def _CheckNoTearDownEGTest(input_api, output_api):
""" Checks that `- (void)tearDown {` is not present in an egtest.mm"""
errors = []
for f in input_api.AffectedFiles():
if not '_egtest.' in f.LocalPath():
continue
for line_num, line in f.ChangedContents():
if line.startswith("- (void)tearDown {"):
errors.append('%s:%s' % (f.LocalPath(), line_num))
if not errors:
return []
warning_message = '\n'.join([
'To support hermetic EarlGrey test cases, tearDown has been renamed '
'to tearDownHelper, and will soon be removed. If tearDown is really '
'necessary for this test, please use addTeardownBlock'
] + errors) + '\n'
return [output_api.PresubmitError(warning_message)]
def CheckChangeOnUpload(input_api, output_api):
results = []
results.extend(_CheckBugInToDo(input_api, output_api))
results.extend(_CheckNullabilityAnnotations(input_api, output_api))
results.extend(_CheckHasNoIncludeDirectives(input_api, output_api))
results.extend(_CheckHasNoPipeInComment(input_api, output_api))
results.extend(_CheckHasNoBoxedBOOL(input_api, output_api))
results.extend(_CheckNoTearDownEGTest(input_api, output_api))
results.extend(_CheckCanImproveTestUsingExpectNSEQ(input_api, output_api))
return results