blob: 4d17327799b10d2aa7c20f6464c6b1bfe753ad04 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371# Copyright (C) 2014 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7# * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13# * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Yang Guo75beda92019-10-28 07:29:2528"""
29DevTools presubmit script
Blink Reformat4c46d092018-04-07 15:32:3730
31See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
32for more details about the presubmit API built into gcl.
33"""
34
35import sys
Tim van der Lippef515fdc2020-03-06 16:18:2536import six
Tim van der Lippefb023462020-08-21 13:10:0637import time
Blink Reformat4c46d092018-04-07 15:32:3738
Alex Rudenko4a7a3242024-04-18 10:36:5039from pathlib import Path
40
Liviu Rauf3028602023-11-10 10:52:0441# Depot tools imports
42import rdb_wrapper
43
Liviu Raufd2e3212019-12-18 15:38:2044AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com"
Tim van der Lippefb1dc172021-05-11 15:40:2645USE_PYTHON3 = True
Mathias Bynensa0a6e292019-12-17 12:24:0846
Tim van der Lippe4d004ec2020-03-03 18:32:0147def _ExecuteSubProcess(input_api, output_api, script_path, args, results):
Tim van der Lippef515fdc2020-03-06 16:18:2548 if isinstance(script_path, six.string_types):
Philip Pfaffef4320aa2022-07-21 11:33:2449 script_path = [input_api.python3_executable, script_path]
Tim van der Lippef515fdc2020-03-06 16:18:2550
Tim van der Lippefb023462020-08-21 13:10:0651 start_time = time.time()
Sigurd Schneiderf3a1ecd2021-03-02 14:46:0352 process = input_api.subprocess.Popen(script_path + args,
53 stdout=input_api.subprocess.PIPE,
54 stderr=input_api.subprocess.STDOUT)
Tim van der Lippe4d004ec2020-03-03 18:32:0155 out, _ = process.communicate()
Tim van der Lippefb023462020-08-21 13:10:0656 end_time = time.time()
57
58 time_difference = end_time - start_time
59 time_info = "Script execution time was %.1fs seconds\n" % (time_difference)
Tim van der Lippe4d004ec2020-03-03 18:32:0160 if process.returncode != 0:
Tim van der Lippefb1dc172021-05-11 15:40:2661 results.append(
62 output_api.PresubmitError(time_info + out.decode('utf-8')))
Tim van der Lippe4d004ec2020-03-03 18:32:0163 else:
Tim van der Lippefb1dc172021-05-11 15:40:2664 results.append(
65 output_api.PresubmitNotifyResult(time_info + out.decode('utf-8')))
Tim van der Lippe4d004ec2020-03-03 18:32:0166 return results
67
68
Sigurd Schneider5c9b4f92021-01-22 10:09:5569def _CheckBugAssociation(input_api, output_api, is_committing):
70 results = [output_api.PresubmitNotifyResult('Bug Association Check:')]
71 bugs = input_api.change.BugsFromDescription()
72 message = (
73 "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n"
74 "the footer of the commit description. If you explicitly don\'t want to\n"
75 "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n"
76 "Note: The footer of the commit description is the last block of lines in\n"
77 "the commit description that doesn't contain empty lines. This means that\n"
78 "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n"
79 "line are not detected by this presubmit check.")
80
81 if not bugs:
82 if is_committing:
83 results.append(output_api.PresubmitError(message))
84 else:
85 results.append(output_api.PresubmitNotifyResult(message))
86
87 for bug in bugs:
88 results.append(output_api.PresubmitNotifyResult(('%s') % bug))
89
90 return results
91
92
Brandon Goddard33104372020-08-13 15:49:2393def _CheckExperimentTelemetry(input_api, output_api):
Brandon Goddard33104372020-08-13 15:49:2394 experiment_telemetry_files = [
95 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
Christy Chenab9a44d2021-07-02 19:54:3096 'entrypoints', 'main', 'MainImpl.ts'),
Brandon Goddard33104372020-08-13 15:49:2397 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
Tim van der Lippee0247312021-04-01 14:25:3098 'core', 'host', 'UserMetrics.ts')
Brandon Goddard33104372020-08-13 15:49:2399 ]
100 affected_main_files = _getAffectedFiles(input_api,
101 experiment_telemetry_files, [],
Christy Chenab9a44d2021-07-02 19:54:30102 ['.ts'])
Brandon Goddard33104372020-08-13 15:49:23103 if len(affected_main_files) == 0:
Tim van der Lippefb023462020-08-21 13:10:06104 return [
105 output_api.PresubmitNotifyResult(
106 'No affected files for telemetry check')
107 ]
Brandon Goddard33104372020-08-13 15:49:23108
Tim van der Lippefb023462020-08-21 13:10:06109 results = [
110 output_api.PresubmitNotifyResult('Running Experiment Telemetry check:')
111 ]
Brandon Goddard33104372020-08-13 15:49:23112 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
113 'scripts', 'check_experiments.js')
114 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
115 return results
116
117
Jack Franklinb5a63092022-11-30 14:32:36118def _CheckESBuildVersion(input_api, output_api):
119 results = [
120 output_api.PresubmitNotifyResult('Running ESBuild version check:')
121 ]
122 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
123 'scripts',
124 'check_esbuild_versions.js')
125 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
126 return results
127
128
Wolfgang Beyere57322c2024-02-08 12:04:24129def _CheckEnumeratedHistograms(input_api, output_api):
130 enumerated_histograms_files = [
131 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
132 'devtools_compatibility.js'),
133 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
134 'core', 'host', 'InspectorFrontendHostAPI.ts')
135 ]
136 affected_main_files = _getAffectedFiles(input_api,
137 enumerated_histograms_files, [],
138 ['.js', '.ts'])
139 if len(affected_main_files) == 0:
140 return [
141 output_api.PresubmitNotifyResult(
142 'No affected files for UMA Enumerated Histograms check')
143 ]
144
145 results = [
146 output_api.PresubmitNotifyResult(
147 'Running UMA Enumerated Histograms check:')
148 ]
149 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
150 'scripts',
151 'check_enumerated_histograms.js')
152 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
153 return results
154
155
Blink Reformat4c46d092018-04-07 15:32:37156def _CheckFormat(input_api, output_api):
Simon Zündcc994132024-02-15 07:34:44157 files_with_potential_large_diffs = _getAffectedFiles(
158 input_api, [
159 input_api.os_path.join(input_api.PresubmitLocalPath(),
160 'node_modules'),
161 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
162 'third_party'),
163 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
164 'generated'),
165 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
166 'models', 'javascript_metadata'),
167 ], [], [])
Tim van der Lippefdbd42e2020-04-07 14:14:36168
Simon Zündcc994132024-02-15 07:34:44169 # Changes to the above directories can produce large diffs. This is a problem on Windows,
170 # where clang-format-diff.py specifies all the diff ranges on the command line when invoking
171 # clang-format. Since command line length is limited on Win, the invocation fails.
172 # As a heuristic, we'll format all touched files fully if we suspect that the diff could
173 # be large.
Tim van der Lippefdbd42e2020-04-07 14:14:36174 # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs.
Simon Zündcc994132024-02-15 07:34:44175 additional_args = []
176 if (len(files_with_potential_large_diffs) > 0):
177 additional_args = ['--full']
Tim van der Lippefdbd42e2020-04-07 14:14:36178
Brandon Goddarde7028672020-01-30 17:31:04179 results = [output_api.PresubmitNotifyResult('Running Format Checks:')]
Blink Reformat4c46d092018-04-07 15:32:37180
Simon Zündcc994132024-02-15 07:34:44181 return _ExecuteSubProcess(input_api, output_api,
182 ['git', 'cl', 'format', '--js'] +
183 additional_args, [], results)
Blink Reformat4c46d092018-04-07 15:32:37184
Jack Franklin1aa212d2021-09-10 14:20:08185
186def _CheckDevToolsRunESLintTests(input_api, output_api):
187 # Check for changes in the eslint_rules directory, and run the eslint rules
188 # tests if so.
189 # We don't do this on every CL as most do not touch the rules, but if we do
190 # change them we need to make sure all the tests are passing.
Jack Franklin03db63a2021-09-16 13:40:56191 original_sys_path = sys.path
192 try:
193 sys.path = sys.path + [
194 input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')
195 ]
196 import devtools_paths
197 finally:
198 sys.path = original_sys_path
Jack Franklin1aa212d2021-09-10 14:20:08199 eslint_rules_dir_path = input_api.os_path.join(
200 input_api.PresubmitLocalPath(), 'scripts', 'eslint_rules')
201 eslint_rules_affected_files = _getAffectedFiles(input_api,
202 [eslint_rules_dir_path],
203 [], [])
204
205 if (len(eslint_rules_affected_files) == 0):
206 return []
207
Jack Franklin03db63a2021-09-16 13:40:56208 mocha_path = devtools_paths.mocha_path()
Jack Franklin1aa212d2021-09-10 14:20:08209 eslint_tests_path = input_api.os_path.join(eslint_rules_dir_path, 'tests',
210 '*_test.js')
211
212 results = [output_api.PresubmitNotifyResult('ESLint rules unit tests')]
213 results.extend(
214 # The dot reporter is more concise which is useful to not get LOADS of
215 # output when just one test fails.
216 _checkWithNodeScript(input_api, output_api, mocha_path,
217 ['--reporter', 'dot', eslint_tests_path]))
218 return results
219
220
Tim van der Lippe800d8752022-02-04 12:49:56221def _CheckDevToolsRunBuildTests(input_api, output_api):
222 # Check for changes in the build/tests directory, and run the tests if so.
223 # We don't do this on every CL as most do not touch the rules, but if we do
224 # change them we need to make sure all the tests are passing.
225 original_sys_path = sys.path
226 try:
227 sys.path = sys.path + [
228 input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')
229 ]
230 import devtools_paths
231 finally:
232 sys.path = original_sys_path
233 scripts_build_dir_path = input_api.os_path.join(
234 input_api.PresubmitLocalPath(), 'scripts', 'build')
235 scripts_build_affected_files = _getAffectedFiles(input_api,
236 [scripts_build_dir_path],
237 [], [])
238
239 if len(scripts_build_affected_files) == 0:
240 return []
241
242 mocha_path = devtools_paths.mocha_path()
243 build_tests_path = input_api.os_path.join(scripts_build_dir_path, 'tests',
244 '*_test.js')
245
246 results = [output_api.PresubmitNotifyResult('Build plugins unit tests')]
247 results.extend(
248 # The dot reporter is more concise which is useful to not get LOADS of
249 # output when just one test fails.
250 _checkWithNodeScript(input_api, output_api, mocha_path,
251 ['--reporter', 'dot', build_tests_path]))
252 return results
253
254
Mathias Bynens1b2c5e42020-06-18 06:29:21255def _CheckDevToolsStyleJS(input_api, output_api):
Tim van der Lippefb023462020-08-21 13:10:06256 results = [output_api.PresubmitNotifyResult('JS style check:')]
Mathias Bynens1b2c5e42020-06-18 06:29:21257 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
258 'scripts', 'test',
Tim van der Lippef9e565e2021-11-08 16:22:11259 'run_lint_check_js.mjs')
Tim van der Lippe4d004ec2020-03-03 18:32:01260
Mathias Bynens1b2c5e42020-06-18 06:29:21261 front_end_directory = input_api.os_path.join(
262 input_api.PresubmitLocalPath(), 'front_end')
Jack Franklinbcfd6ad2021-02-17 10:12:50263 component_docs_directory = input_api.os_path.join(front_end_directory,
Tim van der Lippee622f552021-04-14 14:15:18264 'ui', 'components',
265 'docs')
Alex Rudenko5556a902020-09-29 09:37:23266 inspector_overlay_directory = input_api.os_path.join(
267 input_api.PresubmitLocalPath(), 'inspector_overlay')
Mathias Bynens1b2c5e42020-06-18 06:29:21268 test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
269 'test')
270 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
271 'scripts')
Tim van der Lippe2a4ae2b2020-03-11 17:28:06272
Mathias Bynens1b2c5e42020-06-18 06:29:21273 default_linted_directories = [
Alex Rudenko5556a902020-09-29 09:37:23274 front_end_directory, test_directory, scripts_directory,
275 inspector_overlay_directory
Mathias Bynens1b2c5e42020-06-18 06:29:21276 ]
Tim van der Lippe2a4ae2b2020-03-11 17:28:06277
278 eslint_related_files = [
Mathias Bynens1b2c5e42020-06-18 06:29:21279 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
280 'eslint'),
Tim van der Lippecf4ab402021-02-12 14:30:58281 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
282 '@typescript-eslint'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06283 input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'),
Mathias Bynens1b2c5e42020-06-18 06:29:21284 input_api.os_path.join(input_api.PresubmitLocalPath(),
285 '.eslintignore'),
Tim van der Lippe33543ac2020-12-14 14:37:45286 input_api.os_path.join(front_end_directory, '.eslintrc.js'),
Jack Franklinbcfd6ad2021-02-17 10:12:50287 input_api.os_path.join(component_docs_directory, '.eslintrc.js'),
Tim van der Lippe406249f2020-12-14 14:59:10288 input_api.os_path.join(test_directory, '.eslintrc.js'),
Mathias Bynens1b2c5e42020-06-18 06:29:21289 input_api.os_path.join(scripts_directory, 'test',
290 'run_lint_check_js.py'),
291 input_api.os_path.join(scripts_directory, 'test',
Tim van der Lippef9e565e2021-11-08 16:22:11292 'run_lint_check_js.mjs'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06293 input_api.os_path.join(scripts_directory, '.eslintrc.js'),
294 input_api.os_path.join(scripts_directory, 'eslint_rules'),
295 ]
296
Mathias Bynens1b2c5e42020-06-18 06:29:21297 lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [],
298 ['.js', '.py', '.eslintignore'])
Tim van der Lippe2a4ae2b2020-03-11 17:28:06299
Mathias Bynens0ec56612020-06-19 07:14:03300 should_bail_out, files_to_lint = _getFilesToLint(
301 input_api, output_api, lint_config_files, default_linted_directories,
302 ['.js', '.ts'], results)
303 if should_bail_out:
Mathias Bynens1b2c5e42020-06-18 06:29:21304 return results
Tim van der Lippe2a4ae2b2020-03-11 17:28:06305
Brandon Goddarde34e94f2021-04-12 17:58:26306 # If there are more than 50 files to check, don't bother and check
307 # everything, so as to not run into command line length limits on Windows.
308 if len(files_to_lint) > 50:
309 files_to_lint = []
310
Mathias Bynens1b2c5e42020-06-18 06:29:21311 results.extend(
312 _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint))
Tim van der Lippe98132242020-04-14 16:16:54313 return results
Blink Reformat4c46d092018-04-07 15:32:37314
315
Mathias Bynens1b2c5e42020-06-18 06:29:21316def _CheckDevToolsStyleCSS(input_api, output_api):
Tim van der Lippefb023462020-08-21 13:10:06317 results = [output_api.PresubmitNotifyResult('CSS style check:')]
Mathias Bynens1b2c5e42020-06-18 06:29:21318 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
319 'scripts', 'test',
Jack Franklinbc302342021-01-18 10:03:30320 'run_lint_check_css.js')
Mathias Bynens1b2c5e42020-06-18 06:29:21321
322 front_end_directory = input_api.os_path.join(
323 input_api.PresubmitLocalPath(), 'front_end')
Alex Rudenko5556a902020-09-29 09:37:23324 inspector_overlay_directory = input_api.os_path.join(
325 input_api.PresubmitLocalPath(), 'inspector_overlay')
326 default_linted_directories = [
327 front_end_directory, inspector_overlay_directory
328 ]
Mathias Bynens1b2c5e42020-06-18 06:29:21329
330 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
331 'scripts')
332
333 stylelint_related_files = [
334 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
335 'stylelint'),
336 input_api.os_path.join(input_api.PresubmitLocalPath(),
337 '.stylelintrc.json'),
338 input_api.os_path.join(input_api.PresubmitLocalPath(),
339 '.stylelintignore'),
340 input_api.os_path.join(scripts_directory, 'test',
Sigurd Schneider6523c512021-02-12 09:44:28341 'run_lint_check_css.js'),
Mathias Bynens1b2c5e42020-06-18 06:29:21342 ]
343
344 lint_config_files = _getAffectedFiles(input_api, stylelint_related_files,
Sigurd Schneider6523c512021-02-12 09:44:28345 [], [])
Mathias Bynens1b2c5e42020-06-18 06:29:21346
Sigurd Schneidere3bf6c22021-02-11 14:35:23347 css_should_bail_out, css_files_to_lint = _getFilesToLint(
Mathias Bynens0ec56612020-06-19 07:14:03348 input_api, output_api, lint_config_files, default_linted_directories,
349 ['.css'], results)
Mathias Bynens1b2c5e42020-06-18 06:29:21350
Sigurd Schneiderf3a1ecd2021-03-02 14:46:03351 # If there are more than 50 files to check, don't bother and check
352 # everything, so as to not run into command line length limits on Windows.
353 if not css_should_bail_out:
354 if len(css_files_to_lint) < 50:
355 script_args = ["--files"] + css_files_to_lint
356 else:
357 script_args = [] # The defaults check all CSS files.
358 results.extend(
359 _checkWithNodeScript(input_api, output_api, lint_path,
360 script_args))
361
Jack Franklinbc302342021-01-18 10:03:30362 return results
Mathias Bynens1b2c5e42020-06-18 06:29:21363
364
Tim van der Lippea53672d2021-07-08 14:52:35365def _CheckDevToolsNonJSFileLicenseHeaders(input_api, output_api):
Tim van der Lippe81752502021-05-26 14:38:12366 results = [
367 output_api.PresubmitNotifyResult(
368 'Python-like file license header check:')
369 ]
Tim van der Lippea53672d2021-07-08 14:52:35370 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
371 'scripts', 'test',
372 'run_header_check_non_js_files.js')
Tim van der Lippe81752502021-05-26 14:38:12373
374 front_end_directory = input_api.os_path.join(
375 input_api.PresubmitLocalPath(), 'front_end')
376 inspector_overlay_directory = input_api.os_path.join(
377 input_api.PresubmitLocalPath(), 'inspector_overlay')
378 test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
379 'test')
380 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
381 'scripts')
Tim van der Lippe8b929542021-05-26 14:54:20382 config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
383 'config')
Tim van der Lippe81752502021-05-26 14:38:12384
385 default_linted_directories = [
386 front_end_directory, test_directory, scripts_directory,
Tim van der Lippe8b929542021-05-26 14:54:20387 inspector_overlay_directory, config_directory
Tim van der Lippe81752502021-05-26 14:38:12388 ]
389
390 check_related_files = [lint_path]
391
392 lint_config_files = _getAffectedFiles(input_api, check_related_files, [],
393 ['.js'])
394
395 should_bail_out, files_to_lint = _getFilesToLint(
396 input_api, output_api, lint_config_files, default_linted_directories,
Tim van der Lippea53672d2021-07-08 14:52:35397 ['BUILD.gn', '.gni', '.css'], results)
Tim van der Lippe81752502021-05-26 14:38:12398 if should_bail_out:
399 return results
400
401 # If there are more than 50 files to check, don't bother and check
402 # everything, so as to not run into command line length limits on Windows.
403 if len(files_to_lint) > 50:
404 files_to_lint = []
405
406 results.extend(
407 _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint))
408 return results
409
410
Tim van der Lippe4d004ec2020-03-03 18:32:01411def _CheckGeneratedFiles(input_api, output_api):
Tim van der Lippeb3b90762020-03-04 15:21:52412 v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8')
413 blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink')
414 protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol')
415 scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')
Tim van der Lippe5d2d79b2020-03-23 11:45:04416 scripts_generated_output_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'generated')
Tim van der Lippeb3b90762020-03-04 15:21:52417
418 generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py')
419 generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py')
Simon Zünd2ce67542023-02-07 10:15:14420 generated_deprecation_path = input_api.os_path.join(
421 scripts_build_path, 'generate_deprecations.py')
Tim van der Lippeb3b90762020-03-04 15:21:52422 generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py')
Tim van der Lippe2a1eac22021-05-13 15:19:29423 generated_protocol_typescript_path = input_api.os_path.join(
424 input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript')
Tim van der Lippeb3b90762020-03-04 15:21:52425 concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol',
426 'concatenate_protocols.py')
427
428 affected_files = _getAffectedFiles(input_api, [
429 v8_directory_path,
430 blink_directory_path,
Tim van der Lippe2a1eac22021-05-13 15:19:29431 input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party',
432 'pyjson5'),
Tim van der Lippeb3b90762020-03-04 15:21:52433 generated_aria_path,
434 generated_supported_css_path,
Simon Zünd2ce67542023-02-07 10:15:14435 generated_deprecation_path,
Tim van der Lippeb3b90762020-03-04 15:21:52436 concatenate_protocols_path,
437 generated_protocol_path,
Tim van der Lippe5d2d79b2020-03-23 11:45:04438 scripts_generated_output_path,
Tim van der Lippe2a1eac22021-05-13 15:19:29439 generated_protocol_typescript_path,
440 ], [], ['.pdl', '.json5', '.py', '.js', '.ts'])
Tim van der Lippeb3b90762020-03-04 15:21:52441
442 if len(affected_files) == 0:
Tim van der Lippefb023462020-08-21 13:10:06443 return [
444 output_api.PresubmitNotifyResult(
445 'No affected files for generated files check')
446 ]
Tim van der Lippeb3b90762020-03-04 15:21:52447
Tim van der Lippe4d004ec2020-03-03 18:32:01448 results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')]
Tim van der Lippeb0d65f12020-03-05 12:15:24449 generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps',
450 'generate_protocol_resources.py')
Tim van der Lippe4d004ec2020-03-03 18:32:01451
Tim van der Lippeb0d65f12020-03-05 12:15:24452 return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results)
Tim van der Lippe4d004ec2020-03-03 18:32:01453
454
Simon Zünd9ff4da62022-11-22 09:25:59455def _CheckL10nStrings(input_api, output_api):
Christy Chen2d6d9a62020-09-22 16:04:09456 devtools_root = input_api.PresubmitLocalPath()
457 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
Tim van der Lippe25f11082021-06-24 15:28:08458 script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n',
Simon Zünd9ff4da62022-11-22 09:25:59459 'check-strings.js')
Tim van der Lippe25f11082021-06-24 15:28:08460 affected_front_end_files = _getAffectedFiles(
461 input_api, [devtools_front_end, script_path], [], ['.js', '.ts'])
Christy Chen2d6d9a62020-09-22 16:04:09462 if len(affected_front_end_files) == 0:
463 return [
464 output_api.PresubmitNotifyResult(
Simon Zünd9ff4da62022-11-22 09:25:59465 'No affected files to run check-strings')
Christy Chen2d6d9a62020-09-22 16:04:09466 ]
467
468 results = [
Simon Zünd9ff4da62022-11-22 09:25:59469 output_api.PresubmitNotifyResult('Checking UI strings from front_end:')
Christy Chen2d6d9a62020-09-22 16:04:09470 ]
Tim van der Lippe25f11082021-06-24 15:28:08471 results.extend(
472 _checkWithNodeScript(input_api, output_api, script_path,
473 [devtools_front_end]))
Christy Chen2d6d9a62020-09-22 16:04:09474 return results
475
476
Tim van der Lippe5279f842020-01-14 16:26:38477def _CheckNoUncheckedFiles(input_api, output_api):
478 results = []
479 process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'],
480 stdout=input_api.subprocess.PIPE,
481 stderr=input_api.subprocess.STDOUT)
482 out, _ = process.communicate()
483 if process.returncode != 0:
Jack Franklin324f3042020-09-03 10:28:29484 files_changed_process = input_api.subprocess.Popen(
Tim van der Lippe25f11082021-06-24 15:28:08485 ['git', 'diff'],
Jack Franklin324f3042020-09-03 10:28:29486 stdout=input_api.subprocess.PIPE,
487 stderr=input_api.subprocess.STDOUT)
Tim van der Lippe9bb1cf62020-03-06 16:17:02488 files_changed, _ = files_changed_process.communicate()
489
490 return [
Tim van der Lippefb1dc172021-05-11 15:40:26491 output_api.PresubmitError(
492 'You have changed files that need to be committed:'),
493 output_api.PresubmitError(files_changed.decode('utf-8'))
Tim van der Lippe9bb1cf62020-03-06 16:17:02494 ]
Tim van der Lippe5279f842020-01-14 16:26:38495 return []
496
Tim van der Lippe8fdda112020-01-27 11:27:06497def _CheckForTooLargeFiles(input_api, output_api):
Christy Chen1ab87e02020-01-31 00:32:16498 """Avoid large files, especially binary files, in the repository since
Tim van der Lippe8fdda112020-01-27 11:27:06499 git doesn't scale well for those. They will be in everyone's repo
500 clones forever, forever making Chromium slower to clone and work
501 with."""
Christy Chen1ab87e02020-01-31 00:32:16502 # Uploading files to cloud storage is not trivial so we don't want
503 # to set the limit too low, but the upper limit for "normal" large
504 # files seems to be 1-2 MB, with a handful around 5-8 MB, so
505 # anything over 20 MB is exceptional.
506 TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB
507 too_large_files = []
508 for f in input_api.AffectedFiles():
509 # Check both added and modified files (but not deleted files).
510 if f.Action() in ('A', 'M'):
511 size = input_api.os_path.getsize(f.AbsoluteLocalPath())
512 if size > TOO_LARGE_FILE_SIZE_LIMIT:
513 too_large_files.append("%s: %d bytes" % (f.LocalPath(), size))
514 if too_large_files:
515 message = (
516 'Do not commit large files to git since git scales badly for those.\n' +
517 'Instead put the large files in cloud storage and use DEPS to\n' +
518 'fetch them.\n' + '\n'.join(too_large_files)
519 )
520 return [output_api.PresubmitError(
521 'Too large files found in commit', long_text=message + '\n')]
522 else:
523 return []
Tim van der Lippe8fdda112020-01-27 11:27:06524
Tim van der Lippe5279f842020-01-14 16:26:38525
Andrés Olivares205bf682023-02-01 10:47:13526def _CheckObsoleteScreenshotGoldens(input_api, output_api):
527 results = [
528 output_api.PresubmitNotifyResult('Obsolete screenshot images check')
529 ]
530 interaction_test_root_path = input_api.os_path.join(
531 input_api.PresubmitLocalPath(), 'test', 'interactions')
532 interaction_test_files = [interaction_test_root_path]
533
534 interaction_test_files_changed = _getAffectedFiles(input_api,
535 interaction_test_files,
536 [], [])
537
538 if len(interaction_test_files_changed) > 0:
539 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
540 'scripts', 'test',
541 'check_obsolete_goldens.js')
Andrés Olivares205bf682023-02-01 10:47:13542
Philip Pfaffece5afc02024-04-09 13:08:58543 script_args = []
Andrés Olivares205bf682023-02-01 10:47:13544 errors_from_script = _checkWithNodeScript(input_api, output_api,
545 script_path, script_args)
546 results.extend(errors_from_script)
547
548 return results
549
550
Liviu Rauf3028602023-11-10 10:52:04551def _WithArgs(checkType, **kwargs):
552 def _WithArgsWrapper(input_api, output_api):
553 return checkType(input_api, output_api, **kwargs)
554
555 _WithArgsWrapper.__name__ = checkType.__name__
556 return _WithArgsWrapper
Tim van der Lippef8a87092020-09-14 12:01:18557
558
Liviu Rauf3028602023-11-10 10:52:04559def _CannedChecks(canned_checks):
560 return [
561 canned_checks.CheckForCommitObjects,
562 canned_checks.CheckOwnersFormat,
563 canned_checks.CheckOwners,
564 canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol,
565 _WithArgs(canned_checks.CheckChangeHasNoStrayWhitespace,
566 source_file_filter=lambda file: not file.LocalPath().
567 startswith('node_modules')),
568 canned_checks.CheckGenderNeutral,
569 ]
Jack Franklinb10193f2021-03-19 10:25:08570
Liviu Rauf3028602023-11-10 10:52:04571
572def _CommonChecks(canned_checks):
573 local_checks = [
574 _WithArgs(canned_checks.CheckAuthorizedAuthor,
575 bot_allowlist=[AUTOROLL_ACCOUNT]), _CheckExperimentTelemetry,
576 _CheckGeneratedFiles, _CheckDevToolsStyleJS, _CheckDevToolsStyleCSS,
577 _CheckDevToolsRunESLintTests, _CheckDevToolsRunBuildTests,
578 _CheckDevToolsNonJSFileLicenseHeaders, _CheckFormat,
Wolfgang Beyere57322c2024-02-08 12:04:24579 _CheckESBuildVersion, _CheckEnumeratedHistograms,
Alex Rudenko4a7a3242024-04-18 10:36:50580 _CheckObsoleteScreenshotGoldens, _CheckNodeModules
Liviu Rauf3028602023-11-10 10:52:04581 ]
Tim van der Lippef8a87092020-09-14 12:01:18582 # Run the canned checks from `depot_tools` after the custom DevTools checks.
583 # The canned checks for example check that lines have line endings. The
584 # DevTools presubmit checks automatically fix these issues. If we would run
585 # the canned checks before the DevTools checks, they would erroneously conclude
586 # that there are issues in the code. Since the canned checks are allowed to be
587 # ignored, a confusing message is shown that asks if the failed presubmit can
588 # be continued regardless. By fixing the issues before we reach the canned checks,
589 # we don't show the message to suppress these errors, which would otherwise be
590 # causing CQ to fail.
Liviu Rauf3028602023-11-10 10:52:04591 return local_checks + _CannedChecks(canned_checks)
Kalon Hindsd44fddf2020-12-10 13:43:25592
593
594def _SideEffectChecks(input_api, output_api):
595 """Check side effects caused by other checks"""
596 results = []
Tim van der Lippe5279f842020-01-14 16:26:38597 results.extend(_CheckNoUncheckedFiles(input_api, output_api))
Tim van der Lippe8fdda112020-01-27 11:27:06598 results.extend(_CheckForTooLargeFiles(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37599 return results
600
601
Liviu Rauf3028602023-11-10 10:52:04602def _RunAllChecks(checks, input_api, output_api):
603 with rdb_wrapper.client("presubmit:") as sink:
604 results = []
605 for check in checks:
606 start_time = time.time()
607
608 result = check(input_api, output_api)
609
610 elapsed_time = time.time() - start_time
611 results.extend(result)
612
613 if not sink:
614 continue
615 failure_reason = None
616 status = rdb_wrapper.STATUS_PASS
617 if any(r.fatal for r in result):
618 status = rdb_wrapper.STATUS_FAIL
619 failure_reasons = []
620 for r in result:
621 fields = r.json_format()
622 message = fields['message']
623 items = '\n'.join(' %s' % item
624 for item in fields['items'])
625 failure_reasons.append('\n'.join([message, items]))
626 if failure_reasons:
627 failure_reason = '\n'.join(failure_reasons)
628 sink.report(check.__name__, status, elapsed_time, failure_reason)
629
630 return results
631
632
Liviu Raud614e092020-01-08 09:56:33633def CheckChangeOnUpload(input_api, output_api):
Liviu Rauf3028602023-11-10 10:52:04634 checks = _CommonChecks(input_api.canned_checks) + [
635 _CheckL10nStrings,
636 # Run checks that rely on output from other DevTool checks
637 _SideEffectChecks,
638 _WithArgs(_CheckBugAssociation, is_committing=False),
639 ]
640 return _RunAllChecks(checks, input_api, output_api)
Liviu Raud614e092020-01-08 09:56:33641
642
Blink Reformat4c46d092018-04-07 15:32:37643def CheckChangeOnCommit(input_api, output_api):
Liviu Rauf3028602023-11-10 10:52:04644 checks = _CommonChecks(input_api.canned_checks) + [
645 _CheckL10nStrings,
646 # Run checks that rely on output from other DevTool checks
647 _SideEffectChecks,
648 input_api.canned_checks.CheckChangeHasDescription,
649 _WithArgs(_CheckBugAssociation, is_committing=True),
650 ]
651 return _RunAllChecks(checks, input_api, output_api)
Blink Reformat4c46d092018-04-07 15:32:37652
653
Mandy Chena6be46a2019-07-09 17:06:27654def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 07:29:25655 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27656 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 07:29:25657 """
Mandy Chena6be46a2019-07-09 17:06:27658 local_paths = [
659 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
660 ]
661 affected_files = [
Tim van der Lippefb1dc172021-05-11 15:40:26662 file_name for file_name in local_paths
663 if any(parent_directory in file_name
664 for parent_directory in parent_directories) and (
665 len(accepted_endings) == 0 or any(
666 file_name.endswith(accepted_ending)
667 for accepted_ending in accepted_endings))
Mandy Chena6be46a2019-07-09 17:06:27668 ]
669 return affected_files
670
671
Tim van der Lippec4617122020-03-06 16:24:19672def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37673 original_sys_path = sys.path
674 try:
Yang Guo75beda92019-10-28 07:29:25675 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35676 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37677 finally:
678 sys.path = original_sys_path
679
Tim van der Lippec4617122020-03-06 16:24:19680 return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, [])
Mathias Bynens1b2c5e42020-06-18 06:29:21681
682
683def _getFilesToLint(input_api, output_api, lint_config_files,
684 default_linted_directories, accepted_endings, results):
Mathias Bynens0ec56612020-06-19 07:14:03685 run_full_check = False
Mathias Bynens1b2c5e42020-06-18 06:29:21686 files_to_lint = []
687
688 # We are changing the lint configuration; run the full check.
Tim van der Lippefb1dc172021-05-11 15:40:26689 if len(lint_config_files) != 0:
Mathias Bynens1b2c5e42020-06-18 06:29:21690 results.append(
691 output_api.PresubmitNotifyResult('Running full lint check'))
Mathias Bynens0ec56612020-06-19 07:14:03692 run_full_check = True
Mathias Bynens1b2c5e42020-06-18 06:29:21693 else:
694 # Only run the linter on files that are relevant, to save PRESUBMIT time.
695 files_to_lint = _getAffectedFiles(input_api,
696 default_linted_directories, ['D'],
697 accepted_endings)
698
Jack Franklin130d2ae2022-07-12 09:51:26699 # Exclude front_end/third_party and front_end/generated files.
Tim van der Lippefb1dc172021-05-11 15:40:26700 files_to_lint = [
Jack Franklin130d2ae2022-07-12 09:51:26701 file for file in files_to_lint
702 if "third_party" not in file or "generated" not in file
Tim van der Lippefb1dc172021-05-11 15:40:26703 ]
Paul Lewis2b9224f2020-09-08 17:13:10704
Tim van der Lippefb1dc172021-05-11 15:40:26705 if len(files_to_lint) == 0:
Mathias Bynens1b2c5e42020-06-18 06:29:21706 results.append(
707 output_api.PresubmitNotifyResult(
708 'No affected files for lint check'))
709
Tim van der Lippefb1dc172021-05-11 15:40:26710 should_bail_out = len(files_to_lint) == 0 and not run_full_check
Mathias Bynens0ec56612020-06-19 07:14:03711 return should_bail_out, files_to_lint
Alex Rudenko4a7a3242024-04-18 10:36:50712
713
714def _CheckNodeModules(input_api, output_api):
715
716 files = ['.clang-format', 'OWNERS', 'README.chromium']
717
718 results = []
719 for file in files:
720 file_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
721 'node_modules', file)
722 if not Path(file_path).is_file():
723 results.extend(
724 output_api.PresubmitError(
725 "node_modules/%s is missing. Use npm run install-deps to re-create it."
726 % file))
727
728 return []