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