blob: bd9f6919e4dc073054cdc3a0ba8ca667539cdeff [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
dpranke@chromium.org627ea672011-03-11 23:29:03 +000015
Vadim Shtayura09098852018-06-21 22:50:30 +000016# CIPD ensure manifest for checking CIPD client itself.
17CIPD_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 Shtayura333617b2018-08-07 20:46:38 +000022$VerifiedPlatform linux-arm64 linux-armv6l
23$VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
Robert Iannucci7d47eb52017-12-06 12:18:18 -080024
25%s %s
26'''
27
Edward Lemur595eb192020-03-12 21:53:52 +000028# Timeout for a test to be executed.
Edward Lesmesae3586b2020-03-23 21:21:14 +000029TEST_TIMEOUT_S = 330 # 5m 30s
Edward Lemur595eb192020-03-12 21:53:52 +000030
31
Robert Iannucci7d47eb52017-12-06 12:18:18 -080032
agablef39c3332016-09-26 09:35:42 -070033def DepotToolsPylint(input_api, output_api):
34 """Gather all the pylint logic into one place to make it self-contained."""
35 white_list = [
36 r'^[^/]*\.py$',
37 r'^testing_support/[^/]*\.py$',
38 r'^tests/[^/]*\.py$',
39 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000040 ]
agablef39c3332016-09-26 09:35:42 -070041 black_list = list(input_api.DEFAULT_BLACK_LIST)
szager@chromium.org34071032014-03-18 17:23:48 +000042 if os.path.exists('.gitignore'):
43 with open('.gitignore') as fh:
44 lines = [l.strip() for l in fh.readlines()]
45 black_list.extend([fnmatch.translate(l) for l in lines if
46 l and not l.startswith('#')])
47 if os.path.exists('.git/info/exclude'):
48 with open('.git/info/exclude') as fh:
49 lines = [l.strip() for l in fh.readlines()]
50 black_list.extend([fnmatch.translate(l) for l in lines if
51 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000052 disabled_warnings = [
53 'R0401', # Cyclic import
54 'W0613', # Unused argument
55 ]
agablef39c3332016-09-26 09:35:42 -070056 return input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000057 input_api,
58 output_api,
agablef39c3332016-09-26 09:35:42 -070059 white_list=white_list,
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000060 black_list=black_list,
61 disabled_warnings=disabled_warnings)
agablef39c3332016-09-26 09:35:42 -070062
63
Edward Lemura877ee62019-09-03 20:23:17 +000064def CommonChecks(input_api, output_api, tests_to_black_list, run_on_python3):
Edward Lemur595eb192020-03-12 21:53:52 +000065 input_api.SetTimeout(TEST_TIMEOUT_S)
66
agablef39c3332016-09-26 09:35:42 -070067 results = []
68 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
Edward Lesmescb62e482018-04-19 18:29:35 -040069 results.extend(input_api.canned_checks.CheckOwnersFormat(
70 input_api, output_api))
Aaron Gable1318a302019-05-21 21:46:07 +000071 results.extend(input_api.canned_checks.CheckJsonParses(
72 input_api, output_api))
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000073
74 # Run only selected tests on Windows.
75 tests_to_white_list = [r'.*test\.py$']
76 if input_api.platform.startswith(('cygwin', 'win32')):
77 print('Warning: skipping most unit tests on Windows')
Edward Lemurbbd70fd2019-09-26 18:19:41 +000078 tests_to_black_list = [
79 r'.*auth_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000080 r'.*git_common_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000081 r'.*git_hyper_blame_test\.py$',
Edward Lemurb800fde2020-01-10 23:04:44 +000082 r'.*git_map_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000083 r'.*git_number_test\.py$',
84 r'.*git_rebase_update_test\.py$',
85 r'.*ninjalog_uploader_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000086 r'.*recipes_test\.py$',
87 r'.*roll_dep_test\.py$',
Edward Lemurbbd70fd2019-09-26 18:19:41 +000088 ]
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000089
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000090 # TODO(maruel): Make sure at least one file is modified first.
91 # TODO(maruel): If only tests are modified, only run them.
agablef39c3332016-09-26 09:35:42 -070092 tests = DepotToolsPylint(input_api, output_api)
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000093 tests.extend(input_api.canned_checks.GetUnitTestsInDirectory(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000094 input_api,
95 output_api,
96 'tests',
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000097 whitelist=tests_to_white_list,
Edward Lemura877ee62019-09-03 20:23:17 +000098 blacklist=tests_to_black_list,
99 run_on_python3=run_on_python3))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800100
101 # Validate CIPD manifests.
102 root = input_api.os_path.normpath(
103 input_api.os_path.abspath(input_api.PresubmitLocalPath()))
104 rel_file = lambda rel: input_api.os_path.join(root, rel)
105 cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
106 ('cipd_manifest.txt',),
Robert Iannucciee67b972019-12-10 22:46:56 +0000107 ('bootstrap', 'manifest.txt'),
108 ('bootstrap', 'manifest_bleeding_edge.txt'),
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800109
110 # Also generate a file for the cipd client itself.
111 ('cipd_client_version',),
112 ))
113 affected_manifests = input_api.AffectedFiles(
114 include_deletes=False,
115 file_filter=lambda x:
116 input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
117 for path in affected_manifests:
118 path = path.AbsoluteLocalPath()
119 if path.endswith('.txt'):
120 tests.append(input_api.canned_checks.CheckCIPDManifest(
121 input_api, output_api, path=path))
122 else:
123 pkg = 'infra/tools/cipd/${platform}'
124 ver = input_api.ReadFile(path)
125 tests.append(input_api.canned_checks.CheckCIPDManifest(
Vadim Shtayura09098852018-06-21 22:50:30 +0000126 input_api, output_api,
127 content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
Vadim Shtayuraf9b48452018-09-18 00:22:26 +0000128 tests.append(input_api.canned_checks.CheckCIPDClientDigests(
129 input_api, output_api, client_version_file=path))
Robert Iannucci7d47eb52017-12-06 12:18:18 -0800130
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +0000131 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +0000132 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000133
134
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000135def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000136 # Do not run integration tests on upload since they are way too slow.
137 tests_to_black_list = [
138 r'^checkout_test\.py$',
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000139 r'^cipd_bootstrap_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000140 r'^gclient_smoketest\.py$',
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000141 ]
Edward Lemura877ee62019-09-03 20:23:17 +0000142 # TODO(ehmaldonado): Run Python 3 tests on upload once Python 3 is
143 # bootstrapped on Linux and Mac.
144 return CommonChecks(input_api, output_api, tests_to_black_list, False)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000145
146
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000147def CheckChangeOnCommit(input_api, output_api):
148 output = []
Edward Lemura877ee62019-09-03 20:23:17 +0000149 output.extend(CommonChecks(input_api, output_api, [], True))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000150 output.extend(input_api.canned_checks.CheckDoNotSubmit(
151 input_api,
152 output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000153 return output