subunit.pm: Pass through milliseconds in time reports.
[nivanova/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, $statistics, $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                                         $statistics->{TESTS_ERROR}++;
53                                         $msg_ops->end_test($testname, "error", 1,
54                                                                "reason ($result) interrupted\n");
55                                         return 1;
56                                 }
57                         }
58                         if ($result eq "success" or $result eq "successful") {
59                                 pop(@$open_tests); #FIXME: Check that popped value == $testname 
60                                 $statistics->{TESTS_EXPECTED_OK}++;
61                                 $msg_ops->end_test($testname, "success", 0, $reason);
62                         } elsif ($result eq "xfail" or $result eq "knownfail") {
63                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
64                                 $statistics->{TESTS_EXPECTED_FAIL}++;
65                                 $msg_ops->end_test($testname, "xfail", 0, $reason);
66                                 $expected_fail++;
67                         } elsif ($result eq "failure" or $result eq "fail") {
68                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
69                                 $statistics->{TESTS_UNEXPECTED_FAIL}++;
70                                 $msg_ops->end_test($testname, "failure", 1, $reason);
71                         } elsif ($result eq "skip") {
72                                 $statistics->{TESTS_SKIP}++;
73                                 # Allow tests to be skipped without prior announcement of test
74                                 my $last = pop(@$open_tests);
75                                 if (defined($last) and $last ne $testname) {
76                                         push (@$open_tests, $testname);
77                                 }
78                                 $msg_ops->end_test($testname, "skip", 0, $reason);
79                         } elsif ($result eq "error") {
80                                 $statistics->{TESTS_ERROR}++;
81                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
82                                 $msg_ops->end_test($testname, "error", 1, $reason);
83                         } elsif ($result eq "skip-testsuite") {
84                                 $msg_ops->skip_testsuite($testname);
85                         } elsif ($result eq "testsuite-success") {
86                                 $msg_ops->end_testsuite($testname, "success", $reason);
87                         } elsif ($result eq "testsuite-failure") {
88                                 $msg_ops->end_testsuite($testname, "failure", $reason);
89                         } elsif ($result eq "testsuite-xfail") {
90                                 $msg_ops->end_testsuite($testname, "xfail", $reason);
91                         } elsif ($result eq "testsuite-error") {
92                                 $msg_ops->end_testsuite($testname, "error", $reason);
93                         } 
94                 } elsif (/^testsuite: (.*)\n/) {
95                         $msg_ops->start_testsuite($1);
96                 } else {
97                         $msg_ops->output_msg($_);
98                 }
99         }
100
101         while ($#$open_tests+1 > 0) {
102                 $msg_ops->end_test(pop(@$open_tests), "error", 1,
103                                    "was started but never finished!\n");
104                 $statistics->{TESTS_ERROR}++;
105         }
106
107         # if the Filter module is in use, it will have the right counts
108         if (defined($msg_ops->{total_error})) {
109                 $statistics->{TESTS_ERROR} = $msg_ops->{total_error};
110                 $statistics->{TESTS_UNEXPECTED_FAIL} = $msg_ops->{total_fail};
111                 $statistics->{TESTS_EXPECTED_FAIL} = $msg_ops->{total_xfail};
112         }
113
114         return 1 if $statistics->{TESTS_ERROR} > 0;
115         return 1 if $statistics->{TESTS_UNEXPECTED_FAIL} > 0;
116
117         return 0;
118 }
119
120 sub start_test($)
121 {
122         my ($testname) = @_;
123         print "test: $testname\n";
124 }
125
126 sub end_test($$;$)
127 {
128         my $name = shift;
129         my $result = shift;
130         my $reason = shift;
131         if ($reason) {
132                 print "$result: $name [\n";
133                 print $reason;
134                 if (substr($reason, -1, 1) ne "\n") { print "\n"; }
135                 print "]\n";
136         } else {
137                 print "$result: $name\n";
138         }
139 }
140
141 sub skip_test($;$)
142 {
143         my $name = shift;
144         my $reason = shift;
145         end_test($name, "skip", $reason);
146 }
147
148 sub fail_test($;$)
149 {
150         my $name = shift;
151         my $reason = shift;
152         end_test($name, "fail", $reason);
153 }
154
155 sub success_test($;$)
156 {
157         my $name = shift;
158         my $reason = shift;
159         end_test($name, "success", $reason);
160 }
161
162 sub xfail_test($;$)
163 {
164         my $name = shift;
165         my $reason = shift;
166         end_test($name, "xfail", $reason);
167 }
168
169 sub report_time($)
170 {
171         my ($time) = @_;
172         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
173         $sec = ($time - int($time) + $sec);
174         printf "time: %04d-%02d-%02d %02d:%02d:%02f\n", $year+1900, $mon+1, $mday, $hour, $min, $sec;
175 }
176
177 sub progress_pop()
178 {
179         print "progress: pop\n";
180 }
181
182 sub progress_push()
183 {
184         print "progress: push\n";
185 }
186
187 sub progress($;$)
188 {
189         my ($count, $whence) = @_;
190
191         unless(defined($whence)) {
192                 $whence = "";
193         }
194
195         print "progress: $whence$count\n";
196 }
197
198 # The following are Samba extensions:
199
200 sub start_testsuite($)
201 {
202         my ($name) = @_;
203         print "testsuite: $name\n";
204 }
205
206 sub skip_testsuite($;$)
207 {
208         my ($name, $reason) = @_;
209         if ($reason) {
210                 print "skip-testsuite: $name [\n$reason\n]\n";
211         } else {
212                 print "skip-testsuite: $name\n";
213         }
214 }
215
216 sub end_testsuite($$;$)
217 {
218         my $name = shift;
219         my $result = shift;
220         my $reason = shift;
221         if ($reason) {
222                 print "testsuite-$result: $name [\n";
223                 print "$reason\n";
224                 print "]\n";
225         } else {
226                 print "testsuite-$result: $name\n";
227         }
228 }
229
230 1;