Disable a test file for cast_unittest
[chromium-blink-merge.git] / PRESUBMIT.py
blob632833ff58208975fe7be25d373e39828cb1a931
1 # Copyright (c) 2012 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.
5 """Top-level presubmit script for Chromium.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
9 """
12 import re
13 import subprocess
14 import sys
17 _EXCLUDED_PATHS = (
18 r"^breakpad[\\\/].*",
19 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
21 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
22 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
23 r"^skia[\\\/].*",
24 r"^v8[\\\/].*",
25 r".*MakeFile$",
26 r".+_autogen\.h$",
27 r".+[\\\/]pnacl_shim\.c$",
28 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
31 # Fragment of a regular expression that matches C++ and Objective-C++
32 # implementation files.
33 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
35 # Regular expression that matches code only used for test binaries
36 # (best effort).
37 _TEST_CODE_EXCLUDED_PATHS = (
38 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
39 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
40 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
41 _IMPLEMENTATION_EXTENSIONS,
42 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.*[/\\](test|tool(s)?)[/\\].*',
44 # content_shell is used for running layout tests.
45 r'content[/\\]shell[/\\].*',
46 # At request of folks maintaining this folder.
47 r'chrome[/\\]browser[/\\]automation[/\\].*',
50 _TEST_ONLY_WARNING = (
51 'You might be calling functions intended only for testing from\n'
52 'production code. It is OK to ignore this warning if you know what\n'
53 'you are doing, as the heuristics used to detect the situation are\n'
54 'not perfect. The commit queue will not block on this warning.\n'
55 'Email joi@chromium.org if you have questions.')
58 _INCLUDE_ORDER_WARNING = (
59 'Your #include order seems to be broken. Send mail to\n'
60 'marja@chromium.org if this is not the case.')
63 _BANNED_OBJC_FUNCTIONS = (
65 'addTrackingRect:',
67 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
68 'prohibited. Please use CrTrackingArea instead.',
69 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
71 False,
74 'NSTrackingArea',
76 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
77 'instead.',
78 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
80 False,
83 'convertPointFromBase:',
85 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
86 'Please use |convertPoint:(point) fromView:nil| instead.',
87 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
89 True,
92 'convertPointToBase:',
94 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
95 'Please use |convertPoint:(point) toView:nil| instead.',
96 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
98 True,
101 'convertRectFromBase:',
103 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
104 'Please use |convertRect:(point) fromView:nil| instead.',
105 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
107 True,
110 'convertRectToBase:',
112 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
113 'Please use |convertRect:(point) toView:nil| instead.',
114 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
116 True,
119 'convertSizeFromBase:',
121 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
122 'Please use |convertSize:(point) fromView:nil| instead.',
123 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
125 True,
128 'convertSizeToBase:',
130 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
131 'Please use |convertSize:(point) toView:nil| instead.',
132 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
134 True,
139 _BANNED_CPP_FUNCTIONS = (
140 # Make sure that gtest's FRIEND_TEST() macro is not used; the
141 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
142 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
144 'FRIEND_TEST(',
146 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
147 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
149 False,
153 'ScopedAllowIO',
155 'New code should not use ScopedAllowIO. Post a task to the blocking',
156 'pool or the FILE thread instead.',
158 True,
160 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
161 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
162 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
166 'SkRefPtr',
168 'The use of SkRefPtr is prohibited. ',
169 'Please use skia::RefPtr instead.'
171 True,
175 'SkAutoRef',
177 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
178 'Please use skia::RefPtr instead.'
180 True,
184 'SkAutoTUnref',
186 'The use of SkAutoTUnref is dangerous because it implicitly ',
187 'converts to a raw pointer. Please use skia::RefPtr instead.'
189 True,
193 'SkAutoUnref',
195 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
196 'because it implicitly converts to a raw pointer. ',
197 'Please use skia::RefPtr instead.'
199 True,
205 _VALID_OS_MACROS = (
206 # Please keep sorted.
207 'OS_ANDROID',
208 'OS_BSD',
209 'OS_CAT', # For testing.
210 'OS_CHROMEOS',
211 'OS_FREEBSD',
212 'OS_IOS',
213 'OS_LINUX',
214 'OS_MACOSX',
215 'OS_NACL',
216 'OS_OPENBSD',
217 'OS_POSIX',
218 'OS_SOLARIS',
219 'OS_WIN',
223 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
224 """Attempts to prevent use of functions intended only for testing in
225 non-testing code. For now this is just a best-effort implementation
226 that ignores header files and may have some false positives. A
227 better implementation would probably need a proper C++ parser.
229 # We only scan .cc files and the like, as the declaration of
230 # for-testing functions in header files are hard to distinguish from
231 # calls to such functions without a proper C++ parser.
232 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
234 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
235 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
236 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
237 exclusion_pattern = input_api.re.compile(
238 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
239 base_function_pattern, base_function_pattern))
241 def FilterFile(affected_file):
242 black_list = (_EXCLUDED_PATHS +
243 _TEST_CODE_EXCLUDED_PATHS +
244 input_api.DEFAULT_BLACK_LIST)
245 return input_api.FilterSourceFile(
246 affected_file,
247 white_list=(file_inclusion_pattern, ),
248 black_list=black_list)
250 problems = []
251 for f in input_api.AffectedSourceFiles(FilterFile):
252 local_path = f.LocalPath()
253 lines = input_api.ReadFile(f).splitlines()
254 line_number = 0
255 for line in lines:
256 if (inclusion_pattern.search(line) and
257 not comment_pattern.search(line) and
258 not exclusion_pattern.search(line)):
259 problems.append(
260 '%s:%d\n %s' % (local_path, line_number, line.strip()))
261 line_number += 1
263 if problems:
264 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
265 else:
266 return []
269 def _CheckNoIOStreamInHeaders(input_api, output_api):
270 """Checks to make sure no .h files include <iostream>."""
271 files = []
272 pattern = input_api.re.compile(r'^#include\s*<iostream>',
273 input_api.re.MULTILINE)
274 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
275 if not f.LocalPath().endswith('.h'):
276 continue
277 contents = input_api.ReadFile(f)
278 if pattern.search(contents):
279 files.append(f)
281 if len(files):
282 return [ output_api.PresubmitError(
283 'Do not #include <iostream> in header files, since it inserts static '
284 'initialization into every file including the header. Instead, '
285 '#include <ostream>. See http://crbug.com/94794',
286 files) ]
287 return []
290 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
291 """Checks to make sure no source files use UNIT_TEST"""
292 problems = []
293 for f in input_api.AffectedFiles():
294 if (not f.LocalPath().endswith(('.cc', '.mm'))):
295 continue
297 for line_num, line in f.ChangedContents():
298 if 'UNIT_TEST' in line:
299 problems.append(' %s:%d' % (f.LocalPath(), line_num))
301 if not problems:
302 return []
303 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
304 '\n'.join(problems))]
307 def _CheckNoNewWStrings(input_api, output_api):
308 """Checks to make sure we don't introduce use of wstrings."""
309 problems = []
310 for f in input_api.AffectedFiles():
311 if (not f.LocalPath().endswith(('.cc', '.h')) or
312 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
313 continue
315 allowWString = False
316 for line_num, line in f.ChangedContents():
317 if 'presubmit: allow wstring' in line:
318 allowWString = True
319 elif not allowWString and 'wstring' in line:
320 problems.append(' %s:%d' % (f.LocalPath(), line_num))
321 allowWString = False
322 else:
323 allowWString = False
325 if not problems:
326 return []
327 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
328 ' If you are calling a cross-platform API that accepts a wstring, '
329 'fix the API.\n' +
330 '\n'.join(problems))]
333 def _CheckNoDEPSGIT(input_api, output_api):
334 """Make sure .DEPS.git is never modified manually."""
335 if any(f.LocalPath().endswith('.DEPS.git') for f in
336 input_api.AffectedFiles()):
337 return [output_api.PresubmitError(
338 'Never commit changes to .DEPS.git. This file is maintained by an\n'
339 'automated system based on what\'s in DEPS and your changes will be\n'
340 'overwritten.\n'
341 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
342 'for more information')]
343 return []
346 def _CheckNoBannedFunctions(input_api, output_api):
347 """Make sure that banned functions are not used."""
348 warnings = []
349 errors = []
351 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
352 for f in input_api.AffectedFiles(file_filter=file_filter):
353 for line_num, line in f.ChangedContents():
354 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
355 if func_name in line:
356 problems = warnings;
357 if error:
358 problems = errors;
359 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
360 for message_line in message:
361 problems.append(' %s' % message_line)
363 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
364 for f in input_api.AffectedFiles(file_filter=file_filter):
365 for line_num, line in f.ChangedContents():
366 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
367 def IsBlacklisted(affected_file, blacklist):
368 local_path = affected_file.LocalPath()
369 for item in blacklist:
370 if input_api.re.match(item, local_path):
371 return True
372 return False
373 if IsBlacklisted(f, excluded_paths):
374 continue
375 if func_name in line:
376 problems = warnings;
377 if error:
378 problems = errors;
379 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
380 for message_line in message:
381 problems.append(' %s' % message_line)
383 result = []
384 if (warnings):
385 result.append(output_api.PresubmitPromptWarning(
386 'Banned functions were used.\n' + '\n'.join(warnings)))
387 if (errors):
388 result.append(output_api.PresubmitError(
389 'Banned functions were used.\n' + '\n'.join(errors)))
390 return result
393 def _CheckNoPragmaOnce(input_api, output_api):
394 """Make sure that banned functions are not used."""
395 files = []
396 pattern = input_api.re.compile(r'^#pragma\s+once',
397 input_api.re.MULTILINE)
398 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
399 if not f.LocalPath().endswith('.h'):
400 continue
401 contents = input_api.ReadFile(f)
402 if pattern.search(contents):
403 files.append(f)
405 if files:
406 return [output_api.PresubmitError(
407 'Do not use #pragma once in header files.\n'
408 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
409 files)]
410 return []
413 def _CheckNoTrinaryTrueFalse(input_api, output_api):
414 """Checks to make sure we don't introduce use of foo ? true : false."""
415 problems = []
416 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
417 for f in input_api.AffectedFiles():
418 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
419 continue
421 for line_num, line in f.ChangedContents():
422 if pattern.match(line):
423 problems.append(' %s:%d' % (f.LocalPath(), line_num))
425 if not problems:
426 return []
427 return [output_api.PresubmitPromptWarning(
428 'Please consider avoiding the "? true : false" pattern if possible.\n' +
429 '\n'.join(problems))]
432 def _CheckUnwantedDependencies(input_api, output_api):
433 """Runs checkdeps on #include statements added in this
434 change. Breaking - rules is an error, breaking ! rules is a
435 warning.
437 # We need to wait until we have an input_api object and use this
438 # roundabout construct to import checkdeps because this file is
439 # eval-ed and thus doesn't have __file__.
440 original_sys_path = sys.path
441 try:
442 sys.path = sys.path + [input_api.os_path.join(
443 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
444 import checkdeps
445 from cpp_checker import CppChecker
446 from rules import Rule
447 finally:
448 # Restore sys.path to what it was before.
449 sys.path = original_sys_path
451 added_includes = []
452 for f in input_api.AffectedFiles():
453 if not CppChecker.IsCppFile(f.LocalPath()):
454 continue
456 changed_lines = [line for line_num, line in f.ChangedContents()]
457 added_includes.append([f.LocalPath(), changed_lines])
459 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
461 error_descriptions = []
462 warning_descriptions = []
463 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
464 added_includes):
465 description_with_path = '%s\n %s' % (path, rule_description)
466 if rule_type == Rule.DISALLOW:
467 error_descriptions.append(description_with_path)
468 else:
469 warning_descriptions.append(description_with_path)
471 results = []
472 if error_descriptions:
473 results.append(output_api.PresubmitError(
474 'You added one or more #includes that violate checkdeps rules.',
475 error_descriptions))
476 if warning_descriptions:
477 results.append(output_api.PresubmitPromptOrNotify(
478 'You added one or more #includes of files that are temporarily\n'
479 'allowed but being removed. Can you avoid introducing the\n'
480 '#include? See relevant DEPS file(s) for details and contacts.',
481 warning_descriptions))
482 return results
485 def _CheckFilePermissions(input_api, output_api):
486 """Check that all files have their permissions properly set."""
487 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
488 input_api.change.RepositoryRoot()]
489 for f in input_api.AffectedFiles():
490 args += ['--file', f.LocalPath()]
491 errors = []
492 (errors, stderrdata) = subprocess.Popen(args).communicate()
494 results = []
495 if errors:
496 results.append(output_api.PresubmitError('checkperms.py failed.',
497 errors))
498 return results
501 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
502 """Makes sure we don't include ui/aura/window_property.h
503 in header files.
505 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
506 errors = []
507 for f in input_api.AffectedFiles():
508 if not f.LocalPath().endswith('.h'):
509 continue
510 for line_num, line in f.ChangedContents():
511 if pattern.match(line):
512 errors.append(' %s:%d' % (f.LocalPath(), line_num))
514 results = []
515 if errors:
516 results.append(output_api.PresubmitError(
517 'Header files should not include ui/aura/window_property.h', errors))
518 return results
521 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
522 """Checks that the lines in scope occur in the right order.
524 1. C system files in alphabetical order
525 2. C++ system files in alphabetical order
526 3. Project's .h files
529 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
530 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
531 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
533 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
535 state = C_SYSTEM_INCLUDES
537 previous_line = ''
538 previous_line_num = 0
539 problem_linenums = []
540 for line_num, line in scope:
541 if c_system_include_pattern.match(line):
542 if state != C_SYSTEM_INCLUDES:
543 problem_linenums.append((line_num, previous_line_num))
544 elif previous_line and previous_line > line:
545 problem_linenums.append((line_num, previous_line_num))
546 elif cpp_system_include_pattern.match(line):
547 if state == C_SYSTEM_INCLUDES:
548 state = CPP_SYSTEM_INCLUDES
549 elif state == CUSTOM_INCLUDES:
550 problem_linenums.append((line_num, previous_line_num))
551 elif previous_line and previous_line > line:
552 problem_linenums.append((line_num, previous_line_num))
553 elif custom_include_pattern.match(line):
554 if state != CUSTOM_INCLUDES:
555 state = CUSTOM_INCLUDES
556 elif previous_line and previous_line > line:
557 problem_linenums.append((line_num, previous_line_num))
558 else:
559 problem_linenums.append(line_num)
560 previous_line = line
561 previous_line_num = line_num
563 warnings = []
564 for (line_num, previous_line_num) in problem_linenums:
565 if line_num in changed_linenums or previous_line_num in changed_linenums:
566 warnings.append(' %s:%d' % (file_path, line_num))
567 return warnings
570 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
571 """Checks the #include order for the given file f."""
573 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
574 # Exclude the following includes from the check:
575 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
576 # specific order.
577 # 2) <atlbase.h>, "build/build_config.h"
578 excluded_include_pattern = input_api.re.compile(
579 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
580 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
581 if_pattern = input_api.re.compile(
582 r'\s*#\s*(if|elif|else|endif|define|undef).*')
583 # Some files need specialized order of includes; exclude such files from this
584 # check.
585 uncheckable_includes_pattern = input_api.re.compile(
586 r'\s*#include '
587 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
589 contents = f.NewContents()
590 warnings = []
591 line_num = 0
593 # Handle the special first include. If the first include file is
594 # some/path/file.h, the corresponding including file can be some/path/file.cc,
595 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
596 # etc. It's also possible that no special first include exists.
597 for line in contents:
598 line_num += 1
599 if system_include_pattern.match(line):
600 # No special first include -> process the line again along with normal
601 # includes.
602 line_num -= 1
603 break
604 match = custom_include_pattern.match(line)
605 if match:
606 match_dict = match.groupdict()
607 header_basename = input_api.os_path.basename(
608 match_dict['FILE']).replace('.h', '')
609 if header_basename not in input_api.os_path.basename(f.LocalPath()):
610 # No special first include -> process the line again along with normal
611 # includes.
612 line_num -= 1
613 break
615 # Split into scopes: Each region between #if and #endif is its own scope.
616 scopes = []
617 current_scope = []
618 for line in contents[line_num:]:
619 line_num += 1
620 if uncheckable_includes_pattern.match(line):
621 return []
622 if if_pattern.match(line):
623 scopes.append(current_scope)
624 current_scope = []
625 elif ((system_include_pattern.match(line) or
626 custom_include_pattern.match(line)) and
627 not excluded_include_pattern.match(line)):
628 current_scope.append((line_num, line))
629 scopes.append(current_scope)
631 for scope in scopes:
632 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
633 changed_linenums))
634 return warnings
637 def _CheckIncludeOrder(input_api, output_api):
638 """Checks that the #include order is correct.
640 1. The corresponding header for source files.
641 2. C system files in alphabetical order
642 3. C++ system files in alphabetical order
643 4. Project's .h files in alphabetical order
645 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
646 these rules separately.
649 warnings = []
650 for f in input_api.AffectedFiles():
651 if f.LocalPath().endswith(('.cc', '.h')):
652 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
653 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
655 results = []
656 if warnings:
657 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
658 warnings))
659 return results
662 def _CheckForVersionControlConflictsInFile(input_api, f):
663 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
664 errors = []
665 for line_num, line in f.ChangedContents():
666 if pattern.match(line):
667 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
668 return errors
671 def _CheckForVersionControlConflicts(input_api, output_api):
672 """Usually this is not intentional and will cause a compile failure."""
673 errors = []
674 for f in input_api.AffectedFiles():
675 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
677 results = []
678 if errors:
679 results.append(output_api.PresubmitError(
680 'Version control conflict markers found, please resolve.', errors))
681 return results
684 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
685 def FilterFile(affected_file):
686 """Filter function for use with input_api.AffectedSourceFiles,
687 below. This filters out everything except non-test files from
688 top-level directories that generally speaking should not hard-code
689 service URLs (e.g. src/android_webview/, src/content/ and others).
691 return input_api.FilterSourceFile(
692 affected_file,
693 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
694 black_list=(_EXCLUDED_PATHS +
695 _TEST_CODE_EXCLUDED_PATHS +
696 input_api.DEFAULT_BLACK_LIST))
698 base_pattern = '"[^"]*google\.com[^"]*"'
699 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
700 pattern = input_api.re.compile(base_pattern)
701 problems = [] # items are (filename, line_number, line)
702 for f in input_api.AffectedSourceFiles(FilterFile):
703 for line_num, line in f.ChangedContents():
704 if not comment_pattern.search(line) and pattern.search(line):
705 problems.append((f.LocalPath(), line_num, line))
707 if problems:
708 return [output_api.PresubmitPromptOrNotify(
709 'Most layers below src/chrome/ should not hardcode service URLs.\n'
710 'Are you sure this is correct? (Contact: joi@chromium.org)',
711 [' %s:%d: %s' % (
712 problem[0], problem[1], problem[2]) for problem in problems])]
713 else:
714 return []
717 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
718 """Makes sure there are no abbreviations in the name of PNG files.
720 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
721 errors = []
722 for f in input_api.AffectedFiles(include_deletes=False):
723 if pattern.match(f.LocalPath()):
724 errors.append(' %s' % f.LocalPath())
726 results = []
727 if errors:
728 results.append(output_api.PresubmitError(
729 'The name of PNG files should not have abbreviations. \n'
730 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
731 'Contact oshima@chromium.org if you have questions.', errors))
732 return results
735 def _DepsFilesToCheck(re, changed_lines):
736 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
737 a set of DEPS entries that we should look up."""
738 results = set()
740 # This pattern grabs the path without basename in the first
741 # parentheses, and the basename (if present) in the second. It
742 # relies on the simple heuristic that if there is a basename it will
743 # be a header file ending in ".h".
744 pattern = re.compile(
745 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
746 for changed_line in changed_lines:
747 m = pattern.match(changed_line)
748 if m:
749 path = m.group(1)
750 if not (path.startswith('grit/') or path == 'grit'):
751 results.add('%s/DEPS' % m.group(1))
752 return results
755 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
756 """When a dependency prefixed with + is added to a DEPS file, we
757 want to make sure that the change is reviewed by an OWNER of the
758 target file or directory, to avoid layering violations from being
759 introduced. This check verifies that this happens.
761 changed_lines = set()
762 for f in input_api.AffectedFiles():
763 filename = input_api.os_path.basename(f.LocalPath())
764 if filename == 'DEPS':
765 changed_lines |= set(line.strip()
766 for line_num, line
767 in f.ChangedContents())
768 if not changed_lines:
769 return []
771 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
772 if not virtual_depended_on_files:
773 return []
775 if input_api.is_committing:
776 if input_api.tbr:
777 return [output_api.PresubmitNotifyResult(
778 '--tbr was specified, skipping OWNERS check for DEPS additions')]
779 if not input_api.change.issue:
780 return [output_api.PresubmitError(
781 "DEPS approval by OWNERS check failed: this change has "
782 "no Rietveld issue number, so we can't check it for approvals.")]
783 output = output_api.PresubmitError
784 else:
785 output = output_api.PresubmitNotifyResult
787 owners_db = input_api.owners_db
788 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
789 input_api,
790 owners_db.email_regexp,
791 approval_needed=input_api.is_committing)
793 owner_email = owner_email or input_api.change.author_email
795 reviewers_plus_owner = set(reviewers)
796 if owner_email:
797 reviewers_plus_owner.add(owner_email)
798 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
799 reviewers_plus_owner)
800 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
801 for path in missing_files]
803 if unapproved_dependencies:
804 output_list = [
805 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
806 '\n '.join(sorted(unapproved_dependencies)))]
807 if not input_api.is_committing:
808 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
809 output_list.append(output(
810 'Suggested missing target path OWNERS:\n %s' %
811 '\n '.join(suggested_owners or [])))
812 return output_list
814 return []
817 def _CommonChecks(input_api, output_api):
818 """Checks common to both upload and commit."""
819 results = []
820 results.extend(input_api.canned_checks.PanProjectChecks(
821 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
822 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
823 results.extend(
824 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
825 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
826 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
827 results.extend(_CheckNoNewWStrings(input_api, output_api))
828 results.extend(_CheckNoDEPSGIT(input_api, output_api))
829 results.extend(_CheckNoBannedFunctions(input_api, output_api))
830 results.extend(_CheckNoPragmaOnce(input_api, output_api))
831 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
832 results.extend(_CheckUnwantedDependencies(input_api, output_api))
833 results.extend(_CheckFilePermissions(input_api, output_api))
834 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
835 results.extend(_CheckIncludeOrder(input_api, output_api))
836 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
837 results.extend(_CheckPatchFiles(input_api, output_api))
838 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
839 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
840 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
841 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
842 results.extend(
843 input_api.canned_checks.CheckChangeHasNoTabs(
844 input_api,
845 output_api,
846 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
848 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
849 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
850 input_api, output_api,
851 input_api.PresubmitLocalPath(),
852 whitelist=[r'^PRESUBMIT_test\.py$']))
853 return results
856 def _CheckSubversionConfig(input_api, output_api):
857 """Verifies the subversion config file is correctly setup.
859 Checks that autoprops are enabled, returns an error otherwise.
861 join = input_api.os_path.join
862 if input_api.platform == 'win32':
863 appdata = input_api.environ.get('APPDATA', '')
864 if not appdata:
865 return [output_api.PresubmitError('%APPDATA% is not configured.')]
866 path = join(appdata, 'Subversion', 'config')
867 else:
868 home = input_api.environ.get('HOME', '')
869 if not home:
870 return [output_api.PresubmitError('$HOME is not configured.')]
871 path = join(home, '.subversion', 'config')
873 error_msg = (
874 'Please look at http://dev.chromium.org/developers/coding-style to\n'
875 'configure your subversion configuration file. This enables automatic\n'
876 'properties to simplify the project maintenance.\n'
877 'Pro-tip: just download and install\n'
878 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
880 try:
881 lines = open(path, 'r').read().splitlines()
882 # Make sure auto-props is enabled and check for 2 Chromium standard
883 # auto-prop.
884 if (not '*.cc = svn:eol-style=LF' in lines or
885 not '*.pdf = svn:mime-type=application/pdf' in lines or
886 not 'enable-auto-props = yes' in lines):
887 return [
888 output_api.PresubmitNotifyResult(
889 'It looks like you have not configured your subversion config '
890 'file or it is not up-to-date.\n' + error_msg)
892 except (OSError, IOError):
893 return [
894 output_api.PresubmitNotifyResult(
895 'Can\'t find your subversion config file.\n' + error_msg)
897 return []
900 def _CheckAuthorizedAuthor(input_api, output_api):
901 """For non-googler/chromites committers, verify the author's email address is
902 in AUTHORS.
904 # TODO(maruel): Add it to input_api?
905 import fnmatch
907 author = input_api.change.author_email
908 if not author:
909 input_api.logging.info('No author, skipping AUTHOR check')
910 return []
911 authors_path = input_api.os_path.join(
912 input_api.PresubmitLocalPath(), 'AUTHORS')
913 valid_authors = (
914 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
915 for line in open(authors_path))
916 valid_authors = [item.group(1).lower() for item in valid_authors if item]
917 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
918 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
919 return [output_api.PresubmitPromptWarning(
920 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
921 '\n'
922 'http://www.chromium.org/developers/contributing-code and read the '
923 '"Legal" section\n'
924 'If you are a chromite, verify the contributor signed the CLA.') %
925 author)]
926 return []
929 def _CheckPatchFiles(input_api, output_api):
930 problems = [f.LocalPath() for f in input_api.AffectedFiles()
931 if f.LocalPath().endswith(('.orig', '.rej'))]
932 if problems:
933 return [output_api.PresubmitError(
934 "Don't commit .rej and .orig files.", problems)]
935 else:
936 return []
939 def _DidYouMeanOSMacro(bad_macro):
940 try:
941 return {'A': 'OS_ANDROID',
942 'B': 'OS_BSD',
943 'C': 'OS_CHROMEOS',
944 'F': 'OS_FREEBSD',
945 'L': 'OS_LINUX',
946 'M': 'OS_MACOSX',
947 'N': 'OS_NACL',
948 'O': 'OS_OPENBSD',
949 'P': 'OS_POSIX',
950 'S': 'OS_SOLARIS',
951 'W': 'OS_WIN'}[bad_macro[3].upper()]
952 except KeyError:
953 return ''
956 def _CheckForInvalidOSMacrosInFile(input_api, f):
957 """Check for sensible looking, totally invalid OS macros."""
958 preprocessor_statement = input_api.re.compile(r'^\s*#')
959 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
960 results = []
961 for lnum, line in f.ChangedContents():
962 if preprocessor_statement.search(line):
963 for match in os_macro.finditer(line):
964 if not match.group(1) in _VALID_OS_MACROS:
965 good = _DidYouMeanOSMacro(match.group(1))
966 did_you_mean = ' (did you mean %s?)' % good if good else ''
967 results.append(' %s:%d %s%s' % (f.LocalPath(),
968 lnum,
969 match.group(1),
970 did_you_mean))
971 return results
974 def _CheckForInvalidOSMacros(input_api, output_api):
975 """Check all affected files for invalid OS macros."""
976 bad_macros = []
977 for f in input_api.AffectedFiles():
978 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
979 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
981 if not bad_macros:
982 return []
984 return [output_api.PresubmitError(
985 'Possibly invalid OS macro[s] found. Please fix your code\n'
986 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
989 def CheckChangeOnUpload(input_api, output_api):
990 results = []
991 results.extend(_CommonChecks(input_api, output_api))
992 return results
995 def CheckChangeOnCommit(input_api, output_api):
996 results = []
997 results.extend(_CommonChecks(input_api, output_api))
998 # TODO(thestig) temporarily disabled, doesn't work in third_party/
999 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1000 # input_api, output_api, sources))
1001 # Make sure the tree is 'open'.
1002 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1003 input_api,
1004 output_api,
1005 json_url='http://chromium-status.appspot.com/current?format=json'))
1006 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
1007 output_api, 'http://codereview.chromium.org',
1008 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1009 'tryserver@chromium.org'))
1011 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1012 input_api, output_api))
1013 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1014 input_api, output_api))
1015 results.extend(_CheckSubversionConfig(input_api, output_api))
1016 return results
1019 def GetPreferredTrySlaves(project, change):
1020 files = change.LocalPaths()
1022 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
1023 return []
1025 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
1026 return ['mac_rel', 'mac:compile']
1027 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1028 return ['win_rel', 'win7_aura', 'win:compile']
1029 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
1030 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1031 if all(re.search('^native_client_sdk', f) for f in files):
1032 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1033 if all(re.search('[/_]ios[/_.]', f) for f in files):
1034 return ['ios_rel_device', 'ios_dbg_simulator']
1036 trybots = [
1037 'android_clang_dbg',
1038 'android_dbg',
1039 'ios_dbg_simulator',
1040 'ios_rel_device',
1041 'linux_asan',
1042 'linux_aura',
1043 'linux_chromeos',
1044 'linux_clang:compile',
1045 'linux_rel',
1046 'mac_rel',
1047 'mac:compile',
1048 'win7_aura',
1049 'win_rel',
1050 'win:compile',
1051 'win_x64_rel:compile',
1054 # Match things like path/aura/file.cc and path/file_aura.cc.
1055 # Same for chromeos.
1056 if any(re.search('[/_](aura|chromeos)', f) for f in files):
1057 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1059 # If there are gyp changes to base, build, or chromeos, run a full cros build
1060 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1061 # files have a much higher chance of breaking the cros build, which is
1062 # differnt from the linux_chromeos build that most chrome developers test
1063 # with.
1064 if any(re.search('^(base|build|chromeos).*\.gypi?$', f) for f in files):
1065 trybots += ['cros_x86']
1067 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1068 # unless they're .gyp(i) files as changes to those files can break the gyp
1069 # step on that bot.
1070 if (not all(re.search('^chrome', f) for f in files) or
1071 any(re.search('\.gypi?$', f) for f in files)):
1072 trybots += ['android_aosp']
1074 return trybots