selftest: Properly recognize xfail testsuites.
[ira/wip.git] / selftest / Subunit / Filter.pm
1 #!/usr/bin/perl
2 # Filter a subunit stream
3 # Copyright (C) Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later
5
6 package Subunit::Filter;
7
8 use strict;
9
10 sub read_test_regexes($)
11 {
12         my ($name) = @_;
13         my @ret = ();
14         open(LF, "<$name") or die("unable to read $name: $!");
15         while (<LF>) { 
16                 chomp; 
17                 next if (/^#/);
18                 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
19                         push (@ret, [$1, $4]);
20                 } else {
21                         s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
22                         push (@ret, [$_, undef]); 
23                 }
24         }
25         close(LF);
26         return @ret;
27 }
28
29 sub find_in_list($$)
30 {
31         my ($list, $fullname) = @_;
32
33         foreach (@$list) {
34                 if ($fullname =~ /$$_[0]/) {
35                          return ($$_[1]) if ($$_[1]);
36                          return "";
37                 }
38         }
39
40         return undef;
41 }
42
43 sub control_msg()
44 {
45         # We regenerate control messages, so ignore this
46 }
47
48 sub report_time($$)
49 {
50         my ($self, $time) = @_;
51         Subunit::report_time($time);
52 }
53
54 sub output_msg($$)
55 {
56         my ($self, $msg) = @_;
57         print $msg;
58 }
59
60 sub start_test($$)
61 {
62         my ($self, $testname) = @_;
63
64         if (defined($self->{prefix})) {
65                 $testname = $self->{prefix}.$testname;
66         }
67
68         Subunit::start_test($testname);
69 }
70
71 sub end_test($$$$$)
72 {
73         my ($self, $testname, $result, $unexpected, $reason) = @_;
74
75         if (defined($self->{prefix})) {
76                 $testname = $self->{prefix}.$testname;
77         }
78
79         if (($result eq "fail" or $result eq "failure") and not $unexpected) {
80                 $result = "xfail";
81                 $self->{xfail_added}++;
82         }
83         my $xfail_reason = find_in_list($self->{expected_failures}, $testname);
84         if (defined($xfail_reason) and ($result eq "fail" or $result eq "failure")) {
85                 $result = "xfail";
86                 $self->{xfail_added}++;
87                 $reason .= $xfail_reason;
88         }
89
90         Subunit::end_test($testname, $result, $reason);
91 }
92
93 sub skip_testsuite($;$)
94 {
95         Subunit::skip_testsuite(@_);
96 }
97
98 sub start_testsuite($;$)
99 {
100         my ($self, $name) = @_;
101         Subunit::start_testsuite($name);
102         $self->{xfail_added} = 0;
103 }
104
105 sub end_testsuite($$;$)
106 {
107         my ($self, $name, $result, $reason) = @_;
108         if ($self->{xfail_added} and ($result eq "fail" or $result eq "failure")) {
109                 $result = "xfail";
110         }
111                 
112         Subunit::end_testsuite($name, $result, $reason);
113 }
114
115 sub testsuite_count($$)
116 {
117         my ($self, $count) = @_;
118         Subunit::testsuite_count($count);
119 }
120
121 sub new {
122         my ($class, $prefix, $expected_failures) = @_;
123
124         my $self = { 
125                 prefix => $prefix,
126                 expected_failures => $expected_failures,
127                 xfail_added => 0,
128         };
129         bless($self, $class);
130 }
131
132 1;