blib/lib/TAP/Formatter/JUnit/Session.pm

filter: Statementstmt 84.9% 135 / 159 Branchbran 69.2% 36 / 52 Conditioncond 45.0% 9 / 20 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 21 / 21 Podpod 100% 4 / 4 totaltotal 77.5% 207 / 267 CC 35 SCAR 27.6
CC
35
Cov
78%
close_test
31.3
dump_junit_xml
7.6
_check_for_test_bogosity
5.3
CRAP 15.9
↑ Summary blib/lib/TAP/Formatter/JUnit.pm »
1 package TAP::Formatter::JUnit::Session;
2  
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 extends qw(
6 TAP::Formatter::Console::Session
7 );
8  
9 6 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use TAP::Formatter::JUnit::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has 'testcases' => (
16 is => 'rw',
17 isa => 'ArrayRef',
18 6 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 6 traits => [qw( Array )],
20 handles => {
21 add_testcase => 'push',
22 num_testcases => 'count',
23 },
24 );
25  
26 6 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 6 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 6 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 6 ▶ my ($self, $arg_for) = @_;
Subroutine _initialize: called CC=2 SCAR=7.0
49 6 ▶ $arg_for ||= {};
Condition50%1/2
$arg_for ||= {}
l!l
60
MC/DC0%0/1
$arg_for ||= {}
$arg_for
Truth table50%1/2
A
A= $arg_for
Aresult
11
00
50  
51 6 my $passing_todo_ok = delete $arg_for->{passing_todo_ok};
52 6 $self->passing_todo_ok($passing_todo_ok);
53  
54 6 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 29 ▶ my ($self, $result) = @_;
Subroutine result: called CC=4 SCAR=16.4
Pod documented
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 29 ▶ unless ( ($result->raw() =~ /^# Looks like you failed \d+ tests? of \d+/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->raw =~ /^# Looks like you failed \d+ tests? of \d+/ or $result->raw =~ /^# Looks like you planned \d+ tests? but ran \d+/ or $result->raw =~ /^# Looks like your test died before it could output anything/) 29 0 29
Condition33%2/6
$result->raw =~ /^# Looks like you failed \d+ tests? of \d+/ or $result->raw =~ /^# Looks like you planned \d+ tests? but ran \d+/ or $result->raw =~ /^# Looks like your test died before it could output anything/
l!l&&r!l&&!r
0029
$result->raw =~ /^# Looks like you failed \d+ tests? of \d+/ or $result->raw =~ /^# Looks like you planned \d+ tests? but ran \d+/
l!l&&r!l&&!r
0029
MC/DC0%0/3
$result->raw =~ /^# Looks like you failed \d+ tests? of \d+/ or $result->raw =~ /^# Looks like you planned \d+ tests? but ran \d+/ or $result->raw =~ /^# Looks like your test died before it could output anything/
$result->raw =~ /^# Looks like you failed \d+ tests? of \d+/ $result->raw =~ /^# Looks like you planned \d+ tests? but ran \d+/ $result->raw =~ /^# Looks like your test died before it could output anything/
Truth table25%1/4
A or B or C
A= $result->raw =~ /^# Looks like you failed \d+ tests? of \d+/B= $result->raw =~ /^# Looks like you planned \d+ tests? but ran \d+/C= $result->raw =~ /^# Looks like your test died before it could output anything/
ABCresult
1XX1
01X1
0011
0000
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 29 my $wrapped = TAP::Formatter::JUnit::Result->new(
73 'time' => $self->get_time,
74 'result' => $result,
75 );
76 29 $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 6 ▶ my $self = shift;
Subroutine close_test: called CC=25 SCAR=34.4
Pod documented
89 6 my $xml = $self->xml;
90 6 my $parser = $self->parser;
91  
92 # Process the queued up TAP stream
93 6 my $is_first = 1;
94 6 my $t_start = $self->parser->start_time;
95 6 my $t_last_test = $t_start;
96 6 my $timer_enabled = $self->formatter->timer;
97  
98 6 my $queue = $self->_queue;
99 6 my $index = 0;
100 6 ▶ while ($index < @{$queue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @{$queue;}) 29 6 35
101 29 my $result = $queue->[$index++];
102  
103 # First line of output generates the "init" timing.
104 29 ▶ if ($is_first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first) 6 23 29
105 6 ▶ if ($timer_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer_enabled) 1 5 6
106 1 ▶ unless ($result->is_test) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_test) 1 0 1
107 1 my $duration = $result->time - $t_start;
108 1 my $case = $xml->testcase( {
109 'name' => _squeaky_clean('(init)'),
110 'time' => $duration,
111 } );
112 1 $self->add_testcase($case);
113 1 $t_last_test = $result->time;
114 }
115 }
116 6 $is_first = 0;
117 }
118  
119 # Test output
120 29 ▶ if ($result->is_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_test) 23 6 29
121 # how long did it take for this test?
122 23 my $duration = $result->time - $t_last_test;
123  
124 # slurp in all of the content up until the next test
125 23 my $content = $result->as_string;
126 23 ▶ while ($index < @{$queue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @{$queue;}) 17 6 23
127 17 ▶ last if ($queue->[$index]->is_test);
Branch: 1 branch
BranchTrueFalseTotal
if $queue->[$index]->is_test 17 0 17
128 0 ▶ last if ($queue->[$index]->is_plan);
Branch: 1 branch
BranchTrueFalseTotal
if $queue->[$index]->is_plan 0 0 0
129  
130 0 my $stuff = $queue->[$index++];
131 0 $content .= "\n" . $stuff->as_string;
132 }
133  
134 # create a failure/error element if the test was bogus
135 23 my $failure;
136 23 my $bogosity = $self->_check_for_test_bogosity($result);
137 23 ▶ if ($bogosity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bogosity) 1 22 23
138 1 my $cdata = $self->_cdata($content);
139 1 my $level = $bogosity->{level};
140 $failure = $xml->$level( {
141 type => $bogosity->{type},
142 message => $bogosity->{message},
143 1 }, $cdata );
144 }
145  
146 # add this test to the XML stream
147 23 ▶ my $case = $xml->testcase(
Branch: 1 branch
BranchTrueFalseTotal
$timer_enabled ? : 2 21 23
148 {
149 'name' => _get_testcase_name($result),
150 (
151 $timer_enabled ? ('time' => $duration) : ()
152 ),
153 },
154 $failure,
155 );
156 23 $self->add_testcase($case);
157  
158 # update time of last test seen
159 23 $t_last_test = $result->time;
160 }
161 }
162  
163 # track time for teardown, if needed
164 6 ▶ if ($timer_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer_enabled) 1 5 6
165 1 my $duration = $self->parser->end_time - $queue->[-1]->time;
166 1 my $case = $xml->testcase( {
167 'name' => _squeaky_clean('(teardown)'),
168 'time' => $duration,
169 } );
170 1 $self->add_testcase($case);
171 }
172  
173 # collect up all of the captured test output
174 6 my $captured = join '', map { $_->raw . "\n" } @{$queue};
175  
176 # if the test died unexpectedly, make note of that
177 6 my $die_msg;
178 6 my $exit = $parser->exit();
179 6 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 6 6
180 0 my $wstat = $parser->wait();
181 0 my $status = sprintf("%d (wstat %d, 0x%x)", $exit, $wstat, $wstat);
182 0 $die_msg = "Dubious, test returned $status";
183 }
184  
185 # add system-out/system-err data, as raw CDATA
186 6 my $sys_out = 'system-out';
187 6 ▶ $sys_out = $xml->$sys_out($captured ? $self->_cdata($captured) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$captured ? : 6 0 6
188  
189 6 my $sys_err = 'system-err';
190 6 ▶ $sys_err = $xml->$sys_err($die_msg ? $self->_cdata("$die_msg\n") : undef);
Branch: 1 branch
BranchTrueFalseTotal
$die_msg ? : 0 6 6
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 6 ▶ my $testsrun = $parser->tests_run() || 0;
Condition50%1/2
$parser->tests_run || 0
l!l
60
MC/DC0%0/1
$parser->tests_run || 0
$parser->tests_run
Truth table50%1/2
A
A= $parser->tests_run
Aresult
11
00
205 6 my $time = $parser->end_time() - $parser->start_time();
206 6 my $failures = $parser->failed();
207  
208 6 ▶ my $noplan = $parser->plan() ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$parser->plan ? : 6 0 6
209 6 ▶ my $planned = $parser->tests_planned() || 0;
Condition50%1/2
$parser->tests_planned || 0
l!l
60
MC/DC0%0/1
$parser->tests_planned || 0
$parser->tests_planned
Truth table50%1/2
A
A= $parser->tests_planned
Aresult
11
00
210  
211 6 my $num_errors = 0;
212 6 ▶ $num_errors += $parser->todo_passed() unless $self->passing_todo_ok();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->passing_todo_ok 5 1 6
213 6 ▶ $num_errors += abs($testsrun - $planned) if ($planned);
Branch: 1 branch
BranchTrueFalseTotal
if $planned 6 0 6
214  
215 6 my $suite_err;
216 6 ▶ if ($die_msg) {
Branch: 3 branches
BranchTrueFalseTotal
if ($die_msg) { } 0 6 6
elsif ($noplan) { } 0 6 6
elsif ($planned and $testsrun != $planned) { } 0 6 6
217 0 $suite_err = $xml->error( { message => $die_msg } );
218 0 $num_errors ++;
219 }
220 elsif ($noplan) {
221 0 $suite_err = $xml->error( { message => 'No plan in TAP output' } );
222 0 $num_errors ++;
223 }
224 ▶ elsif ($planned && ($testsrun != $planned)) {
Condition33%1/3
$planned and $testsrun != $planned
!ll&&!rl&&r
060
MC/DC0%0/2
$planned and $testsrun != $planned
$planned $testsrun != $planned
Truth table33%1/3
A and B
A= $plannedB= $testsrun != $planned
ABresult
0X0
100
111
225 0 $suite_err = $xml->error( { message => "Looks like you planned $planned tests but ran $testsrun." } );
226 }
227  
228 6 my @tests = @{$self->testcases()};
229 6 ▶ my %attrs = (
Branch: 1 branch
BranchTrueFalseTotal
$timer_enabled ? : 1 5 6
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 6 my $testsuite = $xml->testsuite(\%attrs, @tests, $sys_out, $sys_err, $suite_err);
239 6 $self->formatter->add_testsuite($testsuite);
240 6 $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 6 ▶ my ($self, $testsuite) = @_;
Subroutine dump_junit_xml: called CC=3 SCAR=20.3
Pod documented
250 6 ▶ if (my $spool_dir = $ENV{PERL_TEST_HARNESS_DUMP_TAP}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spool_dir = $ENV{"PERL_TEST_HARNESS_DUMP_TAP"}) 0 6 6
251 0 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 0 $testsuite = dclone($testsuite);
256  
257 # create target dir
258 0 my ($vol, $dir, undef) = File::Spec->splitpath($spool);
259 0 my $path = File::Spec->catpath($vol, $dir, '');
260 0 mkpath($path);
261  
262 # create JUnit XML, and dump to disk
263 0 my $junit = $self->xml->xml($self->xml->testsuites($testsuite) );
264 0 ▶ my $fout = IO::File->new( $spool, '>:utf8' )
Condition0%0/2
'IO::File'->new($spool, '>:utf8') || die("Can't write $spool ( $! )\n")
l!l
00
MC/DC0%0/1
'IO::File'->new($spool, '>:utf8') || die("Can't write $spool ( $! )\n")
'IO::File'->new($spool, '>:utf8')
Truth table0%0/2
A
A= 'IO::File'->new($spool, '>:utf8')
Aresult
11
00
265 || die "Can't write $spool ( $! )\n";
266 0 $fout->print($junit);
267 0 $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 13 ▶ my $self = shift;
Subroutine xml: called CC=1 SCAR=0.0
Pod documented
278 13 return $self->formatter->xml();
279 }
280  
281 ###############################################################################
282 # Checks for bogosity in the test result.
283 sub _check_for_test_bogosity {
284 23 ▶ my $self = shift;
Subroutine _check_for_test_bogosity: called CC=5 SCAR=16.6
285 23 my $result = shift;
286  
287 23 ▶ if ($result->todo_passed() && !$self->passing_todo_ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->todo_passed and not $self->passing_todo_ok) 1 22 23
Condition100%3/3
$result->todo_passed and not $self->passing_todo_ok
!ll&&!rl&&r
2111
MC/DC100%2/2
$result->todo_passed and not $self->passing_todo_ok
$result->todo_passed not $self->passing_todo_ok
Truth table100%3/3
A and not(B)
A= $result->todo_passedB= not $self->passing_todo_ok
ABresult
0X0
100
111
288 return {
289 1 level => 'error',
290 type => 'TodoTestSucceeded',
291 message => $result->explanation(),
292 };
293 }
294  
295 22 ▶ if ($result->is_unplanned()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_unplanned) 0 22 22
296 return {
297 0 level => 'error',
298 type => 'UnplannedTest',
299 message => $result->as_string(),
300 };
301 }
302  
303 22 ▶ if (not $result->is_ok()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_ok) 0 22 22
304 return {
305 0 level => 'failure',
306 type => 'TestFailed',
307 message => $result->as_string(),
308 };
309 }
310  
311 22 return;
312 }
313  
314 ###############################################################################
315 # Generates the name for a test case.
316 sub _get_testcase_name {
317 23 ▶ my $test = shift;
Subroutine _get_testcase_name: called CC=1 SCAR=0.0
318 23 my $name = join(' ', $test->number(), _clean_test_description($test));
319 23 $name =~ s/\s+$//;
320 23 return $name;
321 }
322  
323 ###############################################################################
324 # Generates the name for the entire test suite.
325 sub _get_testsuite_name {
326 6 ▶ my $self = shift;
Subroutine _get_testsuite_name: called CC=1 SCAR=0.0
327 6 my $name = $self->name;
328 6 $name =~ s{^\./}{};
329 6 $name =~ s{^t/}{};
330 6 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 6 ▶ my $str = shift;
Subroutine _clean_to_java_class_name: called CC=1 SCAR=0.0
338 6 $str =~ s/[^-:_A-Za-z0-9]+/_/gs;
339 6 return $str;
340 }
341  
342 ###############################################################################
343 # Cleans up the description of the given test.
344 sub _clean_test_description {
345 23 ▶ my $test = shift;
Subroutine _clean_test_description: called CC=1 SCAR=0.0
346 23 my $desc = $test->description();
347 23 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 7 ▶ my ($self, $data) = @_;
Subroutine _cdata: called CC=1 SCAR=0.0
355 7 $data = _squeaky_clean($data);
356 7 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 32 ▶ my $string = shift;
Subroutine _squeaky_clean: called CC=1 SCAR=0.4
363 # control characters (except CR and LF)
364 32 $string =~ s/([\x00-\x09\x0b\x0c\x0e-\x1f])/"^".chr(ord($1)+64)/ge;
365 # high-byte characters
366 32 $string =~ s/([\x7f-\xff])/'[\\x'.sprintf('%02x',ord($1)).']'/ge;
367 32 return $string;
368 }
369  
370 6 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 <cpan@howlingfrog.com>
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
↑ Summary blib/lib/TAP/Formatter/JUnit.pm »