BambooExtended.pm

filter: Statementstmt 25.4% 29 / 114 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 60.0% 9 / 15 Podpod 66.6% 2 / 3 totaltotal 22.3% 40 / 179 CC 25 SCAR 46.1
CC
25
Cov
18%
_save_results
240.0
summary
30.0
_fail_reasons
30.0
CRAP 100.9
↑ Summary BambooExtended/Session.pm »
1 package TAP::Formatter::BambooExtended;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use parent qw(TAP::Formatter::Console);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode qw(is_utf8 decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTML::Entities qw(encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use TAP::Formatter::BambooExtended::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '1.01';
17  
18 0 ▶sub _initialize {
Subroutine _initialize: not called CC=2 SCAR=17.9
19 0 my ($self, $arg_for) = @_;
20  
21 # variables that we use for ourselves
22 0 $self->{'_test_results'} = [];
23  
24 0 ▶ return $self->SUPER::_initialize($arg_for || {});
Condition0%0/2
$arg_for || {}
l!l
00
MC/DC0%0/1
$arg_for || {}
$arg_for
Truth table0%0/2
A
A= $arg_for
Aresult
11
00
25 }
26  
27 0 ▶sub add_test_results {
Subroutine add_test_results: not called CC=2 SCAR=17.9
Pod undocumented
28 0 my ($self, $results) = @_;
29 0 ▶ push(@{$self->{'_test_results'}}, $results) if defined($results);
Branch: 1 branch
BranchTrueFalseTotal
if defined $results 0 0 0
30 0 return;
31 }
32  
33 0 ▶sub open_test {
Subroutine open_test: not called CC=1 SCAR=6.9
Pod documented
34 0 my ($self, $test, $parser) = @_;
35 0 my $session = TAP::Formatter::BambooExtended::Session->new({
36 'name' => $test,
37 'formatter' => $self,
38 'parser' => $parser,
39 });
40 0 return $session;
41 }
42  
43 0 ▶sub summary {
Subroutine summary: not called CC=5 SCAR=34.0
Pod documented
44 0 my ($self, $aggregate) = @_;
45  
46 0 my $output_path = Cwd::cwd() . "/prove_db";
47 0 ▶ $output_path = $ENV{'FORMATTER_OUTPUT_DIR'} if defined($ENV{'FORMATTER_OUTPUT_DIR'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"FORMATTER_OUTPUT_DIR"} 0 0 0
48 0 ▶ File::Path::make_path($output_path) unless (-e $output_path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $output_path 0 0 0
49  
50 0 for my $test (@{$self->{'_test_results'}}) {
51 0 my $test_name = $test->{'description'};
52 0 $test_name =~ s/^[\.\/\\]+//g;
53 0 $test_name =~ s/\/|\\/-/g;
54 0 $test_name =~ s/\./_/g;
55 0 $self->_save_results($test, "${output_path}/${test_name}.xml");
56 }
57  
58 0 ▶ return if $self->silent();
Branch: 1 branch
BranchTrueFalseTotal
if $self->silent 0 0 0
59  
60 0 print { $self->stdout } "ALL DONE\n";
61 0 return;
62 }
63  
64 0 ▶sub _save_results {
Subroutine _save_results: not called CC=15 SCAR=54.8
65 0 my ($self, $test, $file_path) = @_;
66 0 my $doc = XML::LibXML::Document->createDocument('1.0', 'UTF-8');
67  
68 0 my $testsuite_name = $test->{'description'};
69 0 $testsuite_name =~ s/^[\.\/\\]+//g;
70 0 $testsuite_name =~ s/\/|\\/-/g;
71 0 $testsuite_name =~ s/\./_/g;
72 0 $testsuite_name =~ s/^\s+|\s+$//g;
73  
74 0 my $suite = $doc->createElement('testsuite');
75 0 $suite->setAttribute('name', $testsuite_name);
76 0 $suite->setAttribute('errors', $test->{'parse_errors'});
77 0 $suite->setAttribute('failures', $test->{'failed'});
78 0 $suite->setAttribute('tests', $test->{'tests_run'});
79 0 $suite->setAttribute('time', $test->{'end_time'} - $test->{'start_time'});
80  
81 0 my $skipped = 0;
82 0 for my $result (@{$test->{'results'}}) {
83 0 ▶ next unless ($result->is_test() || $result->is_bailout());
Branch: 1 branch
BranchTrueFalseTotal
unless $result->is_test or $result->is_bailout 0 0 0
Condition0%0/3
$result->is_test or $result->is_bailout
l!l&&r!l&&!r
000
MC/DC0%0/2
$result->is_test or $result->is_bailout
$result->is_test $result->is_bailout
Truth table0%0/3
A or B
A= $result->is_testB= $result->is_bailout
ABresult
1X1
011
000
84  
85 # bump the skip count?
86 0 ▶ ++$skipped if ($result->has_skip());
Branch: 1 branch
BranchTrueFalseTotal
if $result->has_skip 0 0 0
87  
88 # give it a name if there isn't one
89 0 my $testcase_name = $result->description();
90  
91 # clean up invalid characters
92 0 ▶ $testcase_name = decode("UTF-8", $testcase_name) unless is_utf8($testcase_name);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($testcase_name) 0 0 0
93 0 $testcase_name = encode_entities($testcase_name);
94  
95 # trim trailing/leading space
96 0 $testcase_name =~ s/^\s+|\s+$//g;
97  
98 0 my $testcase = $doc->createElement('testcase');
99 0 ▶ $testcase->setAttribute('name', $testcase_name || "test ${\$result->number()}");
Condition0%0/2
$testcase_name || "test ${\$result->number;}"
l!l
00
MC/DC0%0/1
$testcase_name || "test ${\$result->number;}"
$testcase_name
Truth table0%0/2
A
A= $testcase_name
Aresult
11
00
100  
101 0 my @fail_reasons = _fail_reasons($result);
102 0 ▶ if (scalar(@fail_reasons)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @fail_reasons) 0 0 0
103 0 my $failure = $doc->createElement('failure');
104 0 my $fail_description = '';
105  
106 0 $fail_description .= "Fail reason(s):\n";
107 0 for my $fail (@fail_reasons) {
108 0 $fail_description .= " $fail\n";
109 }
110  
111 0 my $explanation = $result->explanation();
112 0 ▶ if (defined($explanation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explanation) 0 0 0
113 0 chomp($explanation);
114 0 $explanation =~ s/^\s+|\s+$//g;
115 0 ▶ $explanation = decode("UTF-8", $explanation) unless is_utf8($explanation);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($explanation) 0 0 0
116 0 ▶ $fail_description .= "Explanation:\n " . $explanation . "\n" if ($explanation);
Branch: 1 branch
BranchTrueFalseTotal
if $explanation 0 0 0
117 }
118  
119 0 my $output = $result->raw();
120 0 ▶ if (defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) 0 0 0
121 0 chomp($output);
122 0 $output =~ s/^\s+|\s+$//g;
123 0 ▶ $output = decode("UTF-8", $output) unless is_utf8($output);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($output) 0 0 0
124 0 ▶ $fail_description .= "Test output:\n " . $output . "\n" if ($output);
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 0 0
125 }
126  
127 0 $failure->appendChild(XML::LibXML::CDATASection->new($fail_description));
128 0 $testcase->appendChild($failure);
129 }
130  
131 0 $suite->appendChild($testcase);
132 }
133  
134 0 $suite->setAttribute('skipped', $skipped);
135 0 $doc->setDocumentElement($suite);
136 0 $doc->toFile($file_path, 2);
137  
138 0 return;
139 }
140  
141 0 ▶sub _fail_reasons {
Subroutine _fail_reasons: not called CC=5 SCAR=34.0
142 0 my $result = shift;
143 0 my @reasons = ();
144  
145 0 ▶ if (!$result->is_actual_ok()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_actual_ok) 0 0 0
146 0 push(@reasons, "failed test");
147 }
148 0 ▶ if ($result->todo_passed()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->todo_passed) 0 0 0
149 0 push(@reasons, "unexpected TODO passed");
150 }
151 0 ▶ if ($result->is_unplanned()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_unplanned) 0 0 0
152 0 push(@reasons, "unplanned test");
153 }
154  
155 0 ▶ return wantarray ? @reasons : \@reasons;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
156 }
157  
158 1 1;
159  
160 =encoding utf8
161  
162 =head1 NAME
163  
164 TAP::Formatter::BambooExtended - Harness output delegate for Atlassian's Bamboo CI server
165  
166 =head1 SYNOPSIS
167  
168 On the command line, with F<prove>:
169  
170 prove --formatter TAP::Formatter::BambooExtended ...
171  
172 Or, in your own scripts:
173  
174 use TAP::Harness;
175 my $harness = TAP::Harness->new({
176 formatter_class => 'TAP::Formatter::BambooExtended',
177 merge => 1,
178 });
179 $harness->runtests(@tests);
180  
181 =head1 DESCRIPTION
182  
183 C<TAP::Formatter::BambooExtended> provides JUnit output formatting for C<TAP::Harness>,
184 which can be used in Atlassian's Bamboo CI server or any other CI server that
185 looks for JUnit files.
186  
187 This module is based on TAP::Formatter::Bamboo by Piotr Piatkowski
188 <pp@idea7.pl>, main differences are:
189  
190 =over
191  
192 =item Resulting XML is saved as one output file per source test script.
193  
194 =item Each test gets its own result line in the JUnit output rather than
195 grouping all the tests from one test script into one result.
196  
197 =item A summary test result is appended to indicate if there were any problems
198 with the test script itself outside of individual tests.
199  
200 =item Output of failed tests are attached to the test that failed AND the test
201 script itself. Each test script will create one JUnit compatible test result
202 file. The test result file names will match the full path and file name of the
203 test script. By default these files are created in a directory called
204 C<prove_db> that is created in your current working directory. This can be
205 changed by setting the environment variable C<FORMATTER_OUTPUT_DIR> to a
206 relative or absolute path.
207  
208 =back
209  
210 By way of example, when you run a test like this:
211  
212 prove -l --formatter TAP::Formatter::BambooExtended
213  
214 You might see these results on the command line:
215  
216 PASS t/00-load.t
217 ALL DONE
218  
219 Then you'll see a new directory called C<$ENV{'FORMATTER_OUTPUT_DIR'}>. By
220 default, this directory will be created as C<prove_db> in your current working
221 directory. In the output directory you'll see one file for each test script,
222 like this:
223  
224 > ls
225 t-00-load_t.xml
226  
227 In that file you will see one test output for the file itself, named after the
228 file. You'll also see one test output for each individual test in the test
229 script. So if your test script has twenty C<ok> statements, you'll have twenty-
230 one tests in Bamboo -- one for the file itself and then one for each C<ok>
231 statement. This makes it easier to track exactly which tests are failing with
232 Bamboo.
233  
234 =head1 AUTHOR
235  
236 Paul Lockaby <plockaby@cpan.org>
237  
238 Piotr Piatkowski <pp@idea7.pl> (original C<TAP::Formatter::Bamboo>)
239  
240 Graham TerMarsch <cpan@howlingfrog.com> (original C<TAP::Formatter::JUnit>)
241  
242 =head1 COPYRIGHT
243  
244 This is free software; you can redistribute it and/or modify it under the same
245 terms as Perl itself.
246  
247 =head1 SEE ALSO
248  
249 L<TAP::Formatter::Bamboo>,
250 L<TAP::Formatter::JUnit>,
251 L<TAP::Formatter::Console>,
252 L<http://confluence.atlassian.com/display/BAMBOO/JUnit+parsing+in+Bamboo>.
253  
254 =cut
↑ Summary BambooExtended/Session.pm »