Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: presubmit_canned_checks.py

Issue 6778027: Add RunUnitTests() and RunUnitTestsInDirectory() canned checks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Update comment Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « .gitignore ('k') | presubmit_support.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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, sys.executable is used for unit tests ending with ".py".
444 """
445 # We don't want to hinder users from uploading incomplete patches.
446 if input_api.is_committing:
447 message_type = output_api.PresubmitError
448 else:
449 message_type = output_api.PresubmitPromptWarning
450
451 if verbose:
452 pipe = None
453 else:
454 pipe = input_api.subprocess.PIPE
455
456 results = []
457 for unit_test in unit_tests:
458 cmd = []
459 if input_api.platform == 'win32' and unit_test.endswith('.py'):
460 # Windows needs some help.
461 cmd = [input_api.python_executable]
462 cmd.append(unit_test)
463 if verbose:
464 print('Running %s' % unit_test)
465 try:
466 proc = input_api.subprocess.Popen(
467 cmd, cwd=input_api.PresubmitLocalPath(), stdout=pipe, stderr=pipe)
468 out = '\n'.join(filter(None, proc.communicate()))
469 if proc.returncode:
470 results.append(message_type(
471 '%s failed with return code %d\n%s' % (
472 unit_test, proc.returncode, out)))
473 except (OSError, input_api.subprocess.CalledProcessError):
474 results.append(message_type('%s failed' % unit_test))
475 return results
476
477
413 def RunPythonUnitTests(input_api, output_api, unit_tests): 478 def RunPythonUnitTests(input_api, output_api, unit_tests):
414 """Run the unit tests out of process, capture the output and use the result 479 """Run the unit tests out of process, capture the output and use the result
415 code to determine success. 480 code to determine success.
481
482 DEPRECATED.
416 """ 483 """
417 # We don't want to hinder users from uploading incomplete patches. 484 # We don't want to hinder users from uploading incomplete patches.
418 if input_api.is_committing: 485 if input_api.is_committing:
419 message_type = output_api.PresubmitError 486 message_type = output_api.PresubmitError
420 else: 487 else:
421 message_type = output_api.PresubmitNotifyResult 488 message_type = output_api.PresubmitNotifyResult
422 outputs = [] 489 outputs = []
423 for unit_test in unit_tests: 490 for unit_test in unit_tests:
424 # Run the unit tests out of process. This is because some unit tests 491 # 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 492 # stub out base libraries and don't clean up their mess. It's too easy to
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 # We cannot use AffectedFiles here because we want to test every python 537 # 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 538 # file on each single python change. It's because a change in a python file
472 # can break another unmodified file. 539 # can break another unmodified file.
473 # Use code similar to InputApi.FilterSourceFile() 540 # Use code similar to InputApi.FilterSourceFile()
474 def Find(filepath, filters): 541 def Find(filepath, filters):
475 for item in filters: 542 for item in filters:
476 if input_api.re.match(item, filepath): 543 if input_api.re.match(item, filepath):
477 return True 544 return True
478 return False 545 return False
479 546
480 import os
481 files = [] 547 files = []
482 path_len = len(input_api.PresubmitLocalPath()) 548 path_len = len(input_api.PresubmitLocalPath())
483 for dirpath, dirnames, filenames in os.walk(input_api.PresubmitLocalPath()): 549 for dirpath, dirnames, filenames in input_api.os_walk(
550 input_api.PresubmitLocalPath()):
484 # Passes dirnames in black list to speed up search. 551 # Passes dirnames in black list to speed up search.
485 for item in dirnames[:]: 552 for item in dirnames[:]:
486 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] 553 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:]
487 if Find(filepath, black_list): 554 if Find(filepath, black_list):
488 dirnames.remove(item) 555 dirnames.remove(item)
489 for item in filenames: 556 for item in filenames:
490 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] 557 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:]
491 if Find(filepath, white_list) and not Find(filepath, black_list): 558 if Find(filepath, white_list) and not Find(filepath, black_list):
492 files.append(filepath) 559 files.append(filepath)
493 return files 560 return files
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 input_api, output_api, source_file_filter=text_files)) 897 input_api, output_api, source_file_filter=text_files))
831 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( 898 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes(
832 input_api, output_api)) 899 input_api, output_api))
833 results.extend(input_api.canned_checks.CheckLicense( 900 results.extend(input_api.canned_checks.CheckLicense(
834 input_api, output_api, license_header, source_file_filter=sources)) 901 input_api, output_api, license_header, source_file_filter=sources))
835 results.extend(_CheckConstNSObject( 902 results.extend(_CheckConstNSObject(
836 input_api, output_api, source_file_filter=sources)) 903 input_api, output_api, source_file_filter=sources))
837 results.extend(_CheckSingletonInHeaders( 904 results.extend(_CheckSingletonInHeaders(
838 input_api, output_api, source_file_filter=sources)) 905 input_api, output_api, source_file_filter=sources))
839 return results 906 return results
OLDNEW
« no previous file with comments | « .gitignore ('k') | presubmit_support.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698