filter-subunit 3.99 KB
Newer Older
1 2
#!/usr/bin/env python
# Filter a subunit stream
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (C) 2009-2011 Jelmer Vernooij <jelmer@samba.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# NOTE: This script is a hack, meant as a placeholder until we can migrate
# to upstream subunit's filtering tools.
20 21 22 23 24

import optparse
import sys
import signal

25
sys.path.insert(0, "bin/python")
26

27 28
import subunithelper

29
parser = optparse.OptionParser("filter-subunit [options] < instream > outstream")
30 31 32 33 34 35
parser.add_option("--expected-failures", type="string", action="append",
    help=("File or directory containing lists of regexes matching tests "
          "to consider known failures"))
parser.add_option("--flapping", type="string", action="append",
    help=("File or directory containing lists of flapping tests, "
          "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 49
parser.add_option("--perf-test-output", default=False,
    action="store_true", help="orientate output for performance measurement")
50 51
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)
56

57 58 59 60 61 62 63 64 65 66 67 68
if opts.perf_test_output:
    bad_options = []
    for bad_opt in ('fail_immediately', 'strip_passed_output',
                    'flapping', 'expected_failures'):
        if getattr(opts, bad_opt):
            bad_options.append(bad_opt)
    if bad_options:
        print >>sys.stderr, ("--perf-test-output is incompatible with --%s" %
                             (', --'.join(x.replace('_', '-')
                                          for x in bad_options)))
        sys.exit(1)

69
if opts.expected_failures:
70
    expected_failures = subunithelper.read_test_regexes(*opts.expected_failures)
71
else:
72
    expected_failures = {}
73

74 75

if opts.flapping:
76
    flapping = subunithelper.read_test_regexes(*opts.flapping)
77
else:
78
    flapping = {}
79

80
statistics = {
81 82 83 84 85 86
    'TESTS_UNEXPECTED_OK': 0,
    'TESTS_EXPECTED_OK': 0,
    'TESTS_UNEXPECTED_FAIL': 0,
    'TESTS_EXPECTED_FAIL': 0,
    'TESTS_ERROR': 0,
    'TESTS_SKIP': 0,
87 88 89
}

def handle_sigint(sig, stack):
90
    sys.exit(0)
91 92
signal.signal(signal.SIGINT, handle_sigint)

93
out = subunithelper.SubunitOps(sys.stdout)
94 95 96 97 98 99 100 101 102

if opts.perf_test_output:
    msg_ops = subunithelper.PerfFilterOps(out, opts.prefix, opts.suffix)
else:
    msg_ops = subunithelper.FilterOps(out, opts.prefix, opts.suffix,
                                      expected_failures,
                                      opts.strip_passed_output,
                                      fail_immediately=opts.fail_immediately,
                                      flapping=flapping)
103

104
try:
105
    ret = subunithelper.parse_results(msg_ops, statistics, sys.stdin)
106
except subunithelper.ImmediateFail:
107 108
    sys.stdout.flush()
    sys.exit(1)
109 110

if opts.fail_on_empty and not msg_ops.seen_output:
111
    sys.exit(1)
112
else:
113
    sys.exit(ret)