[email protected] | 2fac375 | 2011-11-27 20:56:51 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [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 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 5 | """Presubmit script for changes affecting chrome/ |
| 6 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
tfarina | 78bb92f4 | 2015-01-31 00:20:48 | [diff] [blame] | 8 | for more details about the presubmit API built into depot_tools. |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 9 | """ |
| 10 | |
| 11 | import re |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 12 | |
| 13 | INCLUDE_CPP_FILES_ONLY = ( |
tfarina | 6aed9ae | 2015-03-08 03:54:07 | [diff] [blame] | 14 | r'.*\.(cc|h)$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 15 | ) |
| 16 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 17 | INCLUDE_SOURCE_FILES_ONLY = ( |
| 18 | r'.*\.(c|cc|cpp|h|m|mm)$', |
| 19 | ) |
| 20 | |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 21 | EXCLUDE = ( |
| 22 | # Objective C confuses everything. |
| 23 | r'.*cocoa.*', |
| 24 | r'.*_mac\.(cc|h)$', |
| 25 | r'.*_mac_.*', |
| 26 | # All the messages files do weird multiple include trickery |
[email protected] | 3139e9f | 2013-10-10 21:29:47 | [diff] [blame] | 27 | r'.*_messages.*\.h$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 28 | r'render_messages.h$', |
| 29 | # Autogenerated window resources files are off limits |
| 30 | r'.*resource.h$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 31 | # Header trickery |
| 32 | r'.*-inl\.h$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 33 | # Has safe printf usage that cpplint complains about |
| 34 | r'safe_browsing_util\.cc$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 35 | ) |
| 36 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 37 | def _CheckChangeLintsClean(input_api, output_api): |
| 38 | """Makes sure that the chrome/ code is cpplint clean.""" |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 39 | black_list = input_api.DEFAULT_BLACK_LIST + EXCLUDE |
| 40 | sources = lambda x: input_api.FilterSourceFile( |
| 41 | x, white_list=INCLUDE_CPP_FILES_ONLY, black_list=black_list) |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 42 | return input_api.canned_checks.CheckChangeLintsClean( |
| 43 | input_api, output_api, sources) |
| 44 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 45 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 46 | def _CheckNoContentUnitTestsInChrome(input_api, output_api): |
| 47 | """Makes sure that no unit tests from content/ are included in unit_tests.""" |
| 48 | problems = [] |
| 49 | for f in input_api.AffectedFiles(): |
Lei Zhang | 421a7ac | 2017-11-13 07:16:27 | [diff] [blame] | 50 | if not f.LocalPath().endswith('BUILD.gn'): |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 51 | continue |
| 52 | |
| 53 | for line_num, line in f.ChangedContents(): |
| 54 | m = re.search(r"'(.*\/content\/.*unittest.*)'", line) |
| 55 | if m: |
| 56 | problems.append(m.group(1)) |
| 57 | |
| 58 | if not problems: |
| 59 | return [] |
| 60 | return [output_api.PresubmitPromptWarning( |
| 61 | 'Unit tests located in content/ should be added to the ' + |
Lei Zhang | 421a7ac | 2017-11-13 07:16:27 | [diff] [blame] | 62 | 'content_unittests target.', |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 63 | items=problems)] |
| 64 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 65 | |
| 66 | def _CheckNoOSIOSMacrosInChromeFile(input_api, f): |
| 67 | """Check for OS_IOS in a given file in chrome/.""" |
| 68 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
| 69 | ios_macro = input_api.re.compile(r'defined\(OS_IOS\)') |
| 70 | results = [] |
| 71 | for lnum, line in f.ChangedContents(): |
| 72 | if preprocessor_statement.search(line) and ios_macro.search(line): |
| 73 | results.append(' %s:%d' % (f.LocalPath(), lnum)) |
| 74 | |
| 75 | return results |
| 76 | |
| 77 | |
| 78 | def _CheckNoOSIOSMacrosInChrome(input_api, output_api): |
| 79 | """Check for OS_IOS which isn't used in chrome/.""" |
| 80 | ios_macros = [] |
| 81 | def SourceFilter(affected_file): |
| 82 | return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY, |
| 83 | input_api.DEFAULT_BLACK_LIST) |
| 84 | for f in input_api.AffectedSourceFiles(SourceFilter): |
| 85 | ios_macros.extend(_CheckNoOSIOSMacrosInChromeFile(input_api, f)) |
| 86 | |
| 87 | if not ios_macros: |
| 88 | return [] |
| 89 | |
| 90 | return [output_api.PresubmitError( |
| 91 | 'OS_IOS is not used in chrome/ but found in:\n', ios_macros)] |
| 92 | |
| 93 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 94 | def _CommonChecks(input_api, output_api): |
| 95 | """Checks common to both upload and commit.""" |
| 96 | results = [] |
| 97 | results.extend(_CheckNoContentUnitTestsInChrome(input_api, output_api)) |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 98 | results.extend(_CheckNoOSIOSMacrosInChrome(input_api, output_api)) |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 99 | return results |
| 100 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 101 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 102 | def CheckChangeOnUpload(input_api, output_api): |
| 103 | results = [] |
| 104 | results.extend(_CommonChecks(input_api, output_api)) |
| 105 | results.extend(_CheckChangeLintsClean(input_api, output_api)) |
| 106 | return results |
| 107 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 108 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 109 | def CheckChangeOnCommit(input_api, output_api): |
| 110 | results = [] |
| 111 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 112 | return results |