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