OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import multiprocessing | 6 import multiprocessing |
7 import os | |
8 import sys | 7 import sys |
9 | 8 |
10 from .cover import CoverageContext | 9 from .cover import CoverageContext |
11 | 10 |
12 from . import handle_list, handle_debug, handle_train, handle_test | 11 from . import handle_list, handle_debug, handle_train, handle_test |
13 | 12 |
14 from .pipeline import result_loop | 13 from .pipeline import result_loop |
15 | 14 |
16 | 15 |
17 HANDLERS = { | 16 HANDLERS = { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 '--test_list', metavar='FILE', | 87 '--test_list', metavar='FILE', |
89 help='take the list of test globs from the FILE (use "-" for stdin)' | 88 help='take the list of test globs from the FILE (use "-" for stdin)' |
90 ).completer = lambda **_: [] | 89 ).completer = lambda **_: [] |
91 | 90 |
92 sp.add_argument( | 91 sp.add_argument( |
93 '--html_report', metavar='DIR', | 92 '--html_report', metavar='DIR', |
94 help='directory to write html report (default: disabled)' | 93 help='directory to write html report (default: disabled)' |
95 ).completer = lambda **_: [] | 94 ).completer = lambda **_: [] |
96 | 95 |
97 sp.add_argument( | 96 sp.add_argument( |
98 '--extra_coverage_data', metavar='FILE', nargs='*', | |
99 help='additional coverage data files to incorporate' | |
100 ).completer = lambda **_: [] | |
101 | |
102 sp.add_argument( | |
103 'test_glob', nargs='*', help=( | 97 'test_glob', nargs='*', help=( |
104 'glob to filter the tests acted on. If the glob begins with "-" ' | 98 'glob to filter the tests acted on. If the glob begins with "-" ' |
105 'then it acts as a negation glob and anything which matches it ' | 99 'then it acts as a negation glob and anything which matches it ' |
106 'will be skipped. If a glob doesn\'t have "*" in it, "*" will be ' | 100 'will be skipped. If a glob doesn\'t have "*" in it, "*" will be ' |
107 'implicitly appended to the end') | 101 'implicitly appended to the end') |
108 ).completer = _test_completer(test_gen) | 102 ).completer = _test_completer(test_gen) |
109 | 103 |
110 opts = parser.parse_args(args) | 104 opts = parser.parse_args(args) |
111 | 105 |
112 if not hasattr(opts, 'jobs'): | 106 if not hasattr(opts, 'jobs'): |
113 opts.jobs = 0 | 107 opts.jobs = 0 |
114 elif opts.jobs < 1: | 108 elif opts.jobs < 1: |
115 parser.error('--jobs was less than 1') | 109 parser.error('--jobs was less than 1') |
116 | 110 |
117 if opts.extra_coverage_data: | |
118 for fname in opts.extra_coverage_data: | |
119 if not os.path.exists(fname): | |
120 parser.error('--extra_coverage_data %r does not exist' % fname) | |
121 | |
122 if opts.test_list: | 111 if opts.test_list: |
123 fh = sys.stdin if opts.test_list == '-' else open(opts.test_list, 'rb') | 112 fh = sys.stdin if opts.test_list == '-' else open(opts.test_list, 'rb') |
124 with fh as tl: | 113 with fh as tl: |
125 opts.test_glob += [l.strip() for l in tl.readlines()] | 114 opts.test_glob += [l.strip() for l in tl.readlines()] |
126 | 115 |
127 opts.handler = HANDLERS[opts.mode] | 116 opts.handler = HANDLERS[opts.mode] |
128 | 117 |
129 del opts.test_list | 118 del opts.test_list |
130 del opts.mode | 119 del opts.mode |
131 | 120 |
132 return opts | 121 return opts |
133 | 122 |
134 | 123 |
135 def main(name, test_gen, coverage_includes=None, coverage_omits=None, | 124 def main(name, test_gen, cover_branches=False, args=None): |
136 cover_branches=False, args=None): | |
137 """Entry point for tests using expect_tests. | 125 """Entry point for tests using expect_tests. |
138 | 126 |
139 Example: | 127 Example: |
140 import expect_tests | 128 import expect_tests |
141 | 129 |
142 def happy_fn(val): | 130 def happy_fn(val): |
143 # Usually you would return data which is the result of some deterministic | 131 # Usually you would return data which is the result of some deterministic |
144 # computation. | 132 # computation. |
145 return expect_tests.Result({'neet': '%s string value' % val}) | 133 return expect_tests.Result({'neet': '%s string value' % val}) |
146 | 134 |
147 def Gen(): | 135 def Gen(): |
148 yield expect_tests.Test('happy', happy_fn, args=('happy',)) | 136 yield expect_tests.Test('happy', happy_fn, args=('happy',)) |
149 | 137 |
150 if __name__ == '__main__': | 138 if __name__ == '__main__': |
151 expect_tests.main('happy_test_suite', Gen) | 139 expect_tests.main('happy_test_suite', Gen) |
152 | 140 |
153 @param name: Name of the test suite. | 141 @param name: Name of the test suite. |
154 @param test_gen: A Generator which yields Test objects. | 142 @param test_gen: A Generator which yields Test objects. |
155 @param coverage_includes: A list of path globs to include under coverage. | |
156 @param coverage_omits: A list of path globs to exclude under coverage. | |
157 @param cover_branches: Include branch coverage data (rather than just line | 143 @param cover_branches: Include branch coverage data (rather than just line |
158 coverage) | 144 coverage) |
159 @param args: Commandline args (starting at argv[1]) | 145 @param args: Commandline args (starting at argv[1]) |
160 """ | 146 """ |
161 try: | 147 try: |
162 opts = _parse_args(args, test_gen) | 148 opts = _parse_args(args, test_gen) |
163 | 149 |
164 cover_ctx = CoverageContext(name, coverage_includes, coverage_omits, | 150 cover_ctx = CoverageContext(name, cover_branches, opts.html_report, |
165 cover_branches, opts.html_report, | |
166 opts.extra_coverage_data, | |
167 not opts.handler.SKIP_RUNLOOP) | 151 not opts.handler.SKIP_RUNLOOP) |
168 | 152 |
169 error, killed = result_loop(test_gen, cover_ctx.create_subprocess_context(), | 153 error, killed = result_loop( |
170 opts) | 154 test_gen, cover_ctx.create_subprocess_context(), opts) |
171 | 155 |
172 cover_ctx.cleanup() | 156 cover_ctx.cleanup() |
173 if not killed and not opts.test_glob: | 157 if not killed and not opts.test_glob: |
174 if not cover_ctx.report(opts.verbose): | 158 if not cover_ctx.report(opts.verbose): |
175 sys.exit(2) | 159 sys.exit(2) |
176 | 160 |
177 sys.exit(error or killed) | 161 sys.exit(error or killed) |
178 except KeyboardInterrupt: | 162 except KeyboardInterrupt: |
179 pass | 163 pass |
OLD | NEW |