blob: 3002447e4f516c28450198dfb82432772fb0727b [file] [log] [blame]
maruel@chromium.org2a74d372011-03-29 19:05:50 +00001# Copyright (c) 2011 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.orge94aedc2010-12-13 21:11:30 +000012def CommonChecks(input_api, output_api):
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$',
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000029 r'^python_bin[\/\\].+',
30 r'^svn_bin[\/\\].+',
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000031 r'^tests[\/\\]\w+?[\/\\].+']
32 results.extend(input_api.canned_checks.RunPylint(
maruel@chromium.orge94aedc2010-12-13 21:11:30 +000033 input_api,
34 output_api,
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000035 white_list=[r'.*\.py$', r'^git-try$'],
maruel@chromium.orgbf38a7e2010-12-14 18:15:54 +000036 black_list=black_list))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000037
38 # TODO(maruel): Make sure at least one file is modified first.
39 # TODO(maruel): If only tests are modified, only run them.
40 verbose = False
41 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
42 input_api,
43 output_api,
44 'tests',
45 whitelist=[r'.*test\.py$'],
46 verbose=verbose))
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000047 results.extend(RunGitClTests(input_api, output_api, verbose=verbose))
maruel@chromium.org3c9f7ca2011-04-01 14:52:38 +000048 return results
maruel@chromium.org2a74d372011-03-29 19:05:50 +000049
50
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000051def RunGitClTests(input_api, output_api, verbose):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000052 """Run all the shells scripts in the directory test.
53 """
maruel@chromium.org2a74d372011-03-29 19:05:50 +000054 # First loads a local Rietveld instance.
55 import sys
56 old_sys_path = sys.path
57 try:
58 sys.path = [input_api.PresubmitLocalPath()] + sys.path
59 from tests import local_rietveld # pylint: disable=W0403
60 server = local_rietveld.LocalRietveld()
61 finally:
62 sys.path = old_sys_path
63
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000064 results = []
maruel@chromium.org2a74d372011-03-29 19:05:50 +000065 try:
66 # Start a local rietveld instance to test against.
67 server.start_server()
68 test_path = input_api.os_path.abspath(
69 input_api.os_path.join(input_api.PresubmitLocalPath(), 'tests'))
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000070 for test in input_api.os_listdir(test_path):
maruel@chromium.org2a74d372011-03-29 19:05:50 +000071 # test-lib.sh is not an actual test so it should not be run. The other
72 # tests are tests known to fail.
73 DISABLED_TESTS = (
74 'owners.sh', 'push-from-logs.sh', 'rename.sh', 'test-lib.sh')
75 if test in DISABLED_TESTS or not test.endswith('.sh'):
76 continue
77
78 print('Running %s' % test)
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000079 try:
80 if verbose:
81 input_api.subprocess.check_call(
82 [input_api.os_path.join(test_path, test)], cwd=test_path)
83 else:
84 input_api.subprocess.check_output(
85 [input_api.os_path.join(test_path, test)], cwd=test_path)
86 except (OSError, input_api.subprocess.CalledProcessError), e:
87 results.append(output_api.PresubmitError('%s failed\n%s' % (test, e)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +000088 except local_rietveld.Failure, e:
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000089 results.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args)))
maruel@chromium.org2a74d372011-03-29 19:05:50 +000090 finally:
91 server.stop_server()
maruel@chromium.orgccbb7812011-04-06 13:36:23 +000092 return results
maruel@chromium.orgce30ef72009-05-25 15:20:42 +000093
94
maruel@chromium.orge94aedc2010-12-13 21:11:30 +000095def CheckChangeOnUpload(input_api, output_api):
96 return CommonChecks(input_api, output_api)
97
98
maruel@chromium.orgce30ef72009-05-25 15:20:42 +000099def CheckChangeOnCommit(input_api, output_api):
100 output = []
maruel@chromium.orge94aedc2010-12-13 21:11:30 +0000101 output.extend(CommonChecks(input_api, output_api))
102 output.extend(input_api.canned_checks.CheckDoNotSubmit(
103 input_api,
104 output_api))
maruel@chromium.org7b305e82009-05-19 18:24:20 +0000105 return output