blob: 7aba7c60437031da2ae47de018d64b1db229ad6a [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
szager@chromium.org34071032014-03-18 17:23:48 +000011import fnmatch
12import os
Edward Lemur595eb192020-03-12 21:53:52 +000013import sys
szager@chromium.org34071032014-03-18 17:23:48 +000014
Dirk Pranke61bf6e82021-04-23 00:50:21 +000015# 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.org627ea672011-03-11 23:29:03 +000021
Vadim Shtayura09098852018-06-21 22:50:30 +000022# CIPD ensure manifest for checking CIPD client itself.
23CIPD_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 Shtayura333617b2018-08-07 20:46:38 +000028$VerifiedPlatform linux-arm64 linux-armv6l
29$VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
Robert Iannucci7d47eb52017-12-06 12:18:18 -080030
31%s %s
32'''
33
Edward Lemur595eb192020-03-12 21:53:52 +000034# Timeout for a test to be executed.
Edward Lesmesae3586b2020-03-23 21:21:14 +000035TEST_TIMEOUT_S = 330 # 5m 30s
Edward Lemur595eb192020-03-12 21:53:52 +000036
37
Robert Iannucci7d47eb52017-12-06 12:18:18 -080038
agablef39c3332016-09-26 09:35:42 -070039def DepotToolsPylint(input_api, output_api):
40 """Gather all the pylint logic into one place to make it self-contained."""
Ayu Ishii81a90742020-07-07 19:53:00 +000041 files_to_check = [
agablef39c3332016-09-26 09:35:42 -070042 r'^[^/]*\.py$',
43 r'^testing_support/[^/]*\.py$',
44 r'^tests/[^/]*\.py$',
45 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000046 ]
local_bot9af33fa2020-07-09 03:26:20 +000047 files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP)
szager@chromium.org34071032014-03-18 17:23:48 +000048 if os.path.exists('.gitignore'):
49 with open('.gitignore') as fh:
50 lines = [l.strip() for l in fh.readlines()]
Ayu Ishii81a90742020-07-07 19:53:00 +000051 files_to_skip.extend([fnmatch.translate(l) for l in lines if
szager@chromium.org34071032014-03-18 17:23:48 +000052 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 Ishii81a90742020-07-07 19:53:00 +000056 files_to_skip.extend([fnmatch.translate(l) for l in lines if
szager@chromium.org34071032014-03-18 17:23:48 +000057 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000058 disabled_warnings = [
59 'R0401', # Cyclic import
60 'W0613', # Unused argument
61 ]
agablef39c3332016-09-26 09:35:42 -070062 return input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000063 input_api,
64 output_api,
local_bot9af33fa2020-07-09 03:26:20 +000065 files_to_check=files_to_check,
66 files_to_skip=files_to_skip,
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000067 disabled_warnings=disabled_warnings)
agablef39c3332016-09-26 09:35:42 -070068
69
Dirk Pranke61bf6e82021-04-23 00:50:21 +000070def CommonChecks(input_api, output_api, tests_to_skip_list):
Edward Lemur595eb192020-03-12 21:53:52 +000071 input_api.SetTimeout(TEST_TIMEOUT_S)
72
Josip Sokcevic9eee47a2021-07-13 17:38:53 +000073 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
74 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
75 file_filter=file_filter)
Dirk Pranke61bf6e82021-04-23 00:50:21 +000076
77 # The tests here are assuming this is not defined, so raise an error
78 # if it is.
79 if 'USE_PYTHON3' in globals():
80 results.append(output_api.PresubmitError(
81 'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and '
82 '//tests/PRESUBMIT.py.'))
83 elif sys.version_info.major != 2:
84 results.append(output_api.PresubmitError(
85 'Did not use Python2 for //PRESUBMIT.py by default.'))
86
Aaron Gable1318a302019-05-21 21:46:07 +000087 results.extend(input_api.canned_checks.CheckJsonParses(
88 input_api, output_api))
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000089
90 # Run only selected tests on Windows.
Ayu Ishii81a90742020-07-07 19:53:00 +000091 test_to_run_list = [r'.*test\.py$']
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000092 if input_api.platform.startswith(('cygwin', 'win32')):
93 print('Warning: skipping most unit tests on Windows')
Ayu Ishii81a90742020-07-07 19:53:00 +000094 tests_to_skip_list = [
Edward Lemurbbd70fd2019-09-26 18:19:41 +000095 r'.*auth_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000096 r'.*git_common_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000097 r'.*git_hyper_blame_test\.py$',
Edward Lemurb800fde2020-01-10 23:04:44 +000098 r'.*git_map_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000099 r'.*ninjalog_uploader_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000100 r'.*recipes_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +0000101 ]
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000102
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000103 # TODO(maruel): Make sure at least one file is modified first.
104 # TODO(maruel): If only tests are modified, only run them.
agablef39c3332016-09-26 09:35:42 -0700105 tests = DepotToolsPylint(input_api, output_api)
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000106 tests.extend(input_api.canned_checks.GetUnitTestsInDirectory(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000107 input_api,
108 output_api,
109 'tests',
Josip Sokcevicc6aa1512020-08-25 23:47:13 +0000110 files_to_check=test_to_run_list,
111 files_to_skip=tests_to_skip_list,
Dirk Pranke61bf6e82021-04-23 00:50:21 +0000112 run_on_python3=False))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800113
114 # Validate CIPD manifests.
115 root = input_api.os_path.normpath(
116 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
117 rel_file = lambda rel: input_api.os_path.join(root, rel)
118 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
119 ('cipd_manifest.txt',),
Robert Iannucciee67b972019-12-10 22:46:56 +0000120 ('bootstrap', 'manifest.txt'),
121 ('bootstrap', 'manifest_bleeding_edge.txt'),
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800122
123 # Also generate a file for the cipd client itself.
124 ('cipd_client_version',),
125 ))
126 affected_manifests = input_api.AffectedFiles(
127 include_deletes=False,
128 file_filter=lambda x:
129 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
130 for path in affected_manifests:
131 path = path.AbsoluteLocalPath()
132 if path.endswith('.txt'):
133 tests.append(input_api.canned_checks.CheckCIPDManifest(
134 input_api, output_api, path=path))
135 else:
136 pkg = 'infra/tools/cipd/${platform}'
137 ver = input_api.ReadFile(path)
138 tests.append(input_api.canned_checks.CheckCIPDManifest(
Vadim Shtayura09098852018-06-21 22:50:30 +0000139 input_api, output_api,
140 content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
Vadim Shtayuraf9b48452018-09-18 00:22:26 +0000141 tests.append(input_api.canned_checks.CheckCIPDClientDigests(
142 input_api, output_api, client_version_file=path))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800143
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000144 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +0000145 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000146
147
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000148def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000149 # Do not run integration tests on upload since they are way too slow.
Ayu Ishii81a90742020-07-07 19:53:00 +0000150 tests_to_skip_list = [
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000151 r'^checkout_test\.py$',
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000152 r'^cipd_bootstrap_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000153 r'^gclient_smoketest\.py$',
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000154 ]
Edward Lesmes84046442021-02-09 23:13:09 +0000155 results = []
156 results.extend(input_api.canned_checks.CheckOwners(
157 input_api, output_api, allow_tbr=False))
158 results.extend(input_api.canned_checks.CheckOwnersFormat(
159 input_api, output_api))
Dirk Pranke61bf6e82021-04-23 00:50:21 +0000160 results.extend(CommonChecks(input_api, output_api, tests_to_skip_list))
Edward Lesmes84046442021-02-09 23:13:09 +0000161 return results
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000162
163
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000164def CheckChangeOnCommit(input_api, output_api):
165 output = []
Dirk Pranke61bf6e82021-04-23 00:50:21 +0000166 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000167 output.extend(input_api.canned_checks.CheckDoNotSubmit(
168 input_api,
169 output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000170 return output