Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 1 | # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Presubmit script for ios. |
| 6 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 | for more details about the presubmit API built into depot_tools. |
| 9 | """ |
| 10 | |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 11 | import os |
| 12 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 13 | TODO_PATTERN = r'TO[D]O\(([^\)]*)\)' |
| 14 | CRBUG_PATTERN = r'crbug\.com/\d+$' |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame^] | 15 | ARC_COMPILE_GUARD = [ |
| 16 | '#if !defined(__has_feature) || !__has_feature(objc_arc)', |
| 17 | '#error "This file requires ARC support."', |
| 18 | '#endif', |
| 19 | ] |
| 20 | |
| 21 | def IsSubListOf(needle, hay): |
| 22 | """Returns whether there is a slice of |hay| equal to |needle|.""" |
| 23 | for i, line in enumerate(hay): |
| 24 | if line == needle[0]: |
| 25 | if needle == hay[i:i+len(needle)]: |
| 26 | return True |
| 27 | return False |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 28 | |
| 29 | def _CheckARCCompilationGuard(input_api, output_api): |
| 30 | """ Checks whether new objc files have proper ARC compile guards.""" |
| 31 | files_without_headers = [] |
| 32 | for f in input_api.AffectedFiles(): |
| 33 | if f.Action() != 'A': |
| 34 | continue |
| 35 | |
| 36 | _, ext = os.path.splitext(f.LocalPath()) |
| 37 | if ext not in ('.m', '.mm'): |
| 38 | continue |
| 39 | |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame^] | 40 | if not IsSubListOf(ARC_COMPILE_GUARD, f.NewContents()): |
| 41 | files_without_headers.append(f.LocalPath()) |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 42 | |
| 43 | if not files_without_headers: |
| 44 | return [] |
| 45 | |
| 46 | plural_suffix = '' if len(files_without_headers) == 1 else 's' |
| 47 | error_message = '\n'.join([ |
| 48 | 'Found new Objective-C implementation file%(plural)s without compile' |
| 49 | ' guard%(plural)s. Please use the following compile guard' |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame^] | 50 | ':' % {'plural': plural_suffix} |
| 51 | ] + ARC_COMPILE_GUARD + files_without_headers) + '\n' |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 52 | |
| 53 | return [output_api.PresubmitError(error_message)] |
| 54 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 55 | |
| 56 | def _CheckBugInToDo(input_api, output_api): |
| 57 | """ Checks whether TODOs in ios code are identified by a bug number.""" |
| 58 | todo_regex = input_api.re.compile(TODO_PATTERN) |
| 59 | crbug_regex = input_api.re.compile(CRBUG_PATTERN) |
| 60 | |
| 61 | errors = [] |
| 62 | for f in input_api.AffectedFiles(): |
| 63 | for line_num, line in f.ChangedContents(): |
| 64 | todo_match = todo_regex.search(line) |
| 65 | if not todo_match: |
| 66 | continue |
| 67 | crbug_match = crbug_regex.match(todo_match.group(1)) |
| 68 | if not crbug_match: |
| 69 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 70 | if not errors: |
| 71 | return [] |
| 72 | |
| 73 | plural_suffix = '' if len(errors) == 1 else 's' |
| 74 | error_message = '\n'.join([ |
| 75 | 'Found TO''DO%(plural)s without bug number%(plural)s (expected format is ' |
| 76 | '\"TO''DO(crbug.com/######)\":' % {'plural': plural_suffix} |
| 77 | ] + errors) + '\n' |
| 78 | |
| 79 | return [output_api.PresubmitError(error_message)] |
| 80 | |
| 81 | |
| 82 | def CheckChangeOnUpload(input_api, output_api): |
| 83 | results = [] |
| 84 | results.extend(_CheckBugInToDo(input_api, output_api)) |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 85 | results.extend(_CheckARCCompilationGuard(input_api, output_api)) |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 86 | return results |
Misha Efimov | 015b1d8 | 2017-10-18 18:49:59 | [diff] [blame] | 87 | |
| 88 | def PostUploadHook(cl, change, output_api): |
| 89 | """git cl upload will call this hook after the issue is created/modified. |
| 90 | |
| 91 | This hook adds an extra try bot to the CL description in order to run Cronet |
Menglu Huang | a7e41d21 | 2017-11-09 18:30:12 | [diff] [blame] | 92 | and EarlGrey tests in addition to CQ try bots. |
Misha Efimov | 015b1d8 | 2017-10-18 18:49:59 | [diff] [blame] | 93 | """ |
| 94 | |
Menglu Huang | a7e41d21 | 2017-11-09 18:30:12 | [diff] [blame] | 95 | # TODO(crbug.com/712733): Remove ios-simulator-cronet once Cronet bots are |
| 96 | # deployed on CQ. |
| 97 | # TODO(crbug.com/782735): Remove ios-simulator-full-configs once EarlGrey |
| 98 | # bots are deployed on CQ. |
| 99 | try_bots = ['master.tryserver.chromium.mac:ios-simulator-cronet', |
| 100 | 'master.tryserver.chromium.mac:ios-simulator-full-configs'] |
Misha Efimov | 015b1d8 | 2017-10-18 18:49:59 | [diff] [blame] | 101 | |
| 102 | return output_api.EnsureCQIncludeTrybotsAreAdded( |
Louis Romero | 5fbe979 | 2017-11-17 17:06:15 | [diff] [blame] | 103 | cl, try_bots, 'Automatically added Cronet and EarlGrey trybots to ' |
Menglu Huang | a7e41d21 | 2017-11-09 18:30:12 | [diff] [blame] | 104 | 'run tests on CQ.') |