summaryrefslogtreecommitdiff
path: root/selftest/filter-subunit.pl
blob: 9a2c6f556c70b4d43e752cce952f033c2775696e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl
# Filter a subunit stream
# Copyright (C) Jelmer Vernooij <jelmer@samba.org>
# Published under the GNU GPL, v3 or later

=pod

=head1 NAME

filter-subunit - Filter a subunit stream

=head1 SYNOPSIS

filter-subunit --help

filter-subunit --prefix=PREFIX --known-failures=FILE < in-stream > out-stream

=head1 DESCRIPTION

Simple Subunit stream filter that will change failures to known failures 
based on a list of regular expressions.

=head1 OPTIONS

=over 4

=item I<--prefix>

Add the specified prefix to all test names.

=item I<--expected-failures>

Specify a file containing a list of tests that are expected to fail. Failures 
for these tests will be counted as successes, successes will be counted as 
failures.

The format for the file is, one entry per line:

TESTSUITE-NAME.TEST-NAME

The reason for a test can also be specified, by adding a hash sign (#) and the reason 
after the test name.

=head1 LICENSE

selftest is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.


=head1 AUTHOR

Jelmer Vernooij

=cut


use Getopt::Long;
use strict;
use FindBin qw($RealBin $Script);
use lib "$RealBin";
use Subunit qw(parse_results);

my $opt_expected_failures = undef;
my $opt_help = 0;
my $opt_prefix = undef;
my @expected_failures = ();

my $result = GetOptions(
		'expected-failures=s' => \$opt_expected_failures,
		'prefix=s' => \$opt_prefix,
		'help' => \$opt_help,
	);
exit(1) if (not $result);

if ($opt_help) {
	print "Usage: filter-subunit [--prefix=PREFIX] [--expected-failures=FILE]... < instream > outstream\n";
	exit(0);
}

sub read_test_regexes($)
{
	my ($name) = @_;
	my @ret = ();
	open(LF, "<$name") or die("unable to read $name: $!");
	while (<LF>) { 
		chomp; 
		next if (/^#/);
		if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
			push (@ret, [$1, $4]);
		} else {
			s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
			push (@ret, [$_, undef]); 
		}
	}
	close(LF);
	return @ret;
}

if (defined($opt_expected_failures)) {
	@expected_failures = read_test_regexes($opt_expected_failures);
}

sub find_in_list($$)
{
	my ($list, $fullname) = @_;

	foreach (@$list) {
		if ($fullname =~ /$$_[0]/) {
			 return ($$_[1]) if ($$_[1]);
			 return "NO REASON SPECIFIED";
		}
	}

	return undef;
}

sub expecting_failure($)
{
	my ($name) = @_;
	return find_in_list(\@expected_failures, $name);
}

my $statistics = {
	SUITES_FAIL => 0,

	TESTS_UNEXPECTED_OK => 0,
	TESTS_EXPECTED_OK => 0,
	TESTS_UNEXPECTED_FAIL => 0,
	TESTS_EXPECTED_FAIL => 0,
	TESTS_ERROR => 0,
	TESTS_SKIP => 0,
};

sub control_msg()
{
	# We regenerate control messages, so ignore this
}

sub report_time($$)
{
	my ($self, $time) = @_;
	Subunit::report_time($time);
}

sub output_msg($$)
{
	my ($self, $msg) = @_;
	print $msg;
}

sub start_test($$$)
{
	my ($self, $parents, $testname) = @_;

	if (defined($opt_prefix)) {
		$testname = $opt_prefix.$testname;
	}

	Subunit::start_test($testname);
}

sub end_test($$$$$)
{
	my ($self, $parents, $testname, $result, $unexpected, $reason) = @_;

	if (defined($opt_prefix)) {
		$testname = $opt_prefix.$testname;
	}

	if (($result eq "fail" or $result eq "failure") and not $unexpected) { $result = "xfail"; }
	if (expecting_failure($testname) and ($result eq "fail" or $result eq "failure")) {
		$result = "xfail";
	}

	Subunit::end_test($testname, $result, $reason);
}

my $msg_ops = {};
bless $msg_ops;

parse_results($msg_ops, $statistics, *STDIN, []);

0;