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 | |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 11 | import fnmatch |
| 12 | import os |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 13 | import sys |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 14 | |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 15 | # Whether to run the checks under Python2 or Python3. |
| 16 | # TODO: Uncomment this to run the checks under Python3, and change the tests |
| 17 | # in _CommonChecks in this file and the values and tests in |
| 18 | # //tests/PRESUBMIT.py as well to keep the test coverage of all three cases |
| 19 | # (true, false, and default/not specified). |
| 20 | # USE_PYTHON3 = False |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 21 | |
Vadim Shtayura | 0909885 | 2018-06-21 22:50:30 +0000 | [diff] [blame] | 22 | # CIPD ensure manifest for checking CIPD client itself. |
| 23 | CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r''' |
| 24 | # Full supported. |
| 25 | $VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386 |
| 26 | # Best effort support. |
| 27 | $VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x |
Vadim Shtayura | 333617b | 2018-08-07 20:46:38 +0000 | [diff] [blame] | 28 | $VerifiedPlatform linux-arm64 linux-armv6l |
| 29 | $VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 30 | |
| 31 | %s %s |
| 32 | ''' |
| 33 | |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 34 | # Timeout for a test to be executed. |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 35 | TEST_TIMEOUT_S = 330 # 5m 30s |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 36 | |
| 37 | |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 38 | |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 39 | def DepotToolsPylint(input_api, output_api): |
| 40 | """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] | 41 | files_to_check = [ |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 42 | r'^[^/]*\.py$', |
| 43 | r'^testing_support/[^/]*\.py$', |
| 44 | r'^tests/[^/]*\.py$', |
| 45 | r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules. |
tandrii@chromium.org | 55cb27e | 2016-05-16 17:54:40 +0000 | [diff] [blame] | 46 | ] |
local_bot | 9af33fa | 2020-07-09 03:26:20 +0000 | [diff] [blame] | 47 | files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP) |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 48 | if os.path.exists('.gitignore'): |
| 49 | with open('.gitignore') as fh: |
| 50 | lines = [l.strip() for l in fh.readlines()] |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 51 | 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] | 52 | l and not l.startswith('#')]) |
| 53 | if os.path.exists('.git/info/exclude'): |
| 54 | with open('.git/info/exclude') as fh: |
| 55 | lines = [l.strip() for l in fh.readlines()] |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 56 | 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] | 57 | l and not l.startswith('#')]) |
maruel@chromium.org | ff9a217 | 2012-04-24 16:55:32 +0000 | [diff] [blame] | 58 | disabled_warnings = [ |
| 59 | 'R0401', # Cyclic import |
| 60 | 'W0613', # Unused argument |
| 61 | ] |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 62 | return input_api.canned_checks.GetPylint( |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 63 | input_api, |
| 64 | output_api, |
local_bot | 9af33fa | 2020-07-09 03:26:20 +0000 | [diff] [blame] | 65 | files_to_check=files_to_check, |
| 66 | files_to_skip=files_to_skip, |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 67 | disabled_warnings=disabled_warnings) |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 68 | |
| 69 | |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 70 | def CommonChecks(input_api, output_api, tests_to_skip_list): |
Edward Lemur | 595eb19 | 2020-03-12 21:53:52 +0000 | [diff] [blame] | 71 | input_api.SetTimeout(TEST_TIMEOUT_S) |
| 72 | |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 73 | results = [] |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 74 | |
| 75 | # The tests here are assuming this is not defined, so raise an error |
| 76 | # if it is. |
| 77 | if 'USE_PYTHON3' in globals(): |
| 78 | results.append(output_api.PresubmitError( |
| 79 | 'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and ' |
| 80 | '//tests/PRESUBMIT.py.')) |
| 81 | elif sys.version_info.major != 2: |
| 82 | results.append(output_api.PresubmitError( |
| 83 | 'Did not use Python2 for //PRESUBMIT.py by default.')) |
| 84 | |
Aaron Gable | 1318a30 | 2019-05-21 21:46:07 +0000 | [diff] [blame] | 85 | results.extend(input_api.canned_checks.CheckJsonParses( |
| 86 | input_api, output_api)) |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 87 | |
| 88 | # Run only selected tests on Windows. |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 89 | test_to_run_list = [r'.*test\.py$'] |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 90 | if input_api.platform.startswith(('cygwin', 'win32')): |
| 91 | print('Warning: skipping most unit tests on Windows') |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 92 | tests_to_skip_list = [ |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 93 | r'.*auth_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 94 | r'.*git_common_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 95 | r'.*git_hyper_blame_test\.py$', |
Edward Lemur | b800fde | 2020-01-10 23:04:44 +0000 | [diff] [blame] | 96 | r'.*git_map_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 97 | r'.*ninjalog_uploader_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 98 | r'.*recipes_test\.py$', |
Edward Lemur | bbd70fd | 2019-09-26 18:19:41 +0000 | [diff] [blame] | 99 | ] |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 100 | |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 101 | # TODO(maruel): Make sure at least one file is modified first. |
| 102 | # TODO(maruel): If only tests are modified, only run them. |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 103 | tests = DepotToolsPylint(input_api, output_api) |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 104 | tests.extend(input_api.canned_checks.GetUnitTestsInDirectory( |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 105 | input_api, |
| 106 | output_api, |
| 107 | 'tests', |
Josip Sokcevic | c6aa151 | 2020-08-25 23:47:13 +0000 | [diff] [blame] | 108 | files_to_check=test_to_run_list, |
| 109 | files_to_skip=tests_to_skip_list, |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 110 | run_on_python3=False)) |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 111 | |
| 112 | # Validate CIPD manifests. |
| 113 | root = input_api.os_path.normpath( |
| 114 | input_api.os_path.abspath(input_api.PresubmitLocalPath())) |
| 115 | rel_file = lambda rel: input_api.os_path.join(root, rel) |
| 116 | cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in ( |
| 117 | ('cipd_manifest.txt',), |
Robert Iannucci | ee67b97 | 2019-12-10 22:46:56 +0000 | [diff] [blame] | 118 | ('bootstrap', 'manifest.txt'), |
| 119 | ('bootstrap', 'manifest_bleeding_edge.txt'), |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 120 | |
| 121 | # Also generate a file for the cipd client itself. |
| 122 | ('cipd_client_version',), |
| 123 | )) |
| 124 | affected_manifests = input_api.AffectedFiles( |
| 125 | include_deletes=False, |
| 126 | file_filter=lambda x: |
| 127 | input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests) |
| 128 | for path in affected_manifests: |
| 129 | path = path.AbsoluteLocalPath() |
| 130 | if path.endswith('.txt'): |
| 131 | tests.append(input_api.canned_checks.CheckCIPDManifest( |
| 132 | input_api, output_api, path=path)) |
| 133 | else: |
| 134 | pkg = 'infra/tools/cipd/${platform}' |
| 135 | ver = input_api.ReadFile(path) |
| 136 | tests.append(input_api.canned_checks.CheckCIPDManifest( |
Vadim Shtayura | 0909885 | 2018-06-21 22:50:30 +0000 | [diff] [blame] | 137 | input_api, output_api, |
| 138 | content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver))) |
Vadim Shtayura | f9b4845 | 2018-09-18 00:22:26 +0000 | [diff] [blame] | 139 | tests.append(input_api.canned_checks.CheckCIPDClientDigests( |
| 140 | input_api, output_api, client_version_file=path)) |
Robert Iannucci | 7d47eb5 | 2017-12-06 12:18:18 -0800 | [diff] [blame] | 141 | |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 142 | results.extend(input_api.RunTests(tests)) |
maruel@chromium.org | 3c9f7ca | 2011-04-01 14:52:38 +0000 | [diff] [blame] | 143 | return results |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 144 | |
| 145 | |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 146 | def CheckChangeOnUpload(input_api, output_api): |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 147 | # Do not run integration tests on upload since they are way too slow. |
Ayu Ishii | 81a9074 | 2020-07-07 19:53:00 +0000 | [diff] [blame] | 148 | tests_to_skip_list = [ |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 149 | r'^checkout_test\.py$', |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 150 | r'^cipd_bootstrap_test\.py$', |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 151 | r'^gclient_smoketest\.py$', |
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 152 | ] |
Edward Lesmes | 8404644 | 2021-02-09 23:13:09 +0000 | [diff] [blame] | 153 | results = [] |
| 154 | results.extend(input_api.canned_checks.CheckOwners( |
| 155 | input_api, output_api, allow_tbr=False)) |
| 156 | results.extend(input_api.canned_checks.CheckOwnersFormat( |
| 157 | input_api, output_api)) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 158 | results.extend(CommonChecks(input_api, output_api, tests_to_skip_list)) |
Edward Lesmes | 8404644 | 2021-02-09 23:13:09 +0000 | [diff] [blame] | 159 | return results |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 160 | |
| 161 | |
maruel@chromium.org | ce30ef7 | 2009-05-25 15:20:42 +0000 | [diff] [blame] | 162 | def CheckChangeOnCommit(input_api, output_api): |
| 163 | output = [] |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 164 | output.extend(CommonChecks(input_api, output_api, [])) |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 165 | output.extend(input_api.canned_checks.CheckDoNotSubmit( |
| 166 | input_api, |
| 167 | output_api)) |
maruel@chromium.org | 7b305e8 | 2009-05-19 18:24:20 +0000 | [diff] [blame] | 168 | return output |