DataDirs.pm

filter: Statementstmt 71.9% 77 / 107 Branchbran 43.7% 21 / 48 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 86.6% 13 / 15 Podpod 16.6% 1 / 6 totaltotal 63.1% 120 / 190 CC 31 SCAR 34.0
CC
31
Cov
63%
copy
110.0
dir
12.0
new
11.2
CRAP 29.9
↑ Summary DataDirs/Exporter.pm »
1 =head1 NAME
2  
3 Test::DataDirs - manage t/data and t/temp directories for your tests
4  
5 =head1 VERSION
6  
7 version 0.1.2
8  
9 =head1 SYNOPSIS
10  
11 This class is a convenience which provides data directories from which
12 to source information for your tests, and temp directories you can
13 write data.
14  
15 Declare some temp and data directories you need in your test script as
16 below. These are implicitly relative to C<< t/temp/<yourscriptname> >>
17 and C<< t/data/<yourscriptname> >>. Then you may refer to them
18 using the appropriate entry in the returned hash and assume the dirs
19 exist and that the temp dirs have been (re-)created.
20  
21 # File: t/test-01.t
22 use Test::DataDirs;
23  
24 my %D = Test::DataDirs->new(
25 temp => [temp_stuff => 'actual-dir',
26 more_temp => 'another-dir'],
27 data => [data_stuff => 'actual-dir'],
28 )->hash;
29  
30 print "My test data is checked into $D{data_stuff}\n"
31 print "below $D{data_dir}\n"
32 # Prints (except with absolute paths):
33 # My test data is checked into t/data/test-01/actual-dir
34 # below t/data/test-01
35  
36 print "I can write temp data into $D{temp_stuff}\n"
37 print "and $D{more_temp}, "below $D{temp_dir}\n"
38 # Prints (except with absolute paths):
39 # I can write temp data into t/temp/test-01/actual-dir
40 # and t/temp/test-01/another-dir below t/data/test-01
41  
42  
43 This module defines an OO interface. See also
44 L<Test::DataDirs::Exporter> for a module with similar usage but which
45 imports vars into your namespace.
46  
47 =head1 DESCRIPTION
48  
49 =cut
50  
51 package Test::DataDirs;
52 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use FindBin qw($Bin $Script);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use File::Glob qw(bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use File::Path qw(mkpath rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 4 our $VERSION = '0.1.2'; # VERSION
61  
62 =head2 C<< $obj = $class->new(%params) >>
63  
64 Given parameters including:
65  
66 base => $base_dir,
67  
68 data => [ddir1 => relpath3, ddir2 => relpath4 ...]
69  
70 temp => [tdir1 => relpath1, tdir2 => relpath2 ...]
71  
72 Uses C<base> as a base dir in which to find data dirs C<relpathN> (which
73 are checked to exist), and in which to re-create fresh test dirs
74 C<relpathM>.
75  
76 If C<base> is not given, uses the name of the invoking script, with
77 any leading digits or periods stripped, and any trailing ".t"
78 stripped.
79  
80 Retuns a hash-based object which keys the names C<ddirN> and C<tdirN>
81 to the appropriate paths constructed from C<$base_dir> and the
82 appropriate C<relpath>.
83  
84 =cut
85  
86 9 ▶sub new {
Subroutine new: called CC=11 SCAR=24.2
Pod documented
87 9 my $class = shift;
88 9 my %param = @_;
89 9 my $base = $param{base};
90 9 ▶ ($base) = $Script =~ /^([\d.]*.*?)(\.t)?$/
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 6 3 9
91 unless defined $base;
92  
93 my $self = bless {
94 data_dir => File::Spec->catdir($Bin,'data', $base),
95 temp_dir => File::Spec->catdir($Bin,'temp', $base),
96 dirs => {},
97 data_dirs => [],
98 temp_dirs => [],
99 9 ▶ copy => $param{copy} || [],
Condition50%1/2
$param{'copy'} || []
l!l
09
MC/DC0%0/1
$param{'copy'} || []
$param{'copy'}
Truth table50%1/2
A
A= $param{'copy'}
Aresult
11
00
100 }, $class;
101  
102 # expand the data directories
103 9 ▶ my @data = (data_dir => '', @{ $param{data} || [] });
Condition100%2/2
$param{'data'} || []
l!l
63
MC/DC100%1/1
$param{'data'} || []
$param{'data'}
Truth table100%2/2
A
A= $param{'data'}
Aresult
11
00
104 9 ▶ for(my $ix = 0; $ix < @data; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @data) 19 9 28
105 19 my ($name, $dir) = @data[$ix, $ix+1];
106 die "Can't use dir name '$name': already in use as '$self->{$name}'"
107 19 ▶ if exists $self->{dirs}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"dirs"}{$name} 0 19 19
108
109 19 $dir = File::Spec->catdir($self->{data_dir}, $dir);
110 19 $self->{dirs}{$name} = $dir;
111 19 push @{ $self->{data_dirs} }, $name, $dir;
112 }
113  
114 # ditto the temp directories
115 9 ▶ my @temp = (temp_dir => '', @{ $param{temp} || [] });
Condition100%2/2
$param{'temp'} || []
l!l
63
MC/DC100%1/1
$param{'temp'} || []
$param{'temp'}
Truth table100%2/2
A
A= $param{'temp'}
Aresult
11
00
116 9 ▶ for(my $ix = 0; $ix < @temp; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @temp) 19 9 28
117 19 my ($name, $dir) = @temp[$ix, $ix+1];
118 croak "Can't use dir name '$name': already in use as '$self->{$name}'"
119 19 ▶ if exists $self->{dirs}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"dirs"}{$name} 0 19 19
120
121 19 $dir = File::Spec->catdir($self->{temp_dir}, $dir);
122 19 $self->{dirs}{$name} = $dir;
123 19 push @{ $self->{temp_dirs} }, $name, $dir;
124 }
125  
126 # check the copies attribute
127 9 my $copy = $self->{copy};
128 9 ▶ croak "copy param must be an arrayref with an even number of members"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $copy eq "ARRAY" and not @$copy % 2 0 9 9
Condition33%1/3
ref $copy eq "ARRAY" and not @$copy % 2
!ll&&!rl&&r
009
MC/DC0%0/2
ref $copy eq "ARRAY" and not @$copy % 2
ref $copy eq "ARRAY" not @$copy % 2
Truth table33%1/3
A and not(B)
A= ref $copy eq "ARRAY"B= not @$copy % 2
ABresult
0X0
100
111
129 unless ref $copy eq 'ARRAY'
130 && !(@$copy % 2);
131  
132 9 $self->initialise();
133  
134 4 return $self;
135 }
136  
137  
138 9 ▶sub initialise {
Subroutine initialise: called CC=8 SCAR=21.4
Pod undocumented
139 9 my $self = shift;
140  
141 9 rmtree $self->{temp_dir};
142  
143 # validate the data directories exist
144 9 my $data = $self->{data_dirs};
145 9 ▶ for(my $ix = 0; $ix < @$data; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @$data) 15 4 19
146 15 my ($name, $dir) = @$data[$ix, $ix+1];
147 15 ▶ croak "No such data directory '$dir'"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 5 10 15
148 unless -d $dir;
149 }
150  
151 # recreate the temp directories
152 4 my $temp = $self->{temp_dirs};
153 4 ▶ for(my $ix = 0; $ix < @$temp; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @$temp) 8 4 12
154 8 my ($name, $dir) = @$temp[$ix, $ix+1];
155
156 8 ▶ rmtree $dir if -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -e $dir 0 8 8
157 8 ▶ croak "Can't delete '$dir'"
Branch: 1 branch
BranchTrueFalseTotal
if -e $dir 0 8 8
158 if -e $dir;
159 8 mkpath $dir;
160 8 ▶ croak "Can't create '$dir'"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 8 8
161 unless -d $dir;
162 }
163  
164 # perform any copies
165 4 my $copy = $self->{copy};
166 4 ▶ for(my $ix = 0; $ix < @$copy; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @$copy) 0 4 4
167 0 my ($from, $to) = @$copy[$ix, $ix+1];
168 0 $self->copy($from, $to);
169 }
170 }
171  
172 4 ▶sub dirs {
Subroutine dirs: called CC=2 SCAR=7.2
Pod undocumented
173 4 ▶ croak "->dirs accepts no arguments"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 4 4
174 if @_ > 1;
175 4 return shift->{dirs};
176 }
177  
178 0 ▶sub dir {
Subroutine dir: not called CC=3 SCAR=24.8
Pod undocumented
179 0 my ($self, $alias) = @_;
180 0 ▶ croak "you must supply a directory alias"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alias 0 0 0
181 unless defined $alias;
182 0 ▶ my $dir = $self->{dirs}{$alias}
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->{"dirs"}{$alias} or croak("No directory defined for alias '${alias}'") 0 0 0
183 or croak "No directory defined for alias '$alias'";
184  
185 0 return $dir;
186 }
187  
188 1 ▶sub hash {
Subroutine hash: called CC=2 SCAR=7.0
Pod undocumented
189 1 my $arg = shift;
190 1 ▶ my $self = ref $arg?
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 1 0 1
191 $arg :
192 $arg->new(@_);
193  
194 1 return %{ $self->dirs };
195 }
196  
197  
198 0 ▶sub copy {
Subroutine copy: not called CC=10 SCAR=47.0
Pod undocumented
199 0 my $self = shift;
200 0 my $to = pop;
201  
202 # expand/validate the from aliases
203 my @from_paths = map {
204 0 my ($dir, $pat) = m{^ (.*?) (?: /(.*) )? $}x;
205 0 [$self->dir($dir), $pat];
206 } @_;
207  
208 # validate the to aliases
209 0 my $to_dir = $self->dir($to);
210  
211 0 require File::Copy;
212 0 require File::Glob;
213  
214 0 my $count = 0;
215 0 foreach my $item (@from_paths) {
216 0 my ($from_dir, $pat) = @$item;
217  
218 0 ▶ $pat = "*"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pat 0 0 0
219 unless defined $pat;
220  
221 0 $pat = File::Spec->catdir($from_dir, $pat);
222  
223 # We use bsd_glob because unlike glob, it doesn't (why oh
224 # why?) use spaces as pattern delimiters
225 0 foreach my $src (File::Glob::bsd_glob $pat) {
226 0 my $path = File::Spec->abs2rel($src, $from_dir);
227 0 my $dst = File::Spec->catdir($to_dir, $path);
228  
229 0 ▶ if (-f $src) { # Copy files
Branch: 4 branches
BranchTrueFalseTotal
if (-f $src) { } 0 0 0
elsif (-d $src) { } 0 0 0
elsif (-l $src) { } 0 0 0
elsif (not -e $src) { } 0 0 0
230 0 ▶ File::Copy::copy($src, $dst)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($src, $dst) or croak("failed to copy '${src}' to '${dst}': $!") 0 0 0
231 or croak "failed to copy '$src' to '$dst': $!";
232 }
233 elsif (-d $src) { # Create directories
234 0 mkpath $dst;
235 }
236 elsif (-l $src) { # Duplicate links
237 0 ▶ symlink readlink($src), $dst
Branch: 1 branch
BranchTrueFalseTotal
symlink readlink $src, $dst or croak("failed to copy link '${src}' as '${dst}': $!") 0 0 0
238 or croak "failed to copy link '$src' as '$dst': $!";
239 }
240 elsif (!-e $src) { # the src doesn't exist
241 0 croak "cannot copy $src: no such file";
242 }
243 else { # We don't know how to do the right thing
244 0 croak "cannot copy $src: unsupported file type";
245 }
246 }
247 0 $count++;
248 }
249  
250 0 return $count;
251 }
252  
253 4 ▶no Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
254 4 ▶no File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 4 1;
↑ Summary DataDirs/Exporter.pm »