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