| 1 |
package TAP::Formatter::JUnit::Session; |
| 2 |
|
| 3 |
use Moose; |
| 4 |
use MooseX::NonMoose; |
| 5 |
extends qw( |
| 6 |
TAP::Formatter::Console::Session |
| 7 |
); |
| 8 |
|
| 9 |
use Storable qw(dclone); |
| 10 |
use File::Path qw(mkpath); |
| 11 |
use IO::File; |
| 12 |
use TAP::Formatter::JUnit::Result; |
| 13 |
use namespace::clean; |
| 14 |
|
| 15 |
has 'testcases' => ( |
| 16 |
is => 'rw', |
| 17 |
isa => 'ArrayRef', |
| 18 |
default => sub { [] }, |
| 19 |
traits => [qw( Array )], |
| 20 |
handles => { |
| 21 |
add_testcase => 'push', |
| 22 |
num_testcases => 'count', |
| 23 |
}, |
| 24 |
); |
| 25 |
|
| 26 |
has 'passing_todo_ok' => ( |
| 27 |
is => 'rw', |
| 28 |
isa => 'Bool', |
| 29 |
default => 0, |
| 30 |
); |
| 31 |
|
| 32 |
has '_queue' => ( |
| 33 |
is => 'rw', |
| 34 |
isa => 'ArrayRef', |
| 35 |
default => sub { [] }, |
| 36 |
traits => [qw( Array )], |
| 37 |
handles => { |
| 38 |
_queue_add => 'push', |
| 39 |
}, |
| 40 |
); |
| 41 |
|
| 42 |
############################################################################### |
| 43 |
# Subroutine: _initialize($arg_for) |
| 44 |
############################################################################### |
| 45 |
# Custom initializer, so we can accept a new "passing_todo_ok" argument at |
| 46 |
# instantiation time. |
| 47 |
sub _initialize { |
| 48 |
my ($self, $arg_for) = @_; |
| 49 |
$arg_for ||= {}; |
| 50 |
|
| 51 |
my $passing_todo_ok = delete $arg_for->{passing_todo_ok}; |
| 52 |
$self->passing_todo_ok($passing_todo_ok); |
| 53 |
|
| 54 |
return $self->SUPER::_initialize($arg_for); |
| 55 |
} |
| 56 |
|
| 57 |
############################################################################### |
| 58 |
# Subroutine: result($result) |
| 59 |
############################################################################### |
| 60 |
# Called by the harness for each line of TAP it receives. |
| 61 |
# |
| 62 |
# Queues up all of the TAP output for later conversion to JUnit. |
| 63 |
sub result { |
| 64 |
my ($self, $result) = @_; |
| 65 |
|
| 66 |
# except for a few things we don't want to process as a "test case", add |
| 67 |
# the test result to the queue. |
| 68 |
unless ( ($result->raw() =~ /^# Looks like you failed \d+ tests? of \d+/) |
| 69 |
|| ($result->raw() =~ /^# Looks like you planned \d+ tests? but ran \d+/) |
| 70 |
|| ($result->raw() =~ /^# Looks like your test died before it could output anything/) |
| 71 |
) { |
| 72 |
my $wrapped = TAP::Formatter::JUnit::Result->new( |
| 73 |
'time' => $self->get_time, |
| 74 |
'result' => $result, |
| 75 |
); |
| 76 |
$self->_queue_add($wrapped); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
############################################################################### |
| 81 |
# Subroutine: close_test() |
| 82 |
############################################################################### |
| 83 |
# Called to close the test session. |
| 84 |
# |
| 85 |
# Flushes the queue if we've got anything left in it, dumps the JUnit to disk |
| 86 |
# (if necessary), and adds the XML for this test suite to our formatter. |
| 87 |
sub close_test { |
| 88 |
my $self = shift; |
| 89 |
my $xml = $self->xml; |
| 90 |
my $parser = $self->parser; |
| 91 |
|
| 92 |
# Process the queued up TAP stream |
| 93 |
my $is_first = 1; |
| 94 |
my $t_start = $self->parser->start_time; |
| 95 |
my $t_last_test = $t_start; |
| 96 |
my $timer_enabled = $self->formatter->timer; |
| 97 |
|
| 98 |
my $queue = $self->_queue; |
| 99 |
my $index = 0; |
| 100 |
while ($index < @{$queue}) { |
| 101 |
my $result = $queue->[$index++]; |
| 102 |
|
| 103 |
# First line of output generates the "init" timing. |
| 104 |
if ($is_first) { |
| 105 |
if ($timer_enabled) { |
| 106 |
unless ($result->is_test) { |
| 107 |
my $duration = $result->time - $t_start; |
| 108 |
my $case = $xml->testcase( { |
| 109 |
'name' => _squeaky_clean('(init)'), |
| 110 |
'time' => $duration, |
| 111 |
} ); |
| 112 |
$self->add_testcase($case); |
| 113 |
$t_last_test = $result->time; |
| 114 |
} |
| 115 |
} |
| 116 |
$is_first = 0; |
| 117 |
} |
| 118 |
|
| 119 |
# Test output |
| 120 |
if ($result->is_test) { |
| 121 |
# how long did it take for this test? |
| 122 |
my $duration = $result->time - $t_last_test; |
| 123 |
|
| 124 |
# slurp in all of the content up until the next test |
| 125 |
my $content = $result->as_string; |
| 126 |
while ($index < @{$queue}) { |
| 127 |
last if ($queue->[$index]->is_test); |
| 128 |
last if ($queue->[$index]->is_plan); |
| 129 |
|
| 130 |
my $stuff = $queue->[$index++]; |
| 131 |
$content .= "\n" . $stuff->as_string; |
| 132 |
} |
| 133 |
|
| 134 |
# create a failure/error element if the test was bogus |
| 135 |
my $failure; |
| 136 |
my $bogosity = $self->_check_for_test_bogosity($result); |
| 137 |
if ($bogosity) { |
| 138 |
my $cdata = $self->_cdata($content); |
| 139 |
my $level = $bogosity->{level}; |
| 140 |
$failure = $xml->$level( { |
| 141 |
type => $bogosity->{type}, |
| 142 |
message => $bogosity->{message}, |
| 143 |
}, $cdata ); |
| 144 |
} |
| 145 |
|
| 146 |
# add this test to the XML stream |
| 147 |
my $case = $xml->testcase( |
| 148 |
{ |
| 149 |
'name' => _get_testcase_name($result), |
| 150 |
( |
| 151 |
$timer_enabled ? ('time' => $duration) : () |
| 152 |
), |
| 153 |
}, |
| 154 |
$failure, |
| 155 |
); |
| 156 |
$self->add_testcase($case); |
| 157 |
|
| 158 |
# update time of last test seen |
| 159 |
$t_last_test = $result->time; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
# track time for teardown, if needed |
| 164 |
if ($timer_enabled) { |
| 165 |
my $duration = $self->parser->end_time - $queue->[-1]->time; |
| 166 |
my $case = $xml->testcase( { |
| 167 |
'name' => _squeaky_clean('(teardown)'), |
| 168 |
'time' => $duration, |
| 169 |
} ); |
| 170 |
$self->add_testcase($case); |
| 171 |
} |
| 172 |
|
| 173 |
# collect up all of the captured test output |
| 174 |
my $captured = join '', map { $_->raw . "\n" } @{$queue}; |
| 175 |
|
| 176 |
# if the test died unexpectedly, make note of that |
| 177 |
my $die_msg; |
| 178 |
my $exit = $parser->exit(); |
| 179 |
if ($exit) { |
| 180 |
my $wstat = $parser->wait(); |
| 181 |
my $status = sprintf("%d (wstat %d, 0x%x)", $exit, $wstat, $wstat); |
| 182 |
$die_msg = "Dubious, test returned $status"; |
| 183 |
} |
| 184 |
|
| 185 |
# add system-out/system-err data, as raw CDATA |
| 186 |
my $sys_out = 'system-out'; |
| 187 |
$sys_out = $xml->$sys_out($captured ? $self->_cdata($captured) : undef); |
| 188 |
|
| 189 |
my $sys_err = 'system-err'; |
| 190 |
$sys_err = $xml->$sys_err($die_msg ? $self->_cdata("$die_msg\n") : undef); |
| 191 |
|
| 192 |
# update the testsuite with aggregate info on this test suite |
| 193 |
# |
| 194 |
# tests - total number of tests run |
| 195 |
# time - wallclock time taken for test run (floating point) |
| 196 |
# failures - number of tests that we detected as failing |
| 197 |
# errors - number of errors: |
| 198 |
# - passing TODOs |
| 199 |
# - if a plan was provided, mismatch between that and the |
| 200 |
# number of actual tests that were run |
| 201 |
# - either "no plan was issued" or "test died" (a dying test |
| 202 |
# may not have a plan issued, but should still be considered |
| 203 |
# a single error condition) |
| 204 |
my $testsrun = $parser->tests_run() || 0; |
| 205 |
my $time = $parser->end_time() - $parser->start_time(); |
| 206 |
my $failures = $parser->failed(); |
| 207 |
|
| 208 |
my $noplan = $parser->plan() ? 0 : 1; |
| 209 |
my $planned = $parser->tests_planned() || 0; |
| 210 |
|
| 211 |
my $num_errors = 0; |
| 212 |
$num_errors += $parser->todo_passed() unless $self->passing_todo_ok(); |
| 213 |
$num_errors += abs($testsrun - $planned) if ($planned); |
| 214 |
|
| 215 |
my $suite_err; |
| 216 |
if ($die_msg) { |
| 217 |
$suite_err = $xml->error( { message => $die_msg } ); |
| 218 |
$num_errors ++; |
| 219 |
} |
| 220 |
elsif ($noplan) { |
| 221 |
$suite_err = $xml->error( { message => 'No plan in TAP output' } ); |
| 222 |
$num_errors ++; |
| 223 |
} |
| 224 |
elsif ($planned && ($testsrun != $planned)) { |
| 225 |
$suite_err = $xml->error( { message => "Looks like you planned $planned tests but ran $testsrun." } ); |
| 226 |
} |
| 227 |
|
| 228 |
my @tests = @{$self->testcases()}; |
| 229 |
my %attrs = ( |
| 230 |
'name' => _get_testsuite_name($self), |
| 231 |
'tests' => $testsrun, |
| 232 |
'failures' => $failures, |
| 233 |
'errors' => $num_errors, |
| 234 |
( |
| 235 |
$timer_enabled ? ('time' => $time) : () |
| 236 |
), |
| 237 |
); |
| 238 |
my $testsuite = $xml->testsuite(\%attrs, @tests, $sys_out, $sys_err, $suite_err); |
| 239 |
$self->formatter->add_testsuite($testsuite); |
| 240 |
$self->dump_junit_xml($testsuite); |
| 241 |
} |
| 242 |
|
| 243 |
############################################################################### |
| 244 |
# Subroutine: dump_junit_xml($testsuite) |
| 245 |
############################################################################### |
| 246 |
# Dumps the JUnit for the given XML '$testsuite', to the directory specified by |
| 247 |
# 'PERL_TEST_HARNESS_DUMP_TAP'. |
| 248 |
sub dump_junit_xml { |
| 249 |
my ($self, $testsuite) = @_; |
| 250 |
if (my $spool_dir = $ENV{PERL_TEST_HARNESS_DUMP_TAP}) { |
| 251 |
my $spool = File::Spec->catfile($spool_dir, $self->name() . '.junit.xml'); |
| 252 |
|
| 253 |
# clone the testsuite; XML::Generator only lets us auto-vivify the |
| 254 |
# CDATA sections *ONCE*. |
| 255 |
$testsuite = dclone($testsuite); |
| 256 |
|
| 257 |
# create target dir |
| 258 |
my ($vol, $dir, undef) = File::Spec->splitpath($spool); |
| 259 |
my $path = File::Spec->catpath($vol, $dir, ''); |
| 260 |
mkpath($path); |
| 261 |
|
| 262 |
# create JUnit XML, and dump to disk |
| 263 |
my $junit = $self->xml->xml($self->xml->testsuites($testsuite) ); |
| 264 |
my $fout = IO::File->new( $spool, '>:utf8' ) |
| 265 |
|| die "Can't write $spool ( $! )\n"; |
| 266 |
$fout->print($junit); |
| 267 |
$fout->close(); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
############################################################################### |
| 272 |
# Subroutine: xml() |
| 273 |
############################################################################### |
| 274 |
# Returns a new 'XML::Generator' to generate XML output. This is simply a |
| 275 |
# shortcut to '$self->formatter->xml()'. |
| 276 |
sub xml { |
| 277 |
my $self = shift; |
| 278 |
return $self->formatter->xml(); |
| 279 |
} |
| 280 |
|
| 281 |
############################################################################### |
| 282 |
# Checks for bogosity in the test result. |
| 283 |
sub _check_for_test_bogosity { |
| 284 |
my $self = shift; |
| 285 |
my $result = shift; |
| 286 |
|
| 287 |
if ($result->todo_passed() && !$self->passing_todo_ok()) { |
| 288 |
return { |
| 289 |
level => 'error', |
| 290 |
type => 'TodoTestSucceeded', |
| 291 |
message => $result->explanation(), |
| 292 |
}; |
| 293 |
} |
| 294 |
|
| 295 |
if ($result->is_unplanned()) { |
| 296 |
return { |
| 297 |
level => 'error', |
| 298 |
type => 'UnplannedTest', |
| 299 |
message => $result->as_string(), |
| 300 |
}; |
| 301 |
} |
| 302 |
|
| 303 |
if (not $result->is_ok()) { |
| 304 |
return { |
| 305 |
level => 'failure', |
| 306 |
type => 'TestFailed', |
| 307 |
message => $result->as_string(), |
| 308 |
}; |
| 309 |
} |
| 310 |
|
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
############################################################################### |
| 315 |
# Generates the name for a test case. |
| 316 |
sub _get_testcase_name { |
| 317 |
my $test = shift; |
| 318 |
my $name = join(' ', $test->number(), _clean_test_description($test)); |
| 319 |
$name =~ s/\s+$//; |
| 320 |
return $name; |
| 321 |
} |
| 322 |
|
| 323 |
############################################################################### |
| 324 |
# Generates the name for the entire test suite. |
| 325 |
sub _get_testsuite_name { |
| 326 |
my $self = shift; |
| 327 |
my $name = $self->name; |
| 328 |
$name =~ s{^\./}{}; |
| 329 |
$name =~ s{^t/}{}; |
| 330 |
return _clean_to_java_class_name($name); |
| 331 |
} |
| 332 |
|
| 333 |
############################################################################### |
| 334 |
# Cleans up the given string, removing any characters that aren't suitable for |
| 335 |
# use in a Java class name. |
| 336 |
sub _clean_to_java_class_name { |
| 337 |
my $str = shift; |
| 338 |
$str =~ s/[^-:_A-Za-z0-9]+/_/gs; |
| 339 |
return $str; |
| 340 |
} |
| 341 |
|
| 342 |
############################################################################### |
| 343 |
# Cleans up the description of the given test. |
| 344 |
sub _clean_test_description { |
| 345 |
my $test = shift; |
| 346 |
my $desc = $test->description(); |
| 347 |
return _squeaky_clean($desc); |
| 348 |
} |
| 349 |
|
| 350 |
############################################################################### |
| 351 |
# Creates a CDATA block for the given data (which is made squeaky clean first, |
| 352 |
# so that JUnit parsers like Hudson's don't choke). |
| 353 |
sub _cdata { |
| 354 |
my ($self, $data) = @_; |
| 355 |
$data = _squeaky_clean($data); |
| 356 |
return $self->xml->xmlcdata($data); |
| 357 |
} |
| 358 |
|
| 359 |
############################################################################### |
| 360 |
# Clean a string to the point that JUnit can't possibly have a problem with it. |
| 361 |
sub _squeaky_clean { |
| 362 |
my $string = shift; |
| 363 |
# control characters (except CR and LF) |
| 364 |
$string =~ s/([\x00-\x09\x0b\x0c\x0e-\x1f])/"^".chr(ord($1)+64)/ge; |
| 365 |
# high-byte characters |
| 366 |
$string =~ s/([\x7f-\xff])/'[\\x'.sprintf('%02x',ord($1)).']'/ge; |
| 367 |
return $string; |
| 368 |
} |
| 369 |
|
| 370 |
1; |
| 371 |
|
| 372 |
=for stopwords instantiation testcases |
| 373 |
|
| 374 |
=head1 NAME |
| 375 |
|
| 376 |
TAP::Formatter::JUnit::Session - Harness output delegate for JUnit output |
| 377 |
|
| 378 |
=head1 DESCRIPTION |
| 379 |
|
| 380 |
C<TAP::Formatter::JUnit::Session> provides JUnit output formatting for |
| 381 |
C<TAP::Harness>. |
| 382 |
|
| 383 |
=head1 METHODS |
| 384 |
|
| 385 |
=over |
| 386 |
|
| 387 |
=item _initialize($arg_for) |
| 388 |
|
| 389 |
Over-ridden private initializer, so we can accept a new "passing_todo_ok" |
| 390 |
argument at instantiation time. |
| 391 |
|
| 392 |
=item result($result) |
| 393 |
|
| 394 |
Called by the harness for each line of TAP it receives. |
| 395 |
|
| 396 |
Internally, all of the TAP is added to a queue until we hit the start of |
| 397 |
the "next" test (at which point we flush the queue. This allows us to |
| 398 |
capture any error output or diagnostic info that comes after a test |
| 399 |
failure. |
| 400 |
|
| 401 |
=item close_test() |
| 402 |
|
| 403 |
Called to close the test session. |
| 404 |
|
| 405 |
Flushes the queue if we've got anything left in it, dumps the JUnit to disk |
| 406 |
(if necessary), and adds the XML for this test suite to our formatter. |
| 407 |
|
| 408 |
=item dump_junit_xml($testsuite) |
| 409 |
|
| 410 |
Dumps the JUnit for the given XML C<$testsuite>, to the directory specified |
| 411 |
by C<PERL_TEST_HARNESS_DUMP_TAP>. |
| 412 |
|
| 413 |
=item add_testcase($case) |
| 414 |
|
| 415 |
Adds an XML test C<$case> to the list of testcases we've run in this |
| 416 |
session. |
| 417 |
|
| 418 |
=item xml() |
| 419 |
|
| 420 |
Returns a new C<XML::Generator> to generate XML output. This is simply a |
| 421 |
shortcut to C<$self-E<gt>formatter-E<gt>xml()>. |
| 422 |
|
| 423 |
=back |
| 424 |
|
| 425 |
=head1 AUTHOR |
| 426 |
|
| 427 |
Graham TerMarsch <[email protected]> |
| 428 |
|
| 429 |
=head1 COPYRIGHT |
| 430 |
|
| 431 |
Copyright 2008-2010, Graham TerMarsch. All Rights Reserved. |
| 432 |
|
| 433 |
This is free software; you can redistribute it and/or modify it under the same |
| 434 |
terms as Perl itself. |
| 435 |
|
| 436 |
=head1 SEE ALSO |
| 437 |
|
| 438 |
L<TAP::Formatter::JUnit>. |
| 439 |
|
| 440 |
=cut |