OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Generic presubmit checks that can be reused by other presubmit checks.""" | 6 """Generic presubmit checks that can be reused by other presubmit checks.""" |
7 | 7 |
8 | 8 |
9 ### Description checks | 9 ### Description checks |
10 | 10 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 connection.close() | 249 connection.close() |
250 if input_api.re.match(closed, status): | 250 if input_api.re.match(closed, status): |
251 long_text = status + '\n' + url | 251 long_text = status + '\n' + url |
252 return [output_api.PresubmitPromptWarning("The tree is closed.", | 252 return [output_api.PresubmitPromptWarning("The tree is closed.", |
253 long_text=long_text)] | 253 long_text=long_text)] |
254 except IOError: | 254 except IOError: |
255 pass | 255 pass |
256 return [] | 256 return [] |
257 | 257 |
258 | 258 |
259 def _RunPythonUnitTests_LoadTests(input_api, module_name): | |
260 """Meant to be stubbed out during unit testing.""" | |
261 module = __import__(module_name) | |
262 for part in module_name.split('.')[1:]: | |
263 module = getattr(module, part) | |
264 return input_api.unittest.TestLoader().loadTestsFromModule(module)._tests | |
265 | |
266 | |
267 def RunPythonUnitTests(input_api, output_api, unit_tests): | 259 def RunPythonUnitTests(input_api, output_api, unit_tests): |
268 """Imports the unit_tests modules and run them.""" | 260 """Run the unit tests out of process, capture the output and use the result |
261 code to determine success. | |
262 """ | |
269 # We don't want to hinder users from uploading incomplete patches. | 263 # We don't want to hinder users from uploading incomplete patches. |
270 if input_api.is_committing: | 264 if input_api.is_committing: |
271 message_type = output_api.PresubmitError | 265 message_type = output_api.PresubmitError |
272 else: | 266 else: |
273 message_type = output_api.PresubmitNotifyResult | 267 message_type = output_api.PresubmitNotifyResult |
274 tests_suite = [] | |
275 outputs = [] | 268 outputs = [] |
276 for unit_test in unit_tests: | 269 for unit_test in unit_tests: |
277 try: | 270 # Run the unit tests out of process. This is because some unit tests |
278 tests_suite.extend(_RunPythonUnitTests_LoadTests(input_api, unit_test)) | 271 # stub out base libraries and don't clean up their mess. It's too easy to |
279 except ImportError: | 272 # get subtle bugs. |
280 outputs.append(message_type("Failed to load %s" % unit_test, | 273 cwd = None |
281 long_text=input_api.traceback.format_exc())) | 274 env = None |
282 | 275 unit_test_name = unit_test |
283 buffer = input_api.cStringIO.StringIO() | 276 # "python -m test.unit_test" doesn't work. We need to change to the right |
284 results = input_api.unittest.TextTestRunner(stream=buffer, verbosity=0).run( | 277 # directory instead. |
285 input_api.unittest.TestSuite(tests_suite)) | 278 if '.' in unit_test: |
286 if not results.wasSuccessful(): | 279 # Tests imported in submodules (subdirectories) assume that the current |
287 outputs.append(message_type("%d unit tests failed." % | 280 # directory is in the PYTHONPATH. Manually fix that. |
288 (len(results.failures) + len(results.errors)), | 281 unit_test = unit_test.replace('.', '/') |
289 long_text=buffer.getvalue())) | 282 cwd = input_api.os_path.dirname(unit_test) |
290 return outputs | 283 unit_test = input_api.os_path.basename(unit_test) |
284 env = input_api.environ.copy() | |
285 backpath = [';'.join(['..'] * (cwd.count('/') + 1))] | |
286 if env.get('PYTHONPATH'): | |
287 backpath.append(env.get('PYTHONPATH')) | |
288 env['PYTHONPATH'] = ';'.join((backpath)) | |
289 subproc = input_api.subprocess.Popen( | |
290 [ | |
291 input_api.python_executable, | |
292 "-m", | |
293 "%s" % unit_test | |
294 ], | |
295 cwd=cwd, | |
296 env=env, | |
297 stdin=input_api.subprocess.PIPE, | |
298 stdout=input_api.subprocess.PIPE, | |
299 stderr=input_api.subprocess.PIPE) | |
300 stdoutdata, stderrdata = subproc.communicate() | |
301 # Discard the output if returncode != 0 | |
Jói Sigurðsson
2009/06/23 20:03:51
!= -> ==
| |
302 if subproc.returncode: | |
303 outputs.append("Test '%s' failed with code %d\n%s\n%s\n" % ( | |
304 unit_test_name, subproc.returncode, stdoutdata, stderrdata)) | |
305 if outputs: | |
306 return [message_type("%d unit tests failed." % len(outputs), | |
307 long_text='\n'.join(outputs))] | |
308 return [] | |
OLD | NEW |