blib/lib/Test/Smoke/SourceTree.pm

filter: Statementstmt 85.5% 136 / 159 Branchbran 60.9% 39 / 64 Conditioncond 50.0% 8 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 95.2% 20 / 21 Podpod 100% 12 / 12 totaltotal 77.3% 218 / 282 CC 40 SCAR 21.2
CC
40
Cov
77%
copy_from_MANIFEST
16.6
_read_mani_file
15.3
__ANON__
7.1
CRAP 8.4
« blib/lib/Test/Smoke/App/ConfigSmoke/Files.pm ↑ Summary blib/lib/Test/Smoke/Syncer/Hardlink.pm »
1 package Test::Smoke::SourceTree;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 our $VERSION = '0.008';
5  
6 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Test::Smoke::LogMixin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 our %EXPORT_TAGS = (
14 mani_const => [qw( &ST_MISSING &ST_UNDECLARED )],
15 const => [qw( &ST_MISSING &ST_UNDECLARED )],
16 );
17 18 our @EXPORT_OK = @{ $EXPORT_TAGS{mani_const} };
18  
19 18 ▶our $NOCASE = $^O eq 'MSWin32' || $^O eq 'VMS';
Condition33%1/3
$^O eq 'MSWin32' || $^O eq 'VMS'
l!l&&r!l&&!r
0018
MC/DC0%0/2
$^O eq 'MSWin32' || $^O eq 'VMS'
$^O eq 'MSWin32' $^O eq 'VMS'
Truth table33%1/3
A || B
A= $^O eq 'MSWin32'B= $^O eq 'VMS'
ABresult
1X1
011
000
20  
21 =head1 NAME
22  
23 Test::Smoke::SourceTree - Manipulate the perl source-tree
24  
25 =head1 SYNOPSIS
26  
27 use Test::Smoke::SourceTree qw( :mani_const );
28  
29 my $tree = Test::Smoke::SourceTree->new( $tree_dir );
30  
31 my $mani_check = $tree->check_MANIFEST;
32 foreach my $file ( sort keys %$mani_check ) {
33 if ( $mani_check->{ $file } == ST_MISSING ) {
34 print "MANIFEST declared '$file' but it is missing\n";
35 } elsif ( $mani_check->{ $file } == ST_UNDECLARED ) {
36 print "MANIFEST did not declare '$file'\n";
37 }
38 }
39  
40 $tree->clean_from_MANIFEST;
41  
42 =head1 CONSTANTS
43  
44 =over
45  
46 =item ST_MISSING
47  
48 =item ST_UNDECLARED
49  
50 =back
51  
52 =cut
53  
54 # Define some constants
55 sub ST_MISSING() { 1 }
56 sub ST_UNDECLARED() { 0 }
57  
58 =head1 DESCRIPTION
59  
60 =head2 Test::Smoke::SourceTree->new( $tree_dir[, $verbose] )
61  
62 C<new()> creates a new object, this is a simple scalar containing
63 C<< File::Spec->rel2abs( $tree_dir) >>.
64  
65 =cut
66  
67 24 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
68 24 my $proto = shift;
69 24 ▶ my $class = ref $proto ? ref $proto : $proto;
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 4 20 24
70 24 my ($dir, $verbose) = @_;
71  
72 24 ▶ croak sprintf "Usage: my \$tree = %s->new( <directory> )", __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 23 24
73 unless @_;
74  
75 # it should be a directory!
76 23 my $cwd = abs_path(File::Spec->curdir);
77 23 ▶ chdir $dir or croak "Cannot chdir($dir): $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or croak("Cannot chdir($dir): $!") 23 0 23
78 # try not to trip over an absolute-path to something that somehow is a
79 # symlink. abs_path('.') will give you the real-path; that will fail...
80 23 ▶ my $tree_dir = File::Spec->file_name_is_absolute($dir)
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($dir) ? : 22 1 23
81 ? $dir
82 : abs_path(File::Spec->curdir);
83 23 ▶ my $self = {
Condition100%2/2
$verbose || 0
l!l
122
MC/DC100%1/1
$verbose || 0
$verbose
Truth table100%2/2
A
A= $verbose
Aresult
11
00
84 tree_dir => $tree_dir,
85 verbose => $verbose || 0,
86 };
87 23 chdir $cwd;
88  
89 23 return bless $self, $class;
90 }
91  
92 =head2 $tree->tree_dir
93  
94 Get the directory.
95  
96 =cut
97  
98 238 ▶sub tree_dir { return $_[0]->{tree_dir} }
Subroutine tree_dir: called CC=1 SCAR=0.0
Pod documented
99  
100 =head2 $tree->verbose
101  
102 Get verbosity.
103  
104 =cut
105  
106 314 ▶sub verbose { return $_[0]->{verbose} }
Subroutine verbose: called CC=1 SCAR=0.0
Pod documented
107  
108 =head2 $tree->canonpath( )
109  
110 C<canonpath()> returns the canonical name for the path,
111 see L<File::Spec>.
112  
113 =cut
114  
115 5 ▶sub canonpath {
Subroutine canonpath: called CC=1 SCAR=0.0
Pod documented
116 5 my $self = shift;
117 5 return File::Spec->canonpath( $self->tree_dir );
118 }
119  
120 =head2 $tree->rel2abs( [$base_dir] )
121  
122 C<rel2abs()> returns the absolute path, see L<File::Spec>.
123  
124 =cut
125  
126 1 ▶sub rel2abs {
Subroutine rel2abs: called CC=1 SCAR=0.0
Pod documented
127 1 my $self = shift;
128 1 return File::Spec->rel2abs( $self->tree_dir, @_ );
129 }
130  
131 =head2 $tree->abs2rel( [$base_dir] )
132  
133 C<abs2rel()> returns a relative path,
134 see L<File::Spec>.
135  
136 =cut
137  
138 1 ▶sub abs2rel {
Subroutine abs2rel: called CC=1 SCAR=0.0
Pod documented
139 1 my $self = shift;
140 1 return File::Spec->abs2rel( $self->tree_dir, @_ );
141 }
142  
143 =head2 $tree->mani2abs( $file[, $base_path] )
144  
145 C<mani2abs()> returns the absolute filename of C<$file>, which should
146 be in "MANIFEST" format (i.e. using '/' as directory separator).
147  
148 =cut
149  
150 196 ▶sub mani2abs {
Subroutine mani2abs: called CC=4 SCAR=16.1
Pod documented
151 196 my $self = shift;
152  
153 196 my $path = shift;
154 196 my @dirs = split m{/+}, $path;
155 196 my $file = pop @dirs;
156 196 ▶ if ( $^O eq 'VMS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "VMS") 0 196 196
157 0 my @parts = split m/\./, $file;
158 0 my $last = pop @parts;
159 @parts and
160 0 ▶ $file = join( "_", map { s/[^\w-]/_/g; $_ } @parts ) . ".$last";
Branch: 1 branch
BranchTrueFalseTotal
@parts and $file = join("_", map({s/[^\w-]/_/g; $_;} @parts)) . ".$last" 0 0 0
161 }
162 196 ▶ @dirs and $file = join '/', @dirs, $file;
Branch: 1 branch
BranchTrueFalseTotal
@dirs and $file = join("/", @dirs, $file) 61 135 196
163 196 my @split_path = split m|/|, $file;
164 196 my $base_path = File::Spec->rel2abs( $self->tree_dir, @_ );
165 196 return File::Spec->catfile( $base_path, @split_path );
166 }
167  
168 =head2 $tree->mani2absdir( $dir[, $base_path] )
169  
170 C<mani2abs()> returns the absolute dirname of C<$dir>, which should
171 be in "MANIFEST" format (i.e. using '/' as directory separator).
172  
173 =cut
174  
175 0 ▶sub mani2absdir {
Subroutine mani2absdir: not called CC=1 SCAR=6.9
Pod documented
176 0 my $self = shift;
177  
178 0 my @split_path = split m|/|, shift;
179 0 my $base_path = File::Spec->rel2abs( $self->tree_dir, @_ );
180 0 return File::Spec->catdir( $base_path, @split_path );
181 }
182  
183 =head2 $tree->abs2mani( $file )
184  
185 C<abs2mani()> returns the MANIFEST style filename.
186  
187 =cut
188  
189 25 ▶sub abs2mani {
Subroutine abs2mani: called CC=2 SCAR=6.9
Pod documented
190 25 my $self = shift;
191 25 my ($source_file) = @_;
192  
193 25 my $relfile = File::Spec->abs2rel(
194 File::Spec->canonpath( $source_file ), $self->tree_dir
195 );
196 25 $self->log_debug("[abs2mani($source_file)] $relfile");
197  
198 25 my( undef, $directories, $file ) = File::Spec->splitpath( $relfile );
199 25 ▶ my @dirs = grep $_ && length $_ => File::Spec->splitdir( $directories );
Condition66%2/3
$_ && length $_
!ll&&!rl&&r
808
MC/DC50%1/2
$_ && length $_
$_ length $_
Truth table66%2/3
A && B
A= $_B= length $_
ABresult
0X0
100
111
200 25 push @dirs, $file;
201 25 return join '/', @dirs;
202 }
203  
204 =head2 $tree->check_MANIFEST( @ignore )
205  
206 C<check_MANIFEST()> reads the B<MANIFEST> file from C<< $self->tree_dir >> and
207 compares it with the actual contents of C<< $self->tree_dir >>.
208  
209 Returns a hashref with suspicious entries (if any) as keys that have a
210 value of either B<ST_MISSING> (not in directory) or B<ST_UNDECLARED>
211 (not in MANIFEST).
212  
213 =cut
214  
215 10 ▶sub check_MANIFEST {
Subroutine check_MANIFEST: called CC=3 SCAR=11.0
Pod documented
216 10 my $self = shift;
217  
218 10 my %manifest = %{ $self->_read_mani_file( 'MANIFEST' ) };
219 10 $self->log_debug("Found %d entries in MANIFEST", scalar(keys %manifest));
220  
221 my %ignore = map {
222 59 ▶ my $entry = $NOCASE ? uc $_ : $_;
Branch: 1 branch
BranchTrueFalseTotal
$Test::Smoke::SourceTree::NOCASE ? : 0 59 59
223 59 $entry => undef
224 } ( ".patch", "MANIFEST.SKIP", '.git', '.gitignore', '.mailmap', @_ ),
225 10 keys %{ $self->_read_mani_file( 'MANIFEST.SKIP', 1 ) };
226 10 $self->log_debug("Found %d entries in MANIFEST.SKIP", scalar(keys %ignore));
227  
228 # Walk the tree, remove all found files from %manifest
229 # and add other files to %manifest
230 # unless they are in the ignore list
231 10 my $cwd = abs_path(File::Spec->curdir);
232 10 ▶ chdir $self->tree_dir or die "Cannot chdir($self->tree_dir): $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->tree_dir or die "Cannot chdir($self->tree_dir): $!" 10 0 10
233 10 require File::Find;
234 File::Find::find(
235 236 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
236 236 ▶ -f or return;
Branch: 1 branch
BranchTrueFalseTotal
-f $_ or return 150 86 236
237 150 my $cpath = File::Spec->canonpath($File::Find::name);
238 150 my (undef, $dirs, $file) = File::Spec->splitpath($cpath);
239 150 ▶ my @dirs = grep $_ && length $_ => File::Spec->splitdir($dirs);
Condition66%2/3
$_ && length $_
!ll&&!rl&&r
720137
MC/DC50%1/2
$_ && length $_
$_ length $_
Truth table66%2/3
A && B
A= $_B= length $_
ABresult
0X0
100
111
240 150 ▶ $^O eq 'VMS' and $file =~ s/\.$//;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq "VMS" and $file =~ s/\.$// 0 150 150
241 150 my $mani_name = join '/', @dirs, $file;
242 150 ▶ $NOCASE and $mani_name = uc $mani_name;
Branch: 1 branch
BranchTrueFalseTotal
$Test::Smoke::SourceTree::NOCASE and $mani_name = uc $mani_name 0 150 150
243 150 ▶ if (exists $manifest{$mani_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $manifest{$mani_name}) { } 139 11 150
244 139 $self->log_debug("[manicheck] Matched $mani_name");
245 139 delete $manifest{$mani_name};
246 }
247 else {
248 11 ▶ if (!grep $mani_name =~ /$_/, keys %ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep(($mani_name =~ /$_/), keys %ignore)) { } 2 9 11
249 2 $self->log_debug("[manicheck] Undeclared $mani_name");
250 2 $manifest{$mani_name} = ST_UNDECLARED;
251 }
252 else {
253 9 $self->log_debug("[manicheck] Skipped $mani_name");
254 }
255 }
256 },
257 10 '.'
258 );
259 10 chdir $cwd;
260 10 $self->log_debug("[manicheck] %d entries missing", scalar(keys %manifest));
261  
262 10 return \%manifest;
263 }
264  
265 =head2 $self->_read_mani_file( $path[, $no_croak] )
266  
267 C<_read_mani_file()> reads the contents of C<$path> like it is a
268 MANIFEST typeof file and returns a ref to hash with all values set
269 C<ST_MISSING>.
270  
271 =cut
272  
273 21 ▶sub _read_mani_file {
Subroutine _read_mani_file: called CC=8 SCAR=27.3
274 21 my $self = shift;
275 21 my( $path, $no_croak ) = @_;
276  
277 21 my $manifile = $self->mani2abs( $path );
278 21 local *MANIFEST;
279 21 ▶ open MANIFEST, "< $manifile" or do {
Branch: 1 branch
BranchTrueFalseTotal
open MANIFEST, "< $manifile" or do { $no_croak and return {}; croak("Can't open '${manifile}': $!") } 11 10 21
280 10 ▶ $no_croak and return { };
Branch: 1 branch
BranchTrueFalseTotal
$no_croak and return {} 10 0 10
281 0 croak( "Can't open '$manifile': $!" );
282 };
283  
284 my %manifest = map {
285 11 m|(\S+)|;
286 142 ▶ my $entry = $NOCASE ? uc $1 : $1;
Branch: 1 branch
BranchTrueFalseTotal
$Test::Smoke::SourceTree::NOCASE ? : 0 142 142
287 142 ▶ if ( $^O eq 'VMS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "VMS") 0 142 142
288 0 my @dirs = split m|/|, $entry;
289 0 my $file = pop @dirs;
290 0 my @parts = split /[.@#]/, $file;
291 0 ▶ if ( @parts > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 0 0 0
292 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
293 0 $file = join( "_", @parts ) . ".$ext";
294 }
295 0 ▶ $entry = @dirs ? join( "/", @dirs, $file ) : $file;
Branch: 1 branch
BranchTrueFalseTotal
@dirs ? : 0 0 0
296 }
297 142 ( $entry => ST_MISSING );
298 } <MANIFEST>;
299 11 close MANIFEST;
300  
301 11 return \%manifest;
302 }
303  
304 =head2 $tree->clean_from_MANIFEST( )
305  
306 C<clean_from_MANIFEST()> removes all files from the source-tree that are
307 not declared in the B<MANIFEST> file.
308  
309 =cut
310  
311 3 ▶sub clean_from_MANIFEST {
Subroutine clean_from_MANIFEST: called CC=3 SCAR=15.1
Pod documented
312 3 my $self = shift;
313  
314 3 my $mani_check = $self->check_MANIFEST( @_ );
315 my @to_remove = grep {
316 0 $mani_check->{ $_ } == ST_UNDECLARED
317 } keys %$mani_check;
318  
319 3 foreach my $entry ( @to_remove ) {
320 0 my $file = $self->mani2abs( $entry );
321 0 ▶ 1 while unlink $file;
Branch: 1 branch
BranchTrueFalseTotal
unlink $file and do { '???' } 0 0 0
322 }
323 }
324  
325 =head2 copy_from_MANIFEST( $dest_dir )
326  
327 C<_copy_from_MANIFEST()> uses the B<MANIFEST> file from C<$self->tree_dir>
328 to copy a source-tree to C<< $dest_dir >>.
329  
330 =cut
331  
332 3 ▶sub copy_from_MANIFEST {
Subroutine copy_from_MANIFEST: called CC=14 SCAR=28.1
Pod documented
333 3 my ($self, $dest_dir) = @_;
334  
335 3 my $manifest = $self->mani2abs( 'MANIFEST' );
336  
337 3 local *MANIFEST;
338 3 ▶ open MANIFEST, "< $manifest" or do {
Branch: 1 branch
BranchTrueFalseTotal
open MANIFEST, "< $manifest" or do { carp("Can't open '${manifest}': $!\n"); return undef } 3 0 3
339 0 carp "Can't open '$manifest': $!\n";
340 0 return undef;
341 };
342  
343 3 $self->log_info("Reading from '%s'", $manifest);
344 my @manifest_files = map {
345 3 ▶ /^([^\s]+)/ ? $1 : $_
Branch: 1 branch
BranchTrueFalseTotal
/^([^\s]+)/ ? : 81 0 81
346 } <MANIFEST>;
347 3 close MANIFEST;
348 3 my $dot_patch = $self->mani2abs( '.patch' );
349 3 ▶ -f $dot_patch and push @manifest_files, '.patch';
Branch: 1 branch
BranchTrueFalseTotal
-f $dot_patch and push @manifest_files, ".patch" 3 0 3
350  
351 3 $self->log_info("%s: %d items OK", $manifest, scalar @manifest_files);
352  
353 3 ▶ File::Path::mkpath( $dest_dir, $self->verbose ) unless -d $dest_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest_dir 2 1 3
354 3 my $dest = $self->new( $dest_dir );
355  
356 3 require File::Basename;
357 3 require File::Copy;
358 3 foreach my $file ( @manifest_files ) {
359 84 ▶ $file or next;
Branch: 1 branch
BranchTrueFalseTotal
$file or next 84 0 84
360  
361 84 my $dest_name = $dest->mani2abs( $file );
362 84 my $dest_path = File::Basename::dirname( $dest_name );
363  
364 84 ▶ File::Path::mkpath( $dest_path, $self->verbose ) unless -d $dest_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest_path 17 67 84
365  
366 84 my $abs_file = $self->mani2abs( $file );
367 84 my $mode = ( stat $abs_file )[2] & 07777;
368 84 ▶ -f $dest_name and 1 while unlink $dest_name;
Branch: 2 branches
BranchTrueFalseTotal
unlink $dest_name and do { -f $dest_name and '???' } 1 84 85
-f $dest_name and '???' 0 1 1
369 84 my $ok = File::Copy::syscopy( $abs_file, $dest_name );
370 84 ▶ $ok and $ok &&= chmod $mode, $dest_name;
Branch: 1 branch
BranchTrueFalseTotal
$ok and $ok &&= chmod($mode, $dest_name) 84 0 84
Condition33%1/3
$ok &&= chmod($mode, $dest_name)
!ll&&!rl&&r
0084
MC/DC0%0/2
$ok &&= chmod($mode, $dest_name)
$ok chmod($mode, $dest_name)
Truth table33%1/3
A &&= B
A= $okB= chmod($mode, $dest_name)
ABresult
0X0
100
111
371 84 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 84 0 84
372 84 ▶ $self->log_debug("%s -> %s: %sOK", $abs_file, $dest_name, ($ok ? "" : "NOT "));
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 84 0 84
373 }
374 else {
375 0 $self->log_warn("copy '$file' ($dest_path): $!");
376 }
377 }
378 }
379  
380 18 1;
381  
382 =head1 COPYRIGHT
383  
384 (c) 2002-2015, All rights reserved.
385  
386 * Abe Timmerman <abeltje@cpan.org>
387  
388 This library is free software; you can redistribute it and/or modify
389 it under the same terms as Perl itself.
390  
391 See:
392  
393 =over 4
394  
395 =item * L<http://www.perl.com/perl/misc/Artistic.html>
396  
397 =item * L<http://www.gnu.org/copyleft/gpl.html>
398  
399 =back
400  
401 This program is distributed in the hope that it will be useful,
402 but WITHOUT ANY WARRANTY; without even the implied warranty of
403 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
404  
405 =cut
« blib/lib/Test/Smoke/App/ConfigSmoke/Files.pm ↑ Summary blib/lib/Test/Smoke/Syncer/Hardlink.pm »