OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import argparse | |
6 import multiprocessing | |
7 import sys | |
8 | |
9 from .cover import CoverageContext | |
10 | |
11 from . import handle_list, handle_debug, handle_train, handle_test | |
12 | |
13 from .pipeline import ResultLoop | |
14 | |
15 HANDLERS = { | |
16 'list': handle_list.ListHandler, | |
17 'debug': handle_debug.DebugHandler, | |
18 'train': handle_train.TrainHandler, | |
19 'test': handle_test.TestHandler, | |
20 } | |
21 | |
22 def _parse_args(args): | |
23 args = args or sys.argv[1:] | |
24 | |
25 # Set the default mode if not specified and not passing --help | |
26 search_names = set(HANDLERS.keys() + ['-h', '--help']) | |
27 if not any(arg in search_names for arg in args): | |
28 args.insert(0, 'test') | |
29 | |
30 parser = argparse.ArgumentParser() | |
31 subparsers = parser.add_subparsers( | |
32 title='Mode (default "test")', dest='mode', | |
33 help='See `[mode] --help` for more options.') | |
34 | |
35 for k, h in HANDLERS.iteritems(): | |
36 sp = subparsers.add_parser(k, help=h.__doc__.lower()) | |
Vadim Sh.
2014/04/02 22:37:59
lower only first letter? :)
iannucci
2014/04/03 00:03:49
rrrrrr :p. Done
| |
37 h.add_options(sp) | |
38 | |
39 mg = sp.add_mutually_exclusive_group() | |
40 mg.add_argument( | |
41 '--quiet', action='store_true', | |
42 help='be quiet (only print failures)') | |
43 mg.add_argument( | |
44 '--verbose', action='store_true', help='be verbose') | |
45 | |
46 if not h.SKIP_RUNLOOP: | |
47 sp.add_argument( | |
48 '--jobs', metavar='N', type=int, | |
49 default=multiprocessing.cpu_count(), | |
50 help='run N jobs in parallel (default %(default)s)') | |
51 | |
52 sp.add_argument( | |
53 '--test_list', metavar='FILE', | |
54 help='take the list of test globs from the FILE (use "-" for stdin)') | |
55 | |
56 sp.add_argument( | |
57 'test_glob', nargs='*', help=( | |
58 'glob to filter the tests acted on. If the glob begins with "-" ' | |
59 'then it acts as a negation glob and anything which matches it ' | |
60 'will be skipped.')) | |
61 | |
62 opts = parser.parse_args(args) | |
63 | |
64 if not hasattr(opts, 'jobs'): | |
65 opts.jobs = 0 | |
66 elif opts.jobs < 1: | |
67 parser.error('--jobs was less than 1') | |
68 | |
69 if opts.test_list: | |
70 fh = sys.stdin if opts.test_list == '-' else open(opts.test_list, 'rb') | |
71 with fh as tl: | |
72 opts.test_glob += [l.strip() for l in tl.readlines()] | |
73 | |
74 opts.handler = HANDLERS[opts.mode] | |
75 | |
76 del opts.test_list | |
77 del opts.mode | |
78 | |
79 return opts | |
80 | |
81 | |
82 def main(test_gen, coverage_includes=None, coverage_omits=None, args=None): | |
83 """Entry point for tests using expect_tests. | |
84 | |
85 Example: | |
86 import expect_tests | |
87 | |
88 def happy_fn(val): | |
89 # Usually you would return data which is the result of some deterministic | |
90 # computation. | |
91 return expect_tests.Result({'neet': '%s string value' % val}) | |
92 | |
93 def Gen(): | |
94 yield expect_tests.Test('happy', happy_fn, args=('happy',)) | |
95 | |
96 if __name__ == '__main__': | |
97 expect_tests.main() | |
98 | |
99 @param test_gen: A Generator which yields Test objects. | |
100 @param coverage_includes: A list of path globs to include under coverage. | |
101 @param coverage_omits: A list of path globs to exclude under coverage. | |
102 @param args: Commandline args (starting at argv[1]) | |
103 """ | |
104 try: | |
105 opts = _parse_args(args) | |
106 | |
107 cover_ctx = CoverageContext(coverage_includes, coverage_omits, | |
108 not opts.handler.SKIP_RUNLOOP) | |
109 | |
110 error, killed = ResultLoop(test_gen, cover_ctx.create_subprocess_context(), | |
111 opts) | |
112 | |
113 cover_ctx.cleanup() | |
114 if not killed and not opts.test_glob: | |
115 if not cover_ctx.report(opts.verbose): | |
116 sys.exit(2) | |
117 | |
118 sys.exit(error or killed) | |
119 except KeyboardInterrupt: | |
120 pass | |
OLD | NEW |