2ce9584c2a5305ac41f0807d3131215ce28da338
[obnox/samba/samba-obnox.git] / selftest / filter-subunit
1 #!/usr/bin/env python
2 # Filter a subunit stream
3 # Copyright (C) 2009-2011 Jelmer Vernooij <jelmer@samba.org>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # NOTE: This script is a hack, meant as a placeholder until we can migrate
19 # to upstream subunit's filtering tools.
20
21 import optparse
22 import os
23 import sys
24 import signal
25
26 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../lib/subunit/python"))
27 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../lib/testtools"))
28 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../lib/mimeparse"))
29 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../lib/extras"))
30
31 import subunithelper
32
33 parser = optparse.OptionParser("filter-subunit [options] < instream > outstream")
34 parser.add_option("--expected-failures", type="string",
35     help="File containing list of regexes matching tests to consider known "
36          "failures")
37 parser.add_option("--flapping", type="string",
38     help="File containing list of flapping tests, of which to ignore results.")
39 parser.add_option("--strip-passed-output", action="store_true",
40     help="Whether to strip output from tests that passed")
41 parser.add_option("--fail-immediately", action="store_true",
42     help="Whether to stop on the first error", default=False)
43 parser.add_option("--prefix", type="string",
44     help="Add prefix to all test names")
45 parser.add_option("--suffix", type="string",
46     help="Add suffix to all test names")
47 parser.add_option("--fail-on-empty", default=False,
48     action="store_true", help="Fail if there was no subunit output")
49 parser.add_option("--list", default=False,
50     action="store_true", help="Operate in list mode")
51 opts, args = parser.parse_args()
52
53 if opts.list:
54     prefix = opts.prefix
55     suffix = opts.suffix
56     if not prefix:
57         prefix = ""
58     if not suffix:
59         suffix = ""
60     for l in sys.stdin:
61          sys.stdout.write("%s%s%s\n" % (prefix, l.rstrip(), suffix))
62     sys.exit(0)
63
64 if opts.expected_failures:
65     expected_failures = subunithelper.read_test_regexes(opts.expected_failures)
66 else:
67     expected_failures = {}
68
69
70 if opts.flapping:
71     flapping = subunithelper.read_test_regexes(opts.flapping)
72 else:
73     flapping = {}
74
75 statistics = {
76     'TESTS_UNEXPECTED_OK': 0,
77     'TESTS_EXPECTED_OK': 0,
78     'TESTS_UNEXPECTED_FAIL': 0,
79     'TESTS_EXPECTED_FAIL': 0,
80     'TESTS_ERROR': 0,
81     'TESTS_SKIP': 0,
82 }
83
84 def handle_sigint(sig, stack):
85     sys.exit(0)
86 signal.signal(signal.SIGINT, handle_sigint)
87
88 out = subunithelper.SubunitOps(sys.stdout)
89 msg_ops = subunithelper.FilterOps(out, opts.prefix, opts.suffix, expected_failures,
90     opts.strip_passed_output,
91     fail_immediately=opts.fail_immediately,
92     flapping=flapping)
93
94 try:
95     ret = subunithelper.parse_results(msg_ops, statistics, sys.stdin)
96 except subunithelper.ImmediateFail:
97     sys.stdout.flush()
98     sys.exit(1)
99
100 if opts.fail_on_empty and not msg_ops.seen_output:
101     sys.exit(1)
102 else:
103     sys.exit(ret)