Commit | Line | Data |
---|---|---|
b85894a3 MJ |
1 | # Copyright (c) 2016, Matt Layman |
2 | ||
3 | import argparse | |
4 | import sys | |
5 | import unittest | |
6 | ||
7 | from tap.i18n import _ | |
8 | from tap.loader import Loader | |
9 | ||
10 | ||
11 | def main(argv=sys.argv, stream=sys.stderr): | |
12 | """Entry point for ``tappy`` command.""" | |
13 | args = parse_args(argv) | |
14 | suite = build_suite(args) | |
15 | runner = unittest.TextTestRunner(verbosity=args.verbose, stream=stream) | |
16 | result = runner.run(suite) | |
17 | ||
18 | return get_status(result) | |
19 | ||
20 | ||
21 | def build_suite(args): | |
22 | """Build a test suite by loading TAP files or a TAP stream.""" | |
23 | loader = Loader() | |
24 | if len(args.files) == 0 or args.files[0] == '-': | |
25 | suite = loader.load_suite_from_stdin() | |
26 | else: | |
27 | suite = loader.load(args.files) | |
28 | return suite | |
29 | ||
30 | ||
31 | def parse_args(argv): | |
32 | description = _('A TAP consumer for Python') | |
33 | epilog = _( | |
34 | 'When no files are given or a dash (-) is used for the file name, ' | |
35 | 'tappy will read a TAP stream from STDIN.') | |
36 | parser = argparse.ArgumentParser(description=description, epilog=epilog) | |
37 | parser.add_argument( | |
38 | 'files', metavar='FILE', nargs='*', help=_( | |
39 | 'A file containing TAP output. Any directories listed will be ' | |
40 | 'scanned for files to include as TAP files.')) | |
41 | parser.add_argument( | |
42 | '-v', '--verbose', action='store_const', default=1, const=2, | |
43 | help=_('use verbose messages')) | |
44 | ||
45 | # argparse expects the executable to be removed from argv. | |
46 | args = parser.parse_args(argv[1:]) | |
47 | ||
48 | # When no files are provided, the user wants to use a TAP stream on STDIN. | |
49 | # But they probably didn't mean it if there is no pipe connected. | |
50 | # In that case, print the help and exit. | |
51 | if not args.files and sys.stdin.isatty(): | |
52 | sys.exit(parser.print_help()) | |
53 | ||
54 | return args | |
55 | ||
56 | ||
57 | def get_status(result): | |
58 | """Get a return status from the result.""" | |
59 | if result.wasSuccessful(): | |
60 | return 0 | |
61 | else: | |
62 | return 1 |