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