Merge branch 'master' of ssh://git.samba.org/data/git/samba
[nivanova/samba-autobuild/.git] / lib / subunit / filters / subunit-ls
1 #!/usr/bin/env python
2 #  subunit: extensions to python unittest to get test results from subprocesses.
3 #  Copyright (C) 2008  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 """List tests in a subunit stream."""
18
19 from optparse import OptionParser
20 import sys
21 import unittest
22
23 from subunit import DiscardStream, ProtocolTestCase
24
25 class TestIdPrintingResult(unittest.TestResult):
26
27     def __init__(self, stream, show_times=False):
28         """Create a FilterResult object outputting to stream."""
29         unittest.TestResult.__init__(self)
30         self._stream = stream
31         self.failed_tests = 0
32         self.__time = 0
33         self.show_times = show_times
34         self._test = None
35         self._test_duration = 0
36         
37     def addError(self, test, err):
38         self.failed_tests += 1
39         self._test = test
40
41     def addFailure(self, test, err):
42         self.failed_tests += 1
43         self._test = test
44
45     def addSuccess(self, test):
46         self._test = test
47
48     def reportTest(self, test, duration):
49         if self.show_times:
50             seconds = duration.seconds
51             seconds += duration.days * 3600 * 24
52             seconds += duration.microseconds / 1000000.0
53             self._stream.write(test.id() + ' %0.3f\n' % seconds)
54         else:
55             self._stream.write(test.id() + '\n')
56
57     def startTest(self, test):
58         self._start_time = self._time()
59
60     def stopTest(self, test):
61         test_duration = self._time() - self._start_time
62         self.reportTest(self._test, test_duration)
63
64     def time(self, time):
65         self.__time = time
66
67     def _time(self):
68         return self.__time
69
70     def wasSuccessful(self):
71         "Tells whether or not this result was a success"
72         return self.failed_tests == 0
73
74
75 parser = OptionParser(description=__doc__)
76 parser.add_option("--times", action="store_true",
77     help="list the time each test took (requires a timestamped stream)",
78         default=False)
79 parser.add_option("--no-passthrough", action="store_true",
80     help="Hide all non subunit input.", default=False, dest="no_passthrough")
81 (options, args) = parser.parse_args()
82 result = TestIdPrintingResult(sys.stdout, options.times)
83 if options.no_passthrough:
84     passthrough_stream = DiscardStream()
85 else:
86     passthrough_stream = None
87 test = ProtocolTestCase(sys.stdin, passthrough=passthrough_stream)
88 test.run(result)
89 if result.wasSuccessful():
90     exit_code = 0
91 else:
92     exit_code = 1
93 sys.exit(exit_code)