blob: 3bbc311d8867d92c248b72d87e3b4f094672a023 [file] [log] [blame]
Sylvain Defresnefcda19f2017-06-27 10:14:011# 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
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into depot_tools.
9"""
10
[email protected]0066f732017-12-28 10:33:0811import os
12
Sylvain Defresnefcda19f2017-06-27 10:14:0113TODO_PATTERN = r'TO[D]O\(([^\)]*)\)'
14CRBUG_PATTERN = r'crbug\.com/\d+$'
Gauthier Ambard7fc07bf2018-01-02 09:48:0915ARC_COMPILE_GUARD = [
16 '#if !defined(__has_feature) || !__has_feature(objc_arc)',
17 '#error "This file requires ARC support."',
18 '#endif',
19]
20
21def 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]0066f732017-12-28 10:33:0828
29def _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 Ambard7fc07bf2018-01-02 09:48:0940 if not IsSubListOf(ARC_COMPILE_GUARD, f.NewContents()):
41 files_without_headers.append(f.LocalPath())
[email protected]0066f732017-12-28 10:33:0842
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 Ambard7fc07bf2018-01-02 09:48:0950 ':' % {'plural': plural_suffix}
51 ] + ARC_COMPILE_GUARD + files_without_headers) + '\n'
[email protected]0066f732017-12-28 10:33:0852
53 return [output_api.PresubmitError(error_message)]
54
Sylvain Defresnefcda19f2017-06-27 10:14:0155
56def _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
82def CheckChangeOnUpload(input_api, output_api):
83 results = []
84 results.extend(_CheckBugInToDo(input_api, output_api))
[email protected]0066f732017-12-28 10:33:0885 results.extend(_CheckARCCompilationGuard(input_api, output_api))
Sylvain Defresnefcda19f2017-06-27 10:14:0186 return results
Misha Efimov015b1d82017-10-18 18:49:5987
88def 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 Huanga7e41d212017-11-09 18:30:1292 and EarlGrey tests in addition to CQ try bots.
Misha Efimov015b1d82017-10-18 18:49:5993 """
94
Menglu Huanga7e41d212017-11-09 18:30:1295 # 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 Efimov015b1d82017-10-18 18:49:59101
102 return output_api.EnsureCQIncludeTrybotsAreAdded(
Louis Romero5fbe9792017-11-17 17:06:15103 cl, try_bots, 'Automatically added Cronet and EarlGrey trybots to '
Menglu Huanga7e41d212017-11-09 18:30:12104 'run tests on CQ.')