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