blib/lib/TAP/Harness/Archive/MultipleHarnesses.pm

filter: Statementstmt 97.5% 81 / 83 Branchbran 78.5% 22 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 93.2% 111 / 119 CC 19 SCAR 27.0
CC
19
Cov
93%
runtests
19.2
BEGIN
1.0
BEGIN
1.0
CRAP 14.9
« lib/TAP/Harness/ReportByDescription.pm ↑ Summary t/testlib/alpha.t »
1 package TAP::Harness::Archive::MultipleHarnesses;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use base 'TAP::Harness::Archive';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use lib qw( ./lib );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use TAP::Harness::ReportByDescription;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION = '0.08';
9  
10 7 ▶sub runtests {
Subroutine runtests: called CC=19 SCAR=29.6
Pod documented
11 7 my ($self, $targetsref, @files) = @_;
12  
13 # tell TAP::Harness to put the raw tap someplace we can find it later
14 7 my $dir = $self->{__archive_tempdir};
15 7 $ENV{PERL_TEST_HARNESS_DUMP_TAP} = $dir;
16  
17 # get some meta information about this run
18 7 my @test_labels = ();
19 7 foreach my $subharness (@{$targetsref}) {
20 14 my %har = %$subharness;
21 14 my @tests = @{$har{tests}};
22 14 foreach my $test (@tests) {
23 28 push @test_labels, $test->[1];
24 }
25 }
26 7 my %meta = (
27 file_order => \@test_labels,
28 start_time => time(),
29 );
30  
31 7 my $aggregator = TAP::Parser::Aggregator->new;
32  
33 7 $aggregator->start();
34 7 foreach my $set (@{$targetsref}) {
35 # rewrite environment
36 14 ▶ &{$set->{rule}} if defined $set->{rule};
Branch: 1 branch
BranchTrueFalseTotal
if defined $set->{"rule"} 12 2 14
37 14 my $harness = TAP::Harness::ReportByDescription->new();
38 14 $harness->aggregate_tests($aggregator, @{$set->{tests}});
39 }
40 7 $aggregator->stop();
41  
42 7 $meta{stop_time} = time();
43  
44 7 my @parsers = $aggregator->parsers;
45 7 ▶ for ( my $i = 0; $i < @parsers; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @parsers) 28 7 35
46 28 $parsers[ $i ] = {
47 start_time => $parsers[ $i ]->start_time,
48 end_time => $parsers[ $i ]->end_time,
49 description => $test_labels[ $i ],
50 };
51 }
52 7 $meta{file_attributes} = \@parsers;
53  
54 7 my $cwd = Cwd::getcwd();
55 7 my $is_dir = $self->{__archive_is_directory};
56 7 my ($archive, $output_file);
57 7 ▶ if( $is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) { } 1 6 7
58 1 $output_file = $self->{__archive_tempdir};
59 }
60 else {
61 6 $output_file = $self->{__archive_file};
62  
63 # go into the dir so that we can reference files
64 # relatively and put them in the archive that way
65 6 ▶ chdir($dir) or $self->_croak("Could not change to directory $dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or $self->_croak("Could not change to directory ${dir}: $!") 6 0 6
66  
67 6 ▶ unless (File::Spec->file_name_is_absolute($output_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($output_file)) 0 6 6
68 0 $output_file = File::Spec->catfile($cwd, $output_file);
69 }
70  
71 # create the archive
72 6 $archive = Archive::Tar->new();
73 6 $archive->add_files($self->_get_all_tap_files);
74 6 ▶ chdir($cwd) or $self->_croak("Could not return to directory $cwd: $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or $self->_croak("Could not return to directory ${cwd}: $!") 6 0 6
75 }
76  
77 # add in any extra files
78 7 ▶ if(my $x_files = $self->{__archive_extra_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x_files = $self->{"__archive_extra_files"}) 6 1 7
79 6 my @rel_x_files;
80 6 foreach my $x_file (@$x_files) {
81 # handle both relative and absolute file names
82 12 my $rel_file;
83 12 ▶ if( File::Spec->file_name_is_absolute($x_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($x_file)) { } 0 12 12
84 0 $rel_file = File::Spec->abs2rel($x_file, $cwd);
85 }
86 else {
87 12 $rel_file = $x_file;
88 }
89 12 push(@rel_x_files, $rel_file);
90 }
91 6 ▶ $archive->add_files(@rel_x_files) unless $is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_dir 5 1 6
92 6 $meta{extra_files} = \@rel_x_files;
93 }
94  
95 # add any extra_properties to the meta
96 7 ▶ if(my $extra_props = $self->{__archive_extra_props}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_props = $self->{"__archive_extra_props"}) 6 1 7
97 6 $meta{extra_properties} = $extra_props;
98 }
99  
100 # create the YAML meta file
101 7 my $yaml = YAML::Tiny->new();
102 7 $yaml->[0] = \%meta;
103 7 ▶ if( $is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) { } 1 6 7
104 1 my $meta_file = File::Spec->catfile($output_file, 'meta.yml');
105 1 ▶ open(my $out, '>', $meta_file) or die "Could not create meta.yml: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $meta_file or die "Could not create meta.yml: $!" 1 0 1
106 1 print $out $yaml->write_string;
107 1 close($out);
108 }
109 else {
110 6 $archive->add_data('meta.yml', $yaml->write_string);
111 6 ▶ $archive->write($output_file, $self->{__archive_format} eq 'tar.gz') or die $archive->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$archive->write($output_file, $self->{"__archive_format"} eq "tar.gz") or die $archive->errstr 6 0 6
112 # be nice and clean up
113 6 File::Path::rmtree($dir);
114 }
115  
116 print "\nTAP Archive created at $output_file\n"
117 7 ▶ unless $self->{formatter}->{verbosity} < -1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"formatter"}{"verbosity"} < -1 6 1 7
118  
119 7 return $aggregator;
120 }
121  
122 2 1;
123  
124 =head1 NAME
125  
126 TAP::Harness::Archive::MultipleHarnesses - Create an archive of multiple
127 harnesses of TAP test results
128  
129 =cut
130  
131 =head1 SYNOPSIS
132  
133 use TAP::Harness::Archive::MultipleHarnesses;
134 my $archive = TAP::Harness::Archive::MultipleHarnesses->new(\%args);
135 $archive->runtests(\@targets);
136  
137 =head1 DESCRIPTION
138  
139 This package subclasses Michael Peters' TAP::Harness::Archive package from
140 CPAN. It provides its own C<runtests()> method for the case where you need to
141 create an archive of test results generated by running multiple harnesses
142 sequentially.
143  
144 For a discussion of use cases for this functionality, see the documentation
145 for TAP::Harness::ReportByDescription.
146  
147 perldoc TAP::Harness::ReportByDescription
148  
149 =head1 METHODS
150  
151 =head2 C<new()>
152  
153 Inherited from Test::Harness::Archive.
154  
155 =head2 C<runtests()>
156  
157 Replaces C<Test::Harness::Archive::runtests()>. B<Note that its interface is
158 different from other packages' C<runtests()> interface: It takes a reference
159 to an array of hash references rather than a simple array.>
160  
161 Each hash reference holds information on how a particular set of tests is to
162 be run. The various sets are run and placed into the archive in the order in
163 which they appear in the array.
164  
165 Each hash reference needs three elements:
166  
167 =over 4
168  
169 =item * C<tests>
170  
171 A list of tests to be run (typically expressed as a list of file glob
172 patterns).
173  
174 =item * C<rule>
175  
176 A reference to a subroutine which will be run before a given set of tests is
177 executed. The purpose of this subroutine is to set up the environmental
178 variables as needed for a particular subharness.
179  
180 =item * C<label>
181  
182 A string describing a particular subharness which will be combined with a
183 particular test file's name to form the description of the test both in STDOUT
184 and in the test archive.
185  
186 =back
187  
188 =head2 C<summary()>
189  
190 Inherited from Test::Harness::Archive.
191  
192 =head1 EXAMPLE
193  
194 Adapted (simplified) from Parrot's C<t/fullharness>.
195  
196 use Parrot::Harness::Smoke qw( collect_test_environment_data );
197 use TAP::Harness::Archive::MultipleHarnesses;
198  
199 sub set_runcore_target {
200 my ($target) = @_;
201 return {
202 label => "test$target",
203 rule => sub { set_runcore_environmental_args($target) },
204 tests => [
205 map { [ $_, "test${alt}__$_", ] }
206 @Parrot::Harness::TestSets::runcore_test_files
207 ],
208 };
209 }
210 my @targets = map { set_runcore_target($_) } ( qw| b f r | );
211 my %env_data = collect_test_environment_data();
212  
213 my $archive = TAP::Harness::Archive::MultipleHarnesses->new( {
214 verbosity => $ENV{HARNESS_VERBOSE},
215 archive => 'parrot_test_run.tar.gz',
216 merge => 1,
217 jobs => $ENV{TEST_JOBS} || 1,
218 extra_properties => \%env_data,
219 extra_files => [ 'myconfig', 'config_lib.pir' ],
220 } );
221 my $overall_aggregator = $archive->runtests(\@targets);
222 $archive->summary($overall_aggregator);
223  
224 =head1 AUTHOR
225  
226 This code was derived from Michael Peters' Test::Harness::Archive distribution
227 on CPAN, as well as examples in the documentation for TAP::Harness,
228 TAP::Parser, TAP::Parser::Aggregator and other CPAN modules. Documentation
229 and code assemblage by James E Keenan.
230  
231 =head1 LICENSE
232  
233 This is free software and is released under the same terms as Perl itself.
234  
235 =cut
236  
237 # vim:ts=4:sw=4:et:sta
« lib/TAP/Harness/ReportByDescription.pm ↑ Summary t/testlib/alpha.t »