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 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
879 input_api, output_api, source_file_filter=text_files)) | 878 input_api, output_api, source_file_filter=text_files)) |
880 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( | 879 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( |
881 input_api, output_api)) | 880 input_api, output_api)) |
882 results.extend(input_api.canned_checks.CheckLicense( | 881 results.extend(input_api.canned_checks.CheckLicense( |
883 input_api, output_api, license_header, source_file_filter=sources)) | 882 input_api, output_api, license_header, source_file_filter=sources)) |
884 results.extend(_CheckConstNSObject( | 883 results.extend(_CheckConstNSObject( |
885 input_api, output_api, source_file_filter=sources)) | 884 input_api, output_api, source_file_filter=sources)) |
886 results.extend(_CheckSingletonInHeaders( | 885 results.extend(_CheckSingletonInHeaders( |
887 input_api, output_api, source_file_filter=sources)) | 886 input_api, output_api, source_file_filter=sources)) |
888 return results | 887 return results |
OLD | NEW |