Use Black stable to format python code
[babeltrace.git] / tests / utils / python / tap / main.py
CommitLineData
0235b0db
MJ
1# SPDX-License-Identifier: BSD-2-Clause
2#
b85894a3
MJ
3# Copyright (c) 2016, Matt Layman
4
5import argparse
6import sys
7import unittest
8
9from tap.i18n import _
10from tap.loader import Loader
11
12
13def 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
23def 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
33def 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, '
768f9bcb
MJ
37 'tappy will read a TAP stream from STDIN.'
38 )
b85894a3
MJ
39 parser = argparse.ArgumentParser(description=description, epilog=epilog)
40 parser.add_argument(
768f9bcb
MJ
41 'files',
42 metavar='FILE',
43 nargs='*',
44 help=_(
b85894a3 45 'A file containing TAP output. Any directories listed will be '
768f9bcb
MJ
46 'scanned for files to include as TAP files.'
47 ),
48 )
b85894a3 49 parser.add_argument(
768f9bcb
MJ
50 '-v',
51 '--verbose',
52 action='store_const',
53 default=1,
54 const=2,
55 help=_('use verbose messages'),
56 )
b85894a3
MJ
57
58 # argparse expects the executable to be removed from argv.
59 args = parser.parse_args(argv[1:])
60
61 # When no files are provided, the user wants to use a TAP stream on STDIN.
62 # But they probably didn't mean it if there is no pipe connected.
63 # In that case, print the help and exit.
64 if not args.files and sys.stdin.isatty():
65 sys.exit(parser.print_help())
66
67 return args
68
69
70def get_status(result):
71 """Get a return status from the result."""
72 if result.wasSuccessful():
73 return 0
74 else:
75 return 1
This page took 0.056016 seconds and 4 git commands to generate.