blob: 3ae6bcb0f65eb0d8f3d539d17cfbcd5e04186a02 [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.org725f1c32011-04-01 20:24:54 +000014 import sys
15 if not sys.version.startswith('2.5'):
16 # Depot_tools has the particularity that it needs to be tested on python
17 # 2.5. But we don't want the presubmit check to fail if it is not installed.
18 results.append(output_api.PresubmitNotifyResult(
19 'You should install python 2.5 and run ln -s $(which python2.5) python.'
20 '\n'
21 'A great place to put this symlink is in depot_tools.\n'
22 'Otherwise, you break depot_tools on python 2.5, you get to keep the '
23 'pieces.'))
24
maruel@chromium.org5d0dc432011-01-03 02:40:37 +000025
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000026 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
maruel@chromium.orgbf38a7e2010-12-14 18:15:54 +000027 black_list = list(input_api.DEFAULT_BLACK_LIST) + [
maruel@chromium.org5d0dc432011-01-03 02:40:37 +000028 r'^cpplint\.py$',
asvitkine@chromium.org63c2c1d2011-09-07 21:41:55 +000029 r'^cpplint_chromium\.py$',
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000030 r'^python_bin[\/\\].+',
chrisha@chromium.orgc920ad22012-02-02 18:26:04 +000031 r'^site-packages-py[0-9]\.[0-9][\/\\].+',
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000032 r'^svn_bin[\/\\].+',
maruel@chromium.org0927b7e2011-11-11 16:06:22 +000033 r'^testing_support[\/\\]_rietveld[\/\\].+']
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000034 results.extend(input_api.canned_checks.RunPylint(
maruel@chromium.orge94aedc2010-12-13 21:11:30 +000035 input_api,
36 output_api,
maruel@chromium.org57e48b82011-06-15 17:34:59 +000037 white_list=[r'.*\.py$'],
maruel@chromium.orgbf38a7e2010-12-14 18:15:54 +000038 black_list=black_list))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000039
40 # TODO(maruel): Make sure at least one file is modified first.
41 # TODO(maruel): If only tests are modified, only run them.
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000042 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
43 input_api,
44 output_api,
45 'tests',
maruel@chromium.orgfae707b2011-09-15 18:57:58 +000046 whitelist=[r'.*test\.py$'],
maruel@chromium.orgd52417c2011-09-02 20:13:17 +000047 blacklist=tests_to_black_list))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000048 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +000049
50
maruel@chromium.org899e1c12011-04-07 17:03:18 +000051def RunGitClTests(input_api, output_api):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000052 """Run all the shells scripts in the directory test.
53 """
maruel@chromium.orgc98c0c52011-04-06 13:39:43 +000054 if input_api.platform == 'win32':
55 # Skip for now as long as the test scripts are bash scripts.
56 return []
57
maruel@chromium.org2a74d372011-03-29 19:05:50 +000058 # First loads a local Rietveld instance.
59 import sys
60 old_sys_path = sys.path
61 try:
62 sys.path = [input_api.PresubmitLocalPath()] + sys.path
maruel@chromium.org0927b7e2011-11-11 16:06:22 +000063 from testing_support import local_rietveld
maruel@chromium.org2a74d372011-03-29 19:05:50 +000064 server = local_rietveld.LocalRietveld()
65 finally:
66 sys.path = old_sys_path
67
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000068 results = []
maruel@chromium.org2a74d372011-03-29 19:05:50 +000069 try:
70 # Start a local rietveld instance to test against.
71 server.start_server()
72 test_path = input_api.os_path.abspath(
73 input_api.os_path.join(input_api.PresubmitLocalPath(), 'tests'))
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000074 for test in input_api.os_listdir(test_path):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000075 # test-lib.sh is not an actual test so it should not be run. The other
76 # tests are tests known to fail.
77 DISABLED_TESTS = (
78 'owners.sh', 'push-from-logs.sh', 'rename.sh', 'test-lib.sh')
79 if test in DISABLED_TESTS or not test.endswith('.sh'):
80 continue
81
82 print('Running %s' % test)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000083 try:
maruel@chromium.org899e1c12011-04-07 17:03:18 +000084 if input_api.verbose:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000085 input_api.subprocess.check_call(
86 [input_api.os_path.join(test_path, test)], cwd=test_path)
87 else:
88 input_api.subprocess.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +000089 [input_api.os_path.join(test_path, test)], cwd=test_path,
90 stderr=input_api.subprocess.STDOUT)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000091 except (OSError, input_api.subprocess.CalledProcessError), e:
92 results.append(output_api.PresubmitError('%s failed\n%s' % (test, e)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +000093 except local_rietveld.Failure, e:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000094 results.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +000095 finally:
96 server.stop_server()
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000097 return results
maruel@chromium.orgce30ef72009-05-25 15:20:42 +000098
99
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000100def CheckChangeOnUpload(input_api, output_api):
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000101 # Do not run integration tests on upload since they are way too slow.
102 tests_to_black_list = [
103 r'^checkout_test\.py$',
104 r'^gclient_smoketest\.py$',
105 r'^scm_unittest\.py$',
106 ]
107 return CommonChecks(input_api, output_api, tests_to_black_list)
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000108
109
maruel@chromium.orgce30ef72009-05-25 15:20:42 +0000110def CheckChangeOnCommit(input_api, output_api):
111 output = []
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000112 output.extend(CommonChecks(input_api, output_api, []))
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000113 output.extend(input_api.canned_checks.CheckDoNotSubmit(
114 input_api,
115 output_api))
maruel@chromium.orgd52417c2011-09-02 20:13:17 +0000116 output.extend(RunGitClTests(input_api, output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000117 return output