443baeb7ddb8d70944e16318c2e5177274331977
[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         my ($self, $name, $reason) = @_;
96         Subunit::skip_testsuite($name, $reason);
97 }
98
99 sub start_testsuite($;$)
100 {
101         my ($self, $name) = @_;
102         Subunit::start_testsuite($name);
103         $self->{xfail_added} = 0;
104 }
105
106 sub end_testsuite($$;$)
107 {
108         my ($self, $name, $result, $reason) = @_;
109         if ($self->{xfail_added} and ($result eq "fail" or $result eq "failure")) {
110                 $result = "xfail";
111         }
112                 
113         Subunit::end_testsuite($name, $result, $reason);
114 }
115
116 sub testsuite_count($$)
117 {
118         my ($self, $count) = @_;
119         Subunit::testsuite_count($count);
120 }
121
122 sub new {
123         my ($class, $prefix, $expected_failures) = @_;
124
125         my $self = { 
126                 prefix => $prefix,
127                 expected_failures => $expected_failures,
128                 xfail_added => 0,
129         };
130         bless($self, $class);
131 }
132
133 1;