blob: f082fb010603da8962327f7f7096ca8a13bfba2d [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
13
dpranke@chromium.org627ea672011-03-11 23:29:03 +000014
agablef39c3332016-09-26 09:35:42 -070015def DepotToolsPylint(input_api, output_api):
16 """Gather all the pylint logic into one place to make it self-contained."""
17 white_list = [
18 r'^[^/]*\.py$',
19 r'^testing_support/[^/]*\.py$',
20 r'^tests/[^/]*\.py$',
21 r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
tandrii@chromium.org55cb27e2016-05-16 17:54:40 +000022 ]
agablef39c3332016-09-26 09:35:42 -070023 black_list = list(input_api.DEFAULT_BLACK_LIST)
szager@chromium.org34071032014-03-18 17:23:48 +000024 if os.path.exists('.gitignore'):
25 with open('.gitignore') as fh:
26 lines = [l.strip() for l in fh.readlines()]
27 black_list.extend([fnmatch.translate(l) for l in lines if
28 l and not l.startswith('#')])
29 if os.path.exists('.git/info/exclude'):
30 with open('.git/info/exclude') as fh:
31 lines = [l.strip() for l in fh.readlines()]
32 black_list.extend([fnmatch.translate(l) for l in lines if
33 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000034 disabled_warnings = [
35 'R0401', # Cyclic import
36 'W0613', # Unused argument
37 ]
agablef39c3332016-09-26 09:35:42 -070038 return input_api.canned_checks.GetPylint(
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000039 input_api,
40 output_api,
agablef39c3332016-09-26 09:35:42 -070041 white_list=white_list,
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000042 black_list=black_list,
43 disabled_warnings=disabled_warnings)
agablef39c3332016-09-26 09:35:42 -070044
45
46def CommonChecks(input_api, output_api, tests_to_black_list):
47 results = []
48 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000049 # TODO(maruel): Make sure at least one file is modified first.
50 # TODO(maruel): If only tests are modified, only run them.
agablef39c3332016-09-26 09:35:42 -070051 tests = DepotToolsPylint(input_api, output_api)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000052 unit_tests = input_api.canned_checks.GetUnitTestsInDirectory(
53 input_api,
54 output_api,
55 'tests',
56 whitelist=[r'.*test\.py$'],
57 blacklist=tests_to_black_list)
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000058 if not input_api.platform.startswith(('cygwin', 'win32')):
59 tests.extend(unit_tests)
60 else:
61 print('Warning: not running unit tests on Windows')
62 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000063 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +000064
65
maruel@chromium.org899e1c12011-04-07 17:03:18 +000066def RunGitClTests(input_api, output_api):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000067 """Run all the shells scripts in the directory test.
68 """
maruel@chromium.orgc98c0c52011-04-06 13:39:43 +000069 if input_api.platform == 'win32':
70 # Skip for now as long as the test scripts are bash scripts.
71 return []
72
maruel@chromium.org2a74d372011-03-29 19:05:50 +000073 # First loads a local Rietveld instance.
74 import sys
75 old_sys_path = sys.path
76 try:
77 sys.path = [input_api.PresubmitLocalPath()] + sys.path
maruel@chromium.org0927b7e2011-11-11 16:06:22 +000078 from testing_support import local_rietveld
maruel@chromium.org2a74d372011-03-29 19:05:50 +000079 server = local_rietveld.LocalRietveld()
80 finally:
81 sys.path = old_sys_path
82
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000083 results = []
maruel@chromium.org2a74d372011-03-29 19:05:50 +000084 try:
85 # Start a local rietveld instance to test against.
86 server.start_server()
87 test_path = input_api.os_path.abspath(
88 input_api.os_path.join(input_api.PresubmitLocalPath(), 'tests'))
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000089 for test in input_api.os_listdir(test_path):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000090 # test-lib.sh is not an actual test so it should not be run. The other
91 # tests are tests known to fail.
92 DISABLED_TESTS = (
93 'owners.sh', 'push-from-logs.sh', 'rename.sh', 'test-lib.sh')
94 if test in DISABLED_TESTS or not test.endswith('.sh'):
95 continue
96
97 print('Running %s' % test)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000098 try:
maruel@chromium.org899e1c12011-04-07 17:03:18 +000099 if input_api.verbose:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000100 input_api.subprocess.check_call(
101 [input_api.os_path.join(test_path, test)], cwd=test_path)
102 else:
103 input_api.subprocess.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000104 [input_api.os_path.join(test_path, test)], cwd=test_path,
105 stderr=input_api.subprocess.STDOUT)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000106 except (OSError, input_api.subprocess.CalledProcessError), e:
107 results.append(output_api.PresubmitError('%s failed\n%s' % (test, e)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000108 except local_rietveld.Failure, e:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000109 results.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000110 finally:
111 server.stop_server()
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000112 return results
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000113
114
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000115def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000116 # Do not run integration tests on upload since they are way too slow.
117 tests_to_black_list = [
118 r'^checkout_test\.py$',
119 r'^gclient_smoketest\.py$',
120 r'^scm_unittest\.py$',
ilevy@chromium.org58565622013-03-17 23:18:37 +0000121 r'^subprocess2_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000122 ]
123 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000124
125
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000126def CheckChangeOnCommit(input_api, output_api):
127 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000128 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000129 output.extend(input_api.canned_checks.CheckDoNotSubmit(
130 input_api,
131 output_api))
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000132 output.extend(RunGitClTests(input_api, output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000133 return output