subunit.pm: Simplify subunit handling in perl.
[ambi/samba-autobuild/.git] / selftest / Subunit.pm
1 # Perl module for parsing and generating the Subunit protocol
2 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package Subunit;
18 use POSIX;
19
20 require Exporter;
21 @ISA = qw(Exporter);
22 @EXPORT_OK = qw(parse_results);
23
24 use strict;
25
26 sub parse_results($$)
27 {
28         my ($msg_ops, $fh) = @_;
29         my $expected_fail = 0;
30         my $open_tests = [];
31
32         while(<$fh>) {
33                 if (/^test: (.+)\n/) {
34                         $msg_ops->control_msg($_);
35                         $msg_ops->start_test($1);
36                         push (@$open_tests, $1);
37                 } elsif (/^(success|successful|failure|fail|skip|knownfail|error|xfail|skip-testsuite|testsuite-failure|testsuite-xfail|testsuite-success|testsuite-error): (.*?)( \[)?([ \t]*)( multipart)?\n/) {
38                         $msg_ops->control_msg($_);
39                         my $result = $1;
40                         my $testname = $2;
41                         my $reason = undef;
42                         if ($3) {
43                                 $reason = "";
44                                 # reason may be specified in next lines
45                                 my $terminated = 0;
46                                 while(<$fh>) {
47                                         $msg_ops->control_msg($_);
48                                         if ($_ eq "]\n") { $terminated = 1; last; } else { $reason .= $_; }
49                                 }
50
51                                 unless ($terminated) {
52                                         $msg_ops->end_test($testname, "error", 1,
53                                                                "reason ($result) interrupted\n");
54                                         return 1;
55                                 }
56                         }
57                         if ($result eq "success" or $result eq "successful") {
58                                 pop(@$open_tests); #FIXME: Check that popped value == $testname 
59                                 $msg_ops->end_test($testname, "success", 0, $reason);
60                         } elsif ($result eq "xfail" or $result eq "knownfail") {
61                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
62                                 $msg_ops->end_test($testname, "xfail", 0, $reason);
63                                 $expected_fail++;
64                         } elsif ($result eq "failure" or $result eq "fail") {
65                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
66                                 $msg_ops->end_test($testname, "failure", 1, $reason);
67                         } elsif ($result eq "skip") {
68                                 # Allow tests to be skipped without prior announcement of test
69                                 my $last = pop(@$open_tests);
70                                 if (defined($last) and $last ne $testname) {
71                                         push (@$open_tests, $testname);
72                                 }
73                                 $msg_ops->end_test($testname, "skip", 0, $reason);
74                         } elsif ($result eq "error") {
75                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
76                                 $msg_ops->end_test($testname, "error", 1, $reason);
77                         } elsif ($result eq "skip-testsuite") {
78                                 $msg_ops->skip_testsuite($testname);
79                         } elsif ($result eq "testsuite-success") {
80                                 $msg_ops->end_testsuite($testname, "success", $reason);
81                         } elsif ($result eq "testsuite-failure") {
82                                 $msg_ops->end_testsuite($testname, "failure", $reason);
83                         } elsif ($result eq "testsuite-xfail") {
84                                 $msg_ops->end_testsuite($testname, "xfail", $reason);
85                         } elsif ($result eq "testsuite-error") {
86                                 $msg_ops->end_testsuite($testname, "error", $reason);
87                         }
88                 } elsif (/^testsuite: (.*)\n/) {
89                         $msg_ops->start_testsuite($1);
90                 } else {
91                         $msg_ops->output_msg($_);
92                 }
93         }
94
95         while ($#$open_tests+1 > 0) {
96                 $msg_ops->end_test(pop(@$open_tests), "error", 1,
97                                    "was started but never finished!\n");
98         }
99 }
100
101 sub start_test($)
102 {
103         my ($testname) = @_;
104         print "test: $testname\n";
105 }
106
107 sub end_test($$;$)
108 {
109         my $name = shift;
110         my $result = shift;
111         my $reason = shift;
112         if ($reason) {
113                 print "$result: $name [\n";
114                 print $reason;
115                 if (substr($reason, -1, 1) ne "\n") { print "\n"; }
116                 print "]\n";
117         } else {
118                 print "$result: $name\n";
119         }
120 }
121
122 sub skip_test($;$)
123 {
124         my $name = shift;
125         my $reason = shift;
126         end_test($name, "skip", $reason);
127 }
128
129 sub fail_test($;$)
130 {
131         my $name = shift;
132         my $reason = shift;
133         end_test($name, "fail", $reason);
134 }
135
136 sub success_test($;$)
137 {
138         my $name = shift;
139         my $reason = shift;
140         end_test($name, "success", $reason);
141 }
142
143 sub xfail_test($;$)
144 {
145         my $name = shift;
146         my $reason = shift;
147         end_test($name, "xfail", $reason);
148 }
149
150 sub report_time($)
151 {
152         my ($time) = @_;
153         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
154         $sec = ($time - int($time) + $sec);
155         my $msg = sprintf("%f", $sec);
156         if (substr($msg, 1, 1) eq ".") {
157                 $msg = "0" . $msg;
158         }
159         printf "time: %04d-%02d-%02d %02d:%02d:%s\n", $year+1900, $mon+1, $mday, $hour, $min, $msg;
160 }
161
162 sub progress_pop()
163 {
164         print "progress: pop\n";
165 }
166
167 sub progress_push()
168 {
169         print "progress: push\n";
170 }
171
172 sub progress($;$)
173 {
174         my ($count, $whence) = @_;
175
176         unless(defined($whence)) {
177                 $whence = "";
178         }
179
180         print "progress: $whence$count\n";
181 }
182
183 # The following are Samba extensions:
184
185 sub start_testsuite($)
186 {
187         my ($name) = @_;
188         print "testsuite: $name\n";
189 }
190
191 sub skip_testsuite($;$)
192 {
193         my ($name, $reason) = @_;
194         if ($reason) {
195                 print "skip-testsuite: $name [\n$reason\n]\n";
196         } else {
197                 print "skip-testsuite: $name\n";
198         }
199 }
200
201 sub end_testsuite($$;$)
202 {
203         my $name = shift;
204         my $result = shift;
205         my $reason = shift;
206         if ($reason) {
207                 print "testsuite-$result: $name [\n";
208                 print "$reason\n";
209                 print "]\n";
210         } else {
211                 print "testsuite-$result: $name\n";
212         }
213 }
214
215 1;