File Coverage

blib/lib/TAP/Harness/Archive.pm
Criterion Covered Total %
statement 160 166 96.3
branch 56 82 68.2
condition 7 14 50.0
subroutine 20 20 100.0
pod 3 3 100.0
total 246 285 86.3


line stmt bran cond sub pod time code
1             package TAP::Harness::Archive;
2 3     3   112124 use warnings;
  3         7  
  3         111  
3 3     3   12 use strict;
  3         4  
  3         73  
4 3     3   13 use base 'TAP::Harness';
  3         7  
  3         1696  
5 3     3   19805 use Cwd ();
  3         6  
  3         35  
6 3     3   11 use File::Basename ();
  3         3  
  3         29  
7 3     3   11 use File::Temp ();
  3         2  
  3         31  
8 3     3   14 use File::Spec ();
  3         3  
  3         42  
9 3     3   11 use File::Path ();
  3         4  
  3         36  
10 3     3   11 use File::Find ();
  3         4  
  3         34  
11 3     3   2214 use Archive::Tar ();
  3         249020  
  3         78  
12 3     3   2103 use TAP::Parser ();
  3         111042  
  3         66  
13 3     3   1940 use YAML::Tiny ();
  3         15038  
  3         101  
14 3     3   1852 use TAP::Parser::Aggregator ();
  3         18660  
  3         264  
15              
16             =head1 NAME
17              
18             TAP::Harness::Archive - Create an archive of TAP test results
19              
20             =cut
21              
22             our $VERSION = '0.16';
23              
24             =head1 SYNOPSIS
25              
26             use TAP::Harness::Archive;
27             my $harness = TAP::Harness::Archive->new(\%args);
28             $harness->runtests(@tests);
29              
30             =head1 DESCRIPTION
31              
32             This module is a direct subclass of L and behaves
33             in exactly the same way except for one detail. In addition to
34             outputting a running progress of the tests and an ending summary
35             it can also capture all of the raw TAP from the individual test
36             files or streams into an archive file (C<.tar> or C<.tar.gz>).
37              
38             =head1 METHODS
39              
40             All methods are exactly the same as our base L except
41             for the following.
42              
43             =head2 new
44              
45             In addition to the options that L allow to this method,
46             we also allow the following:
47              
48             =over
49              
50             =item archive
51              
52             This is the name of the archive file to generate. We use L
53             in the background so we only support C<.tar> and C<.tar.gz> archive file
54             formats. This can optionally be an existing directory that will have
55             the TAP archive's contents deposited therein without any file archiving
56             (no L involved).
57              
58             =item extra_files
59              
60             This is an array reference to extra files that you want to include in the TAP
61             archive but which are not TAP files themselves. This is useful if you want to
62             include some log files that contain useful information about the test run.
63              
64             =item extra_properties
65              
66             This is a hash reference of extra properties that you've collected during your
67             test run. Some things you might want to include are the Perl version, the system's
68             architecture, the operating system, etc.
69              
70             =back
71              
72             =cut
73              
74             my (%ARCHIVE_TYPES, @ARCHIVE_EXTENSIONS);
75             BEGIN {
76 3     3   25 %ARCHIVE_TYPES = (
77             'tar' => 'tar',
78             'tar.gz' => 'tar.gz',
79             'tgz' => 'tar.gz',
80             );
81 3         13 @ARCHIVE_EXTENSIONS = map { ".$_" } keys %ARCHIVE_TYPES;
  9         4360  
82             }
83              
84             sub new {
85 7     7 1 11945 my ($class, $args) = @_;
86 7   100     31 $args ||= {};
87              
88             # these can't be passed on to Test::Harness
89 7         25 my $archive = delete $args->{archive};
90 7         16 my $extra_files = delete $args->{extra_files};
91 7         16 my $extra_props = delete $args->{extra_properties};
92              
93 7 100       68 $class->_croak("You must provide the name of the archive to create!")
94             unless $archive;
95              
96 6         72 my $self = $class->SUPER::new($args);
97              
98 6 100       22475 my $is_directory = -d $archive ? 1 : 0;
99 6 100       41 if ($is_directory) {
100 1         4 $self->{__archive_is_directory} = $is_directory;
101 1         4 $self->{__archive_tempdir} = $archive;
102             } else {
103 5         29 my $format = $class->_get_archive_format_from_filename($archive);
104              
105             # if it's not a format we understand, or it's not a directory
106 5 100 66     49 $class->_croak("Archive is not a known format type!")
107             unless $format && $ARCHIVE_TYPES{$format};
108              
109 4         12 $self->{__archive_file} = $archive;
110 4         11 $self->{__archive_format} = $format;
111 4         24 $self->{__archive_tempdir} = File::Temp::tempdir();
112             }
113              
114             # handle any extra files
115 5 100       1327 if($extra_files) {
116 1 50       4 ref $extra_files eq 'ARRAY'
117             or $class->_croak("extra_files must be an array reference!");
118 1         3 foreach my $file (@$extra_files) {
119 3 50       29 $class->_croak("extra_file $file does not exist!") unless -e $file;
120 3 50       19 $class->_croak("extra_file $file is not readable!") unless -r $file;
121             }
122 1         4 $self->{__archive_extra_files} = $extra_files;
123             }
124              
125 5 100       15 if($extra_props) {
126 1 50       5 ref $extra_props eq 'HASH'
127             or $class->_croak("extra_properties must be a hash reference!");
128 1         2 $self->{__archive_extra_props} = $extra_props;
129             }
130              
131 5         16 return $self;
132             }
133              
134             sub _get_archive_format_from_filename {
135 5     5   12 my ($self, $filename) = @_;
136              
137             # try to guess it if we don't have one
138 5         348 my (undef, undef, $suffix) = File::Basename::fileparse($filename, @ARCHIVE_EXTENSIONS);
139 5         23 $suffix =~ s/^\.//;
140 5         60 return $ARCHIVE_TYPES{$suffix};
141             }
142              
143             =head2 runtests
144              
145             Takes the same arguments as L's version and returns the
146             same thing (a L object). The only difference
147             is that in addition to the normal test running and progress output
148             we also create the TAP Archive when it's all done.
149              
150             =cut
151              
152             sub runtests {
153 5     5 1 65 my ($self, @files) = @_;
154              
155             # tell TAP::Harness to put the raw tap someplace we can find it later
156 5         11 my $dir = $self->{__archive_tempdir};
157 5         33 $ENV{PERL_TEST_HARNESS_DUMP_TAP} = $dir;
158              
159             # get some meta information about this run
160 5         39 my %meta = (
161             file_order => \@files,
162             start_time => time(),
163             );
164              
165 5         52 my $aggregator = $self->SUPER::runtests(@files);
166              
167 5         357474 $meta{stop_time} = time();
168              
169 5         34 my @parsers = $aggregator->parsers;
170 5         111 for ( my $i = 0; $i < @parsers; $i++ ) {
171 10         95 $parsers[ $i ] = {
172             start_time => $parsers[ $i ]->start_time,
173             end_time => $parsers[ $i ]->end_time,
174             description => $files[ $i ],
175             };
176             }
177 5         63 $meta{file_attributes} = \@parsers;
178              
179 5         45 my $cwd = Cwd::getcwd();
180 5         15 my $is_dir = $self->{__archive_is_directory};
181 5         8 my ($archive, $output_file);
182 5 100       23 if( $is_dir ) {
183 1         6 $output_file = $self->{__archive_tempdir};
184             } else {
185 4         16 $output_file = $self->{__archive_file};
186              
187             # go into the dir so that we can reference files
188             # relatively and put them in the archive that way
189 4 50       201 chdir($dir) or $self->_croak("Could not change to directory $dir: $!");
190              
191 4 50       135 unless (File::Spec->file_name_is_absolute($output_file)) {
192 0         0 $output_file = File::Spec->catfile($cwd, $output_file);
193             }
194              
195             # create the archive
196 4         62 $archive = Archive::Tar->new();
197 4         109 $archive->add_files($self->_get_all_tap_files);
198 4 50       3503 chdir($cwd) or $self->_croak("Could not return to directory $cwd: $!");
199             }
200            
201             # add in any extra files
202 5 100       34 if(my $x_files = $self->{__archive_extra_files}) {
203 1         1 my @rel_x_files;
204 1         5 foreach my $x_file (@$x_files) {
205             # handle both relative and absolute file names
206 3         3 my $rel_file;
207 3 50       15 if( File::Spec->file_name_is_absolute($x_file) ) {
208 0         0 $rel_file = File::Spec->abs2rel($x_file, $cwd);
209             } else {
210 3         5 $rel_file = $x_file;
211             }
212 3         5 push(@rel_x_files, $rel_file);
213             }
214 1 50       10 $archive->add_files(@rel_x_files) unless $is_dir;
215 1         896 $meta{extra_files} = \@rel_x_files;
216             }
217              
218             # add any extra_properties to the meta
219 5 100       24 if(my $extra_props = $self->{__archive_extra_props}) {
220 1         4 $meta{extra_properties} = $extra_props;
221             }
222              
223             # create the YAML meta file
224 5         61 my $yaml = YAML::Tiny->new();
225 5         321 $yaml->[0] = \%meta;
226 5 100       18 if( $is_dir ) {
227 1         68 my $meta_file = File::Spec->catfile($output_file, 'meta.yml');
228 1 50       148 open(my $out, '>', $meta_file) or die "Could not create meta.yml: $!";
229 1         9 print $out $yaml->write_string;
230 1         543 close($out);
231             } else {
232 4         31 $archive->add_data('meta.yml', $yaml->write_string);
233 4 50       3923 $archive->write($output_file, $self->{__archive_format} eq 'tar.gz') or die $archive->errstr;
234             # be nice and clean up
235 4         37606 File::Path::rmtree($dir);
236             }
237              
238 5 50       72 print "\nTAP Archive created at $output_file\n" unless $self->verbosity < -1;
239              
240 5         507 return $aggregator;
241             }
242              
243             sub _get_all_tap_files {
244 4     4   7 my ($self, $dir, $meta) = @_;
245 4   33     42 $dir ||= $self->{__archive_tempdir};
246 4         5 my @files;
247             my %x_files;
248 4 50 33     20 if($meta && $meta->{extra_files}) {
249 0         0 %x_files = map { $_ => 1 } @{$meta->{extra_files}};
  0         0  
  0         0  
250             }
251              
252             File::Find::find(
253             {
254             no_chdir => 1,
255             wanted => sub {
256 16 50   16   55 return if /^\./;
257 16 100       1197 return if -d;
258 8         846 my $rel_name = File::Spec->abs2rel($_, $dir);
259 8 50       35 return if $rel_name eq 'meta.yml';
260 8 50       219 push(@files, $rel_name) unless $x_files{$rel_name};
261             },
262             },
263 4         488 $dir
264             );
265 4         63 return @files;
266             }
267              
268             =head2 aggregator_from_archive
269              
270             This class method will return a L object
271             when given a TAP Archive to open and parse. It's pretty much the reverse
272             of creating a TAP Archive from using C and C.
273              
274             It takes a hash of arguments which are as follows:
275              
276             =over
277              
278             =item archive
279              
280             The path to the archive file. This can also be a directory if you created
281             the archive as a directory. This is required.
282              
283             =item parser_callbacks
284              
285             This is a hash ref containing callbacks for the L objects
286             that are created while parsing the TAP files. See the L
287             documentation for details about these callbacks.
288              
289             =item made_parser_callback
290              
291             This callback is executed every time a new L object
292             is created. It will be passed the new parser object, the name
293             of the file to be parsed, and also the full (temporary) path of that file.
294              
295             =item meta_yaml_callback
296              
297             This is a subroutine that will be called if we find and parse a YAML
298             file containing meta information about the test run in the archive.
299             The structure of the YAML file will be passed in as an argument.
300              
301             =back
302              
303             my $aggregator = TAP::Harness::Archive->aggregator_from_archive(
304             {
305             archive => 'my_tests.tar.gz',
306             parser_callbacks => {
307             plan => sub { warn "Nice to see you plan ahead..." },
308             unknown => sub { warn "Your TAP is bad!" },
309             },
310             made_parser_callback => sub {
311             my ($parser, $file, $full_path) = @_;
312             warn "$file is temporarily located at $full_path\n";
313             }
314            
315             }
316             );
317              
318             =cut
319              
320             sub aggregator_from_archive {
321 5     5 1 3037 my ($class, $args) = @_;
322 5         10 my $meta;
323              
324 5 50       175 my $file = Cwd::abs_path( $args->{archive} )
325             or $class->_croak("You must provide the path to the archive!");
326              
327 5         85 my $is_directory = -d $file;
328              
329             # extract the files out into a temporary directory
330 5 100       55 my $dir = $is_directory ? $file : File::Temp::tempdir();
331 5         1873 my $cwd = Cwd::getcwd();
332 5 50       53 chdir($dir) or $class->_croak("Could not change to directory $dir: $!");
333 5         8 my @files;
334              
335 5 100       43 Archive::Tar->new()->extract_archive($file) unless $is_directory;
336 5         69567 my @tap_files;
337              
338             # do we have a meta.yml file in the archive?
339 5         76 my $yaml_file = File::Spec->catfile($dir, 'meta.yml');
340 5 50       107 if( -e $yaml_file) {
341              
342             # parse it into a structure
343 5         68 $meta = YAML::Tiny->new()->read($yaml_file);
344 5 50       39121 die "Could not read YAML $yaml_file: " . YAML::Tiny->errstr if YAML::Tiny->errstr;
345              
346 5 50       1193 if($args->{meta_yaml_callback}) {
347 5         29 $args->{meta_yaml_callback}->($meta);
348             }
349 5         5892 $meta = $meta->[0];
350              
351 5 50 33     66 if($meta->{file_order} && ref $meta->{file_order} eq 'ARRAY') {
352 5         10 foreach my $file (@{$meta->{file_order}}) {
  5         24  
353 10 50       150 push(@tap_files, $file) if -e $file;
354             }
355             }
356             }
357              
358             # if we didn't get the files from the YAML file, just find them all
359 5 50       34 unless(@tap_files) {
360 0         0 @tap_files = $class->_get_all_tap_files($dir, $meta);
361             }
362              
363             # now create the aggregator
364 5         89 my $aggregator = TAP::Parser::Aggregator->new();
365 5         320 foreach my $tap_file (@tap_files) {
366 10 50       919 open(my $fh, $tap_file) or die "Could not open $tap_file for reading: $!";
367 10         114 my $parser = TAP::Parser->new({source => $fh, callbacks => $args->{parser_callbacks}});
368 10 100       4339 if($args->{made_parser_callback}) {
369 6         89 $args->{made_parser_callback}->($parser, $tap_file, File::Spec->catfile($dir, $tap_file));
370             }
371 10         5478 $parser->run;
372 10         10561 $aggregator->add($tap_file, $parser);
373             }
374              
375             # be nice and clean up
376 5 50       542 chdir($cwd) or $class->_croak("Could not return to directory $cwd: $!");
377 5 100       3364 File::Path::rmtree($dir) unless $is_directory;
378              
379 5         105 return $aggregator;
380             }
381              
382             =head1 AUTHOR
383              
384             Michael Peters, C<< >>
385              
386             =head1 BUGS
387              
388             Please report any bugs or feature requests to
389             C, or through the web interface at
390             L.
391             I will be notified, and then you'll automatically be notified of progress on
392             your bug as I make changes.
393              
394             =head1 SUPPORT
395              
396             You can find documentation for this module with the perldoc command.
397              
398             perldoc TAP::Harness::Archive
399              
400             You can also look for information at:
401              
402             =over 4
403              
404             =item * AnnoCPAN: Annotated CPAN documentation
405              
406             L
407              
408             =item * CPAN Ratings
409              
410             L
411              
412             =item * RT: CPAN's request tracker
413              
414             L
415              
416             =item * Search CPAN
417              
418             L
419              
420             =back
421              
422             =head1 ACKNOWLEDGEMENTS
423              
424             =over
425              
426             =item * A big thanks to Plus Three, LP (L) for sponsoring my work on this module and other open source pursuits.
427              
428             =item * Andy Armstrong
429              
430             =back
431              
432             =head1 COPYRIGHT & LICENSE
433              
434             Copyright 2007 Michael Peters, all rights reserved.
435              
436             This program is free software; you can redistribute it and/or modify it
437             under the same terms as Perl itself.
438              
439             =cut
440              
441             1; # End of TAP::Harness::Archive