eae1e7aaac27f9b9057d24e45a09cab871ef1919
[sfrench/samba-autobuild/.git] / selftest / output / plain.pm
1 #!/usr/bin/perl
2 # Plain text output for selftest
3 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 package output::plain;
18 use Exporter;
19 @ISA = qw(Exporter);
20
21 use FindBin qw($RealBin);
22 use lib "$RealBin/..";
23
24 use strict;
25
26 sub new($$$$$$$) {
27         my ($class, $summaryfile, $verbose, $immediate, $statistics, $totaltests) = @_;
28         my $self = { 
29                 verbose => $verbose, 
30                 immediate => $immediate, 
31                 statistics => $statistics,
32                 start_time => undef,
33                 test_output => {},
34                 suitesfailed => [],
35                 suites_ok => 0,
36                 skips => {},
37                 summaryfile => $summaryfile,
38                 index => 0,
39                 totalsuites => $totaltests,
40         };
41         bless($self, $class);
42 }
43
44 sub testsuite_count($$)
45 {
46         my ($self, $count) = @_;
47         $self->{totalsuites} = $count;
48 }
49
50 sub report_time($$)
51 {
52         my ($self, $time) = @_;
53         unless ($self->{start_time}) {
54                 $self->{start_time} = $time;
55         }
56         $self->{last_time} = $time;
57 }
58
59 sub output_msg($$);
60
61 sub start_testsuite($$)
62 {
63         my ($self, $name) = @_;
64
65         $self->{index}++;
66         $self->{NAME} = $name;
67         $self->{START_TIME} = $self->{last_time};
68
69         my $duration = $self->{START_TIME} - $self->{start_time};
70
71         $self->{test_output}->{$name} = "" unless($self->{verbose});
72
73         my $out = "";
74         $out .= "[$self->{index}";
75         if ($self->{totalsuites}) {
76                 $out .= "/$self->{totalsuites}";
77         }
78         $out.= " in ".$duration."s";
79         $out .= sprintf(", %d errors", ($#{$self->{suitesfailed}}+1)) if ($#{$self->{suitesfailed}} > -1);
80         $out .= "] $name"; 
81         if ($self->{immediate}) {
82                 print "$out\n";
83         } else {
84                 print "$out: ";
85         }
86 }
87
88 sub output_msg($$)
89 {
90         my ($self, $output) = @_;
91
92         if ($self->{verbose}) {
93                 require FileHandle;
94                 print $output;
95                 STDOUT->flush();
96         } elsif (defined($self->{NAME})) {
97                 $self->{test_output}->{$self->{NAME}} .= $output;
98         } else {
99                 print $output;
100         }
101 }
102
103 sub control_msg($$)
104 {
105         my ($self, $output) = @_;
106
107         #$self->output_msg($output);
108 }
109
110 sub end_testsuite($$$$)
111 {
112         my ($self, $name, $result, $reason) = @_;
113         my $out = "";
114         my $unexpected = 0;
115
116         if (not defined($self->{test_output}->{$name})) {
117                 print "no output for name[$name]\n";
118         }
119
120         if ($result eq "success" or $result eq "xfail") {
121                 $self->{suites_ok}++;
122         } else {
123                 $self->output_msg("ERROR: Testsuite[$name]\nREASON: $reason\n");
124                 push (@{$self->{suitesfailed}}, $name);
125                 if ($self->{immediate} and not $self->{verbose}) {
126                         $out .= $self->{test_output}->{$name};
127                 }
128                 $unexpected = 1;
129         }
130
131         if (not $self->{immediate}) {
132                 unless($unexpected) {
133                         $out .= " ok\n";
134                 } else {
135                         $out .= " " . uc($result) . "\n";
136                 }
137         }
138
139         print $out;
140 }
141
142 sub start_test($$$)
143 {
144         my ($self, $testname) = @_;
145 }
146
147 sub end_test($$$$$)
148 {
149         my ($self, $testname, $result, $unexpected, $reason) = @_;
150         
151         my $append = "";
152
153         unless ($unexpected) {
154                 $self->{test_output}->{$self->{NAME}} = "";
155                 if (not $self->{immediate}) {
156                         if ($result eq "failure") { print "f"; }
157                         elsif ($result eq "xfail") { print "X"; }
158                         elsif ($result eq "skip") { print "s"; }
159                         elsif ($result eq "success") { print "."; }
160                         else { print "?($result)"; }
161                 }
162                 return;
163         }
164
165         chomp $reason;
166
167         $append = "UNEXPECTED($result): $testname\nREASON: $reason\n";
168
169         $self->{test_output}->{$self->{NAME}} .= $append;
170
171         if ($self->{immediate} and not $self->{verbose}) {
172                 print $self->{test_output}->{$self->{NAME}};
173                 $self->{test_output}->{$self->{NAME}} = "";
174         }
175
176         if (not $self->{immediate}) {
177                 if ($result eq "error") { print "E"; } 
178                 elsif ($result eq "failure") { print "F"; }
179                 elsif ($result eq "success") { print "S"; }
180                 else { print "?"; }
181         }
182 }
183
184 sub summary($)
185 {
186         my ($self) = @_;
187
188         open(SUMMARY, ">$self->{summaryfile}");
189
190         if ($#{$self->{suitesfailed}} > -1) {
191                 print SUMMARY "= Failed tests =\n";
192
193                 foreach (@{$self->{suitesfailed}}) {
194                         print SUMMARY "== $_ ==\n";
195                         print SUMMARY $self->{test_output}->{$_}."\n\n";
196                 }
197
198                 print SUMMARY "\n";
199         }
200
201         if (not $self->{immediate} and not $self->{verbose}) {
202                 foreach (@{$self->{suitesfailed}}) {
203                         print "===============================================================================\n";
204                         print "FAIL: $_\n";
205                         print $self->{test_output}->{$_};
206                         print "\n";
207                 }
208         }
209
210         print SUMMARY "= Skipped tests =\n";
211         foreach my $reason (keys %{$self->{skips}}) {
212                 print SUMMARY "$reason\n";
213                 foreach my $name (@{$self->{skips}->{$reason}}) {
214                         print SUMMARY "\t$name\n";
215                 }
216                 print SUMMARY "\n";
217         }
218         close(SUMMARY);
219
220         print "\nA summary with detailed information can be found in:\n  $self->{summaryfile}\n";
221
222         if ($#{$self->{suitesfailed}} == -1) {
223                 my $ok = $self->{statistics}->{TESTS_EXPECTED_OK} + 
224                                  $self->{statistics}->{TESTS_EXPECTED_FAIL};
225                 print "\nALL OK ($ok tests in $self->{suites_ok} testsuites)\n";
226         } else {
227                 print "\nFAILED ($self->{statistics}->{TESTS_UNEXPECTED_FAIL} failures and $self->{statistics}->{TESTS_ERROR} errors in ". ($#{$self->{suitesfailed}}+1) ." testsuites)\n";
228         }
229
230 }
231
232 sub skip_testsuite($$$)
233 {
234         my ($self, $name, $reason) = @_;
235
236         unless (defined($reason)) {
237                 $reason = "UNKNOWN";
238         }
239         push (@{$self->{skips}->{$reason}}, $name);
240
241         if ($self->{totalsuites}) {
242                 $self->{totalsuites}--;
243         }
244 }
245
246 1;