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