bea795d2bdd801f546c41e1d2b6d6dbcefd79ccc
[metze/samba/wip.git] / lib / subunit / filters / subunit2junitxml
1 #!/usr/bin/env python
2 #  subunit: extensions to python unittest to get test results from subprocesses.
3 #  Copyright (C) 2009  Robert Collins <robertc@robertcollins.net>
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
17 """Filter a subunit stream to get aggregate statistics."""
18
19 from optparse import OptionParser
20 import sys
21 import unittest
22
23 from subunit import DiscardStream, ProtocolTestCase
24 try:
25     from junitxml import JUnitXmlResult
26 except ImportError:
27     sys.stderr.write("python-junitxml (https://launchpad.net/pyjunitxml or "
28         "http://pypi.python.org/pypi/junitxml) is required for this filter.")
29     raise
30
31 parser = OptionParser(description=__doc__)
32 parser.add_option("--no-passthrough", action="store_true",
33     help="Hide all non subunit input.", default=False, dest="no_passthrough")
34 parser.add_option("-o", "--output-to",
35     help="Output the XML to this path rather than stdout.")
36 parser.add_option("-f", "--forward", action="store_true", default=False,
37     help="Forward subunit stream on stdout.")
38 (options, args) = parser.parse_args()
39 if options.output_to is None:
40     output_to = sys.stdout
41 else:
42     output_to = file(options.output_to, 'wb')
43 try:
44     result = JUnitXmlResult(output_to)
45     if options.no_passthrough:
46         passthrough_stream = DiscardStream()
47     else:
48         passthrough_stream = None
49     if options.forward:
50         forward_stream = sys.stdout
51     else:
52         forward_stream = None
53     test = ProtocolTestCase(sys.stdin, passthrough=passthrough_stream,
54        forward=forward_stream)
55     result.startTestRun()
56     test.run(result)
57     result.stopTestRun()
58 finally:
59     if options.output_to is not None:
60         output_to.close()
61 if result.wasSuccessful():
62     exit_code = 0
63 else:
64     exit_code = 1
65 sys.exit(exit_code)