OLD | NEW |
1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generic presubmit checks that can be reused by other presubmit checks.""" | 5 """Generic presubmit checks that can be reused by other presubmit checks.""" |
6 | 6 |
7 | 7 |
8 ### Description checks | 8 ### Description checks |
9 | 9 |
10 def CheckChangeHasTestField(input_api, output_api): | 10 def CheckChangeHasTestField(input_api, output_api): |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 if input_api.re.match(closed, status): | 404 if input_api.re.match(closed, status): |
405 long_text = status + '\n' + url | 405 long_text = status + '\n' + url |
406 return [output_api.PresubmitError('The tree is closed.', | 406 return [output_api.PresubmitError('The tree is closed.', |
407 long_text=long_text)] | 407 long_text=long_text)] |
408 except IOError: | 408 except IOError: |
409 pass | 409 pass |
410 return [] | 410 return [] |
411 | 411 |
412 | 412 |
413 def RunUnitTestsInDirectory( | 413 def RunUnitTestsInDirectory( |
414 input_api, output_api, directory, whitelist=None, blacklist=None, | 414 input_api, output_api, directory, whitelist=None, blacklist=None): |
415 verbose=False): | |
416 """Lists all files in a directory and runs them. Doesn't recurse. | 415 """Lists all files in a directory and runs them. Doesn't recurse. |
417 | 416 |
418 It's mainly a wrapper for RunUnitTests. USe whitelist and blacklist to filter | 417 It's mainly a wrapper for RunUnitTests. USe whitelist and blacklist to filter |
419 tests accordingly. | 418 tests accordingly. |
420 """ | 419 """ |
421 unit_tests = [] | 420 unit_tests = [] |
422 test_path = input_api.os_path.abspath( | 421 test_path = input_api.os_path.abspath( |
423 input_api.os_path.join(input_api.PresubmitLocalPath(), directory)) | 422 input_api.os_path.join(input_api.PresubmitLocalPath(), directory)) |
424 | 423 |
425 def check(filename, filters): | 424 def check(filename, filters): |
426 return any(True for i in filters if input_api.re.match(i, filename)) | 425 return any(True for i in filters if input_api.re.match(i, filename)) |
427 | 426 |
428 for filename in input_api.os_listdir(test_path): | 427 for filename in input_api.os_listdir(test_path): |
429 fullpath = input_api.os_path.join(test_path, filename) | 428 fullpath = input_api.os_path.join(test_path, filename) |
430 if not input_api.os_path.isfile(fullpath): | 429 if not input_api.os_path.isfile(fullpath): |
431 continue | 430 continue |
432 if whitelist and not check(filename, whitelist): | 431 if whitelist and not check(filename, whitelist): |
433 continue | 432 continue |
434 if blacklist and check(filename, blacklist): | 433 if blacklist and check(filename, blacklist): |
435 continue | 434 continue |
436 unit_tests.append(input_api.os_path.join(directory, filename)) | 435 unit_tests.append(input_api.os_path.join(directory, filename)) |
437 return RunUnitTests(input_api, output_api, unit_tests, verbose) | 436 return RunUnitTests(input_api, output_api, unit_tests) |
438 | 437 |
439 | 438 |
440 def RunUnitTests(input_api, output_api, unit_tests, verbose=False): | 439 def RunUnitTests(input_api, output_api, unit_tests): |
441 """Runs all unit tests in a directory. | 440 """Runs all unit tests in a directory. |
442 | 441 |
443 On Windows, sys.executable is used for unit tests ending with ".py". | 442 On Windows, sys.executable is used for unit tests ending with ".py". |
444 """ | 443 """ |
445 # We don't want to hinder users from uploading incomplete patches. | 444 # We don't want to hinder users from uploading incomplete patches. |
446 if input_api.is_committing: | 445 if input_api.is_committing: |
447 message_type = output_api.PresubmitError | 446 message_type = output_api.PresubmitError |
448 else: | 447 else: |
449 message_type = output_api.PresubmitPromptWarning | 448 message_type = output_api.PresubmitPromptWarning |
450 | 449 |
451 results = [] | 450 results = [] |
452 for unit_test in unit_tests: | 451 for unit_test in unit_tests: |
453 cmd = [] | 452 cmd = [] |
454 if input_api.platform == 'win32' and unit_test.endswith('.py'): | 453 if input_api.platform == 'win32' and unit_test.endswith('.py'): |
455 # Windows needs some help. | 454 # Windows needs some help. |
456 cmd = [input_api.python_executable] | 455 cmd = [input_api.python_executable] |
457 cmd.append(unit_test) | 456 cmd.append(unit_test) |
458 if verbose: | 457 if input_api.verbose: |
459 print('Running %s' % unit_test) | 458 print('Running %s' % unit_test) |
460 try: | 459 try: |
461 if verbose: | 460 if input_api.verbose: |
462 input_api.subprocess.check_call(cmd, cwd=input_api.PresubmitLocalPath()) | 461 input_api.subprocess.check_call(cmd, cwd=input_api.PresubmitLocalPath()) |
463 else: | 462 else: |
464 input_api.subprocess.check_output( | 463 input_api.subprocess.check_output( |
465 cmd, cwd=input_api.PresubmitLocalPath()) | 464 cmd, cwd=input_api.PresubmitLocalPath()) |
466 except (OSError, input_api.subprocess.CalledProcessError), e: | 465 except (OSError, input_api.subprocess.CalledProcessError), e: |
467 results.append(message_type('%s failed!\n%s' % (unit_test, e))) | 466 results.append(message_type('%s failed!\n%s' % (unit_test, e))) |
468 return results | 467 return results |
469 | 468 |
470 | 469 |
471 def RunPythonUnitTests(input_api, output_api, unit_tests): | 470 def RunPythonUnitTests(input_api, output_api, unit_tests): |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 if Find(filepath, white_list) and not Find(filepath, black_list): | 536 if Find(filepath, white_list) and not Find(filepath, black_list): |
538 files.append(filepath) | 537 files.append(filepath) |
539 return files | 538 return files |
540 | 539 |
541 | 540 |
542 def RunPylint(input_api, output_api, white_list=None, black_list=None): | 541 def RunPylint(input_api, output_api, white_list=None, black_list=None): |
543 """Run pylint on python files. | 542 """Run pylint on python files. |
544 | 543 |
545 The default white_list enforces looking only a *.py files. | 544 The default white_list enforces looking only a *.py files. |
546 """ | 545 """ |
547 verbose = False | |
548 white_list = white_list or ['.*\.py$'] | 546 white_list = white_list or ['.*\.py$'] |
549 black_list = black_list or input_api.DEFAULT_BLACK_LIST | 547 black_list = black_list or input_api.DEFAULT_BLACK_LIST |
550 if input_api.is_committing: | 548 if input_api.is_committing: |
551 error_type = output_api.PresubmitError | 549 error_type = output_api.PresubmitError |
552 else: | 550 else: |
553 error_type = output_api.PresubmitPromptWarning | 551 error_type = output_api.PresubmitPromptWarning |
554 | 552 |
555 # Only trigger if there is at least one python file affected. | 553 # Only trigger if there is at least one python file affected. |
556 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) | 554 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) |
557 if not input_api.AffectedSourceFiles(src_filter): | 555 if not input_api.AffectedSourceFiles(src_filter): |
(...skipping 25 matching lines...) Expand all Loading... |
583 | 581 |
584 def run_lint(files): | 582 def run_lint(files): |
585 try: | 583 try: |
586 lint.Run(files) | 584 lint.Run(files) |
587 assert False | 585 assert False |
588 except SystemExit, e: | 586 except SystemExit, e: |
589 # pylint has the bad habit of calling sys.exit(), trap it here. | 587 # pylint has the bad habit of calling sys.exit(), trap it here. |
590 return e.code | 588 return e.code |
591 | 589 |
592 result = None | 590 result = None |
593 if not verbose: | 591 if not input_api.verbose: |
594 result = run_lint(sorted(files)) | 592 result = run_lint(sorted(files)) |
595 else: | 593 else: |
596 for filename in sorted(files): | 594 for filename in sorted(files): |
597 print('Running pylint on %s' % filename) | 595 print('Running pylint on %s' % filename) |
598 out = run_lint([filename]) | 596 out = run_lint([filename]) |
599 if out: | 597 if out: |
600 result = out | 598 result = out |
601 if result: | 599 if result: |
602 return [error_type('Fix pylint errors first.')] | 600 return [error_type('Fix pylint errors first.')] |
603 return [] | 601 return [] |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
895 input_api, output_api, source_file_filter=text_files)) | 893 input_api, output_api, source_file_filter=text_files)) |
896 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( | 894 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( |
897 input_api, output_api)) | 895 input_api, output_api)) |
898 results.extend(input_api.canned_checks.CheckLicense( | 896 results.extend(input_api.canned_checks.CheckLicense( |
899 input_api, output_api, license_header, source_file_filter=sources)) | 897 input_api, output_api, license_header, source_file_filter=sources)) |
900 results.extend(_CheckConstNSObject( | 898 results.extend(_CheckConstNSObject( |
901 input_api, output_api, source_file_filter=sources)) | 899 input_api, output_api, source_file_filter=sources)) |
902 results.extend(_CheckSingletonInHeaders( | 900 results.extend(_CheckSingletonInHeaders( |
903 input_api, output_api, source_file_filter=sources)) | 901 input_api, output_api, source_file_filter=sources)) |
904 return results | 902 return results |
OLD | NEW |