blob: c1cc5de20cfcc204950311bf6e47d7f844b0c60d [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
maruel@chromium.orgd52417c2011-09-02 20:13:17 +000015def CommonChecks(input_api, output_api, tests_to_black_list):
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000016 results = []
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000017 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
maruel@chromium.orgbf38a7e2010-12-14 18:15:54 +000018 black_list = list(input_api.DEFAULT_BLACK_LIST) + [
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +000019 r'^\.recipe_deps[\/\\].*',
maruel@chromium.org5d0dc432011-01-03 02:40:37 +000020 r'^cpplint\.py$',
asvitkine@chromium.org63c2c1d2011-09-07 21:41:55 +000021 r'^cpplint_chromium\.py$',
raphael.kubo.da.costa@intel.com7cc38e82015-01-08 22:28:34 +000022 r'^external_bin[\/\\].+',
bratell@opera.com154c36c2013-12-12 14:13:44 +000023 r'^python[0-9]*_bin[\/\\].+',
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +000024 r'^recipes\.py$',
chrisha@chromium.orgc920ad22012-02-02 18:26:04 +000025 r'^site-packages-py[0-9]\.[0-9][\/\\].+',
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000026 r'^svn_bin[\/\\].+',
sergiyb@chromium.org150aa7b2015-11-09 19:20:55 +000027 r'^testing_support[\/\\]_rietveld[\/\\].+']
szager@chromium.org34071032014-03-18 17:23:48 +000028 if os.path.exists('.gitignore'):
29 with open('.gitignore') as fh:
30 lines = [l.strip() for l in fh.readlines()]
31 black_list.extend([fnmatch.translate(l) for l in lines if
32 l and not l.startswith('#')])
33 if os.path.exists('.git/info/exclude'):
34 with open('.git/info/exclude') as fh:
35 lines = [l.strip() for l in fh.readlines()]
36 black_list.extend([fnmatch.translate(l) for l in lines if
37 l and not l.startswith('#')])
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000038 disabled_warnings = [
39 'R0401', # Cyclic import
40 'W0613', # Unused argument
41 ]
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000042 pylint = input_api.canned_checks.GetPylint(
43 input_api,
44 output_api,
45 white_list=[r'.*\.py$'],
46 black_list=black_list,
47 disabled_warnings=disabled_warnings)
48 # TODO(maruel): Make sure at least one file is modified first.
49 # TODO(maruel): If only tests are modified, only run them.
50 unit_tests = input_api.canned_checks.GetUnitTestsInDirectory(
51 input_api,
52 output_api,
53 'tests',
54 whitelist=[r'.*test\.py$'],
55 blacklist=tests_to_black_list)
56 tests = pylint
57 if not input_api.platform.startswith(('cygwin', 'win32')):
58 tests.extend(unit_tests)
59 else:
60 print('Warning: not running unit tests on Windows')
61 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000062 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +000063
64
maruel@chromium.org899e1c12011-04-07 17:03:18 +000065def RunGitClTests(input_api, output_api):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000066 """Run all the shells scripts in the directory test.
67 """
maruel@chromium.orgc98c0c52011-04-06 13:39:43 +000068 if input_api.platform == 'win32':
69 # Skip for now as long as the test scripts are bash scripts.
70 return []
71
maruel@chromium.org2a74d372011-03-29 19:05:50 +000072 # First loads a local Rietveld instance.
73 import sys
74 old_sys_path = sys.path
75 try:
76 sys.path = [input_api.PresubmitLocalPath()] + sys.path
maruel@chromium.org0927b7e2011-11-11 16:06:22 +000077 from testing_support import local_rietveld
maruel@chromium.org2a74d372011-03-29 19:05:50 +000078 server = local_rietveld.LocalRietveld()
79 finally:
80 sys.path = old_sys_path
81
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000082 results = []
maruel@chromium.org2a74d372011-03-29 19:05:50 +000083 try:
84 # Start a local rietveld instance to test against.
85 server.start_server()
86 test_path = input_api.os_path.abspath(
87 input_api.os_path.join(input_api.PresubmitLocalPath(), 'tests'))
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000088 for test in input_api.os_listdir(test_path):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000089 # test-lib.sh is not an actual test so it should not be run. The other
90 # tests are tests known to fail.
91 DISABLED_TESTS = (
92 'owners.sh', 'push-from-logs.sh', 'rename.sh', 'test-lib.sh')
93 if test in DISABLED_TESTS or not test.endswith('.sh'):
94 continue
95
96 print('Running %s' % test)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000097 try:
maruel@chromium.org899e1c12011-04-07 17:03:18 +000098 if input_api.verbose:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000099 input_api.subprocess.check_call(
100 [input_api.os_path.join(test_path, test)], cwd=test_path)
101 else:
102 input_api.subprocess.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000103 [input_api.os_path.join(test_path, test)], cwd=test_path,
104 stderr=input_api.subprocess.STDOUT)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000105 except (OSError, input_api.subprocess.CalledProcessError), e:
106 results.append(output_api.PresubmitError('%s failed\n%s' % (test, e)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000107 except local_rietveld.Failure, e:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000108 results.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +0000109 finally:
110 server.stop_server()
maruel@chromium.orgccbb7812011-04-06 13:36:23 +0000111 return results
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000112
113
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000114def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000115 # Do not run integration tests on upload since they are way too slow.
116 tests_to_black_list = [
117 r'^checkout_test\.py$',
118 r'^gclient_smoketest\.py$',
119 r'^scm_unittest\.py$',
ilevy@chromium.org58565622013-03-17 23:18:37 +0000120 r'^subprocess2_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000121 ]
122 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000123
124
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000125def CheckChangeOnCommit(input_api, output_api):
126 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000127 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000128 output.extend(input_api.canned_checks.CheckDoNotSubmit(
129 input_api,
130 output_api))
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000131 output.extend(RunGitClTests(input_api, output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000132 return output