Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | # 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 Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 28 | """ |
| 29 | DevTools presubmit script |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | |
| 31 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 32 | for more details about the presubmit API built into gcl. |
| 33 | """ |
| 34 | |
| 35 | import sys |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 36 | import six |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 37 | import time |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame^] | 39 | from pathlib import Path |
| 40 | |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 41 | # Depot tools imports |
| 42 | import rdb_wrapper |
| 43 | |
Liviu Rau | fd2e321 | 2019-12-18 15:38:20 | [diff] [blame] | 44 | AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com" |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 45 | USE_PYTHON3 = True |
Mathias Bynens | a0a6e29 | 2019-12-17 12:24:08 | [diff] [blame] | 46 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 47 | def _ExecuteSubProcess(input_api, output_api, script_path, args, results): |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 48 | if isinstance(script_path, six.string_types): |
Philip Pfaffe | f4320aa | 2022-07-21 11:33:24 | [diff] [blame] | 49 | script_path = [input_api.python3_executable, script_path] |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 50 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 51 | start_time = time.time() |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 52 | process = input_api.subprocess.Popen(script_path + args, |
| 53 | stdout=input_api.subprocess.PIPE, |
| 54 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 55 | out, _ = process.communicate() |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 56 | 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 Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 60 | if process.returncode != 0: |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 61 | results.append( |
| 62 | output_api.PresubmitError(time_info + out.decode('utf-8'))) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 63 | else: |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 64 | results.append( |
| 65 | output_api.PresubmitNotifyResult(time_info + out.decode('utf-8'))) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 66 | return results |
| 67 | |
| 68 | |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 69 | def _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 Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 93 | def _CheckExperimentTelemetry(input_api, output_api): |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 94 | experiment_telemetry_files = [ |
| 95 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame] | 96 | 'entrypoints', 'main', 'MainImpl.ts'), |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 97 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Tim van der Lippe | e024731 | 2021-04-01 14:25:30 | [diff] [blame] | 98 | 'core', 'host', 'UserMetrics.ts') |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 99 | ] |
| 100 | affected_main_files = _getAffectedFiles(input_api, |
| 101 | experiment_telemetry_files, [], |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame] | 102 | ['.ts']) |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 103 | if len(affected_main_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 104 | return [ |
| 105 | output_api.PresubmitNotifyResult( |
| 106 | 'No affected files for telemetry check') |
| 107 | ] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 108 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 109 | results = [ |
| 110 | output_api.PresubmitNotifyResult('Running Experiment Telemetry check:') |
| 111 | ] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 112 | 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 Franklin | b5a6309 | 2022-11-30 14:32:36 | [diff] [blame] | 118 | def _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 Beyer | e57322c | 2024-02-08 12:04:24 | [diff] [blame] | 129 | def _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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 156 | def _CheckFormat(input_api, output_api): |
Simon Zünd | cc99413 | 2024-02-15 07:34:44 | [diff] [blame] | 157 | 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 Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 168 | |
Simon Zünd | cc99413 | 2024-02-15 07:34:44 | [diff] [blame] | 169 | # 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 Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 174 | # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs. |
Simon Zünd | cc99413 | 2024-02-15 07:34:44 | [diff] [blame] | 175 | additional_args = [] |
| 176 | if (len(files_with_potential_large_diffs) > 0): |
| 177 | additional_args = ['--full'] |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 178 | |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 179 | results = [output_api.PresubmitNotifyResult('Running Format Checks:')] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | |
Simon Zünd | cc99413 | 2024-02-15 07:34:44 | [diff] [blame] | 181 | return _ExecuteSubProcess(input_api, output_api, |
| 182 | ['git', 'cl', 'format', '--js'] + |
| 183 | additional_args, [], results) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | |
Jack Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 185 | |
| 186 | def _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 Franklin | 03db63a | 2021-09-16 13:40:56 | [diff] [blame] | 191 | 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 Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 199 | 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 Franklin | 03db63a | 2021-09-16 13:40:56 | [diff] [blame] | 208 | mocha_path = devtools_paths.mocha_path() |
Jack Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 209 | 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 Lippe | 800d875 | 2022-02-04 12:49:56 | [diff] [blame] | 221 | def _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 Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 255 | def _CheckDevToolsStyleJS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 256 | results = [output_api.PresubmitNotifyResult('JS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 257 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 258 | 'scripts', 'test', |
Tim van der Lippe | f9e565e | 2021-11-08 16:22:11 | [diff] [blame] | 259 | 'run_lint_check_js.mjs') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 260 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 261 | front_end_directory = input_api.os_path.join( |
| 262 | input_api.PresubmitLocalPath(), 'front_end') |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 | [diff] [blame] | 263 | component_docs_directory = input_api.os_path.join(front_end_directory, |
Tim van der Lippe | e622f55 | 2021-04-14 14:15:18 | [diff] [blame] | 264 | 'ui', 'components', |
| 265 | 'docs') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 266 | inspector_overlay_directory = input_api.os_path.join( |
| 267 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 268 | 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 Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 272 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 273 | default_linted_directories = [ |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 274 | front_end_directory, test_directory, scripts_directory, |
| 275 | inspector_overlay_directory |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 276 | ] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 277 | |
| 278 | eslint_related_files = [ |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 279 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 280 | 'eslint'), |
Tim van der Lippe | cf4ab40 | 2021-02-12 14:30:58 | [diff] [blame] | 281 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 282 | '@typescript-eslint'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 283 | input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 284 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 285 | '.eslintignore'), |
Tim van der Lippe | 33543ac | 2020-12-14 14:37:45 | [diff] [blame] | 286 | input_api.os_path.join(front_end_directory, '.eslintrc.js'), |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 | [diff] [blame] | 287 | input_api.os_path.join(component_docs_directory, '.eslintrc.js'), |
Tim van der Lippe | 406249f | 2020-12-14 14:59:10 | [diff] [blame] | 288 | input_api.os_path.join(test_directory, '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 289 | 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 Lippe | f9e565e | 2021-11-08 16:22:11 | [diff] [blame] | 292 | 'run_lint_check_js.mjs'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 293 | input_api.os_path.join(scripts_directory, '.eslintrc.js'), |
| 294 | input_api.os_path.join(scripts_directory, 'eslint_rules'), |
| 295 | ] |
| 296 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 297 | lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [], |
| 298 | ['.js', '.py', '.eslintignore']) |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 299 | |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 300 | 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 Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 304 | return results |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 305 | |
Brandon Goddard | e34e94f | 2021-04-12 17:58:26 | [diff] [blame] | 306 | # 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 Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 311 | results.extend( |
| 312 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
Tim van der Lippe | 9813224 | 2020-04-14 16:16:54 | [diff] [blame] | 313 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 314 | |
| 315 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 316 | def _CheckDevToolsStyleCSS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 317 | results = [output_api.PresubmitNotifyResult('CSS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 318 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 319 | 'scripts', 'test', |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 320 | 'run_lint_check_css.js') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 321 | |
| 322 | front_end_directory = input_api.os_path.join( |
| 323 | input_api.PresubmitLocalPath(), 'front_end') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 324 | 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 Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 329 | |
| 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 Schneider | 6523c51 | 2021-02-12 09:44:28 | [diff] [blame] | 341 | 'run_lint_check_css.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 342 | ] |
| 343 | |
| 344 | lint_config_files = _getAffectedFiles(input_api, stylelint_related_files, |
Sigurd Schneider | 6523c51 | 2021-02-12 09:44:28 | [diff] [blame] | 345 | [], []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 346 | |
Sigurd Schneider | e3bf6c2 | 2021-02-11 14:35:23 | [diff] [blame] | 347 | css_should_bail_out, css_files_to_lint = _getFilesToLint( |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 348 | input_api, output_api, lint_config_files, default_linted_directories, |
| 349 | ['.css'], results) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 350 | |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 351 | # 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 Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 362 | return results |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 363 | |
| 364 | |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 365 | def _CheckDevToolsNonJSFileLicenseHeaders(input_api, output_api): |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 366 | results = [ |
| 367 | output_api.PresubmitNotifyResult( |
| 368 | 'Python-like file license header check:') |
| 369 | ] |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 370 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 371 | 'scripts', 'test', |
| 372 | 'run_header_check_non_js_files.js') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 373 | |
| 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 Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 382 | config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 383 | 'config') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 384 | |
| 385 | default_linted_directories = [ |
| 386 | front_end_directory, test_directory, scripts_directory, |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 387 | inspector_overlay_directory, config_directory |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 388 | ] |
| 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 Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 397 | ['BUILD.gn', '.gni', '.css'], results) |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 398 | 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 Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 411 | def _CheckGeneratedFiles(input_api, output_api): |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 412 | 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 Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 416 | scripts_generated_output_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'generated') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 417 | |
| 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ünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 420 | generated_deprecation_path = input_api.os_path.join( |
| 421 | scripts_build_path, 'generate_deprecations.py') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 422 | generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py') |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 423 | generated_protocol_typescript_path = input_api.os_path.join( |
| 424 | input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 425 | 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 Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 431 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', |
| 432 | 'pyjson5'), |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 433 | generated_aria_path, |
| 434 | generated_supported_css_path, |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 435 | generated_deprecation_path, |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 436 | concatenate_protocols_path, |
| 437 | generated_protocol_path, |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 438 | scripts_generated_output_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 439 | generated_protocol_typescript_path, |
| 440 | ], [], ['.pdl', '.json5', '.py', '.js', '.ts']) |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 441 | |
| 442 | if len(affected_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 443 | return [ |
| 444 | output_api.PresubmitNotifyResult( |
| 445 | 'No affected files for generated files check') |
| 446 | ] |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 447 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 448 | results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')] |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 449 | generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps', |
| 450 | 'generate_protocol_resources.py') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 451 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 452 | return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 453 | |
| 454 | |
Simon Zünd | 9ff4da6 | 2022-11-22 09:25:59 | [diff] [blame] | 455 | def _CheckL10nStrings(input_api, output_api): |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 456 | devtools_root = input_api.PresubmitLocalPath() |
| 457 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 458 | script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n', |
Simon Zünd | 9ff4da6 | 2022-11-22 09:25:59 | [diff] [blame] | 459 | 'check-strings.js') |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 460 | affected_front_end_files = _getAffectedFiles( |
| 461 | input_api, [devtools_front_end, script_path], [], ['.js', '.ts']) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 462 | if len(affected_front_end_files) == 0: |
| 463 | return [ |
| 464 | output_api.PresubmitNotifyResult( |
Simon Zünd | 9ff4da6 | 2022-11-22 09:25:59 | [diff] [blame] | 465 | 'No affected files to run check-strings') |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 466 | ] |
| 467 | |
| 468 | results = [ |
Simon Zünd | 9ff4da6 | 2022-11-22 09:25:59 | [diff] [blame] | 469 | output_api.PresubmitNotifyResult('Checking UI strings from front_end:') |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 470 | ] |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 471 | results.extend( |
| 472 | _checkWithNodeScript(input_api, output_api, script_path, |
| 473 | [devtools_front_end])) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 474 | return results |
| 475 | |
| 476 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 477 | def _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 Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 484 | files_changed_process = input_api.subprocess.Popen( |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 485 | ['git', 'diff'], |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 486 | stdout=input_api.subprocess.PIPE, |
| 487 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 9bb1cf6 | 2020-03-06 16:17:02 | [diff] [blame] | 488 | files_changed, _ = files_changed_process.communicate() |
| 489 | |
| 490 | return [ |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 491 | 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 Lippe | 9bb1cf6 | 2020-03-06 16:17:02 | [diff] [blame] | 494 | ] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 495 | return [] |
| 496 | |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 497 | def _CheckForTooLargeFiles(input_api, output_api): |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 498 | """Avoid large files, especially binary files, in the repository since |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 499 | 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 Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 502 | # 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 Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 524 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 525 | |
Andrés Olivares | 205bf68 | 2023-02-01 10:47:13 | [diff] [blame] | 526 | def _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 Olivares | 205bf68 | 2023-02-01 10:47:13 | [diff] [blame] | 542 | |
Philip Pfaffe | ce5afc0 | 2024-04-09 13:08:58 | [diff] [blame] | 543 | script_args = [] |
Andrés Olivares | 205bf68 | 2023-02-01 10:47:13 | [diff] [blame] | 544 | 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 Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 551 | def _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 Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 557 | |
| 558 | |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 559 | def _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 Franklin | b10193f | 2021-03-19 10:25:08 | [diff] [blame] | 570 | |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 571 | |
| 572 | def _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 Beyer | e57322c | 2024-02-08 12:04:24 | [diff] [blame] | 579 | _CheckESBuildVersion, _CheckEnumeratedHistograms, |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame^] | 580 | _CheckObsoleteScreenshotGoldens, _CheckNodeModules |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 581 | ] |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 582 | # 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 Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 591 | return local_checks + _CannedChecks(canned_checks) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 592 | |
| 593 | |
| 594 | def _SideEffectChecks(input_api, output_api): |
| 595 | """Check side effects caused by other checks""" |
| 596 | results = [] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 597 | results.extend(_CheckNoUncheckedFiles(input_api, output_api)) |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 598 | results.extend(_CheckForTooLargeFiles(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 599 | return results |
| 600 | |
| 601 | |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 602 | def _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 Rau | d614e09 | 2020-01-08 09:56:33 | [diff] [blame] | 633 | def CheckChangeOnUpload(input_api, output_api): |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 634 | 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 Rau | d614e09 | 2020-01-08 09:56:33 | [diff] [blame] | 641 | |
| 642 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 643 | def CheckChangeOnCommit(input_api, output_api): |
Liviu Rau | f302860 | 2023-11-10 10:52:04 | [diff] [blame] | 644 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 652 | |
| 653 | |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 654 | def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 655 | """Return absolute file paths of affected files (not due to an excluded action) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 656 | under a parent directory with an accepted file ending. |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 657 | """ |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 658 | 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 Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 662 | 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 Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 668 | ] |
| 669 | return affected_files |
| 670 | |
| 671 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 | [diff] [blame] | 672 | def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 673 | original_sys_path = sys.path |
| 674 | try: |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 675 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')] |
Yang Guo | d817698 | 2019-10-04 20:30:35 | [diff] [blame] | 676 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 677 | finally: |
| 678 | sys.path = original_sys_path |
| 679 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 | [diff] [blame] | 680 | return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 681 | |
| 682 | |
| 683 | def _getFilesToLint(input_api, output_api, lint_config_files, |
| 684 | default_linted_directories, accepted_endings, results): |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 685 | run_full_check = False |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 686 | files_to_lint = [] |
| 687 | |
| 688 | # We are changing the lint configuration; run the full check. |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 689 | if len(lint_config_files) != 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 690 | results.append( |
| 691 | output_api.PresubmitNotifyResult('Running full lint check')) |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 692 | run_full_check = True |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 693 | 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 Franklin | 130d2ae | 2022-07-12 09:51:26 | [diff] [blame] | 699 | # Exclude front_end/third_party and front_end/generated files. |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 700 | files_to_lint = [ |
Jack Franklin | 130d2ae | 2022-07-12 09:51:26 | [diff] [blame] | 701 | file for file in files_to_lint |
| 702 | if "third_party" not in file or "generated" not in file |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 703 | ] |
Paul Lewis | 2b9224f | 2020-09-08 17:13:10 | [diff] [blame] | 704 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 705 | if len(files_to_lint) == 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 706 | results.append( |
| 707 | output_api.PresubmitNotifyResult( |
| 708 | 'No affected files for lint check')) |
| 709 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 710 | should_bail_out = len(files_to_lint) == 0 and not run_full_check |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 711 | return should_bail_out, files_to_lint |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame^] | 712 | |
| 713 | |
| 714 | def _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 [] |