blob: ef1c98781069f39ef221646fff98f040cdd30df7 [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
szager@chromium.org34071032014-03-18 17:23:48 +000013import fnmatch
14import os
Edward Lemur595eb192020-03-12 21:53:52 +000015import sys
szager@chromium.org34071032014-03-18 17:23:48 +000016
Dirk Pranke61bf6e82021-04-23 00:50:21 +000017# Whether to run the checks under Python2 or Python3.
18# TODO: Uncomment this to run the checks under Python3, and change the tests
19# in _CommonChecks in this file and the values and tests in
20# //tests/PRESUBMIT.py as well to keep the test coverage of all three cases
21# (true, false, and default/not specified).
22# USE_PYTHON3 = False
dpranke@chromium.org627ea672011-03-11 23:29:03 +000023
Vadim Shtayura09098852018-06-21 22:50:30 +000024# CIPD ensure manifest for checking CIPD client itself.
25CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
26# Full supported.
27$VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
28# Best effort support.
29$VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
Vadim Shtayura333617b2018-08-07 20:46:38 +000030$VerifiedPlatform linux-arm64 linux-armv6l
31$VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
Robert Iannucci7d47eb52017-12-06 12:18:18 -080032
33%s %s
34'''
35
Edward Lemur595eb192020-03-12 21:53:52 +000036# Timeout for a test to be executed.
Edward Lesmesae3586b2020-03-23 21:21:14 +000037TEST_TIMEOUT_S = 330 # 5m 30s
Edward Lemur595eb192020-03-12 21:53:52 +000038
39
Josip Sokcevic57c928c2021-11-02 22:56:28 +000040def CheckPylint(input_api, output_api):
agablef39c3332016-09-26 09:35:42 -070041 """Gather all the pylint logic into one place to make it self-contained."""
Ayu Ishii81a90742020-07-07 19:53:00 +000042 files_to_check = [
agablef39c3332016-09-26 09:35:42 -070043 r'^[^/]*\.py$',
44 r'^testing_support/[^/]*\.py$',
45 r'^tests/[^/]*\.py$',
46 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000047 ]
local_bot9af33fa2020-07-09 03:26:20 +000048 files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP)
szager@chromium.org34071032014-03-18 17:23:48 +000049 if os.path.exists('.gitignore'):
50 with open('.gitignore') as fh:
51 lines = [l.strip() for l in fh.readlines()]
Ayu Ishii81a90742020-07-07 19:53:00 +000052 files_to_skip.extend([fnmatch.translate(l) for l in lines if
szager@chromium.org34071032014-03-18 17:23:48 +000053 l and not l.startswith('#')])
54 if os.path.exists('.git/info/exclude'):
55 with open('.git/info/exclude') as fh:
56 lines = [l.strip() for l in fh.readlines()]
Ayu Ishii81a90742020-07-07 19:53:00 +000057 files_to_skip.extend([fnmatch.translate(l) for l in lines if
szager@chromium.org34071032014-03-18 17:23:48 +000058 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000059 disabled_warnings = [
Josip Sokcevic42c5bbb2022-01-24 21:42:28 +000060 'R0401', # Cyclic import
61 'W0613', # Unused argument
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000062 ]
Josip Sokcevic57c928c2021-11-02 22:56:28 +000063 return input_api.RunTests(input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000064 input_api,
65 output_api,
local_bot9af33fa2020-07-09 03:26:20 +000066 files_to_check=files_to_check,
67 files_to_skip=files_to_skip,
Josip Sokcevic42c5bbb2022-01-24 21:42:28 +000068 disabled_warnings=disabled_warnings))
agablef39c3332016-09-26 09:35:42 -070069
70
Josip Sokcevic57c928c2021-11-02 22:56:28 +000071def CheckRecipes(input_api, output_api):
Josip Sokcevic9eee47a2021-07-13 17:38:53 +000072 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
Josip Sokcevic57c928c2021-11-02 22:56:28 +000073 return input_api.canned_checks.CheckJsonParses(input_api, output_api,
74 file_filter=file_filter)
Dirk Pranke61bf6e82021-04-23 00:50:21 +000075
Josip Sokcevic57c928c2021-11-02 22:56:28 +000076def CheckPythonVersion(input_api, output_api):
Dirk Pranke61bf6e82021-04-23 00:50:21 +000077 # The tests here are assuming this is not defined, so raise an error
78 # if it is.
79 if 'USE_PYTHON3' in globals():
Josip Sokcevic57c928c2021-11-02 22:56:28 +000080 return [output_api.PresubmitError(
Dirk Pranke61bf6e82021-04-23 00:50:21 +000081 'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and '
Josip Sokcevic57c928c2021-11-02 22:56:28 +000082 '//tests/PRESUBMIT.py.')]
83 if sys.version_info.major != 2:
84 return [output_api.PresubmitError(
85 'Did not use Python2 for //PRESUBMIT.py by default.')]
86 return []
Dirk Pranke61bf6e82021-04-23 00:50:21 +000087
Josip Sokcevic57c928c2021-11-02 22:56:28 +000088
89def CheckJsonFiles(input_api, output_api):
90 return input_api.canned_checks.CheckJsonParses(
91 input_api, output_api)
92
93
94def CheckUnitTestsOnCommit(input_api, output_api):
95 # Do not run integration tests on upload since they are way too slow.
96 input_api.SetTimeout(TEST_TIMEOUT_S)
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000097
98 # Run only selected tests on Windows.
Ayu Ishii81a90742020-07-07 19:53:00 +000099 test_to_run_list = [r'.*test\.py$']
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000100 tests_to_skip_list = []
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000101 if input_api.platform.startswith(('cygwin', 'win32')):
102 print('Warning: skipping most unit tests on Windows')
Josip Sokcevic4940cc42021-10-05 23:55:34 +0000103 tests_to_skip_list.extend([
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000104 r'.*auth_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000105 r'.*git_common_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000106 r'.*git_hyper_blame_test\.py$',
Edward Lemurb800fde2020-01-10 23:04:44 +0000107 r'.*git_map_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000108 r'.*ninjalog_uploader_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000109 r'.*recipes_test\.py$',
Josip Sokcevic4940cc42021-10-05 23:55:34 +0000110 ])
Josip Sokcevic11c32b52021-11-19 01:10:50 +0000111 py2_only_tests = [
Josip Sokcevic11c32b52021-11-19 01:10:50 +0000112 'recipes_test.py',
113 ]
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000114
Takuto Ikutaa6573312022-01-20 00:24:57 +0000115 py3_only_tests = ['ninjalog_uploader_test.py']
116
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000117 tests = input_api.canned_checks.GetUnitTestsInDirectory(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000118 input_api,
119 output_api,
120 'tests',
Josip Sokcevicc6aa1512020-08-25 23:47:13 +0000121 files_to_check=test_to_run_list,
Takuto Ikutaa6573312022-01-20 00:24:57 +0000122 files_to_skip=tests_to_skip_list + py2_only_tests + py3_only_tests,
Josip Sokcevic11c32b52021-11-19 01:10:50 +0000123 run_on_python3=True)
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800124
Josip Sokcevic11c32b52021-11-19 01:10:50 +0000125 # TODO: once py3 compatbile, remove those tests
Takuto Ikutaa6573312022-01-20 00:24:57 +0000126 tests.extend(
127 input_api.canned_checks.GetUnitTestsInDirectory(
128 input_api,
129 output_api,
130 'tests',
131 files_to_check=py2_only_tests,
132 files_to_skip=tests_to_skip_list,
133 run_on_python3=False))
134
135 # TODO: use this for all tests when py2 support is dropped.
136 tests.extend(
137 input_api.canned_checks.GetUnitTestsInDirectory(
138 input_api,
139 output_api,
140 'tests',
141 files_to_check=py3_only_tests,
142 files_to_skip=tests_to_skip_list,
143 run_on_python3=True,
144 run_on_python2=False))
Josip Sokcevic4940cc42021-10-05 23:55:34 +0000145
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000146 return input_api.RunTests(tests)
147
148
149def CheckCIPDManifest(input_api, output_api):
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800150 # Validate CIPD manifests.
151 root = input_api.os_path.normpath(
152 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
153 rel_file = lambda rel: input_api.os_path.join(root, rel)
154 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
155 ('cipd_manifest.txt',),
Robert Iannucciee67b972019-12-10 22:46:56 +0000156 ('bootstrap', 'manifest.txt'),
157 ('bootstrap', 'manifest_bleeding_edge.txt'),
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800158
159 # Also generate a file for the cipd client itself.
160 ('cipd_client_version',),
161 ))
162 affected_manifests = input_api.AffectedFiles(
163 include_deletes=False,
164 file_filter=lambda x:
165 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000166 tests = []
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800167 for path in affected_manifests:
168 path = path.AbsoluteLocalPath()
169 if path.endswith('.txt'):
170 tests.append(input_api.canned_checks.CheckCIPDManifest(
171 input_api, output_api, path=path))
172 else:
173 pkg = 'infra/tools/cipd/${platform}'
174 ver = input_api.ReadFile(path)
175 tests.append(input_api.canned_checks.CheckCIPDManifest(
Vadim Shtayura09098852018-06-21 22:50:30 +0000176 input_api, output_api,
177 content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
Vadim Shtayuraf9b48452018-09-18 00:22:26 +0000178 tests.append(input_api.canned_checks.CheckCIPDClientDigests(
179 input_api, output_api, client_version_file=path))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800180
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000181 return input_api.RunTests(tests)
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000182
183
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000184def CheckOwnersFormat(input_api, output_api):
185 return input_api.canned_checks.CheckOwnersFormat(input_api, output_api)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000186
187
Josip Sokcevic57c928c2021-11-02 22:56:28 +0000188def CheckOwnersOnUpload(input_api, output_api):
189 return input_api.canned_checks.CheckOwners(input_api, output_api,
190 allow_tbr=False)
191
192def CheckDoNotSubmitOnCommit(input_api, output_api):
193 return input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)