chrisha@chromium.org | c920ad2 | 2012-02-02 18:26:04 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for depot tools. |
| 6 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 8 | details on the presubmit API built into depot_tools. |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 9 | """ |
| 10 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 11 | PRESUBMIT_VERSION = '2.0.0' |
| 12 | |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 13 | import fnmatch |
| 14 | import os |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 15 | import sys |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 16 | |
Vadim Shtayura | 0909885 | 2018-06-21 22:50:30 +0000 | [diff] [blame] | 17 | # CIPD ensure manifest for checking CIPD client itself. |
| 18 | CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r''' |
| 19 | # Full supported. |
| 20 | $VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386 |
| 21 | # Best effort support. |
| 22 | $VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x |
Vadim Shtayura | 333617b | 2018-08-07 20:46:38 +0000 | [diff] [blame] | 23 | $VerifiedPlatform linux-arm64 linux-armv6l |
| 24 | $VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 25 | |
| 26 | %s %s |
| 27 | ''' |
| 28 | |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 29 | # Timeout for a test to be executed. |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 30 | TEST_TIMEOUT_S = 330 # 5m 30s |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 31 | |
| 32 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 33 | def CheckPylint(input_api, output_api): |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 34 | """Gather all the pylint logic into one place to make it self-contained.""" |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 35 | files_to_check = [ |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 36 | r'^[^/]*\.py$', |
| 37 | r'^testing_support/[^/]*\.py$', |
| 38 | r'^tests/[^/]*\.py$', |
| 39 | r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules. |
tandrii@chromium.org | 55cb27e | 2016-05-16 17:54:40 +0000 | [diff] [blame] | 40 | ] |
local_bot | 9af33fa | 2020-07-09 03:26:20 +0000 | [diff] [blame] | 41 | files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP) |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 42 | if os.path.exists('.gitignore'): |
Bruce Dawson | 4248148 | 2023-03-01 00:45:05 +0000 | [diff] [blame] | 43 | with open('.gitignore', encoding='utf-8') as fh: |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 44 | lines = [l.strip() for l in fh.readlines()] |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 45 | files_to_skip.extend([fnmatch.translate(l) for l in lines if |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 46 | l and not l.startswith('#')]) |
| 47 | if os.path.exists('.git/info/exclude'): |
Bruce Dawson | 4248148 | 2023-03-01 00:45:05 +0000 | [diff] [blame] | 48 | with open('.git/info/exclude', encoding='utf-8') as fh: |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 49 | lines = [l.strip() for l in fh.readlines()] |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 50 | files_to_skip.extend([fnmatch.translate(l) for l in lines if |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 51 | l and not l.startswith('#')]) |
maruel@chromium.org | ff9a217 | 2012-04-24 16:55:32 +0000 | [diff] [blame] | 52 | disabled_warnings = [ |
Aravind Vasudevan | c5f0cbb | 2022-01-24 23:56:57 +0000 | [diff] [blame] | 53 | 'R0401', # Cyclic import |
| 54 | 'W0613', # Unused argument |
| 55 | 'C0415', # import-outside-toplevel |
| 56 | 'R1710', # inconsistent-return-statements |
| 57 | 'E1101', # no-member |
| 58 | 'E1120', # no-value-for-parameter |
| 59 | 'R1708', # stop-iteration-return |
| 60 | 'W1510', # subprocess-run-check |
| 61 | # Checks which should be re-enabled after Python 2 support is removed. |
| 62 | 'R0205', # useless-object-inheritance |
| 63 | 'R1725', # super-with-arguments |
| 64 | 'W0707', # raise-missing-from |
| 65 | 'W1113', # keyword-arg-before-vararg |
maruel@chromium.org | ff9a217 | 2012-04-24 16:55:32 +0000 | [diff] [blame] | 66 | ] |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 67 | return input_api.RunTests(input_api.canned_checks.GetPylint( |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 68 | input_api, |
| 69 | output_api, |
local_bot | 9af33fa | 2020-07-09 03:26:20 +0000 | [diff] [blame] | 70 | files_to_check=files_to_check, |
| 71 | files_to_skip=files_to_skip, |
Aravind Vasudevan | c5f0cbb | 2022-01-24 23:56:57 +0000 | [diff] [blame] | 72 | disabled_warnings=disabled_warnings, |
| 73 | version='2.7'), parallel=False) |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 74 | |
| 75 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 76 | def CheckRecipes(input_api, output_api): |
Josip Sokcevic | 9eee47a | 2021-07-13 17:38:53 +0000 | [diff] [blame] | 77 | file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg' |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 78 | return input_api.canned_checks.CheckJsonParses(input_api, output_api, |
| 79 | file_filter=file_filter) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 80 | |
Josip Sokcevic | 95c7da1 | 2022-09-07 16:55:52 +0000 | [diff] [blame] | 81 | |
| 82 | def CheckUsePython3(input_api, output_api): |
| 83 | results = [] |
| 84 | |
| 85 | if sys.version_info.major != 3: |
| 86 | results.append( |
| 87 | output_api.PresubmitError( |
| 88 | 'Did not use Python3 for //tests/PRESUBMIT.py.')) |
| 89 | |
| 90 | return results |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 91 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 92 | |
| 93 | def CheckJsonFiles(input_api, output_api): |
| 94 | return input_api.canned_checks.CheckJsonParses( |
| 95 | input_api, output_api) |
| 96 | |
| 97 | |
| 98 | def CheckUnitTestsOnCommit(input_api, output_api): |
Josip Sokcevic | 95c7da1 | 2022-09-07 16:55:52 +0000 | [diff] [blame] | 99 | """ Do not run integration tests on upload since they are way too slow.""" |
| 100 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 101 | input_api.SetTimeout(TEST_TIMEOUT_S) |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 102 | |
| 103 | # Run only selected tests on Windows. |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 104 | test_to_run_list = [r'.*test\.py$'] |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 105 | tests_to_skip_list = [] |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 106 | if input_api.platform.startswith(('cygwin', 'win32')): |
| 107 | print('Warning: skipping most unit tests on Windows') |
Josip Sokcevic | 4940cc4 | 2021-10-05 23:55:34 +0000 | [diff] [blame] | 108 | tests_to_skip_list.extend([ |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 109 | r'.*auth_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 110 | r'.*git_common_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 111 | r'.*git_hyper_blame_test\.py$', |
Edward Lemur | b800fde | 2020-01-10 23:04:44 +0000 | [diff] [blame] | 112 | r'.*git_map_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 113 | r'.*ninjalog_uploader_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 114 | r'.*recipes_test\.py$', |
Josip Sokcevic | 4940cc4 | 2021-10-05 23:55:34 +0000 | [diff] [blame] | 115 | ]) |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 116 | |
Robert Iannucci | 7160223 | 2023-05-10 23:37:36 +0000 | [diff] [blame] | 117 | tests = input_api.canned_checks.GetUnitTestsInDirectory( |
| 118 | input_api, |
| 119 | output_api, |
| 120 | 'tests', |
| 121 | files_to_check=test_to_run_list, |
Takuto Ikuta | 90a9eaf | 2023-05-30 16:41:30 +0000 | [diff] [blame^] | 122 | files_to_skip=tests_to_skip_list) |
Josip Sokcevic | 4940cc4 | 2021-10-05 23:55:34 +0000 | [diff] [blame] | 123 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 124 | return input_api.RunTests(tests) |
| 125 | |
| 126 | |
| 127 | def CheckCIPDManifest(input_api, output_api): |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 128 | # Validate CIPD manifests. |
| 129 | root = input_api.os_path.normpath( |
| 130 | input_api.os_path.abspath(input_api.PresubmitLocalPath())) |
| 131 | rel_file = lambda rel: input_api.os_path.join(root, rel) |
| 132 | cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in ( |
| 133 | ('cipd_manifest.txt',), |
Robert Iannucci | ee67b97 | 2019-12-10 22:46:56 +0000 | [diff] [blame] | 134 | ('bootstrap', 'manifest.txt'), |
| 135 | ('bootstrap', 'manifest_bleeding_edge.txt'), |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 136 | |
| 137 | # Also generate a file for the cipd client itself. |
| 138 | ('cipd_client_version',), |
| 139 | )) |
| 140 | affected_manifests = input_api.AffectedFiles( |
| 141 | include_deletes=False, |
| 142 | file_filter=lambda x: |
| 143 | input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests) |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 144 | tests = [] |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 145 | for path in affected_manifests: |
| 146 | path = path.AbsoluteLocalPath() |
| 147 | if path.endswith('.txt'): |
| 148 | tests.append(input_api.canned_checks.CheckCIPDManifest( |
| 149 | input_api, output_api, path=path)) |
| 150 | else: |
| 151 | pkg = 'infra/tools/cipd/${platform}' |
| 152 | ver = input_api.ReadFile(path) |
| 153 | tests.append(input_api.canned_checks.CheckCIPDManifest( |
Vadim Shtayura | 0909885 | 2018-06-21 22:50:30 +0000 | [diff] [blame] | 154 | input_api, output_api, |
| 155 | content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver))) |
Vadim Shtayura | f9b4845 | 2018-09-18 00:22:26 +0000 | [diff] [blame] | 156 | tests.append(input_api.canned_checks.CheckCIPDClientDigests( |
| 157 | input_api, output_api, client_version_file=path)) |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 158 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 159 | return input_api.RunTests(tests) |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 160 | |
| 161 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 162 | def CheckOwnersFormat(input_api, output_api): |
| 163 | return input_api.canned_checks.CheckOwnersFormat(input_api, output_api) |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 164 | |
| 165 | |
Josip Sokcevic | 57c928c | 2021-11-02 22:56:28 +0000 | [diff] [blame] | 166 | def CheckOwnersOnUpload(input_api, output_api): |
| 167 | return input_api.canned_checks.CheckOwners(input_api, output_api, |
| 168 | allow_tbr=False) |
| 169 | |
| 170 | def CheckDoNotSubmitOnCommit(input_api, output_api): |
| 171 | return input_api.canned_checks.CheckDoNotSubmit(input_api, output_api) |
Garrett Beaty | f41670f | 2022-08-29 22:26:42 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | def CheckPatchFormatted(input_api, output_api): |
| 175 | # TODO(https://crbug.com/979330) If clang-format is fixed for non-chromium |
| 176 | # repos, remove check_clang_format=False so that proto files can be formatted |
| 177 | return input_api.canned_checks.CheckPatchFormatted(input_api, |
| 178 | output_api, |
| 179 | check_clang_format=False) |