t/TestLib.pm

filter: Statementstmt 22.2% 35 / 157 Branchbran 2.3% 2 / 84 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 42.8% 9 / 21 Podpod 100% 12 / 12 totaltotal 18.8% 58 / 307 CC 51 SCAR 41.3
CC
51
Cov
14%
find_untargz
182.0
vms_untargz
90.0
do_untargz
56.0
CRAP 62.1
« blib/lib/Test/Smoke/Mailer.pm ↑ Summary blib/lib/Test/Smoke/Syncer/Snapshot.pm »
1 package TestLib;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '0.06';
5  
6 2 ▶use Test::Smoke::Util qw( whereis );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our @EXPORT = qw(
10 &whereis
11 &find_a_patch
12 &find_unzip &do_unzip
13 &find_untargz &do_untargz
14 &manify_path
15 &get_dir &get_file &put_file
16 &rmtree &mkpath
17 );
18  
19 =head1 NAME
20  
21 TestLib - Stuff to help the test-suite
22  
23 =head1 SYNOPSIS
24  
25 use TestLib;
26  
27 =head1 DESCRIPTION
28  
29 What is in here?
30  
31 =over 4
32  
33 =cut
34  
35 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use File::Spec::Functions qw( :DEFAULT abs2rel rel2abs
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 splitdir splitpath catpath);
39 2 require File::Path;
40 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 =item manify_path( $path )
44  
45 Do a OS-specific split on the path, and join with '/' for MANIFEST
46 format.
47  
48 =cut
49  
50 0 ▶sub manify_path($) {
Subroutine manify_path: not called CC=4 SCAR=30.0
Pod documented
51 0 ▶ my $path = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or return 0 0 0
52 # There should be no volume on these file_paths
53 0 $path = File::Spec->canonpath( $path );
54 0 my( undef, $dirs, $file ) = File::Spec->splitpath( $path );
55  
56 0 ▶ my @subdirs = grep $_ && length $_ => File::Spec->splitdir( $dirs );
Condition0%0/3
$_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && length $_
$_ length $_
Truth table0%0/3
A && B
A= $_B= length $_
ABresult
0X0
100
111
57 0 ▶ $^O eq 'VMS' and $file =~ s/\.$//;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq "VMS" and $file =~ s/\.$// 0 0 0
58  
59 0 push @subdirs, $file;
60  
61 0 return join '/', @subdirs;
62 }
63  
64 =item get_dir( $path )
65  
66 Returns a list of filenames (no directory-names) in C<$path>.
67  
68 =cut
69  
70 0 ▶sub get_dir($) {
Subroutine get_dir: not called CC=3 SCAR=24.8
Pod documented
71 0 my( $path ) = @_;
72 0 my $cwd = cwd();
73 0 ▶ chdir $path or die "Cannot chdir($path): $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $path or die "Cannot chdir($path): $!" 0 0 0
74 0 my @files;
75 0 ▶ find sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
76 0 ▶ -f or return;
Branch: 1 branch
BranchTrueFalseTotal
-f $_ or return 0 0 0
77 0 my $cname = File::Spec->canonpath( $File::Find::name );
78 0 push @files, $cname;
79 0 }, '.';
80  
81 0 ▶ chdir $cwd or die "Cannot chdir($cwd) back: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or die "Cannot chdir($cwd) back: $!" 0 0 0
82 0 return @files;
83 }
84  
85 =item get_file( @path )
86  
87 The contents of C<@path> are passed to B<< File::Spec->catfile() >>
88  
89 Returns the contents of a file, takes note of context (scalar/list).
90  
91 =cut
92  
93 0 ▶sub get_file {
Subroutine get_file: not called CC=3 SCAR=24.8
Pod documented
94 0 my $filename = File::Spec->catfile( @_ );
95  
96 0 local *MYFILE;
97 0 my @content;
98 0 ▶ if ( open MYFILE, "< $filename" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open MYFILE, "< $filename") { } 0 0 0
99 0 @content = <MYFILE>;
100 0 close MYFILE;
101 } else {
102 0 Carp::carp("(@{[cwd]})$filename: $!");
103 }
104  
105 0 ▶ return wantarray ? @content : join "", @content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
106 }
107  
108 =item put_file( $content, @path )
109  
110 The contents of C<@path> are passed to B<< File::Spec->catfile() >>
111  
112 Writes C<$content> to that file and returns the success/failure.
113  
114 =cut
115  
116 4 ▶sub put_file {
Subroutine put_file: called CC=3 SCAR=13.7
Pod documented
117 4 my $contents = shift;
118 4 my $filename = File::Spec->catfile( @_ );
119  
120 4 local *MYFILE;
121 4 ▶ if ( open MYFILE, "> $filename" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open MYFILE, "> $filename") { } 4 0 4
122 4 print MYFILE $contents;
123 4 ▶ close MYFILE or do {
Branch: 1 branch
BranchTrueFalseTotal
close MYFILE or do { warn "Cannot close (@{[cwd()];})${filename}: $!"; return } 4 0 4
124 0 warn "Cannot close (@{[cwd]})$filename: $!";
125 0 return;
126 };
127 } else {
128 0 warn "Cannot create (@{[cwd]})$filename: $!";
129 0 return;
130 }
131  
132 4 return 1;
133 }
134  
135 =item rmtree( @_ )
136  
137 This is B<< File::Path::rmtree() >>.
138  
139 =cut
140  
141 0 ▶sub rmtree { File::Path::rmtree( @_ ) }
Subroutine rmtree: not called CC=1 SCAR=6.9
Pod documented
142  
143 =item mkpath( @_ )
144  
145 This is B<< File::Path::mkpath() >>.
146  
147 =cut
148  
149 0 ▶sub mkpath { File::Path::mkpath( @_ ) }
Subroutine mkpath: not called CC=1 SCAR=6.9
Pod documented
150  
151 =item find_a_patch()
152  
153 Loop over some known names for gnu-patch and see if they know about --version.
154  
155 =cut
156  
157 0 ▶sub find_a_patch {
Subroutine find_a_patch: not called CC=5 SCAR=34.0
Pod documented
158  
159 0 my $patch_bin;
160 0 foreach my $patch (qw( gpatch npatch patch )) {
161 0 ▶ $patch_bin = whereis( $patch ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$patch_bin = whereis($patch) or next 0 0 0
162 0 ▶ my $quoted = $patch_bin =~ /\s/ ? qq{"$patch_bin"} : $patch_bin;
Branch: 1 branch
BranchTrueFalseTotal
$patch_bin =~ /\s/ ? : 0 0 0
163 0 my $version = `$quoted --version 2>&1`;
164 0 ▶ $? or return $quoted;
Branch: 1 branch
BranchTrueFalseTotal
$? or return $quoted 0 0 0
165 }
166 }
167  
168 =item find_unzip()
169  
170 Check C<< wheris( 'gzip' ) >> or C<< eval{ require Compress::Zlib } >>.
171  
172 =cut
173  
174 0 ▶sub find_unzip {
Subroutine find_unzip: not called CC=6 SCAR=37.4
Pod documented
175 0 my $unzip = whereis( 'gzip' );
176 0 ▶ my $qunzip = defined $unzip && $unzip =~ /\s/ ? qq{"$unzip"} : $unzip;
Branch: 1 branch
BranchTrueFalseTotal
defined $unzip && $unzip =~ /\s/ ? : 0 0 0
Condition0%0/3
defined $unzip and $unzip =~ /\s/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $unzip and $unzip =~ /\s/
defined $unzip $unzip =~ /\s/
Truth table0%0/3
A and B
A= defined $unzipB= $unzip =~ /\s/
ABresult
0X0
100
111
177  
178 0 ▶ my $dounzip = $unzip ? "$qunzip -cd " : "";
Branch: 1 branch
BranchTrueFalseTotal
$unzip ? : 0 0 0
179  
180 0 ▶ unless ( $dounzip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dounzip) 0 0 0
181 0 eval { require Compress::Zlib };
182 0 ▶ $dounzip = 'Compress::Zlib' unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
183 }
184  
185 0 return $dounzip;
186 }
187  
188 =item do_unzip( $unzip, $uzfile )
189  
190 Returns the gunzipped contents of C<$uzfile>.
191  
192 =cut
193  
194 0 ▶sub do_unzip {
Subroutine do_unzip: not called CC=6 SCAR=37.4
Pod documented
195 0 my( $unzip, $uzfile ) = @_;
196 0 ▶ return undef unless -f $uzfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $uzfile 0 0 0
197  
198 0 my $content;
199 0 ▶ if ( $unzip eq 'Compress::Zlib' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unzip eq 'Compress::Zlib') { } 0 0 0
200 0 require Compress::Zlib;
201 0 ▶ my $unzipper = Compress::Zlib::gzopen( $uzfile, 'rb' ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $unzipper = Compress::Zlib::gzopen($uzfile, "rb") or do { require Carp; Carp::carp("Can't open '${uzfile}': $Compress::Zlib::gzerrno"); return undef } 0 0 0
202 0 require Carp;
203 0 Carp::carp( "Can't open '$uzfile': $Compress::Zlib::gzerrno" );
204 0 return undef;
205 };
206  
207 0 my $buffer;
208 0 ▶ $content .= $buffer while $unzipper->gzread( $buffer ) > 0;
Branch: 1 branch
BranchTrueFalseTotal
$unzipper->gzread($buffer) > 0 and do { $content .= $buffer } 0 0 0
209  
210 0 ▶ unless ( $Compress::Zlib::gzerrno == Compress::Zlib::Z_STREAM_END() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Compress::Zlib::gzerrno == Compress::Zlib::Z_STREAM_END()) 0 0 0
211 0 require Carp;
212 0 Carp::carp( "Error reading '$uzfile': $Compress::Zlib::gzerrno" );
213 }
214  
215 0 $unzipper->gzclose;
216 } else {
217  
218 # this calls out for `$unzip $uzfile`
219 # {unzip} could be like 'zcat', 'gunzip -c', 'gzip -dc'
220  
221 0 $content = `$unzip $uzfile`;
222 }
223  
224 0 return $content;
225  
226 }
227  
228 =item find_untargz()
229  
230 Find either B<gzip>/B<tar> or B<Compress::Zlib>/B<Archive::Tar>
231  
232 =cut
233  
234 0 ▶sub find_untargz {
Subroutine find_untargz: not called CC=13 SCAR=52.0
Pod documented
235 0 my $tar = whereis( 'tar' );
236 0 ▶ my $qtar = defined $tar && $tar =~ /\s/ ? qq{"$tar"} : $tar;
Branch: 1 branch
BranchTrueFalseTotal
defined $tar && $tar =~ /\s/ ? : 0 0 0
Condition0%0/3
defined $tar and $tar =~ /\s/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tar and $tar =~ /\s/
defined $tar $tar =~ /\s/
Truth table0%0/3
A and B
A= defined $tarB= $tar =~ /\s/
ABresult
0X0
100
111
237  
238 0 my $uncompress = '';
239 0 ▶ if ( $tar ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tar) 0 0 0
240 0 my $zip = whereis( 'gzip' );
241 0 ▶ my $qzip = defined $zip && $zip =~ /\s/ ? qq{"$zip"} : $zip;
Branch: 1 branch
BranchTrueFalseTotal
defined $zip && $zip =~ /\s/ ? : 0 0 0
Condition0%0/3
defined $zip and $zip =~ /\s/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $zip and $zip =~ /\s/
defined $zip $zip =~ /\s/
Truth table0%0/3
A and B
A= defined $zipB= $zip =~ /\s/
ABresult
0X0
100
111
242 0 ▶ $uncompress = "$qzip -cd %s | $qtar -xf -" if $zip;
Branch: 1 branch
BranchTrueFalseTotal
if $zip 0 0 0
243 }
244  
245 0 ▶ unless ( $uncompress ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uncompress) 0 0 0
246 0 eval { require Archive::Tar; };
247 0 ▶ unless ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
248 0 ▶ if ( $Archive::Tar::VERSION >= 0.99 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::VERSION >= 0.99) { } 0 0 0
249 0 eval { require IO::Zlib };
250 } else {
251 0 eval { require Compress::Zlib };
252 }
253 0 ▶ $uncompress = 'Archive::Tar' unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
254 }
255 }
256  
257 0 ▶ if ( $tar && !$uncompress ) { # try tar by it self
Branch: 1 branch
BranchTrueFalseTotal
if ($tar and not $uncompress) 0 0 0
Condition0%0/3
$tar and not $uncompress
!ll&&!rl&&r
000
MC/DC0%0/2
$tar and not $uncompress
$tar not $uncompress
Truth table0%0/3
A and not(B)
A= $tarB= not $uncompress
ABresult
0X0
100
111
258 0 $uncompress = "$qtar -xzf %s";
259 }
260  
261 0 return $uncompress;
262 }
263  
264 =item do_untargz( $untargz, $tgzfile )
265  
266 Gunzip and extract the archive in C<$tgzfile>.
267  
268 =cut
269  
270 0 ▶sub do_untargz {
Subroutine do_untargz: not called CC=7 SCAR=40.3
Pod documented
271 0 my( $untgz, $tgzfile ) = @_;
272  
273 0 ▶ if ( $untgz eq 'Archive::Tar' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untgz eq 'Archive::Tar') { } 0 0 0
274 0 require Archive::Tar;
275  
276 0 ▶ my $archive = Archive::Tar->new() or do {
Branch: 1 branch
BranchTrueFalseTotal
my $archive = "Archive::Tar"->new or do { warn q[Can't Archive::Tar->new: ] . $Archive::Tar::error; return undef } 0 0 0
277 0 warn "Can't Archive::Tar->new: " . $Archive::Tar::error;
278 0 return undef;
279 };
280  
281 0 $archive->read( $tgzfile, 1 );
282 0 ▶ $Archive::Tar::error and do {
Branch: 1 branch
BranchTrueFalseTotal
$Archive::Tar::error and do { warn "Error reading '${tgzfile}': " . $Archive::Tar::error; return undef } 0 0 0
283 0 warn "Error reading '$tgzfile': ".$Archive::Tar::error;
284 0 return undef;
285 };
286 0 my @files = $archive->list_files;
287 0 $archive->extract( @files );
288  
289 } else { # assume command
290 0 ▶ $^O eq 'VMS' and return vms_untargz( $untgz, $tgzfile );
Branch: 1 branch
BranchTrueFalseTotal
$^O eq "VMS" and return vms_untargz($untgz, $tgzfile) 0 0 0
291  
292 0 my $command = sprintf $untgz, $tgzfile;
293 0 ▶ $command .= " $tgzfile" if $command eq $untgz;
Branch: 1 branch
BranchTrueFalseTotal
if $command eq $untgz 0 0 0
294  
295 0 ▶ if ( system $command ) {
Branch: 1 branch
BranchTrueFalseTotal
if (system $command) 0 0 0
296 0 my $error = $? >> 8;
297 0 warn "Error in command: $error";
298 0 return undef;
299 };
300 }
301 0 return 1;
302 }
303  
304 =item vms_untargz( $untargz, $tgzfile )
305  
306 Gunzip and extract the archive in C<$tgzfile>.
307  
308 =cut
309  
310 0 ▶sub vms_untargz {
Subroutine vms_untargz: not called CC=9 SCAR=45.0
Pod documented
311 0 my( $cmd, $file ) = @_;
312 0 my( $vol, $path, $fname ) = splitpath( $file );
313 0 my @parts = split /[.@#]/, $fname;
314 0 ▶ if ( @parts > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 0 0 0
315 0 ▶ my $ext = ( pop @parts ) || '';
Condition0%0/2
pop @parts || ''
l!l
00
MC/DC0%0/1
pop @parts || ''
pop @parts
Truth table0%0/2
A
A= pop @parts
Aresult
11
00
316 0 $fname = join( "_", @parts ) . ".$ext";
317 }
318 0 $file = catpath( $vol, $path, $fname );
319  
320 0 my( $gzip_cmd, $tar_cmd ) = split /\s*\|\s*/, $cmd;
321 0 ▶ my $gzip = $gzip_cmd =~ /^(\S+)/ ? $1 : 'GZIP';
Branch: 1 branch
BranchTrueFalseTotal
$gzip_cmd =~ /^(\S+)/ ? : 0 0 0
322 0 ▶ my $tar = $tar_cmd =~ /^(\S+)/
Branch: 1 branch
BranchTrueFalseTotal
$tar_cmd =~ /^(\S+)/ ? : 0 0 0
Condition0%0/3
whereis("vmstar") or whereis("tar")
l!l&&r!l&&!r
000
MC/DC0%0/2
whereis("vmstar") or whereis("tar")
whereis("vmstar") whereis("tar")
Truth table0%0/3
A or B
A= whereis("vmstar")B= whereis("tar")
ABresult
1X1
011
000
323 ? $1 : (whereis( 'vmstar' ) || whereis( 'tar' ) );
324  
325 0 local *TMPCOM;
326 0 ▶ open TMPCOM, "> TS-UNTGZ.COM" or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open TMPCOM, "> TS-UNTGZ.COM" or return 0 0 0 0
327 0 ▶ print TMPCOM <<EO_UNTGZ; close TMPCOM or return 0;
Branch: 1 branch
BranchTrueFalseTotal
close TMPCOM or return 0 0 0 0
328 \$ define/user sys\$output TS-UNTGZ.TAR
329 \$ $gzip "-cd" $file
330 \$ $tar "-xf" TS-UNTGZ.TAR
331 \$ delete TS-UNTGZ.TAR;*
332 EO_UNTGZ
333  
334 0 my $ret = system "\@TS-UNTGZ.COM";
335 0 ▶ 1 while unlink "TS-UNTGZ.COM";
Branch: 1 branch
BranchTrueFalseTotal
unlink "TS-UNTGZ.COM" and do { '???' } 0 0 0
336  
337 0 return ! $ret;
338 }
339  
340 2 1;
341  
342 =back
343  
344 =head1 COPYRIGHT
345  
346 (c) 2001-2003, All rights reserved.
347  
348 * Abe Timmerman <abeltje@cpan.org>
349  
350 This library is free software; you can redistribute it and/or modify
351 it under the same terms as Perl itself.
352  
353 See:
354  
355 =over 4
356  
357 =item * L<http://www.perl.com/perl/misc/Artistic.html>
358  
359 =item * L<http://www.gnu.org/copyleft/gpl.html>
360  
361 =back
362  
363 This program is distributed in the hope that it will be useful,
364 but WITHOUT ANY WARRANTY; without even the implied warranty of
365 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
366  
367 =cut
« blib/lib/Test/Smoke/Mailer.pm ↑ Summary blib/lib/Test/Smoke/Syncer/Snapshot.pm »