ctdb-common: Add command line processing abstraction
[Samba.git] / selftest / filter-subunit
blobd71112296f2a8d74f8f3100b7370f57c2675cf6e
1 #!/usr/bin/env python
2 # Filter a subunit stream
3 # Copyright (C) 2009-2011 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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/>.
18 # NOTE: This script is a hack, meant as a placeholder until we can migrate
19 # to upstream subunit's filtering tools.
21 import optparse
22 import sys
23 import signal
25 sys.path.insert(0, "bin/python")
27 import subunithelper
29 parser = optparse.OptionParser("filter-subunit [options] < instream > outstream")
30 parser.add_option("--expected-failures", type="string", action="append",
31 help=("File or directory containing lists of regexes matching tests "
32 "to consider known failures"))
33 parser.add_option("--flapping", type="string", action="append",
34 help=("File or directory containing lists of flapping tests, "
35 "of which to ignore results."))
36 parser.add_option("--strip-passed-output", action="store_true",
37 help="Whether to strip output from tests that passed")
38 parser.add_option("--fail-immediately", action="store_true",
39 help="Whether to stop on the first error", default=False)
40 parser.add_option("--prefix", type="string", default='',
41 help="Add prefix to all test names")
42 parser.add_option("--suffix", type="string", default='',
43 help="Add suffix to all test names")
44 parser.add_option("--fail-on-empty", default=False,
45 action="store_true", help="Fail if there was no subunit output")
46 parser.add_option("--list", default=False,
47 action="store_true", help="Operate in list mode")
48 parser.add_option("--perf-test-output", default=False,
49 action="store_true", help="orientate output for performance measurement")
50 opts, args = parser.parse_args()
52 if opts.list:
53 for l in sys.stdin:
54 sys.stdout.write("%s%s%s\n" % (opts.prefix, l.rstrip(), opts.suffix))
55 sys.exit(0)
57 if opts.perf_test_output:
58 bad_options = []
59 for bad_opt in ('fail_immediately', 'strip_passed_output',
60 'flapping', 'expected_failures'):
61 if getattr(opts, bad_opt):
62 bad_options.append(bad_opt)
63 if bad_options:
64 print >>sys.stderr, ("--perf-test-output is incompatible with --%s" %
65 (', --'.join(x.replace('_', '-')
66 for x in bad_options)))
67 sys.exit(1)
69 if opts.expected_failures:
70 expected_failures = subunithelper.read_test_regexes(*opts.expected_failures)
71 else:
72 expected_failures = {}
75 if opts.flapping:
76 flapping = subunithelper.read_test_regexes(*opts.flapping)
77 else:
78 flapping = {}
80 statistics = {
81 'TESTS_UNEXPECTED_OK': 0,
82 'TESTS_EXPECTED_OK': 0,
83 'TESTS_UNEXPECTED_FAIL': 0,
84 'TESTS_EXPECTED_FAIL': 0,
85 'TESTS_ERROR': 0,
86 'TESTS_SKIP': 0,
89 def handle_sigint(sig, stack):
90 sys.exit(0)
91 signal.signal(signal.SIGINT, handle_sigint)
93 out = subunithelper.SubunitOps(sys.stdout)
95 if opts.perf_test_output:
96 msg_ops = subunithelper.PerfFilterOps(out, opts.prefix, opts.suffix)
97 else:
98 msg_ops = subunithelper.FilterOps(out, opts.prefix, opts.suffix,
99 expected_failures,
100 opts.strip_passed_output,
101 fail_immediately=opts.fail_immediately,
102 flapping=flapping)
104 try:
105 ret = subunithelper.parse_results(msg_ops, statistics, sys.stdin)
106 except subunithelper.ImmediateFail:
107 sys.stdout.flush()
108 sys.exit(1)
110 if opts.fail_on_empty and not msg_ops.seen_output:
111 sys.exit(1)
112 else:
113 sys.exit(ret)