OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Run a test. | |
7 | |
8 Sample usage: | |
9 ./run.py \ | |
10 -a src/xcodebuild/Release-iphoneos/base_unittests.app \ | |
11 -o /tmp/out \ | |
12 -p iPhone 5s \ | |
13 -v 9.3 | |
14 | |
15 Installs base_unittests.app in an iPhone 5s simulator running iOS 9.3, | |
16 runs it, and captures all test data in /tmp/out. | |
17 """ | |
18 | |
19 import argparse | |
20 import json | |
21 import os | |
22 import sys | |
23 import traceback | |
24 | |
25 import test_runner | |
26 | |
27 | |
28 def main(args, test_args): | |
29 summary = {} | |
30 tr = None | |
31 | |
32 if not os.path.exists(args.out_dir): | |
33 os.makedirs(args.out_dir) | |
34 | |
35 try: | |
36 if args.iossim and args.platform and args.version: | |
37 tr = test_runner.SimulatorTestRunner( | |
38 args.app, | |
39 args.iossim, | |
40 args.platform, | |
41 args.version, | |
42 args.xcode_version, | |
43 args.out_dir, | |
44 env_vars=args.env_var, | |
45 test_args=test_args, | |
46 xctest=args.xctest, | |
47 ) | |
48 else: | |
49 tr = test_runner.DeviceTestRunner( | |
50 args.app, | |
51 args.xcode_version, | |
52 args.out_dir, | |
53 env_vars=args.env_var, | |
54 test_args=test_args, | |
55 xctest=args.xctest, | |
56 ) | |
57 | |
58 return 0 if tr.launch() else 1 | |
59 except test_runner.TestRunnerError as e: | |
60 sys.stderr.write(traceback.format_exc()) | |
61 summary['step_text'] = '%s%s' % ( | |
62 e.__class__.__name__, ': %s' % e.args[0] if e.args else '') | |
63 | |
64 # test_runner.Launch returns 0 on success, 1 on failure, so return 2 | |
65 # on exception to distinguish between a test failure, and a failure | |
66 # to launch the test at all. | |
67 return 2 | |
68 finally: | |
69 if tr: | |
70 summary['logs'] = tr.logs | |
71 | |
72 with open(os.path.join(args.out_dir, 'summary.json'), 'w') as f: | |
73 json.dump(summary, f) | |
74 | |
75 | |
76 if __name__ == '__main__': | |
77 parser = argparse.ArgumentParser() | |
78 | |
79 parser.add_argument( | |
80 '-a', | |
81 '--app', | |
82 help='Compiled .app to run.', | |
83 metavar='app', | |
84 required=True, | |
85 ) | |
86 parser.add_argument( | |
87 '-e', | |
88 '--env-var', | |
89 action='append', | |
90 help='Environment variable to pass to the test itself.', | |
91 metavar='ENV=val', | |
92 ) | |
93 parser.add_argument( | |
94 '-i', | |
95 '--iossim', | |
96 help='Compiled iossim to run the app on.', | |
97 metavar='iossim', | |
98 ) | |
99 parser.add_argument( | |
100 '-j', | |
101 '--args-json', | |
102 default='{}', | |
103 help='Specify "env_var": [...] and "test_args": [...] using a JSON dict.', | |
104 metavar='{}', | |
105 ) | |
106 parser.add_argument( | |
107 '-o', | |
108 '--out-dir', | |
109 help='Directory to store all test data in.', | |
110 metavar='dir', | |
111 required=True, | |
112 ) | |
113 parser.add_argument( | |
114 '-p', | |
115 '--platform', | |
116 help='Platform to simulate.', | |
117 metavar='sim', | |
118 ) | |
119 parser.add_argument( | |
120 '-v', | |
121 '--version', | |
122 help='Version of iOS the simulator should run.', | |
123 metavar='ver', | |
124 ) | |
125 parser.add_argument( | |
126 '-x', | |
127 '--xcode-version', | |
128 help='Version of Xcode to use.', | |
129 metavar='ver', | |
130 required=True, | |
131 ) | |
132 parser.add_argument( | |
133 '--xctest', | |
134 action='store_true', | |
135 help='Whether or not the given app should be run as an XCTest.', | |
136 ) | |
137 | |
138 args, test_args = parser.parse_known_args() | |
139 if args.iossim or args.platform or args.version: | |
140 # If any of --iossim, --platform, or --version | |
141 # are specified then they must all be specified. | |
142 if not (args.iossim and args.platform and args.version): | |
143 parser.error( | |
144 'must specify all or none of -i/--iossim, -p/--platform, -v/--version') | |
145 | |
146 args_json = json.loads(args.args_json) | |
147 args.env_var = args.env_var or [] | |
148 args.env_var.extend(args_json.get('env_var', [])) | |
149 args.xctest = args_json.get('xctest', args.xctest) | |
150 test_args.extend(args_json.get('test_args', [])) | |
151 | |
152 sys.exit(main(args, test_args)) | |
OLD | NEW |