9b467c11231e8079f20ae85ac0c507397e7886fb
[third_party/subunit] / python / subunit / _output.py
1 #!/usr/bin/env python
2 #  subunit: extensions to python unittest to get test results from subprocesses.
3 #  Copyright (C) 2013  Thomi Richards <thomi.richards@canonical.com>
4 #
5 #  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 #  license at the users choice. A copy of both licenses are available in the
7 #  project source as Apache-2.0 and BSD. You may not use this file except in
8 #  compliance with one of these two licences.
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13 #  license you chose for the specific language governing permissions and
14 #  limitations under that license.
15
16 from argparse import ArgumentParser
17 import datetime
18 from sys import stdout
19
20 from subunit.v2 import StreamResultToBytes
21
22 def output_main():
23     args = parse_arguments()
24     output = get_output_stream_writer()
25     generate_bytestream(args, output)
26
27     return 0
28
29
30 def parse_arguments(args=None):
31     """Parse arguments from the command line.
32
33     If specified, args must be a list of strings, similar to sys.argv[1:].
34
35     """
36     parser = ArgumentParser(
37         prog='subunit-output',
38         description="A tool to generate a subunit result byte-stream",
39     )
40
41     common_args = ArgumentParser(add_help=False)
42     common_args.add_argument("test_id", help="""A string that uniquely
43         identifies this test.""")
44     sub_parsers = parser.add_subparsers(dest="action")
45
46     parser_start = sub_parsers.add_parser("start", help="Start a test.",
47         parents=[common_args])
48
49     parser_pass = sub_parsers.add_parser("pass", help="Pass a test.",
50         parents=[common_args])
51
52     parser_fail = sub_parsers.add_parser("fail", help="Fail a test.",
53         parents=[common_args])
54
55     parser_skip = sub_parsers.add_parser("skip", help="Skip a test.",
56         parents=[common_args])
57
58     return parser.parse_args(args)
59
60
61 def translate_command_name(command_name):
62     """Turn the friendly command names we show users on the command line into
63     something subunit understands.
64
65     """
66     return {
67         'start': 'inprogress',
68         'pass': 'success',
69     }.get(command_name, command_name)
70
71
72 def get_output_stream_writer():
73     return StreamResultToBytes(stdout)
74
75
76 def generate_bytestream(args, output_writer):
77     output_writer.startTestRun()
78     output_writer.status(
79         test_id=args.test_id,
80         test_status=translate_command_name(args.action),
81         timestamp=create_timestamp()
82         )
83     output_writer.stopTestRun()
84
85
86 _ZERO = datetime.timedelta(0)
87
88
89 class UTC(datetime.tzinfo):
90     """UTC"""
91     def utcoffset(self, dt):
92         return _ZERO
93     def tzname(self, dt):
94         return "UTC"
95     def dst(self, dt):
96         return _ZERO
97
98
99 utc = UTC()
100
101
102 def create_timestamp():
103     return datetime.datetime.now(utc)