Chromium Code Reviews| Index: presubmit_canned_checks.py |
| diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py |
| index 6fb954f1f1463ed2f366948d6e12f0038e1672dc..6184bc062b7f7feabdb75449c1cdda91c3972237 100644 |
| --- a/presubmit_canned_checks.py |
| +++ b/presubmit_canned_checks.py |
| @@ -410,9 +410,77 @@ def CheckTreeIsOpen(input_api, output_api, |
| return [] |
| +def RunUnitTestsInDirectory( |
| + input_api, output_api, directory, whitelist=None, blacklist=None, |
| + verbose=False): |
| + """Lists all files in a directory and runs them. Doesn't recurse. |
| + |
| + It's mainly a wrapper for RunUnitTests. USe whitelist and blacklist to filter |
| + tests accordingly. |
| + """ |
| + unit_tests = [] |
| + test_path = input_api.os_path.abspath( |
| + input_api.os_path.join(input_api.PresubmitLocalPath(), directory)) |
| + |
| + def check(filename, filters): |
| + return any(True for i in filters if input_api.re.match(i, filename)) |
| + |
| + for filename in input_api.os_listdir(test_path): |
| + fullpath = input_api.os_path.join(test_path, filename) |
| + if not input_api.os_path.isfile(fullpath): |
| + continue |
| + if whitelist and not check(filename, whitelist): |
| + continue |
| + if blacklist and check(filename, blacklist): |
| + continue |
| + unit_tests.append(input_api.os_path.join(directory, filename)) |
| + return RunUnitTests(input_api, output_api, unit_tests, verbose) |
| + |
| + |
| +def RunUnitTests(input_api, output_api, unit_tests, verbose=False): |
| + """Runs all unit tests in a directory. |
| + |
| + On Windows, if the unit tests ends with ".py", it will automatically prepend |
| + sys.executable. |
|
Dirk Pranke
2011/03/30 20:18:58
I read "it" here as Windows (or Python), but you m
|
| + """ |
| + # We don't want to hinder users from uploading incomplete patches. |
| + if input_api.is_committing: |
| + message_type = output_api.PresubmitError |
| + else: |
| + message_type = output_api.PresubmitPromptWarning |
| + |
| + if verbose: |
| + pipe = None |
| + else: |
| + pipe = input_api.subprocess.PIPE |
| + |
| + results = [] |
| + for unit_test in unit_tests: |
| + cmd = [] |
| + if input_api.platform == 'win32' and unit_test.endswith('.py'): |
| + # Windows needs some help. |
| + cmd = [input_api.python_executable] |
| + cmd.append(unit_test) |
| + if verbose: |
| + print('Running %s' % unit_test) |
| + try: |
| + proc = input_api.subprocess.Popen( |
| + cmd, cwd=input_api.PresubmitLocalPath(), stdout=pipe, stderr=pipe) |
| + out = '\n'.join(filter(None, proc.communicate())) |
| + if proc.returncode: |
| + results.append(message_type( |
| + '%s failed with return code %d\n%s' % ( |
| + unit_test, proc.returncode, out))) |
| + except (OSError, input_api.subprocess.CalledProcessError): |
| + results.append(message_type('%s failed' % unit_test)) |
| + return results |
| + |
| + |
| def RunPythonUnitTests(input_api, output_api, unit_tests): |
| """Run the unit tests out of process, capture the output and use the result |
| code to determine success. |
| + |
| + DEPRECATED. |
| """ |
|
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
|
| # We don't want to hinder users from uploading incomplete patches. |
| if input_api.is_committing: |
| @@ -477,10 +545,10 @@ def _FetchAllFiles(input_api, white_list, black_list): |
| return True |
| return False |
| - import os |
| files = [] |
| path_len = len(input_api.PresubmitLocalPath()) |
| - for dirpath, dirnames, filenames in os.walk(input_api.PresubmitLocalPath()): |
| + for dirpath, dirnames, filenames in input_api.os_walk( |
| + input_api.PresubmitLocalPath()): |
| # Passes dirnames in black list to speed up search. |
| for item in dirnames[:]: |
| filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |