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

Side by Side Diff: mojo/tools/test_runner.py

Issue 728783003: Add infrastructure to run tests on android. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 6 years 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
« no previous file with comments | « mojo/tools/mopy/transitive_hash.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
16 from mopy.transitive_hash import transitive_hash 17 from mopy.paths import Paths
18 from mopy.transitive_hash import file_hash, 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 if args.android:
99 gtest_hash = file_hash(gtest_file)
100 else:
101 gtest_hash = transitive_hash(gtest_file)
82 except subprocess.CalledProcessError: 102 except subprocess.CalledProcessError:
83 print "Failed to get transitive hash for %s" % gtest 103 print "Failed to get transitive hash for %s" % gtest
84 return 1 104 return 1
85 _logging.debug(" Transitive hash: %s" % gtest_hash) 105 _logging.debug(" Transitive hash: %s" % gtest_hash)
86 106
87 if gtest_hash in successes: 107 if gtest_hash in successes:
88 print "Skipping %s (previously succeeded)" % gtest 108 print "Skipping %s (previously succeeded)" % gtest
89 continue 109 continue
90 110
91 print "Running %s...." % gtest, 111 print "Running %s...." % gtest,
92 sys.stdout.flush() 112 sys.stdout.flush()
93 try: 113 try:
94 subprocess.check_output(["./" + gtest], stderr=subprocess.STDOUT) 114 if args.android:
115 command = [
116 "python",
117 os.path.join(paths.src_root, "build", "android", "test_runner.py"),
118 "gtest",
119 "--output-directory",
120 args.root_dir,
121 "-s",
122 gtest,
123 ]
124 else:
125 command = ["./" + gtest]
126 subprocess.check_output(command, stderr=subprocess.STDOUT)
95 print "Succeeded" 127 print "Succeeded"
96 # Record success. 128 # Record success.
97 if successes_cache_filename and cacheable: 129 if args.successes_cache_filename and cacheable:
98 successes.add(gtest_hash) 130 successes.add(gtest_hash)
99 successes_cache_file.write(gtest_hash + '\n') 131 successes_cache_file.write(gtest_hash + '\n')
100 successes_cache_file.flush() 132 successes_cache_file.flush()
101 except subprocess.CalledProcessError as e: 133 except subprocess.CalledProcessError as e:
102 print "Failed with exit code %d and output:" % e.returncode 134 print "Failed with exit code %d and output:" % e.returncode
103 print 72 * '-' 135 print 72 * '-'
104 print e.output 136 print e.output
105 print 72 * '-' 137 print 72 * '-'
106 return 1 138 return 1
107 except OSError as e: 139 except OSError as e:
108 print " Failed to start test" 140 print " Failed to start test"
109 return 1 141 return 1
110 print "All tests succeeded" 142 print "All tests succeeded"
111 if successes_cache_file: 143 if successes_cache_file:
112 successes_cache_file.close() 144 successes_cache_file.close()
113 145
114 return 0 146 return 0
115 147
116 if __name__ == '__main__': 148 if __name__ == '__main__':
117 sys.exit(main(sys.argv)) 149 sys.exit(main())
OLDNEW
« no previous file with comments | « mojo/tools/mopy/transitive_hash.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698