Support reporting current time in selftest output.
[ira/wip.git] / selftest / output / html.pm
1 #!/usr/bin/perl
2 # HTML output for selftest
3 # Copyright (C) 2008 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::html;
18 use Exporter;
19 @ISA = qw(Exporter);
20
21 use strict;
22 use warnings;
23
24 use FindBin qw($RealBin);
25 use lib "$RealBin/..";
26
27 use Subunit qw(parse_results);
28
29 sub new($$$) {
30         my ($class, $dirname, $statistics) = @_;
31         my $self = { 
32                 dirname => $dirname,
33                 active_test => undef,
34                 local_statistics => {},
35                 statistics => $statistics,
36                 msg => "",
37                 error_summary => { 
38                         skip => [],
39                         expected_success => [],
40                         unexpected_success => [],
41                         expected_failure => [],
42                         unexpected_failure => [],
43                         skip_testsuites => [],
44                         error => []
45                 }
46         };
47
48         link("$RealBin/output/testresults.css", "$dirname/testresults.css");
49
50         open(INDEX, ">$dirname/index.html");
51
52         bless($self, $class);
53
54         $self->print_html_header("Samba Testsuite Run", *INDEX);
55
56         print INDEX "  <center>";
57         print INDEX "  <table>\n";
58         print INDEX "  <tr>\n";
59         print INDEX "    <td class=\"tableHead\">Test</td>\n";
60         print INDEX "    <td class=\"tableHead\">Result</td>\n";
61         print INDEX "  </tr>\n";
62
63         return $self;
64 }
65
66 sub print_html_header($$$)
67 {
68         my ($self, $title, $fh) = @_;
69
70         print $fh "<html lang=\"en\">\n";
71         print $fh "<head>\n";
72         print $fh "  <title>$title</title>\n";
73         print $fh "  <link rel=\"stylesheet\" type=\"text/css\" href=\"testresults.css\"/>\n";
74         print $fh "</head>\n";
75         print $fh "<body>\n";
76         print $fh "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n";
77         print $fh "  <tr><td class=\"title\">$title</td></tr>\n";
78         print $fh "  <tr><td>\n";
79 }
80
81 sub print_html_footer($$)
82 {
83         my ($self, $fh) = @_;
84
85         print $fh "</td></tr>\n";
86         print $fh "</table>\n";
87         print $fh "</body>\n";
88         print $fh "</html>\n";
89 }
90
91 sub output_msg($$);
92
93 sub start_testsuite($$)
94 {
95         my ($self, $name) = @_;
96
97         $self->{local_statistics} = {
98                 success => 0,
99                 skip => 0,
100                 error => 0,
101                 failure => 0
102         };
103
104         $self->{NAME} = $name;
105         $self->{HTMLFILE} = "$name.html";
106         $self->{HTMLFILE} =~ s/[:\t\n \/]/_/g;
107
108         open(TEST, ">$self->{dirname}/$self->{HTMLFILE}") or die("Unable to open $self->{HTMLFILE} for writing");
109
110         $self->print_html_header("Test Results for $name", *TEST);
111
112         print TEST "<h2>Tests</h2>\n";
113
114         print TEST "  <table>\n";
115 }
116
117 sub control_msg($$)
118 {
119         my ($self, $output) = @_;
120
121         $self->{msg} .=  "<span class=\"control\">$output<br/></span>\n";
122 }
123
124 sub output_msg($$)
125 {
126         my ($self, $output) = @_;
127
128         unless (defined($self->{active_test})) {
129                 print TEST "$output<br/>";
130         } else {
131                 $self->{msg} .= "$output<br/>";
132         }
133 }
134
135 sub end_testsuite($$$$)
136 {
137         my ($self, $name, $result, $unexpected, $reason) = @_;
138
139         print TEST "</table>\n";
140
141         print TEST "<div class=\"duration\">Duration: " . ($self->{last_time} - $self->{START_TIME}) . "s</div>\n";
142
143         $self->print_html_footer(*TEST);
144
145         close(TEST);
146
147         print INDEX "<tr>\n";
148         print INDEX "  <td class=\"testSuite\"><a href=\"$self->{HTMLFILE}\">$name</a></td>\n";
149         my $st = $self->{local_statistics};
150
151         if (not $unexpected) {
152                 if ($result eq "failure") {
153                         print INDEX "  <td class=\"resultExpectedFailure\">";
154                 } else {
155                         print INDEX "  <td class=\"resultOk\">";
156                 }
157         } else {
158                 print INDEX "  <td class=\"resultFailure\">";
159         }
160
161         my $l = 0;
162         if ($st->{success} > 0) {
163                 print INDEX "$st->{success} ok";
164                 $l++;
165         }
166         if ($st->{skip} > 0) {
167                 print INDEX ", " if ($l);
168                 print INDEX "$st->{skip} skipped";
169                 $l++;
170         }
171         if ($st->{failure} > 0) {
172                 print INDEX ", " if ($l);
173                 print INDEX "$st->{failure} failures";
174                 $l++;
175         }
176         if ($st->{error} > 0) {
177                 print INDEX ", " if ($l);
178                 print INDEX "$st->{error} errors";
179                 $l++;
180         }
181
182         if ($l == 0) {
183                 if (not $unexpected) {
184                         print INDEX "OK";
185                 } else {
186                         print INDEX "FAIL";
187                 }
188         }
189
190         print INDEX "</td>";
191                 
192         print INDEX "</tr>\n";
193 }
194
195 sub report_time($$)
196 {
197         my ($self, $time) = @_;
198         $self->{last_time} = $time;
199 }
200
201 sub start_test($$)
202 {
203         my ($self, $parents, $testname) = @_;
204
205         if ($#$parents == -1) {
206                 $self->{START_TIME} = $self->{last_time};
207                 $self->start_testsuite($testname);
208                 return;
209         }
210
211         $self->{active_test} = $testname;
212         $self->{msg} = "";
213 }
214
215 sub end_test($$$$$$)
216 {
217         my ($self, $parents, $testname, $result, $unexpected, $reason) = @_;
218
219         if ($#$parents == -1) {
220                 $self->end_testsuite($testname, $result, $unexpected, $reason);
221                 return;
222         }
223
224         print TEST "<tr>";
225
226         $self->{local_statistics}->{$result}++;
227
228         my $track_class;
229
230         if ($result eq "skip") {
231                 print TEST "<td class=\"outputSkipped\">\n";
232                 $track_class = "skip";
233         } elsif ($unexpected) {
234                 print TEST "<td class=\"outputFailure\">\n";
235                 if ($result eq "error") {
236                         $track_class = "error";
237                 } else {
238                         $track_class = "unexpected_$result";
239                 }
240         } else {
241                 if ($result eq "failure") {
242                         print TEST "<td class=\"outputExpectedFailure\">\n";
243                 } else {
244                         print TEST "<td class=\"outputOk\">\n";
245                 }
246                 $track_class = "expected_$result";
247         }
248
249         push(@{$self->{error_summary}->{$track_class}}, ,
250                  [$self->{HTMLFILE}, $testname, $self->{NAME}, 
251                   $reason]);
252
253         print TEST "<a name=\"$testname\"><h3>$testname</h3></a>\n";
254
255         print TEST $self->{msg};
256
257         if (defined($reason)) {
258                 print TEST "<div class=\"reason\">$reason</div>\n";
259         }
260
261         print TEST "</td></tr>\n";
262
263         $self->{active_test} = undef;
264 }
265
266 sub summary($)
267 {
268         my ($self) = @_;
269
270         my $st = $self->{statistics};
271         print INDEX "<tr>\n";
272         print INDEX "  <td class=\"testSuiteTotal\">Total</td>\n";
273
274         if ($st->{TESTS_UNEXPECTED_OK} == 0 and 
275             $st->{TESTS_UNEXPECTED_FAIL} == 0 and
276                 $st->{TESTS_ERROR} == 0) {
277                 print INDEX "  <td class=\"resultOk\">";
278         } else {
279                 print INDEX "  <td class=\"resultFailure\">";
280         }
281         print INDEX ($st->{TESTS_EXPECTED_OK} + $st->{TESTS_UNEXPECTED_OK}) . " ok";
282         if ($st->{TESTS_UNEXPECTED_OK} > 0) {
283                 print INDEX " ($st->{TESTS_UNEXPECTED_OK} unexpected)";
284         }
285         if ($st->{TESTS_SKIP} > 0) {
286                 print INDEX ", $st->{TESTS_SKIP} skipped";
287         }
288         if (($st->{TESTS_UNEXPECTED_FAIL} + $st->{TESTS_EXPECTED_FAIL}) > 0) {
289                 print INDEX ", " . ($st->{TESTS_UNEXPECTED_FAIL} + $st->{TESTS_EXPECTED_FAIL}) . " failures";
290                 if ($st->{TESTS_UNEXPECTED_FAIL} > 0) {
291                         print INDEX " ($st->{TESTS_EXPECTED_FAIL} expected)";
292                 }
293         }
294         if ($st->{TESTS_ERROR} > 0) {
295                 print INDEX ", $st->{TESTS_ERROR} errors";
296         }
297
298         print INDEX "</td>";
299
300         print INDEX "</tr>\n";
301
302         print INDEX "</table>\n";
303         print INDEX "<a href=\"summary.html\">Summary</a>\n";
304         print INDEX "</center>\n";
305         $self->print_html_footer(*INDEX);
306         close(INDEX);
307
308         my $summ = $self->{error_summary};
309         open(SUMMARY, ">$self->{dirname}/summary.html");
310         $self->print_html_header("Summary", *SUMMARY);
311         sub print_table($$) {
312                 my ($title, $list) = @_;
313                 return if ($#$list == -1);
314                 print SUMMARY "<h3>$title</h3>\n";
315                 print SUMMARY "<table>\n";
316                 print SUMMARY "<tr>\n";
317                 print SUMMARY "  <td class=\"tableHead\">Testsuite</td>\n";
318                 print SUMMARY "  <td class=\"tableHead\">Test</td>\n";
319                 print SUMMARY "  <td class=\"tableHead\">Reason</td>\n";
320                 print SUMMARY "</tr>\n";
321
322                 foreach (@$list) {
323                         print SUMMARY "<tr>\n";
324                         print SUMMARY "  <td><a href=\"" . $$_[0] . "\">$$_[2]</a></td>\n";
325                         print SUMMARY "  <td><a href=\"" . $$_[0] . "#$$_[1]\">$$_[1]</a></td>\n";
326                         if (defined($$_[3])) {
327                                 print SUMMARY "  <td>$$_[3]</td>\n";
328                         } else {
329                                 print SUMMARY "  <td></td>\n";
330                         }
331                         print SUMMARY "</tr>\n";
332                 }
333
334                 print SUMMARY "</table>";
335         }
336         print_table("Errors", $summ->{error});
337         print_table("Unexpected successes", $summ->{unexpected_success});
338         print_table("Unexpected failures", $summ->{unexpected_failure});
339         print_table("Skipped tests", $summ->{skip});
340         print_table("Expected failures", $summ->{expected_failure});
341
342         print SUMMARY "<h3>Skipped testsuites</h3>\n";
343         print SUMMARY "<table>\n";
344         print SUMMARY "<tr>\n";
345         print SUMMARY "  <td class=\"tableHead\">Testsuite</td>\n";
346         print SUMMARY "  <td class=\"tableHead\">Reason</td>\n";
347         print SUMMARY "</tr>\n";
348
349         foreach (@{$summ->{skip_testsuites}}) {
350                 print SUMMARY "<tr>\n";
351                 print SUMMARY "  <td>$$_[0]</td>\n";
352                 if (defined($$_[1])) {
353                         print SUMMARY "  <td>$$_[1]</td>\n";
354                 } else {
355                         print SUMMARY "  <td></td>\n";
356                 }
357                 print SUMMARY "</tr>\n";
358         }
359
360         print SUMMARY "</table>";
361
362         $self->print_html_footer(*SUMMARY);
363         close(SUMMARY);
364 }
365
366 sub skip_testsuite($$$$)
367 {
368         my ($self, $name, $reason) = @_;
369
370         push (@{$self->{error_summary}->{skip_testsuites}}, 
371                   [$name, $reason]);
372 }
373
374 1;