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