| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 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 """A "smart" test runner for gtest unit tests (that caches successes).""" | 6 """A "smart" test runner for gtest unit tests (that caches successes).""" |
| 7 | 7 |
| 8 import argparse |
| 8 import logging | 9 import logging |
| 9 import os | 10 import os |
| 10 import platform | 11 import platform |
| 11 import subprocess | 12 import subprocess |
| 12 import sys | 13 import sys |
| 13 | 14 |
| 14 _logging = logging.getLogger() | 15 _logging = logging.getLogger() |
| 15 | 16 |
| 17 from mopy.paths import Paths |
| 16 from mopy.transitive_hash import transitive_hash | 18 from mopy.transitive_hash import transitive_hash |
| 17 | 19 |
| 18 def main(argv): | 20 paths = Paths() |
| 21 |
| 22 def main(): |
| 19 logging.basicConfig() | 23 logging.basicConfig() |
| 20 # Uncomment to debug: | 24 # Uncomment to debug: |
| 21 # _logging.setLevel(logging.DEBUG) | 25 # _logging.setLevel(logging.DEBUG) |
| 22 | 26 |
| 23 if len(argv) < 3 or len(argv) > 4: | 27 parser = argparse.ArgumentParser( |
| 24 print "Usage: %s gtest_list_file root_dir [successes_cache_file]" % \ | 28 description="A 'smart' test runner for gtest unit tests (that caches " |
| 25 os.path.basename(argv[0]) | 29 "successes).") |
| 26 return 0 if len(argv) < 2 else 1 | |
| 27 | 30 |
| 28 _logging.debug("Test list file: %s", argv[1]) | 31 os_group = parser.add_mutually_exclusive_group() |
| 29 with open(argv[1], 'rb') as f: | 32 os_group.add_argument("--android", help="Run tests for android", |
| 33 action='store_true') |
| 34 |
| 35 parser.add_argument("gtest_list_file", |
| 36 help="The file containing the tests to run.") |
| 37 parser.add_argument("root_dir", help="The build directory.") |
| 38 parser.add_argument("successes_cache_filename", |
| 39 help="The file caching test results.", default=None, |
| 40 nargs='?') |
| 41 args = parser.parse_args() |
| 42 |
| 43 _logging.debug("Test list file: %s", args.gtest_list_file) |
| 44 with open(args.gtest_list_file, 'rb') as f: |
| 30 gtest_list = [y for y in [x.strip() for x in f.readlines()] \ | 45 gtest_list = [y for y in [x.strip() for x in f.readlines()] \ |
| 31 if y and y[0] != '#'] | 46 if y and y[0] != '#'] |
| 32 _logging.debug("Test list: %s" % gtest_list) | 47 _logging.debug("Test list: %s" % gtest_list) |
| 33 | 48 |
| 34 print "Running tests in directory: %s" % argv[2] | 49 print "Running tests in directory: %s" % args.root_dir |
| 35 os.chdir(argv[2]) | 50 os.chdir(args.root_dir) |
| 36 | 51 |
| 37 if len(argv) == 4 and argv[3]: | 52 if args.successes_cache_filename: |
| 38 successes_cache_filename = argv[3] | 53 print "Successes cache file: %s" % args.successes_cache_filename |
| 39 print "Successes cache file: %s" % successes_cache_filename | |
| 40 else: | 54 else: |
| 41 successes_cache_filename = None | |
| 42 print "No successes cache file (will run all tests unconditionally)" | 55 print "No successes cache file (will run all tests unconditionally)" |
| 43 | 56 |
| 44 if successes_cache_filename: | 57 if args.successes_cache_filename: |
| 45 # This file simply contains a list of transitive hashes of tests that | 58 # This file simply contains a list of transitive hashes of tests that |
| 46 # succeeded. | 59 # succeeded. |
| 47 try: | 60 try: |
| 48 _logging.debug("Trying to read successes cache file: %s", | 61 _logging.debug("Trying to read successes cache file: %s", |
| 49 successes_cache_filename) | 62 args.successes_cache_filename) |
| 50 with open(argv[3], 'rb') as f: | 63 with open(args.successes_cache_filename, 'rb') as f: |
| 51 successes = set([x.strip() for x in f.readlines()]) | 64 successes = set([x.strip() for x in f.readlines()]) |
| 52 _logging.debug("Successes: %s", successes) | 65 _logging.debug("Successes: %s", successes) |
| 53 except IOError: | 66 except IOError: |
| 54 # Just assume that it didn't exist, or whatever. | 67 # Just assume that it didn't exist, or whatever. |
| 55 print "Failed to read successes cache file %s (will create)" % argv[3] | 68 print ("Failed to read successes cache file %s (will create)" % |
| 69 args.successes_cache_filename) |
| 56 successes = set() | 70 successes = set() |
| 57 | 71 |
| 58 # Run gtests with color if we're on a TTY (and we're not being told explicitly | 72 # Run gtests with color if we're on a TTY (and we're not being told explicitly |
| 59 # what to do). | 73 # what to do). |
| 60 if sys.stdout.isatty() and 'GTEST_COLOR' not in os.environ: | 74 if sys.stdout.isatty() and 'GTEST_COLOR' not in os.environ: |
| 61 _logging.debug("Setting GTEST_COLOR=yes") | 75 _logging.debug("Setting GTEST_COLOR=yes") |
| 62 os.environ['GTEST_COLOR'] = 'yes' | 76 os.environ['GTEST_COLOR'] = 'yes' |
| 63 | 77 |
| 64 # TODO(vtl): We may not close this file on failure. | 78 # TODO(vtl): We may not close this file on failure. |
| 65 successes_cache_file = open(successes_cache_filename, 'ab') \ | 79 successes_cache_file = open(args.successes_cache_filename, 'ab') \ |
| 66 if successes_cache_filename else None | 80 if args.successes_cache_filename else None |
| 67 for gtest in gtest_list: | 81 for gtest in gtest_list: |
| 68 if gtest[0] == '*': | 82 if gtest[0] == '*': |
| 69 gtest = gtest[1:] | 83 gtest = gtest[1:] |
| 70 _logging.debug("%s is marked as non-cacheable" % gtest) | 84 _logging.debug("%s is marked as non-cacheable" % gtest) |
| 71 cacheable = False | 85 cacheable = False |
| 72 else: | 86 else: |
| 73 cacheable = True | 87 cacheable = True |
| 74 | 88 |
| 89 gtest_file = gtest |
| 75 if platform.system() == 'Windows': | 90 if platform.system() == 'Windows': |
| 76 gtest += ".exe" | 91 gtest_file += ".exe" |
| 92 if args.android: |
| 93 gtest_file = gtest + "_apk/" + gtest + "-debug.apk" |
| 77 | 94 |
| 78 if successes_cache_file and cacheable: | 95 if successes_cache_file and cacheable: |
| 79 _logging.debug("Getting transitive hash for %s ... " % gtest) | 96 _logging.debug("Getting transitive hash for %s ... " % gtest) |
| 80 try: | 97 try: |
| 81 gtest_hash = transitive_hash(gtest) | 98 gtest_hash = transitive_hash(gtest_file, not args.android) |
| 82 except subprocess.CalledProcessError: | 99 except subprocess.CalledProcessError: |
| 83 print "Failed to get transitive hash for %s" % gtest | 100 print "Failed to get transitive hash for %s" % gtest |
| 84 return 1 | 101 return 1 |
| 85 _logging.debug(" Transitive hash: %s" % gtest_hash) | 102 _logging.debug(" Transitive hash: %s" % gtest_hash) |
| 86 | 103 |
| 87 if gtest_hash in successes: | 104 if gtest_hash in successes: |
| 88 print "Skipping %s (previously succeeded)" % gtest | 105 print "Skipping %s (previously succeeded)" % gtest |
| 89 continue | 106 continue |
| 90 | 107 |
| 91 print "Running %s...." % gtest, | 108 print "Running %s...." % gtest, |
| 92 sys.stdout.flush() | 109 sys.stdout.flush() |
| 93 try: | 110 try: |
| 94 subprocess.check_output(["./" + gtest], stderr=subprocess.STDOUT) | 111 if args.android: |
| 112 command = [ "python" ] |
| 113 command.append(os.path.join(paths.src_root, |
| 114 "build", "android", "test_runner.py")) |
| 115 command.append("gtest") |
| 116 command.append("--output-directory") |
| 117 command.append(args.root_dir) |
| 118 command.append("-s") |
| 119 command.append(gtest) |
| 120 else: |
| 121 command = [ "./" + gtest ] |
| 122 subprocess.check_output(command, stderr=subprocess.STDOUT) |
| 95 print "Succeeded" | 123 print "Succeeded" |
| 96 # Record success. | 124 # Record success. |
| 97 if successes_cache_filename and cacheable: | 125 if args.successes_cache_filename and cacheable: |
| 98 successes.add(gtest_hash) | 126 successes.add(gtest_hash) |
| 99 successes_cache_file.write(gtest_hash + '\n') | 127 successes_cache_file.write(gtest_hash + '\n') |
| 100 successes_cache_file.flush() | 128 successes_cache_file.flush() |
| 101 except subprocess.CalledProcessError as e: | 129 except subprocess.CalledProcessError as e: |
| 102 print "Failed with exit code %d and output:" % e.returncode | 130 print "Failed with exit code %d and output:" % e.returncode |
| 103 print 72 * '-' | 131 print 72 * '-' |
| 104 print e.output | 132 print e.output |
| 105 print 72 * '-' | 133 print 72 * '-' |
| 106 return 1 | 134 return 1 |
| 107 except OSError as e: | 135 except OSError as e: |
| 108 print " Failed to start test" | 136 print " Failed to start test" |
| 109 return 1 | 137 return 1 |
| 110 print "All tests succeeded" | 138 print "All tests succeeded" |
| 111 if successes_cache_file: | 139 if successes_cache_file: |
| 112 successes_cache_file.close() | 140 successes_cache_file.close() |
| 113 | 141 |
| 114 return 0 | 142 return 0 |
| 115 | 143 |
| 116 if __name__ == '__main__': | 144 if __name__ == '__main__': |
| 117 sys.exit(main(sys.argv)) | 145 sys.exit(main()) |
| OLD | NEW |