Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | # Copyright 2017 The Chromium Authors |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 4 | """Presubmit script for ios. |
| 5 | |
| 6 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 7 | for more details about the presubmit API built into depot_tools. |
| 8 | """ |
| 9 | |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 10 | import os |
Gauthier Ambard | 3803d8f | 2025-01-07 19:36:07 | [diff] [blame] | 11 | import xml.etree.ElementTree as ElementTree |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 12 | |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 13 | NULLABILITY_PATTERN = r'(nonnull|nullable|_Nullable|_Nonnull)' |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 14 | TODO_PATTERN = r'TO[D]O\(([^\)]*)\)' |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 15 | BUG_PATTERN = r'^(crbug\.com|b)/\d+$' |
| 16 | DEPRECATED_BUG_PATTERN = r'^b/\d+$' |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 17 | INCLUDE_PATTERN = r'^#include' |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 18 | PIPE_IN_COMMENT_PATTERN = r'//.*[^|]\|(?!\|)' |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 19 | IOS_PACKAGE_PATTERN = r'^ios' |
Louis Romero | e33c2da | 2023-01-19 14:18:53 | [diff] [blame] | 20 | BOXED_BOOL_PATTERN = r'@\((YES|NO)\)' |
Federica Germinario | 2ce51a8 | 2025-01-27 14:44:10 | [diff] [blame] | 21 | USER_DEFAULTS_PATTERN = r'\[NSUserDefaults standardUserDefaults]' |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 22 | |
Benjamin Williams | 715f023 | 2025-02-20 12:44:09 | [diff] [blame] | 23 | # Color management constants |
| 24 | COLOR_SHARED_DIR = 'ios/chrome/common/ui/colors/' |
| 25 | COLOR_FILE_PATTERN = '.colorset/Contents.json' |
| 26 | |
| 27 | |
| 28 | def FormatMessageWithFiles(message, errors): |
| 29 | """Helper to format warning/error messages with affected files.""" |
| 30 | if not errors: |
| 31 | return message |
| 32 | return '\n'.join([message + '\n\nAffected file(s):'] + errors) + '\n' |
| 33 | |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 34 | def IsSubListOf(needle, hay): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 35 | """Returns whether there is a slice of |hay| equal to |needle|.""" |
| 36 | for i, line in enumerate(hay): |
| 37 | if line == needle[0]: |
| 38 | if needle == hay[i:i + len(needle)]: |
| 39 | return True |
| 40 | return False |
| 41 | |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 42 | |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 43 | def _CheckNullabilityAnnotations(input_api, output_api): |
Daniel Bratell | 5cb4c1e2 | 2024-04-17 07:49:06 | [diff] [blame] | 44 | """ Checks whether there are nullability annotations in ios code. |
| 45 | |
| 46 | They are accepted in ios/web_view/public since it tries to mimic |
| 47 | the platform library but not anywhere else. |
| 48 | """ |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 49 | nullability_regex = input_api.re.compile(NULLABILITY_PATTERN) |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 50 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 51 | errors = [] |
| 52 | for f in input_api.AffectedFiles(): |
Daniel Bratell | 5cb4c1e2 | 2024-04-17 07:49:06 | [diff] [blame] | 53 | if f.LocalPath().startswith('ios/web_view/public/'): |
| 54 | # ios/web_view/public tries to mimic an existing API that |
| 55 | # might have nullability in it and that is acceptable. |
| 56 | continue |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 57 | for line_num, line in f.ChangedContents(): |
| 58 | if nullability_regex.search(line): |
| 59 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 60 | if not errors: |
| 61 | return [] |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 62 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 63 | plural_suffix = '' if len(errors) == 1 else 's' |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 64 | warning_message = ('Found Nullability annotation%(plural)s. ' |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 65 | 'Prefer DCHECKs in ios code to check for nullness:' % { |
| 66 | 'plural': plural_suffix |
| 67 | }) |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 68 | |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 69 | return [output_api.PresubmitPromptWarning(warning_message, items=errors)] |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def _CheckBugInToDo(input_api, output_api): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 73 | """ Checks whether TODOs in ios code are identified by a bug number.""" |
| 74 | errors = [] |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 75 | warnings = [] |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 76 | for f in input_api.AffectedFiles(): |
| 77 | for line_num, line in f.ChangedContents(): |
| 78 | if _HasToDoWithNoBug(input_api, line): |
| 79 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 80 | if _HasToDoWithDeprecatedBug(input_api, line): |
| 81 | warnings.append('%s:%s' % (f.LocalPath(), line_num)) |
| 82 | if not errors and not warnings: |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 83 | return [] |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 84 | |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 85 | output = [] |
| 86 | if errors: |
Gauthier Ambard | 304538e | 2025-01-07 14:07:05 | [diff] [blame] | 87 | singular_article = 'a ' if len(errors) == 1 else '' |
| 88 | plural_suffix = '' if len(errors) == 1 else 's' |
| 89 | error_message = '\n'.join([ |
| 90 | 'Found TO' |
| 91 | 'DO%(plural)s without %(a)sbug number%(plural)s (expected format ' |
| 92 | 'is \"TO' |
| 93 | 'DO(crbug.com/######)\"):' % { |
| 94 | 'plural': plural_suffix, |
| 95 | 'a' : singular_article |
| 96 | } |
| 97 | ] + errors) + '\n' |
| 98 | output.append(output_api.PresubmitError(error_message)) |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 99 | |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 100 | if warnings: |
Gauthier Ambard | 304538e | 2025-01-07 14:07:05 | [diff] [blame] | 101 | singular_article = 'a ' if len(warnings) == 1 else '' |
| 102 | plural_suffix = '' if len(warnings) == 1 else 's' |
| 103 | warning_message = '\n'.join([ |
| 104 | 'Found TO' |
| 105 | 'DO%(plural)s with %(a)sdeprecated bug link%(plural)s (found ' |
| 106 | '"b/#####\", expected format is \"crbug.com/######"):' % { |
| 107 | 'plural': plural_suffix, |
| 108 | 'a' : singular_article |
| 109 | } |
| 110 | ] + warnings) + '\n' |
| 111 | output.append(output_api.PresubmitPromptWarning(warning_message)) |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 112 | |
| 113 | return output |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 114 | |
| 115 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 116 | def _CheckHasNoIncludeDirectives(input_api, output_api): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 117 | """ Checks that #include preprocessor directives are not present.""" |
| 118 | errors = [] |
| 119 | for f in input_api.AffectedFiles(): |
| 120 | if not _IsInIosPackage(input_api, f.LocalPath()): |
| 121 | continue |
| 122 | _, ext = os.path.splitext(f.LocalPath()) |
| 123 | if ext != '.mm': |
| 124 | continue |
| 125 | for line_num, line in f.ChangedContents(): |
| 126 | if _HasIncludeDirective(input_api, line): |
| 127 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 128 | if not errors: |
| 129 | return [] |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 130 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 131 | singular_plural = 'it' if len(errors) == 1 else 'them' |
| 132 | plural_suffix = '' if len(errors) == 1 else 's' |
| 133 | error_message = '\n'.join([ |
| 134 | 'Found usage of `#include` preprocessor directive%(plural)s! Please, ' |
| 135 | 'replace %(singular_plural)s with `#import` preprocessor ' |
| 136 | 'directive%(plural)s instead. ' |
| 137 | 'Consider replacing all existing `#include` with `#import` (if any) in ' |
| 138 | 'this file for the code clean up. See ' |
| 139 | 'https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main' |
| 140 | '/styleguide/objective-c/objective-c.md' |
| 141 | '#import-and-include-in-the-directory for more details. ' |
| 142 | '\n\nAffected file%(plural)s:' % { |
| 143 | 'plural': plural_suffix, |
| 144 | 'singular_plural': singular_plural |
| 145 | } |
| 146 | ] + errors) + '\n' |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 147 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 148 | return [output_api.PresubmitError(error_message)] |
| 149 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 150 | |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 151 | def _CheckHasNoPipeInComment(input_api, output_api): |
| 152 | """ Checks that comments don't contain pipes.""" |
| 153 | pipe_regex = input_api.re.compile(PIPE_IN_COMMENT_PATTERN) |
| 154 | |
| 155 | errors = [] |
| 156 | for f in input_api.AffectedFiles(): |
| 157 | if not _IsInIosPackage(input_api, f.LocalPath()): |
| 158 | continue |
| 159 | for line_num, line in f.ChangedContents(): |
| 160 | if pipe_regex.search(line): |
| 161 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 162 | if not errors: |
| 163 | return [] |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 164 | warning_message = '\n'.join([ |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 165 | 'Please use backticks "`" instead of pipes "|" if you need to quote' |
| 166 | ' variable names and symbols in comments.\n' |
| 167 | 'Found potential uses of pipes in:' |
| 168 | ] + errors) + '\n' |
| 169 | |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 170 | return [output_api.PresubmitPromptWarning(warning_message)] |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 171 | |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 172 | def _CheckCanImproveTestUsingExpectNSEQ(input_api, output_api): |
| 173 | """ Checks that test files use EXPECT_NSEQ when possible.""" |
| 174 | errors = [] |
| 175 | # Substrings that should not be used together with EXPECT_TRUE or |
| 176 | # EXPECT_FALSE in tests. |
| 177 | wrong_patterns = ["isEqualToString:", "isEqualToData:", "isEqualToArray:"] |
| 178 | for f in input_api.AffectedFiles(): |
| 179 | if not '_unittest.' in f.LocalPath(): |
Gauthier Ambard | 304538e | 2025-01-07 14:07:05 | [diff] [blame] | 180 | continue |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 181 | for line_num, line in f.ChangedContents(): |
| 182 | if line.startswith(("EXPECT_TRUE", "EXPECT_FALSE")): |
Gauthier Ambard | 304538e | 2025-01-07 14:07:05 | [diff] [blame] | 183 | # Condition is in one line. |
| 184 | if any(x in line for x in wrong_patterns): |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 185 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
Gauthier Ambard | 304538e | 2025-01-07 14:07:05 | [diff] [blame] | 186 | # Condition is split on multiple lines. |
| 187 | elif not line.endswith(";"): |
| 188 | # Check this is not the last line. |
| 189 | if line_num < len(f.NewContents()): |
| 190 | next_line = f.NewContents()[line_num] |
| 191 | if any(x in next_line for x in wrong_patterns): |
| 192 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 193 | |
| 194 | if not errors: |
| 195 | return [] |
| 196 | |
| 197 | plural_suffix = '' if len(errors) == 1 else 's' |
| 198 | warning_message = '\n'.join([ |
| 199 | 'Found possible improvement in unittest. Prefer using' |
| 200 | ' EXPECT_NSEQ() or EXPECT_NSNE() when possible.' |
| 201 | '\n\nAffected file%(plural)s:' % { |
| 202 | 'plural': plural_suffix, |
| 203 | } |
| 204 | ] + errors) + '\n' |
| 205 | |
| 206 | return [output_api.PresubmitPromptWarning(warning_message)] |
| 207 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 208 | def _IsInIosPackage(input_api, path): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 209 | """ Returns True if path is within ios package""" |
| 210 | ios_package_regex = input_api.re.compile(IOS_PACKAGE_PATTERN) |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 211 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 212 | return ios_package_regex.search(path) |
| 213 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 214 | |
| 215 | def _HasIncludeDirective(input_api, line): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 216 | """ Returns True if #include is found in the line""" |
| 217 | include_regex = input_api.re.compile(INCLUDE_PATTERN) |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 218 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 219 | return include_regex.search(line) |
| 220 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 221 | |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 222 | def _HasToDoWithNoBug(input_api, line): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 223 | """ Returns True if TODO is not identified by a bug number.""" |
| 224 | todo_regex = input_api.re.compile(TODO_PATTERN) |
Tomasz Jurkiewicz | 55a0aa18 | 2023-03-01 18:53:46 | [diff] [blame] | 225 | bug_regex = input_api.re.compile(BUG_PATTERN) |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 226 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 227 | todo_match = todo_regex.search(line) |
| 228 | if not todo_match: |
| 229 | return False |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 230 | |
Tomasz Jurkiewicz | 55a0aa18 | 2023-03-01 18:53:46 | [diff] [blame] | 231 | return not bug_regex.match(todo_match.group(1)) |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 232 | |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 233 | def _HasToDoWithDeprecatedBug(input_api, line): |
| 234 | """ Returns True if TODO is identified by a deprecated bug number format.""" |
| 235 | todo_regex = input_api.re.compile(TODO_PATTERN) |
| 236 | deprecated_bug_regex = input_api.re.compile(DEPRECATED_BUG_PATTERN) |
| 237 | |
| 238 | todo_match = todo_regex.search(line) |
| 239 | if not todo_match: |
| 240 | return False |
| 241 | return deprecated_bug_regex.match(todo_match.group(1)) |
| 242 | |
Louis Romero | e33c2da | 2023-01-19 14:18:53 | [diff] [blame] | 243 | def _CheckHasNoBoxedBOOL(input_api, output_api): |
| 244 | """ Checks that there are no @(YES) or @(NO).""" |
| 245 | boxed_BOOL_regex = input_api.re.compile(BOXED_BOOL_PATTERN) |
| 246 | |
| 247 | errors = [] |
| 248 | for f in input_api.AffectedFiles(): |
| 249 | for line_num, line in f.ChangedContents(): |
| 250 | if boxed_BOOL_regex.search(line): |
| 251 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 252 | if not errors: |
| 253 | return [] |
| 254 | |
| 255 | plural_suffix = '' if len(errors) == 1 else 's' |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 256 | warning_message = ('Found boxed BOOL%(plural)s. ' |
Louis Romero | e33c2da | 2023-01-19 14:18:53 | [diff] [blame] | 257 | 'Prefer @YES or @NO in ios code:' % { |
| 258 | 'plural': plural_suffix |
| 259 | }) |
| 260 | |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 261 | return [output_api.PresubmitPromptWarning(warning_message, items=errors)] |
Nohemi Fernandez | a383337 | 2020-03-23 17:22:19 | [diff] [blame] | 262 | |
Justin Cohen | 5f8ac41 | 2024-10-15 15:09:19 | [diff] [blame] | 263 | def _CheckNoTearDownEGTest(input_api, output_api): |
| 264 | """ Checks that `- (void)tearDown {` is not present in an egtest.mm""" |
| 265 | errors = [] |
| 266 | for f in input_api.AffectedFiles(): |
| 267 | if not '_egtest.' in f.LocalPath(): |
Gauthier Ambard | 304538e | 2025-01-07 14:07:05 | [diff] [blame] | 268 | continue |
Justin Cohen | 5f8ac41 | 2024-10-15 15:09:19 | [diff] [blame] | 269 | for line_num, line in f.ChangedContents(): |
| 270 | if line.startswith("- (void)tearDown {"): |
| 271 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 272 | |
| 273 | if not errors: |
| 274 | return [] |
| 275 | warning_message = '\n'.join([ |
| 276 | 'To support hermetic EarlGrey test cases, tearDown has been renamed ' |
| 277 | 'to tearDownHelper, and will soon be removed. If tearDown is really ' |
| 278 | 'necessary for this test, please use addTeardownBlock' |
| 279 | ] + errors) + '\n' |
| 280 | |
| 281 | return [output_api.PresubmitError(warning_message)] |
| 282 | |
Gauthier Ambard | 3803d8f | 2025-01-07 19:36:07 | [diff] [blame] | 283 | |
| 284 | def _IsAlphabeticallySortedXML(file): |
| 285 | """Check that the `file` is alphabetically sorted""" |
| 286 | parser = ElementTree.XMLParser(target=ElementTree.TreeBuilder( |
| 287 | insert_comments=True)) |
dpapad | c885f14 | 2025-02-21 23:05:50 | [diff] [blame] | 288 | with open(file, 'r', encoding='utf8') as xml_file: |
Gauthier Ambard | 3803d8f | 2025-01-07 19:36:07 | [diff] [blame] | 289 | tree = ElementTree.parse(xml_file, parser) |
| 290 | root = tree.getroot() |
| 291 | |
| 292 | original_tree_string = ElementTree.tostring(root, encoding='utf8') |
| 293 | |
| 294 | messages_element = tree.findall('.//messages')[0] |
| 295 | messages = messages_element.findall('message') |
| 296 | messages.sort(key=lambda message: message.attrib["name"]) |
| 297 | for message in messages: |
| 298 | messages_element.remove(message) |
| 299 | for message in messages: |
| 300 | messages_element.append(message) |
| 301 | ordered_tree_string = ElementTree.tostring(root, encoding='utf8') |
| 302 | return ordered_tree_string == original_tree_string |
| 303 | |
| 304 | |
| 305 | def _CheckOrderedStringFile(input_api, output_api): |
| 306 | """ Checks that the string files are alphabetically ordered""" |
| 307 | errors = [] |
Gauthier Ambard | 7a243cc | 2025-03-18 13:45:30 | [diff] [blame] | 308 | for f in input_api.AffectedFiles(include_deletes=False): |
Gauthier Ambard | 3803d8f | 2025-01-07 19:36:07 | [diff] [blame] | 309 | if not f.LocalPath().endswith("_strings.grd"): |
| 310 | continue |
| 311 | if not _IsAlphabeticallySortedXML(f.AbsoluteLocalPath()): |
| 312 | errors.append(' python3 ios/tools/order_string_file.py ' + |
| 313 | f.LocalPath()) |
| 314 | |
| 315 | if not errors: |
| 316 | return [] |
| 317 | warning_message = '\n'.join( |
| 318 | ['Files not alphabetically sorted, try running:'] + errors) + '\n' |
| 319 | |
| 320 | return [output_api.PresubmitPromptWarning(warning_message)] |
| 321 | |
| 322 | |
Federica Germinario | 2ce51a8 | 2025-01-27 14:44:10 | [diff] [blame] | 323 | def _CheckNotUsingNSUserDefaults(input_api, output_api): |
| 324 | """ Checks the added code to limit new usage of NSUserDefaults """ |
| 325 | user_defaults_regex = input_api.re.compile(USER_DEFAULTS_PATTERN) |
| 326 | |
| 327 | errors = [] |
| 328 | for f in input_api.AffectedFiles(): |
| 329 | if (not f.LocalPath().endswith('.mm')): |
| 330 | continue |
| 331 | for line_num, line in f.ChangedContents(): |
| 332 | if user_defaults_regex.search(line): |
| 333 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 334 | |
| 335 | if not errors: |
| 336 | return [] |
| 337 | warning_message = '\n'.join([ |
| 338 | 'A new use of NSUserDefaults was added. If this is a newly added key ' |
| 339 | 'consider storing it to PrefService instead.' |
| 340 | ] + errors) + '\n' |
| 341 | |
| 342 | return [output_api.PresubmitPromptWarning(warning_message)] |
| 343 | |
| 344 | |
Benjamin Williams | 715f023 | 2025-02-20 12:44:09 | [diff] [blame] | 345 | def _CheckNewColorIntroduction(input_api, output_api): |
| 346 | """Checks for new or modified colorset files. |
| 347 | |
| 348 | Ensures colors are properly added to the shared directory. |
| 349 | """ |
| 350 | results = [] |
| 351 | |
| 352 | affected_files = [ |
| 353 | f for f in input_api.AffectedFiles() |
| 354 | if f.LocalPath().endswith(COLOR_FILE_PATTERN) |
| 355 | ] |
| 356 | |
| 357 | warnings = { |
| 358 | 'shared_added': [], |
| 359 | 'shared_modified': [], |
| 360 | 'other_modified': [] |
| 361 | } |
| 362 | errors = [] |
| 363 | |
| 364 | for affected_file in affected_files: |
| 365 | action = affected_file.Action() |
| 366 | local_path = affected_file.LocalPath() |
| 367 | file_path_error = '%s' % (affected_file.LocalPath()) |
| 368 | |
| 369 | if COLOR_SHARED_DIR in local_path: |
| 370 | if action == 'A': |
| 371 | warnings['shared_added'].append(file_path_error) |
| 372 | elif action == 'M': |
| 373 | warnings['shared_modified'].append(file_path_error) |
| 374 | else: |
| 375 | if action == 'A': |
| 376 | errors.append(file_path_error) |
| 377 | elif action == 'M': |
| 378 | warnings['other_modified'].append(file_path_error) |
| 379 | |
| 380 | output = [] |
| 381 | |
| 382 | if errors: |
| 383 | error_message = ('New color(s) must be added to the %s directory.' % |
| 384 | COLOR_SHARED_DIR) |
| 385 | output.append( |
| 386 | output_api.PresubmitError( |
| 387 | FormatMessageWithFiles(error_message, errors))) |
| 388 | |
| 389 | warning_message = ('Please ensure the color does not already exist in the ' |
| 390 | 'shared %s directory.' % COLOR_SHARED_DIR) |
| 391 | |
| 392 | if warnings['shared_added']: |
| 393 | shared_added_message = ('New color(s) added in %s. %s' % |
| 394 | (COLOR_SHARED_DIR, warning_message)) |
| 395 | output.append( |
| 396 | output_api.PresubmitPromptWarning( |
| 397 | FormatMessageWithFiles(shared_added_message, |
| 398 | warnings['shared_added']))) |
| 399 | |
| 400 | if warnings['shared_modified']: |
| 401 | shared_modified_message = ('Color(s) modified in %s. %s' % |
| 402 | (COLOR_SHARED_DIR, warning_message)) |
| 403 | output.append( |
| 404 | output_api.PresubmitPromptWarning( |
| 405 | FormatMessageWithFiles(shared_modified_message, |
| 406 | warnings['shared_modified']))) |
| 407 | |
| 408 | if warnings['other_modified']: |
| 409 | modified_message = ('Color(s) modified. %s' % warning_message) |
| 410 | output.append( |
| 411 | output_api.PresubmitPromptWarning( |
| 412 | FormatMessageWithFiles(modified_message, |
| 413 | warnings['other_modified']))) |
| 414 | |
| 415 | return output |
| 416 | |
dpapad | 5c82883 | 2025-02-25 19:47:12 | [diff] [blame] | 417 | def _CheckStyleESLint(input_api, output_api): |
| 418 | results = [] |
| 419 | |
| 420 | try: |
| 421 | import sys |
| 422 | old_sys_path = sys.path[:] |
| 423 | cwd = input_api.PresubmitLocalPath() |
| 424 | sys.path += [input_api.os_path.join(cwd, '..', 'tools')] |
| 425 | from web_dev_style import presubmit_support |
| 426 | results += presubmit_support.CheckStyleESLint(input_api, output_api) |
| 427 | finally: |
| 428 | sys.path = old_sys_path |
| 429 | |
| 430 | return results |
| 431 | |
Federica Germinario | 9420401 | 2025-04-16 18:20:59 | [diff] [blame] | 432 | def _CheckUIGraphicsBeginImageContextWithOptions(input_api, output_api): |
| 433 | """ Checks that UIGraphicsBeginImageContextWithOptions is not used""" |
| 434 | deprecated_regex = input_api.re.compile( |
| 435 | r'UIGraphicsBeginImageContextWithOptions\(') |
| 436 | |
| 437 | errors = [] |
| 438 | for f in input_api.AffectedFiles(): |
| 439 | if (not f.LocalPath().endswith('.mm')): |
| 440 | continue |
| 441 | for line_num, line in f.ChangedContents(): |
| 442 | if deprecated_regex.search(line): |
| 443 | errors.append('%s:%s' % (f.LocalPath(), line_num)) |
| 444 | |
| 445 | if not errors: |
| 446 | return [] |
| 447 | error_message = '\n'.join([ |
| 448 | 'UIGraphicsBeginImageContextWithOptions is deprecated, use ' |
| 449 | 'UIGraphicsImageRenderer instead.' |
| 450 | ] + errors) + '\n' |
| 451 | |
| 452 | return [output_api.PresubmitError(error_message)] |
| 453 | |
dpapad | c885f14 | 2025-02-21 23:05:50 | [diff] [blame] | 454 | def CheckChange(input_api, output_api): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 455 | results = [] |
| 456 | results.extend(_CheckBugInToDo(input_api, output_api)) |
| 457 | results.extend(_CheckNullabilityAnnotations(input_api, output_api)) |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 458 | results.extend(_CheckHasNoIncludeDirectives(input_api, output_api)) |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 459 | results.extend(_CheckHasNoPipeInComment(input_api, output_api)) |
Louis Romero | e33c2da | 2023-01-19 14:18:53 | [diff] [blame] | 460 | results.extend(_CheckHasNoBoxedBOOL(input_api, output_api)) |
Justin Cohen | 5f8ac41 | 2024-10-15 15:09:19 | [diff] [blame] | 461 | results.extend(_CheckNoTearDownEGTest(input_api, output_api)) |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 462 | results.extend(_CheckCanImproveTestUsingExpectNSEQ(input_api, output_api)) |
Gauthier Ambard | 3803d8f | 2025-01-07 19:36:07 | [diff] [blame] | 463 | results.extend(_CheckOrderedStringFile(input_api, output_api)) |
Federica Germinario | 2ce51a8 | 2025-01-27 14:44:10 | [diff] [blame] | 464 | results.extend(_CheckNotUsingNSUserDefaults(input_api, output_api)) |
Benjamin Williams | 715f023 | 2025-02-20 12:44:09 | [diff] [blame] | 465 | results.extend(_CheckNewColorIntroduction(input_api, output_api)) |
dpapad | 5c82883 | 2025-02-25 19:47:12 | [diff] [blame] | 466 | results.extend(_CheckStyleESLint(input_api, output_api)) |
Federica Germinario | 9420401 | 2025-04-16 18:20:59 | [diff] [blame] | 467 | results.extend( |
| 468 | _CheckUIGraphicsBeginImageContextWithOptions(input_api, output_api)) |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 469 | return results |
dpapad | c885f14 | 2025-02-21 23:05:50 | [diff] [blame] | 470 | |
| 471 | def CheckChangeOnUpload(input_api, output_api): |
| 472 | return CheckChange(input_api, output_api) |
| 473 | |
| 474 | def CheckChangeOnCommit(input_api, output_api): |
| 475 | return CheckChange(input_api, output_api) |