blob: a56b4834a9dd2bf157b5b01fff7246e4bb5a0cd1 [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
dpranke@chromium.org627ea672011-03-11 23:29:03 +000011
maruel@chromium.orgd52417c2011-09-02 20:13:17 +000012def CommonChecks(input_api, output_api, tests_to_black_list):
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000013 results = []
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000014 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
maruel@chromium.orgbf38a7e2010-12-14 18:15:54 +000015 black_list = list(input_api.DEFAULT_BLACK_LIST) + [
maruel@chromium.org5d0dc432011-01-03 02:40:37 +000016 r'^cpplint\.py$',
asvitkine@chromium.org63c2c1d2011-09-07 21:41:55 +000017 r'^cpplint_chromium\.py$',
bratell@opera.com154c36c2013-12-12 14:13:44 +000018 r'^python[0-9]*_bin[\/\\].+',
chrisha@chromium.orgc920ad22012-02-02 18:26:04 +000019 r'^site-packages-py[0-9]\.[0-9][\/\\].+',
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000020 r'^svn_bin[\/\\].+',
maruel@chromium.org0927b7e2011-11-11 16:06:22 +000021 r'^testing_support[\/\\]_rietveld[\/\\].+']
maruel@chromium.orgff9a2172012-04-24 16:55:32 +000022 disabled_warnings = [
23 'R0401', # Cyclic import
24 'W0613', # Unused argument
25 ]
scottmg@chromium.org69ae86a2014-01-17 03:55:45 +000026 pylint = input_api.canned_checks.GetPylint(
27 input_api,
28 output_api,
29 white_list=[r'.*\.py$'],
30 black_list=black_list,
31 disabled_warnings=disabled_warnings)
32 # TODO(maruel): Make sure at least one file is modified first.
33 # TODO(maruel): If only tests are modified, only run them.
34 unit_tests = input_api.canned_checks.GetUnitTestsInDirectory(
35 input_api,
36 output_api,
37 'tests',
38 whitelist=[r'.*test\.py$'],
39 blacklist=tests_to_black_list)
40 tests = pylint
41 if not input_api.platform.startswith(('cygwin', 'win32')):
42 tests.extend(unit_tests)
43 else:
44 print('Warning: not running unit tests on Windows')
45 results.extend(input_api.RunTests(tests))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000046 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +000047
48
maruel@chromium.org899e1c12011-04-07 17:03:18 +000049def RunGitClTests(input_api, output_api):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000050 """Run all the shells scripts in the directory test.
51 """
maruel@chromium.orgc98c0c52011-04-06 13:39:43 +000052 if input_api.platform == 'win32':
53 # Skip for now as long as the test scripts are bash scripts.
54 return []
55
maruel@chromium.org2a74d372011-03-29 19:05:50 +000056 # First loads a local Rietveld instance.
57 import sys
58 old_sys_path = sys.path
59 try:
60 sys.path = [input_api.PresubmitLocalPath()] + sys.path
maruel@chromium.org0927b7e2011-11-11 16:06:22 +000061 from testing_support import local_rietveld
maruel@chromium.org2a74d372011-03-29 19:05:50 +000062 server = local_rietveld.LocalRietveld()
63 finally:
64 sys.path = old_sys_path
65
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000066 results = []
maruel@chromium.org2a74d372011-03-29 19:05:50 +000067 try:
68 # Start a local rietveld instance to test against.
69 server.start_server()
70 test_path = input_api.os_path.abspath(
71 input_api.os_path.join(input_api.PresubmitLocalPath(), 'tests'))
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000072 for test in input_api.os_listdir(test_path):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000073 # test-lib.sh is not an actual test so it should not be run. The other
74 # tests are tests known to fail.
75 DISABLED_TESTS = (
76 'owners.sh', 'push-from-logs.sh', 'rename.sh', 'test-lib.sh')
77 if test in DISABLED_TESTS or not test.endswith('.sh'):
78 continue
79
80 print('Running %s' % test)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000081 try:
maruel@chromium.org899e1c12011-04-07 17:03:18 +000082 if input_api.verbose:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000083 input_api.subprocess.check_call(
84 [input_api.os_path.join(test_path, test)], cwd=test_path)
85 else:
86 input_api.subprocess.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +000087 [input_api.os_path.join(test_path, test)], cwd=test_path,
88 stderr=input_api.subprocess.STDOUT)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000089 except (OSError, input_api.subprocess.CalledProcessError), e:
90 results.append(output_api.PresubmitError('%s failed\n%s' % (test, e)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +000091 except local_rietveld.Failure, e:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000092 results.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +000093 finally:
94 server.stop_server()
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000095 return results
maruel@chromium.orgce30ef72009-05-25 15:20:42 +000096
97
maruel@chromium.orge94aedc2010-12-13 21:11:30 +000098def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +000099 # Do not run integration tests on upload since they are way too slow.
100 tests_to_black_list = [
101 r'^checkout_test\.py$',
102 r'^gclient_smoketest\.py$',
103 r'^scm_unittest\.py$',
ilevy@chromium.org58565622013-03-17 23:18:37 +0000104 r'^subprocess2_test\.py$',
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000105 ]
106 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000107
108
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000109def CheckChangeOnCommit(input_api, output_api):
110 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000111 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000112 output.extend(input_api.canned_checks.CheckDoNotSubmit(
113 input_api,
114 output_api))
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000115 output.extend(RunGitClTests(input_api, output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000116 return output