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