blob: af0d075229a32161e44960c6974bf09d183db26f [file] [log] [blame]
chrisha@chromium.orgc920ad22012-02-02 18:26:04 +00001# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.org3d235242009-05-15 12:40:48 +00002# 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
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
maruel@chromium.org2a74d372011-03-29 19:05:50 +00008details on the presubmit API built into depot_tools.
maruel@chromium.org3d235242009-05-15 12:40:48 +00009"""
10
Josip Sokcevic57c928c2021-11-02 22:56:28 +000011PRESUBMIT_VERSION = '2.0.0'
12
Josip Sokcevic95c7da12022-09-07 16:55:52 +000013USE_PYTHON3 = True
14
szager@chromium.org34071032014-03-18 17:23:48 +000015import fnmatch
16import os
Edward Lemur595eb192020-03-12 21:53:52 +000017import sys
szager@chromium.org34071032014-03-18 17:23:48 +000018
Dirk Pranke61bf6e82021-04-23 00:50:21 +000019# Whether to run the checks under Python2 or Python3.
20# TODO: Uncomment this to run the checks under Python3, and change the tests
21# in _CommonChecks in this file and the values and tests in
22# //tests/PRESUBMIT.py as well to keep the test coverage of all three cases
23# (true, false, and default/not specified).
24# USE_PYTHON3 = False
dpranke@chromium.org627ea672011-03-11 23:29:03 +000025
Vadim Shtayura09098852018-06-21 22:50:30 +000026# CIPD ensure manifest for checking CIPD client itself.
27CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
28# Full supported.
29$VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
30# Best effort support.
31$VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
Vadim Shtayura333617b2018-08-07 20:46:38 +000032$VerifiedPlatform linux-arm64 linux-armv6l
33$VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
Robert Iannucci7d47eb52017-12-06 12:18:18 -080034
35%s %s
36'''
37
Edward Lemur595eb192020-03-12 21:53:52 +000038# Timeout for a test to be executed.
Edward Lesmesae3586b2020-03-23 21:21:14 +000039TEST_TIMEOUT_S = 330 # 5m 30s
Edward Lemur595eb192020-03-12 21:53:52 +000040
41
Josip Sokcevic57c928c2021-11-02 22:56:28 +000042def CheckPylint(input_api, output_api):
agablef39c3332016-09-26 09:35:42 -070043 """Gather all the pylint logic into one place to make it self-contained."""
Ayu Ishii81a90742020-07-07 19:53:00 +000044 files_to_check = [
agablef39c3332016-09-26 09:35:42 -070045 r'^[^/]*\.py$',
46 r'^testing_support/[^/]*\.py$',
47 r'^tests/[^/]*\.py$',
48 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000049 ]
local_bot9af33fa2020-07-09 03:26:20 +000050 files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP)
szager@chromium.org34071032014-03-18 17:23:48 +000051 if os.path.exists('.gitignore'):
Bruce Dawson42481482023-03-01 00:45:05 +000052 with open('.gitignore', encoding='utf-8') as fh:
szager@chromium.org34071032014-03-18 17:23:48 +000053 lines = [l.strip() for l in fh.readlines()]
Ayu Ishii81a90742020-07-07 19:53:00 +000054 files_to_skip.extend([fnmatch.translate(l) for l in lines if
szager@chromium.org34071032014-03-18 17:23:48 +000055 l and not l.startswith('#')])
56 if os.path.exists('.git/info/exclude'):
Bruce Dawson42481482023-03-01 00:45:05 +000057 with open('.git/info/exclude', encoding='utf-8') as fh:
szager@chromium.org34071032014-03-18 17:23:48 +000058 lines = [l.strip() for l in fh.readlines()]
Ayu Ishii81a90742020-07-07 19:53:00 +000059 files_to_skip.extend([fnmatch.translate(l) for l in lines if
szager@chromium.org34071032014-03-18 17:23:48 +000060 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000061 disabled_warnings = [
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000062 'R0401', # Cyclic import
63 'W0613', # Unused argument
64 'C0415', # import-outside-toplevel
65 'R1710', # inconsistent-return-statements
66 'E1101', # no-member
67 'E1120', # no-value-for-parameter
68 'R1708', # stop-iteration-return
69 'W1510', # subprocess-run-check
70 # Checks which should be re-enabled after Python 2 support is removed.
71 'R0205', # useless-object-inheritance
72 'R1725', # super-with-arguments
73 'W0707', # raise-missing-from
74 'W1113', # keyword-arg-before-vararg
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000075 ]
Josip Sokcevic57c928c2021-11-02 22:56:28 +000076 return input_api.RunTests(input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000077 input_api,
78 output_api,
local_bot9af33fa2020-07-09 03:26:20 +000079 files_to_check=files_to_check,
80 files_to_skip=files_to_skip,
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000081 disabled_warnings=disabled_warnings,
82 version='2.7'), parallel=False)
agablef39c3332016-09-26 09:35:42 -070083
84
Josip Sokcevic57c928c2021-11-02 22:56:28 +000085def CheckRecipes(input_api, output_api):
Josip Sokcevic9eee47a2021-07-13 17:38:53 +000086 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
Josip Sokcevic57c928c2021-11-02 22:56:28 +000087 return input_api.canned_checks.CheckJsonParses(input_api, output_api,
88 file_filter=file_filter)
Dirk Pranke61bf6e82021-04-23 00:50:21 +000089
Josip Sokcevic95c7da12022-09-07 16:55:52 +000090
91def CheckUsePython3(input_api, output_api):
92 results = []
93
94 if sys.version_info.major != 3:
95 results.append(
96 output_api.PresubmitError(
97 'Did not use Python3 for //tests/PRESUBMIT.py.'))
98
99 return results
Dirk Pranke61bf6e82021-04-23 00:50:21 +0000100
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000101
102def CheckJsonFiles(input_api, output_api):
103 return input_api.canned_checks.CheckJsonParses(
104 input_api, output_api)
105
106
107def CheckUnitTestsOnCommit(input_api, output_api):
Josip Sokcevic95c7da12022-09-07 16:55:52 +0000108 """ Do not run integration tests on upload since they are way too slow."""
109
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000110 input_api.SetTimeout(TEST_TIMEOUT_S)
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000111
112 # Run only selected tests on Windows.
Ayu Ishii81a90742020-07-07 19:53:00 +0000113 test_to_run_list = [r'.*test\.py$']
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000114 tests_to_skip_list = []
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000115 if input_api.platform.startswith(('cygwin', 'win32')):
116 print('Warning: skipping most unit tests on Windows')
Josip Sokcevic4940cc42021-10-05 23:55:34 +0000117 tests_to_skip_list.extend([
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000118 r'.*auth_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000119 r'.*git_common_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000120 r'.*git_hyper_blame_test\.py$',
Edward Lemurb800fde2020-01-10 23:04:44 +0000121 r'.*git_map_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000122 r'.*ninjalog_uploader_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000123 r'.*recipes_test\.py$',
Josip Sokcevic4940cc42021-10-05 23:55:34 +0000124 ])
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000125
Robert Iannucci71602232023-05-10 23:37:36 +0000126 tests = input_api.canned_checks.GetUnitTestsInDirectory(
127 input_api,
128 output_api,
129 'tests',
130 files_to_check=test_to_run_list,
131 files_to_skip=tests_to_skip_list,
132 run_on_python3=True,
133 run_on_python2=False)
Josip Sokcevic4940cc42021-10-05 23:55:34 +0000134
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000135 return input_api.RunTests(tests)
136
137
138def CheckCIPDManifest(input_api, output_api):
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800139 # Validate CIPD manifests.
140 root = input_api.os_path.normpath(
141 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
142 rel_file = lambda rel: input_api.os_path.join(root, rel)
143 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
144 ('cipd_manifest.txt',),
Robert Iannucciee67b972019-12-10 22:46:56 +0000145 ('bootstrap', 'manifest.txt'),
146 ('bootstrap', 'manifest_bleeding_edge.txt'),
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800147
148 # Also generate a file for the cipd client itself.
149 ('cipd_client_version',),
150 ))
151 affected_manifests = input_api.AffectedFiles(
152 include_deletes=False,
153 file_filter=lambda x:
154 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000155 tests = []
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800156 for path in affected_manifests:
157 path = path.AbsoluteLocalPath()
158 if path.endswith('.txt'):
159 tests.append(input_api.canned_checks.CheckCIPDManifest(
160 input_api, output_api, path=path))
161 else:
162 pkg = 'infra/tools/cipd/${platform}'
163 ver = input_api.ReadFile(path)
164 tests.append(input_api.canned_checks.CheckCIPDManifest(
Vadim Shtayura09098852018-06-21 22:50:30 +0000165 input_api, output_api,
166 content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
Vadim Shtayuraf9b48452018-09-18 00:22:26 +0000167 tests.append(input_api.canned_checks.CheckCIPDClientDigests(
168 input_api, output_api, client_version_file=path))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800169
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000170 return input_api.RunTests(tests)
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000171
172
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000173def CheckOwnersFormat(input_api, output_api):
174 return input_api.canned_checks.CheckOwnersFormat(input_api, output_api)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000175
176
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000177def CheckOwnersOnUpload(input_api, output_api):
178 return input_api.canned_checks.CheckOwners(input_api, output_api,
179 allow_tbr=False)
180
181def CheckDoNotSubmitOnCommit(input_api, output_api):
182 return input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)
Garrett Beatyf41670f2022-08-29 22:26:42 +0000183
184
185def CheckPatchFormatted(input_api, output_api):
186 # TODO(https://crbug.com/979330) If clang-format is fixed for non-chromium
187 # repos, remove check_clang_format=False so that proto files can be formatted
188 return input_api.canned_checks.CheckPatchFormatted(input_api,
189 output_api,
190 check_clang_format=False)