2b390d189a8ed80ec672c06465336dbbde8399f8
[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         unless(defined($self->{output})) {
58                 print $msg;
59         } else {
60                 $self->{output}.=$msg;
61         }
62 }
63
64 sub start_test($$)
65 {
66         my ($self, $testname) = @_;
67
68         if (defined($self->{prefix})) {
69                 $testname = $self->{prefix}.$testname;
70         }
71
72         if ($self->{strip_ok_output}) {
73                 $self->{output} = "";
74         }
75
76         Subunit::start_test($testname);
77 }
78
79 sub is_failure($)
80 {
81         my ($result) = @_;
82         return $result eq "fail" or $result eq "failure" or $result eq "error";
83 }
84
85 sub end_test($$$$$)
86 {
87         my ($self, $testname, $result, $unexpected, $reason) = @_;
88
89         if (defined($self->{prefix})) {
90                 $testname = $self->{prefix}.$testname;
91         }
92
93         if (is_failure($result) and not $unexpected) {
94                 $result = "xfail";
95                 $self->{xfail_added}++;
96         }
97         my $xfail_reason = find_in_list($self->{expected_failures}, $testname);
98         if (defined($xfail_reason) and is_failure($result)) {
99                 $result = "xfail";
100                 $self->{xfail_added}++;
101                 $reason .= $xfail_reason;
102         }
103
104         if ($self->{strip_ok_output}) {
105                 unless ($result eq "success" or $result eq "xfail" or $result eq "skip") {
106                         print $self->{output}
107                 }
108         }
109         $self->{output} = undef;
110
111         Subunit::end_test($testname, $result, $reason);
112 }
113
114 sub skip_testsuite($;$)
115 {
116         my ($self, $name, $reason) = @_;
117         Subunit::skip_testsuite($name, $reason);
118 }
119
120 sub start_testsuite($;$)
121 {
122         my ($self, $name) = @_;
123         Subunit::start_testsuite($name);
124         $self->{xfail_added} = 0;
125 }
126
127 sub end_testsuite($$;$)
128 {
129         my ($self, $name, $result, $reason) = @_;
130         if ($self->{xfail_added} and is_failure($result)) {
131                 $result = "xfail";
132         }
133                 
134         Subunit::end_testsuite($name, $result, $reason);
135 }
136
137 sub testsuite_count($$)
138 {
139         my ($self, $count) = @_;
140         Subunit::testsuite_count($count);
141 }
142
143 sub new {
144         my ($class, $prefix, $expected_failures, $strip_ok_output) = @_;
145
146         my $self = { 
147                 prefix => $prefix,
148                 expected_failures => $expected_failures,
149                 strip_ok_output => $strip_ok_output,
150                 xfail_added => 0,
151         };
152         bless($self, $class);
153 }
154
155 1;