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

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