Chromium Code Reviews| 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 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 connection.close() | 403 connection.close() |
| 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( | |
| 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. | |
| 417 | |
| 418 It's mainly a wrapper for RunUnitTests. USe whitelist and blacklist to filter | |
| 419 tests accordingly. | |
| 420 """ | |
| 421 unit_tests = [] | |
| 422 test_path = input_api.os_path.abspath( | |
| 423 input_api.os_path.join(input_api.PresubmitLocalPath(), directory)) | |
| 424 | |
| 425 def check(filename, filters): | |
| 426 return any(True for i in filters if input_api.re.match(i, filename)) | |
| 427 | |
| 428 for filename in input_api.os_listdir(test_path): | |
| 429 fullpath = input_api.os_path.join(test_path, filename) | |
| 430 if not input_api.os_path.isfile(fullpath): | |
| 431 continue | |
| 432 if whitelist and not check(filename, whitelist): | |
| 433 continue | |
| 434 if blacklist and check(filename, blacklist): | |
| 435 continue | |
| 436 unit_tests.append(input_api.os_path.join(directory, filename)) | |
| 437 return RunUnitTests(input_api, output_api, unit_tests, verbose) | |
| 438 | |
| 439 | |
| 440 def RunUnitTests(input_api, output_api, unit_tests, verbose=False): | |
| 441 """Runs all unit tests in a directory. | |
| 442 | |
| 443 On Windows, if the unit tests ends with ".py", it will automatically prepend | |
| 444 sys.executable. | |
|
Dirk Pranke
2011/03/30 20:18:58
I read "it" here as Windows (or Python), but you m
| |
| 445 """ | |
| 446 # We don't want to hinder users from uploading incomplete patches. | |
| 447 if input_api.is_committing: | |
| 448 message_type = output_api.PresubmitError | |
| 449 else: | |
| 450 message_type = output_api.PresubmitPromptWarning | |
| 451 | |
| 452 if verbose: | |
| 453 pipe = None | |
| 454 else: | |
| 455 pipe = input_api.subprocess.PIPE | |
| 456 | |
| 457 results = [] | |
| 458 for unit_test in unit_tests: | |
| 459 cmd = [] | |
| 460 if input_api.platform == 'win32' and unit_test.endswith('.py'): | |
| 461 # Windows needs some help. | |
| 462 cmd = [input_api.python_executable] | |
| 463 cmd.append(unit_test) | |
| 464 if verbose: | |
| 465 print('Running %s' % unit_test) | |
| 466 try: | |
| 467 proc = input_api.subprocess.Popen( | |
| 468 cmd, cwd=input_api.PresubmitLocalPath(), stdout=pipe, stderr=pipe) | |
| 469 out = '\n'.join(filter(None, proc.communicate())) | |
| 470 if proc.returncode: | |
| 471 results.append(message_type( | |
| 472 '%s failed with return code %d\n%s' % ( | |
| 473 unit_test, proc.returncode, out))) | |
| 474 except (OSError, input_api.subprocess.CalledProcessError): | |
| 475 results.append(message_type('%s failed' % unit_test)) | |
| 476 return results | |
| 477 | |
| 478 | |
| 413 def RunPythonUnitTests(input_api, output_api, unit_tests): | 479 def RunPythonUnitTests(input_api, output_api, unit_tests): |
| 414 """Run the unit tests out of process, capture the output and use the result | 480 """Run the unit tests out of process, capture the output and use the result |
| 415 code to determine success. | 481 code to determine success. |
| 482 | |
| 483 DEPRECATED. | |
| 416 """ | 484 """ |
|
Dirk Pranke
2011/03/30 20:18:58
Can we make this just call RunUnitTests()? Do we n
M-A Ruel
2011/03/31 00:11:06
The code is really different. And I'll simply remo
| |
| 417 # We don't want to hinder users from uploading incomplete patches. | 485 # We don't want to hinder users from uploading incomplete patches. |
| 418 if input_api.is_committing: | 486 if input_api.is_committing: |
| 419 message_type = output_api.PresubmitError | 487 message_type = output_api.PresubmitError |
| 420 else: | 488 else: |
| 421 message_type = output_api.PresubmitNotifyResult | 489 message_type = output_api.PresubmitNotifyResult |
| 422 outputs = [] | 490 outputs = [] |
| 423 for unit_test in unit_tests: | 491 for unit_test in unit_tests: |
| 424 # Run the unit tests out of process. This is because some unit tests | 492 # Run the unit tests out of process. This is because some unit tests |
| 425 # stub out base libraries and don't clean up their mess. It's too easy to | 493 # stub out base libraries and don't clean up their mess. It's too easy to |
| 426 # get subtle bugs. | 494 # get subtle bugs. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 # We cannot use AffectedFiles here because we want to test every python | 538 # We cannot use AffectedFiles here because we want to test every python |
| 471 # file on each single python change. It's because a change in a python file | 539 # file on each single python change. It's because a change in a python file |
| 472 # can break another unmodified file. | 540 # can break another unmodified file. |
| 473 # Use code similar to InputApi.FilterSourceFile() | 541 # Use code similar to InputApi.FilterSourceFile() |
| 474 def Find(filepath, filters): | 542 def Find(filepath, filters): |
| 475 for item in filters: | 543 for item in filters: |
| 476 if input_api.re.match(item, filepath): | 544 if input_api.re.match(item, filepath): |
| 477 return True | 545 return True |
| 478 return False | 546 return False |
| 479 | 547 |
| 480 import os | |
| 481 files = [] | 548 files = [] |
| 482 path_len = len(input_api.PresubmitLocalPath()) | 549 path_len = len(input_api.PresubmitLocalPath()) |
| 483 for dirpath, dirnames, filenames in os.walk(input_api.PresubmitLocalPath()): | 550 for dirpath, dirnames, filenames in input_api.os_walk( |
| 551 input_api.PresubmitLocalPath()): | |
| 484 # Passes dirnames in black list to speed up search. | 552 # Passes dirnames in black list to speed up search. |
| 485 for item in dirnames[:]: | 553 for item in dirnames[:]: |
| 486 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] | 554 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
| 487 if Find(filepath, black_list): | 555 if Find(filepath, black_list): |
| 488 dirnames.remove(item) | 556 dirnames.remove(item) |
| 489 for item in filenames: | 557 for item in filenames: |
| 490 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] | 558 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
| 491 if Find(filepath, white_list) and not Find(filepath, black_list): | 559 if Find(filepath, white_list) and not Find(filepath, black_list): |
| 492 files.append(filepath) | 560 files.append(filepath) |
| 493 return files | 561 return files |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 830 input_api, output_api, source_file_filter=text_files)) | 898 input_api, output_api, source_file_filter=text_files)) |
| 831 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( | 899 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( |
| 832 input_api, output_api)) | 900 input_api, output_api)) |
| 833 results.extend(input_api.canned_checks.CheckLicense( | 901 results.extend(input_api.canned_checks.CheckLicense( |
| 834 input_api, output_api, license_header, source_file_filter=sources)) | 902 input_api, output_api, license_header, source_file_filter=sources)) |
| 835 results.extend(_CheckConstNSObject( | 903 results.extend(_CheckConstNSObject( |
| 836 input_api, output_api, source_file_filter=sources)) | 904 input_api, output_api, source_file_filter=sources)) |
| 837 results.extend(_CheckSingletonInHeaders( | 905 results.extend(_CheckSingletonInHeaders( |
| 838 input_api, output_api, source_file_filter=sources)) | 906 input_api, output_api, source_file_filter=sources)) |
| 839 return results | 907 return results |
| OLD | NEW |