Inline/IO/File.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 71.4% 20 / 28 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 92.8% 13 / 14 Podpod 77.7% 7 / 9 totaltotal 86.6% 104 / 120 CC 17 SCAR 12.1
CC
17
Cov
88%
write
7.3
class_file
3.0
read
3.0
CRAP 3.4
« Inline/Content/Default.pm ↑ Summary Inline/Extract.pm »
1 package Test::Inline::IO::File;
2 # ABSTRACT: Test::Inline Local Filesystem IO Handler
3  
4 #pod =pod
5 #pod
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod B<Test::Inline::IO::File> is the default IO handler for L<Test::Inline>.
9 #pod
10 #pod L<Test::Inline> 2.0 was conceived in an enterprise setting, and retains
11 #pod the flexibilty, power, and bulk that this created, although for most
12 #pod users the power and complexity that is available is largely hidden away
13 #pod under multiple layers of sensible defaults.
14 #pod
15 #pod The intent with the C<InputHandler> and C<OutputHandle> parameters is to
16 #pod allow L<Test::Inline> to be able to pull source data from anywhere, and
17 #pod write the resulting test scripts to anywhere.
18 #pod
19 #pod Until a more powerful pure-OO file-system API comes along, this module
20 #pod serves as a minimalist implementation of the subset of functionality
21 #pod that L<Test::Inline> needs in order to work.
22 #pod
23 #pod An alternative IO Handler class need not subclass this one (although it
24 #pod is recommended), merely implement the same interface, taking whatever
25 #pod alternative arguments to the C<new> constructor that it wishes.
26 #pod
27 #pod All methods in this class are provided with unix-style paths, and should do
28 #pod the translating to the underlying filesystem themselves if required.
29 #pod
30 #pod =head1 METHODS
31 #pod
32 #pod =cut
33  
34 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 12 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 12 ▶use File::chmod ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 12 ▶use File::Remove ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 12 our $VERSION = '2.214';
40  
41  
42  
43  
44  
45 #####################################################################
46 # Constructor and Accessors
47  
48 #pod =pod
49 #pod
50 #pod =head2 new
51 #pod
52 #pod # Simplified usage
53 #pod $io_handler = Test::Inline::IO::File->new( $path );
54 #pod
55 #pod # Full key/value usage
56 #pod $io_handler = Test::Inline::IO::File->new(
57 #pod path => $path,
58 #pod readonly => 1,
59 #pod );
60 #pod
61 #pod The C<new> constructor takes a root path on the local filesystem
62 #pod and returns a new C<Test::Inline::IO::File> object to that
63 #pod location.
64 #pod
65 #pod =cut
66  
67 35 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
68 35 my $class = shift;
69 35 my @params = @_;
70 35 ▶ if ( @params < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params < 2) 18 17 35
71 18 ▶ my $path = defined $_[0] ? shift : File::Spec->curdir;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 18 0 18
72 18 @params = ( path => $path );
73 }
74  
75 # Create the object
76 35 my $self = bless { @params }, $class;
77  
78 # Apply defaults
79 35 $self->{readonly} = !! $self->{readonly};
80  
81 35 return $self;
82 }
83  
84 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod undocumented
85 0 $_[0]->{path};
86 }
87  
88 18 ▶sub readonly {
Subroutine readonly: called CC=1 SCAR=0.0
Pod undocumented
89 18 $_[0]->{readonly};
90 }
91  
92 # Resolve the full path for any file
93 66 ▶sub _path {
Subroutine _path: called CC=2 SCAR=7.1
94 66 my $self = shift;
95 66 ▶ my $file = defined $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 66 0 66
96 66 File::Spec->catfile( $self->{path}, $file );
97 }
98  
99  
100  
101  
102  
103 #####################################################################
104 # Filesystem API
105  
106 #pod =pod
107 #pod
108 #pod =head2 exists_file $file
109 #pod
110 #pod The C<exists_file> method checks to see if a particular file currently
111 #pod exists in the input handler.
112 #pod
113 #pod Returns true if it exists, or false if not.
114 #pod
115 #pod =cut
116  
117 17 ▶sub exists_file {
Subroutine exists_file: called CC=2 SCAR=7.1
Pod documented
118 17 my $self = shift;
119 17 ▶ my $file = $self->_path(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_path(shift()) or return undef 17 0 17
120 17 !! -f $file;
121 }
122  
123 #pod =pod
124 #pod
125 #pod =head2 exists_dir $dir
126 #pod
127 #pod The C<exists_dir> method checks to see if a particular directory currently
128 #pod exists in the input handler.
129 #pod
130 #pod Returns true if it exists, or false if not.
131 #pod
132 #pod =cut
133  
134 6 ▶sub exists_dir {
Subroutine exists_dir: called CC=2 SCAR=7.1
Pod documented
135 6 my $self = shift;
136 6 ▶ my $dir = $self->_path(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->_path(shift()) or return undef 6 0 6
137 6 !! -d $dir;
138 }
139  
140 #pod =pod
141 #pod
142 #pod =head2 read $file
143 #pod
144 #pod The C<read> method reads in the entire contents of a single file,
145 #pod returning it as a reference to a SCALAR. It also localises the
146 #pod newlines as it does this, so files from different operating
147 #pod systems should read as you expect.
148 #pod
149 #pod Returns a SCALAR reference, or C<undef> on error.
150 #pod
151 #pod =cut
152  
153 21 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod documented
154 21 my $self = shift;
155 21 ▶ my $file = $self->_path(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_path(shift()) or return undef 21 0 21
156 21 require File::Flat;
157 21 ▶ my $content = File::Flat->slurp($file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $content = "File::Flat"->slurp($file) or return undef 20 1 21
158 20 $$content =~ s/\015{1,2}\012|\015|\012/\n/g;
159 20 $content;
160 }
161  
162 #pod =pod
163 #pod
164 #pod =head2 write $file, $content
165 #pod
166 #pod The C<write> method writes a string to a file in one hit, creating
167 #pod it and it's path if needed.
168 #pod
169 #pod =cut
170  
171 18 ▶sub write {
Subroutine write: called CC=7 SCAR=19.8
Pod documented
172 18 my $self = shift;
173 18 ▶ my $file = $self->_path(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_path(shift()) or return undef 18 0 18
174 18 ▶ if ( -f $file and ! -w $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and not -w $file) 4 14 18
Condition66%2/3
-f $file and not -w $file
!ll&&!rl&&r
1404
MC/DC50%1/2
-f $file and not -w $file
-f $file not -w $file
Truth table66%2/3
A and not(B)
A= -f $fileB= not -w $file
ABresult
0X0
100
111
175 4 ▶ File::Remove::remove($file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&File::Remove::remove($file) or return undef 4 0 4
176  
177 }
178 18 require File::Flat;
179 18 my $rv = File::Flat->write( $file, @_ );
180 18 ▶ if ( $rv and $self->readonly ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $self->readonly) 8 10 18
Condition66%2/3
$rv and $self->readonly
!ll&&!rl&&r
0108
MC/DC50%1/2
$rv and $self->readonly
$rv $self->readonly
Truth table66%2/3
A and B
A= $rvB= $self->readonly
ABresult
0X0
100
111
181 8 File::chmod::symchmod('a-w', $file);
182 }
183 18 return $rv;
184 }
185  
186 #pod =pod
187 #pod
188 #pod =head2 class_file $class
189 #pod
190 #pod Assuming your input FileHandler is pointing at the root directory
191 #pod of a lib path (meaning that My::Module will be located at My/Module.pm
192 #pod within it) the C<class_file> method will take a class name, and check to see
193 #pod if the file for that class exists in the FileHandler.
194 #pod
195 #pod Returns a reference to an ARRAY containing the filename if it exists,
196 #pod or C<undef> on error.
197 #pod
198 #pod =cut
199  
200 2 ▶sub class_file {
Subroutine class_file: called CC=3 SCAR=11.0
Pod documented
201 2 my $self = shift;
202 2 ▶ my $_class = defined $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 0 2
203 2 my $file = File::Spec->catfile( split /(?:::|')/, $_class ) . '.pm';
204 2 ▶ $self->exists_file($file) and [ $file ];
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_file($file) and [$file] 1 1 2
205 }
206  
207 #pod =pod
208 #pod
209 #pod =head2 find $class
210 #pod
211 #pod The C<find> method takes as argument a directory root class, and then scans within
212 #pod the input FileHandler to find all files contained in that class or any
213 #pod other classes under it's namespace.
214 #pod
215 #pod Returns a reference to an ARRAY containing all the files within the class,
216 #pod or C<undef> on error.
217 #pod
218 #pod =cut
219  
220 5 ▶sub find {
Subroutine find: called CC=2 SCAR=6.9
Pod documented
221 5 my $self = shift;
222 5 ▶ my $dir = $self->exists_dir($_[0]) ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_dir($_[0]) ? : 4 1 5
223  
224 # Search within the path
225 4 require File::Find::Rule;
226 4 my @files = File::Find::Rule->file
227 ->name('*.pm')
228 ->relative
229 ->in( $self->_path($dir) );
230 4 @files = map { File::Spec->catfile( $dir, $_ ) } sort @files;
231 4 return \@files;
232 }
233  
234 12 1;
235  
236 __END__
« Inline/Content/Default.pm ↑ Summary Inline/Extract.pm »