c3ed4ca5e7bd532d4395a59ac2f38abfe182e423
[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->{START_TIME} = $self->{last_time};
98
99         $self->{local_statistics} = {
100                 success => 0,
101                 skip => 0,
102                 error => 0,
103                 failure => 0
104         };
105
106         $self->{NAME} = $name;
107         $self->{HTMLFILE} = "$name.html";
108         $self->{HTMLFILE} =~ s/[:\t\n \/]/_/g;
109
110         open(TEST, ">$self->{dirname}/$self->{HTMLFILE}") or die("Unable to open $self->{HTMLFILE} for writing");
111
112         $self->print_html_header("Test Results for $name", *TEST);
113
114         print TEST "<h2>Tests</h2>\n";
115
116         print TEST "  <table>\n";
117 }
118
119 sub control_msg($$)
120 {
121         my ($self, $output) = @_;
122
123         $self->{msg} .=  "<span class=\"control\">$output<br/></span>\n";
124 }
125
126 sub output_msg($$)
127 {
128         my ($self, $output) = @_;
129
130         unless (defined($self->{active_test})) {
131                 print TEST "$output<br/>";
132         } else {
133                 $self->{msg} .= "$output<br/>";
134         }
135 }
136
137 sub end_testsuite($$$$)
138 {
139         my ($self, $name, $result, $unexpected, $reason) = @_;
140
141         print TEST "</table>\n";
142
143         print TEST "<div class=\"duration\">Duration: " . ($self->{last_time} - $self->{START_TIME}) . "s</div>\n";
144
145         $self->print_html_footer(*TEST);
146
147         close(TEST);
148
149         print INDEX "<tr>\n";
150         print INDEX "  <td class=\"testSuite\"><a href=\"$self->{HTMLFILE}\">$name</a></td>\n";
151         my $st = $self->{local_statistics};
152
153         if (not $unexpected) {
154                 if ($result eq "failure") {
155                         print INDEX "  <td class=\"resultExpectedFailure\">";
156                 } else {
157                         print INDEX "  <td class=\"resultOk\">";
158                 }
159         } else {
160                 print INDEX "  <td class=\"resultFailure\">";
161         }
162
163         my $l = 0;
164         if ($st->{success} > 0) {
165                 print INDEX "$st->{success} ok";
166                 $l++;
167         }
168         if ($st->{skip} > 0) {
169                 print INDEX ", " if ($l);
170                 print INDEX "$st->{skip} skipped";
171                 $l++;
172         }
173         if ($st->{failure} > 0) {
174                 print INDEX ", " if ($l);
175                 print INDEX "$st->{failure} failures";
176                 $l++;
177         }
178         if ($st->{error} > 0) {
179                 print INDEX ", " if ($l);
180                 print INDEX "$st->{error} errors";
181                 $l++;
182         }
183
184         if ($l == 0) {
185                 if (not $unexpected) {
186                         print INDEX "OK";
187                 } else {
188                         print INDEX "FAIL";
189                 }
190         }
191
192         print INDEX "</td>";
193                 
194         print INDEX "</tr>\n";
195 }
196
197 sub report_time($$)
198 {
199         my ($self, $time) = @_;
200         $self->{last_time} = $time;
201 }
202
203 sub start_test($$)
204 {
205         my ($self, $testname) = @_;
206
207         $self->{active_test} = $testname;
208         $self->{msg} = "";
209 }
210
211 sub end_test($$$$)
212 {
213         my ($self, $testname, $result, $unexpected, $reason) = @_;
214
215         print TEST "<tr>";
216
217         $self->{local_statistics}->{$result}++;
218
219         my $track_class;
220
221         if ($result eq "skip") {
222                 print TEST "<td class=\"outputSkipped\">\n";
223                 $track_class = "skip";
224         } elsif ($unexpected) {
225                 print TEST "<td class=\"outputFailure\">\n";
226                 if ($result eq "error") {
227                         $track_class = "error";
228                 } else {
229                         $track_class = "unexpected_$result";
230                 }
231         } else {
232                 if ($result eq "failure") {
233                         print TEST "<td class=\"outputExpectedFailure\">\n";
234                 } else {
235                         print TEST "<td class=\"outputOk\">\n";
236                 }
237                 $track_class = "expected_$result";
238         }
239
240         push(@{$self->{error_summary}->{$track_class}}, ,
241                  [$self->{HTMLFILE}, $testname, $self->{NAME}, 
242                   $reason]);
243
244         print TEST "<a name=\"$testname\"><h3>$testname</h3></a>\n";
245
246         print TEST $self->{msg};
247
248         if (defined($reason)) {
249                 print TEST "<div class=\"reason\">$reason</div>\n";
250         }
251
252         print TEST "</td></tr>\n";
253
254         $self->{active_test} = undef;
255 }
256
257 sub summary($)
258 {
259         my ($self) = @_;
260
261         my $st = $self->{statistics};
262         print INDEX "<tr>\n";
263         print INDEX "  <td class=\"testSuiteTotal\">Total</td>\n";
264
265         if ($st->{TESTS_UNEXPECTED_OK} == 0 and 
266             $st->{TESTS_UNEXPECTED_FAIL} == 0 and
267                 $st->{TESTS_ERROR} == 0) {
268                 print INDEX "  <td class=\"resultOk\">";
269         } else {
270                 print INDEX "  <td class=\"resultFailure\">";
271         }
272         print INDEX ($st->{TESTS_EXPECTED_OK} + $st->{TESTS_UNEXPECTED_OK}) . " ok";
273         if ($st->{TESTS_UNEXPECTED_OK} > 0) {
274                 print INDEX " ($st->{TESTS_UNEXPECTED_OK} unexpected)";
275         }
276         if ($st->{TESTS_SKIP} > 0) {
277                 print INDEX ", $st->{TESTS_SKIP} skipped";
278         }
279         if (($st->{TESTS_UNEXPECTED_FAIL} + $st->{TESTS_EXPECTED_FAIL}) > 0) {
280                 print INDEX ", " . ($st->{TESTS_UNEXPECTED_FAIL} + $st->{TESTS_EXPECTED_FAIL}) . " failures";
281                 if ($st->{TESTS_UNEXPECTED_FAIL} > 0) {
282                         print INDEX " ($st->{TESTS_EXPECTED_FAIL} expected)";
283                 }
284         }
285         if ($st->{TESTS_ERROR} > 0) {
286                 print INDEX ", $st->{TESTS_ERROR} errors";
287         }
288
289         print INDEX "</td>";
290
291         print INDEX "</tr>\n";
292
293         print INDEX "</table>\n";
294         print INDEX "<a href=\"summary.html\">Summary</a>\n";
295         print INDEX "</center>\n";
296         $self->print_html_footer(*INDEX);
297         close(INDEX);
298
299         my $summ = $self->{error_summary};
300         open(SUMMARY, ">$self->{dirname}/summary.html");
301         $self->print_html_header("Summary", *SUMMARY);
302         sub print_table($$) {
303                 my ($title, $list) = @_;
304                 return if ($#$list == -1);
305                 print SUMMARY "<h3>$title</h3>\n";
306                 print SUMMARY "<table>\n";
307                 print SUMMARY "<tr>\n";
308                 print SUMMARY "  <td class=\"tableHead\">Testsuite</td>\n";
309                 print SUMMARY "  <td class=\"tableHead\">Test</td>\n";
310                 print SUMMARY "  <td class=\"tableHead\">Reason</td>\n";
311                 print SUMMARY "</tr>\n";
312
313                 foreach (@$list) {
314                         print SUMMARY "<tr>\n";
315                         print SUMMARY "  <td><a href=\"" . $$_[0] . "\">$$_[2]</a></td>\n";
316                         print SUMMARY "  <td><a href=\"" . $$_[0] . "#$$_[1]\">$$_[1]</a></td>\n";
317                         if (defined($$_[3])) {
318                                 print SUMMARY "  <td>$$_[3]</td>\n";
319                         } else {
320                                 print SUMMARY "  <td></td>\n";
321                         }
322                         print SUMMARY "</tr>\n";
323                 }
324
325                 print SUMMARY "</table>";
326         }
327         print_table("Errors", $summ->{error});
328         print_table("Unexpected successes", $summ->{unexpected_success});
329         print_table("Unexpected failures", $summ->{unexpected_failure});
330         print_table("Skipped tests", $summ->{skip});
331         print_table("Expected failures", $summ->{expected_failure});
332
333         print SUMMARY "<h3>Skipped testsuites</h3>\n";
334         print SUMMARY "<table>\n";
335         print SUMMARY "<tr>\n";
336         print SUMMARY "  <td class=\"tableHead\">Testsuite</td>\n";
337         print SUMMARY "  <td class=\"tableHead\">Reason</td>\n";
338         print SUMMARY "</tr>\n";
339
340         foreach (@{$summ->{skip_testsuites}}) {
341                 print SUMMARY "<tr>\n";
342                 print SUMMARY "  <td>$$_[0]</td>\n";
343                 if (defined($$_[1])) {
344                         print SUMMARY "  <td>$$_[1]</td>\n";
345                 } else {
346                         print SUMMARY "  <td></td>\n";
347                 }
348                 print SUMMARY "</tr>\n";
349         }
350
351         print SUMMARY "</table>";
352
353         $self->print_html_footer(*SUMMARY);
354         close(SUMMARY);
355 }
356
357 sub skip_testsuite($$$$)
358 {
359         my ($self, $name, $reason) = @_;
360
361         push (@{$self->{error_summary}->{skip_testsuites}}, 
362                   [$name, $reason]);
363 }
364
365 1;