DistDir.pm

filter: Statementstmt 99.1% 113 / 114 Branchbran 70.0% 42 / 60 Conditioncond 70.2% 26 / 37 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 100% 17 / 17 Podpod 100% 5 / 5 totaltotal 83.2% 214 / 257 CC 47 SCAR 22.9
CC
47
Cov
86%
copy
19.3
_copy_directory_contents
11.8
_collect_entries
7.0
CRAP 9.9
↑ Summary DistDir/CLI.pm »
1 package File::DistDir;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '1.0.0';
15  
16 ########################################################################
17 1 ▶sub new {
Subroutine new: called CC=5 SCAR=18.4
Pod documented
18 ########################################################################
19 1 my ( $class, %args ) = @_;
20  
21 1 my $distribution = $args{distribution};
22  
23 1 ▶ die "ERROR: distribution is required\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution and length $distribution 0 1 1
Condition33%1/3
defined $distribution and length $distribution
!ll&&!rl&&r
001
MC/DC0%0/2
defined $distribution and length $distribution
defined $distribution length $distribution
Truth table33%1/3
A and B
A= defined $distributionB= length $distribution
ABresult
0X0
100
111
24 if !defined $distribution || !length $distribution;
25  
26 1 my $root = abs_path( dist_dir($distribution) );
27  
28 1 ▶ die sprintf "ERROR: distribution share directory not found: %s\n", $distribution
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and -d $root 0 1 1
Condition33%1/3
defined $root and -d $root
!ll&&!rl&&r
001
MC/DC0%0/2
defined $root and -d $root
defined $root -d $root
Truth table33%1/3
A and B
A= defined $rootB= -d $root
ABresult
0X0
100
111
29 if !defined $root || !-d $root;
30  
31 1 return bless {
32 distribution => $distribution,
33 dir => $root,
34 }, $class;
35 }
36  
37 ########################################################################
38 21 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
39 ########################################################################
40 21 my ($self) = @_;
41 21 return $self->{dir};
42 }
43  
44 ########################################################################
45 10 ▶sub path {
Subroutine path: called CC=6 SCAR=18.1
Pod documented
46 ########################################################################
47 10 my ( $self, $relative ) = @_;
48  
49 10 ▶ $relative //= q{.};
Condition50%1/2
$relative //= "."
l!l
100
MC/DC0%0/1
$relative //= "."
$relative
Truth table50%1/2
A
A= $relative
Aresult
11
00
50  
51 10 ▶ die "ERROR: source must be relative to the distribution directory\n"
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($relative) 0 10 10
52 if File::Spec->file_name_is_absolute($relative);
53  
54 10 my $root = $self->dir;
55 10 my $candidate = File::Spec->catfile( $root, $relative );
56 10 my $resolved = abs_path($candidate);
57  
58 10 ▶ die sprintf "ERROR: source not found: %s\n", $relative
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resolved 0 10 10
59 if !defined $resolved;
60  
61 10 ▶ die sprintf "ERROR: source is outside distribution directory: %s\n", $relative
Branch: 1 branch
BranchTrueFalseTotal
if $resolved ne $root and index($resolved, $root . '/') != 0 1 9 10
Condition100%3/3
$resolved ne $root and index($resolved, $root . '/') != 0
!ll&&!rl&&r
451
MC/DC100%2/2
$resolved ne $root and index($resolved, $root . '/') != 0
$resolved ne $root index($resolved, $root . '/') != 0
Truth table100%3/3
A and B
A= $resolved ne $rootB= index($resolved, $root . '/') != 0
ABresult
0X0
100
111
62 if $resolved ne $root && index( $resolved, $root . q{/} ) != 0;
63  
64 9 return $resolved;
65 }
66  
67 ########################################################################
68 9 ▶sub copy {
Subroutine copy: called CC=17 SCAR=29.6
Pod documented
69 ########################################################################
70 9 my ( $self, $source, $destination, %options ) = @_;
71  
72 9 ▶ die "ERROR: copy requires a source\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 9 9
73 if !defined $source;
74  
75 9 ▶ die "ERROR: destination is required for file copying\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $destination and $source ne "." 0 9 9
Condition66%2/3
not defined $destination and $source ne "."
!ll&&!rl&&r
810
MC/DC0%0/2
not defined $destination and $source ne "."
not defined $destination $source ne "."
Truth table66%2/3
not(A) and B
A= not defined $destinationB= $source ne "."
ABresult
0X0
100
111
76 if !defined $destination && $source ne q{.};
77  
78 9 ▶ $destination //= q{.};
Condition100%2/2
$destination //= "."
l!l
81
MC/DC100%1/1
$destination //= "."
$destination
Truth table100%2/2
A
A= $destination
Aresult
11
00
79  
80 9 my $source_path = File::Spec->catfile( $self->dir, $source );
81 9 _reject_symlink_components($source_path);
82  
83 8 my $resolved = $self->path($source);
84 8 _reject_symlink_components($destination);
85  
86 7 ▶ if ( -d $resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $resolved) 6 1 7
87 die "ERROR: copying directories requires recursive => 1\n"
88 6 ▶ if !$options{recursive};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"recursive"} 1 5 6
89  
90 5 ▶ die sprintf "ERROR: destination is not a directory: %s\n", $destination
Branch: 1 branch
BranchTrueFalseTotal
if -e $destination and not -d $destination 0 5 5
Condition66%2/3
-e $destination and not -d $destination
!ll&&!rl&&r
410
MC/DC0%0/2
-e $destination and not -d $destination
-e $destination not -d $destination
Truth table66%2/3
A and not(B)
A= -e $destinationB= not -d $destination
ABresult
0X0
100
111
91 if -e $destination && !-d $destination;
92  
93 5 my $dest_abs = File::Spec->rel2abs($destination);
94 5 my $dest_existing = abs_path($destination);
95 5 ▶ if ( defined $dest_existing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest_existing) 5 0 5
96 5 $dest_abs = $dest_existing;
97 }
98  
99 5 ▶ die sprintf "ERROR: destination is inside source directory: %s\n", $destination
Branch: 1 branch
BranchTrueFalseTotal
if $dest_abs eq $resolved or index($dest_abs, $resolved . '/') == 0 1 4 5
Condition66%2/3
$dest_abs eq $resolved or index($dest_abs, $resolved . '/') == 0
l!l&&r!l&&!r
014
MC/DC50%1/2
$dest_abs eq $resolved or index($dest_abs, $resolved . '/') == 0
$dest_abs eq $resolved index($dest_abs, $resolved . '/') == 0
Truth table66%2/3
A or B
A= $dest_abs eq $resolvedB= index($dest_abs, $resolved . '/') == 0
ABresult
1X1
011
000
100 if $dest_abs eq $resolved || index( $dest_abs, $resolved . q{/} ) == 0;
101  
102 4 ▶ make_path($destination)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $destination 3 1 4
103 if !-d $destination;
104  
105 4 _copy_directory_contents( $resolved, $destination );
106 3 return 1;
107 }
108  
109 1 ▶ die sprintf "ERROR: source is not a regular file: %s\n", $source
Branch: 1 branch
BranchTrueFalseTotal
unless -f $resolved 0 1 1
110 if !-f $resolved;
111  
112 1 ▶ if ( -d $destination ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $destination) 0 1 1
113 0 $destination = File::Spec->catfile( $destination, basename($source) );
114 }
115  
116 1 ▶ die sprintf "ERROR: destination directory does not exist: %s\n", dirname($destination)
Branch: 1 branch
BranchTrueFalseTotal
unless -d dirname($destination) 0 1 1
117 if !-d dirname($destination);
118  
119 1 ▶ die sprintf "ERROR: unable to copy %s => %s: %s\n", $resolved, $destination, $OS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless File::Copy::copy($resolved, $destination) 0 1 1
120 if !File::Copy::copy( $resolved, $destination );
121  
122 1 return 1;
123 }
124  
125 ########################################################################
126 1 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
127 ########################################################################
128 1 my ($self) = @_;
129  
130 1 my @files;
131 1 _collect_entries( $self->dir, q{}, \@files );
132  
133 1 return @files;
134 }
135  
136 ########################################################################
137 2 ▶sub _collect_entries {
Subroutine _collect_entries: called CC=7 SCAR=19.5
138 ########################################################################
139 2 my ( $root, $prefix, $result ) = @_;
140  
141 2 ▶ opendir my $dh, $root
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $root or die sprintf("ERROR: cannot open directory %s: %s\n", $root, $File::DistDir::OS_ERROR) 2 0 2
142 or die sprintf "ERROR: cannot open directory %s: %s\n", $root, $OS_ERROR;
143  
144 2 ▶ my @entries = sort grep { $_ ne q{.} && $_ ne q{..} } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
223
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
145 2 closedir $dh;
146  
147 2 foreach my $entry (@entries) {
148 3 ▶ my $relative = length $prefix ? File::Spec->catfile( $prefix, $entry ) : $entry;
Branch: 1 branch
BranchTrueFalseTotal
length $prefix ? : 1 2 3
149 3 my $full = File::Spec->catfile( $root, $entry );
150  
151 3 push @{$result}, $relative;
152  
153 3 ▶ if ( -d $full && !-l $full ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $full and not -l $full) 1 2 3
Condition66%2/3
-d $full and not -l $full
!ll&&!rl&&r
201
MC/DC50%1/2
-d $full and not -l $full
-d $full not -l $full
Truth table66%2/3
A and not(B)
A= -d $fullB= not -l $full
ABresult
0X0
100
111
154 1 _collect_entries( $full, $relative, $result );
155 }
156 }
157  
158 2 return;
159 }
160  
161 ########################################################################
162 28 ▶sub _reject_symlink_components {
Subroutine _reject_symlink_components: called CC=6 SCAR=17.9
163 ########################################################################
164 28 my ($path) = @_;
165  
166 # Preserve explicit .. components while inspecting the actual path walked
167 # by the filesystem. rel2abs() would normalize them prematurely.
168 28 ▶ my $absolute
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($path) ? : 24 4 28
169 = File::Spec->file_name_is_absolute($path)
170 ? $path
171 : File::Spec->catfile( getcwd(), $path );
172 28 my $current = File::Spec->rootdir;
173  
174 28 foreach my $part ( File::Spec->splitdir($absolute) ) {
175 139 ▶ next if !length $part || $part eq q{.};
Branch: 1 branch
BranchTrueFalseTotal
if not length $part or $part eq "." 33 106 139
Condition100%3/3
not length $part or $part eq "."
l!l&&r!l&&!r
285106
MC/DC100%2/2
not length $part or $part eq "."
not length $part $part eq "."
Truth table100%3/3
not(A) or B
A= not length $partB= $part eq "."
ABresult
1X1
011
000
176  
177 106 $current = File::Spec->catdir( $current, $part );
178  
179 106 ▶ die sprintf "ERROR: symbolic links are not supported: %s\n", $current
Branch: 1 branch
BranchTrueFalseTotal
if -l $current 2 104 106
180 if -l $current;
181 }
182  
183 26 return;
184 }
185  
186 ########################################################################
187 7 ▶sub _copy_directory_contents {
Subroutine _copy_directory_contents: called CC=11 SCAR=24.6
188 ########################################################################
189 7 my ( $source, $destination ) = @_;
190  
191 7 ▶ opendir my $dh, $source
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $source or die sprintf("ERROR: cannot open directory %s: %s\n", $source, $File::DistDir::OS_ERROR) 7 0 7
192 or die sprintf "ERROR: cannot open directory %s: %s\n", $source, $OS_ERROR;
193  
194 7 ▶ my @entries = grep { $_ ne q{.} && $_ ne q{..} } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
7711
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
195 7 closedir $dh;
196  
197 7 foreach my $entry (@entries) {
198 11 my $from = File::Spec->catfile( $source, $entry );
199 11 my $to = File::Spec->catfile( $destination, $entry );
200  
201 11 _reject_symlink_components($to);
202  
203 11 ▶ die sprintf "ERROR: symbolic links are not supported: %s\n", $from
Branch: 1 branch
BranchTrueFalseTotal
if -l $from 1 10 11
204 if -l $from;
205  
206 10 ▶ if ( -d $from ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $from) 3 7 10
207 3 ▶ die sprintf "ERROR: destination is not a directory: %s\n", $to
Branch: 1 branch
BranchTrueFalseTotal
if -e $to and not -d $to 0 3 3
Condition33%1/3
-e $to and not -d $to
!ll&&!rl&&r
300
MC/DC0%0/2
-e $to and not -d $to
-e $to not -d $to
Truth table33%1/3
A and not(B)
A= -e $toB= not -d $to
ABresult
0X0
100
111
208 if -e $to && !-d $to;
209  
210 3 ▶ make_path($to)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $to 3 0 3
211 if !-d $to;
212  
213 3 _copy_directory_contents( $from, $to );
214 3 next;
215 }
216  
217 7 ▶ die sprintf "ERROR: unsupported resource type: %s\n", $from
Branch: 1 branch
BranchTrueFalseTotal
unless -f $from 0 7 7
218 if !-f $from;
219  
220 7 ▶ die sprintf "ERROR: unable to copy %s => %s: %s\n", $from, $to, $OS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless File::Copy::copy($from, $to) 0 7 7
221 if !File::Copy::copy( $from, $to );
222 }
223  
224 6 return;
225 }
226  
227 2 1;
228  
229 __END__
↑ Summary DistDir/CLI.pm »