chrisha@chromium.org | c920ad2 | 2012-02-02 18:26:04 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 2 | # 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 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 8 | details on the presubmit API built into depot_tools. |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 9 | """ |
| 10 | |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 11 | import fnmatch |
| 12 | import os |
| 13 | |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 14 | |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 15 | def CommonChecks(input_api, output_api, tests_to_black_list): |
maruel@chromium.org | 3c9f7ca | 2011-04-01 14:52:38 +0000 | [diff] [blame] | 16 | results = [] |
maruel@chromium.org | 3c9f7ca | 2011-04-01 14:52:38 +0000 | [diff] [blame] | 17 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
maruel@chromium.org | bf38a7e | 2010-12-14 18:15:54 +0000 | [diff] [blame] | 18 | black_list = list(input_api.DEFAULT_BLACK_LIST) + [ |
luqui@chromium.org | f7bb4cf | 2015-12-04 23:30:38 +0000 | [diff] [blame] | 19 | r'^\.recipe_deps[\/\\].*', |
phajdan.jr@chromium.org | 4c39694 | 2016-02-02 16:13:01 +0000 | [diff] [blame] | 20 | r'^infra[\/\\]\.recipe_deps[\/\\].*', |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 21 | r'^cpplint\.py$', |
asvitkine@chromium.org | 63c2c1d | 2011-09-07 21:41:55 +0000 | [diff] [blame] | 22 | r'^cpplint_chromium\.py$', |
raphael.kubo.da.costa@intel.com | 7cc38e8 | 2015-01-08 22:28:34 +0000 | [diff] [blame] | 23 | r'^external_bin[\/\\].+', |
bratell@opera.com | 154c36c | 2013-12-12 14:13:44 +0000 | [diff] [blame] | 24 | r'^python[0-9]*_bin[\/\\].+', |
luqui@chromium.org | f7bb4cf | 2015-12-04 23:30:38 +0000 | [diff] [blame] | 25 | r'^recipes\.py$', |
chrisha@chromium.org | c920ad2 | 2012-02-02 18:26:04 +0000 | [diff] [blame] | 26 | r'^site-packages-py[0-9]\.[0-9][\/\\].+', |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 27 | r'^svn_bin[\/\\].+', |
tandrii@chromium.org | 55cb27e | 2016-05-16 17:54:40 +0000 | [diff] [blame] | 28 | r'^testing_support[\/\\]_rietveld[\/\\].+', |
| 29 | r'^testing_support[\/\\]_infra[\/\\].+', |
| 30 | ] |
szager@chromium.org | 3407103 | 2014-03-18 17:23:48 +0000 | [diff] [blame] | 31 | if os.path.exists('.gitignore'): |
| 32 | with open('.gitignore') as fh: |
| 33 | lines = [l.strip() for l in fh.readlines()] |
| 34 | black_list.extend([fnmatch.translate(l) for l in lines if |
| 35 | l and not l.startswith('#')]) |
| 36 | if os.path.exists('.git/info/exclude'): |
| 37 | with open('.git/info/exclude') as fh: |
| 38 | lines = [l.strip() for l in fh.readlines()] |
| 39 | black_list.extend([fnmatch.translate(l) for l in lines if |
| 40 | l and not l.startswith('#')]) |
maruel@chromium.org | ff9a217 | 2012-04-24 16:55:32 +0000 | [diff] [blame] | 41 | disabled_warnings = [ |
| 42 | 'R0401', # Cyclic import |
| 43 | 'W0613', # Unused argument |
| 44 | ] |
scottmg@chromium.org | 69ae86a | 2014-01-17 03:55:45 +0000 | [diff] [blame] | 45 | pylint = input_api.canned_checks.GetPylint( |
| 46 | input_api, |
| 47 | output_api, |
| 48 | white_list=[r'.*\.py$'], |
| 49 | black_list=black_list, |
| 50 | disabled_warnings=disabled_warnings) |
| 51 | # TODO(maruel): Make sure at least one file is modified first. |
| 52 | # TODO(maruel): If only tests are modified, only run them. |
| 53 | unit_tests = input_api.canned_checks.GetUnitTestsInDirectory( |
| 54 | input_api, |
| 55 | output_api, |
| 56 | 'tests', |
| 57 | whitelist=[r'.*test\.py$'], |
| 58 | blacklist=tests_to_black_list) |
| 59 | tests = pylint |
| 60 | if not input_api.platform.startswith(('cygwin', 'win32')): |
| 61 | tests.extend(unit_tests) |
| 62 | else: |
| 63 | print('Warning: not running unit tests on Windows') |
| 64 | results.extend(input_api.RunTests(tests)) |
maruel@chromium.org | 3c9f7ca | 2011-04-01 14:52:38 +0000 | [diff] [blame] | 65 | return results |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 66 | |
| 67 | |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 68 | def RunGitClTests(input_api, output_api): |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 69 | """Run all the shells scripts in the directory test. |
| 70 | """ |
maruel@chromium.org | c98c0c5 | 2011-04-06 13:39:43 +0000 | [diff] [blame] | 71 | if input_api.platform == 'win32': |
| 72 | # Skip for now as long as the test scripts are bash scripts. |
| 73 | return [] |
| 74 | |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 75 | # First loads a local Rietveld instance. |
| 76 | import sys |
| 77 | old_sys_path = sys.path |
| 78 | try: |
| 79 | sys.path = [input_api.PresubmitLocalPath()] + sys.path |
maruel@chromium.org | 0927b7e | 2011-11-11 16:06:22 +0000 | [diff] [blame] | 80 | from testing_support import local_rietveld |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 81 | server = local_rietveld.LocalRietveld() |
| 82 | finally: |
| 83 | sys.path = old_sys_path |
| 84 | |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 85 | results = [] |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 86 | try: |
| 87 | # Start a local rietveld instance to test against. |
| 88 | server.start_server() |
| 89 | test_path = input_api.os_path.abspath( |
| 90 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tests')) |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 91 | for test in input_api.os_listdir(test_path): |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 92 | # test-lib.sh is not an actual test so it should not be run. The other |
| 93 | # tests are tests known to fail. |
| 94 | DISABLED_TESTS = ( |
| 95 | 'owners.sh', 'push-from-logs.sh', 'rename.sh', 'test-lib.sh') |
| 96 | if test in DISABLED_TESTS or not test.endswith('.sh'): |
| 97 | continue |
| 98 | |
| 99 | print('Running %s' % test) |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 100 | try: |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 101 | if input_api.verbose: |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 102 | input_api.subprocess.check_call( |
| 103 | [input_api.os_path.join(test_path, test)], cwd=test_path) |
| 104 | else: |
| 105 | input_api.subprocess.check_output( |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 106 | [input_api.os_path.join(test_path, test)], cwd=test_path, |
| 107 | stderr=input_api.subprocess.STDOUT) |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 108 | except (OSError, input_api.subprocess.CalledProcessError), e: |
| 109 | results.append(output_api.PresubmitError('%s failed\n%s' % (test, e))) |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 110 | except local_rietveld.Failure, e: |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 111 | results.append(output_api.PresubmitError('\n'.join(str(i) for i in e.args))) |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 112 | finally: |
| 113 | server.stop_server() |
maruel@chromium.org | ccbb781 | 2011-04-06 13:36:23 +0000 | [diff] [blame] | 114 | return results |
maruel@chromium.org | ce30ef7 | 2009-05-25 15:20:42 +0000 | [diff] [blame] | 115 | |
| 116 | |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 117 | def CheckChangeOnUpload(input_api, output_api): |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 118 | # Do not run integration tests on upload since they are way too slow. |
| 119 | tests_to_black_list = [ |
| 120 | r'^checkout_test\.py$', |
| 121 | r'^gclient_smoketest\.py$', |
| 122 | r'^scm_unittest\.py$', |
ilevy@chromium.org | 5856562 | 2013-03-17 23:18:37 +0000 | [diff] [blame] | 123 | r'^subprocess2_test\.py$', |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 124 | ] |
| 125 | return CommonChecks(input_api, output_api, tests_to_black_list) |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 126 | |
| 127 | |
maruel@chromium.org | ce30ef7 | 2009-05-25 15:20:42 +0000 | [diff] [blame] | 128 | def CheckChangeOnCommit(input_api, output_api): |
| 129 | output = [] |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 130 | output.extend(CommonChecks(input_api, output_api, [])) |
maruel@chromium.org | e94aedc | 2010-12-13 21:11:30 +0000 | [diff] [blame] | 131 | output.extend(input_api.canned_checks.CheckDoNotSubmit( |
| 132 | input_api, |
| 133 | output_api)) |
maruel@chromium.org | d52417c | 2011-09-02 20:13:17 +0000 | [diff] [blame] | 134 | output.extend(RunGitClTests(input_api, output_api)) |
maruel@chromium.org | 7b305e8 | 2009-05-19 18:24:20 +0000 | [diff] [blame] | 135 | return output |