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