lib/XT/Files.pm

filter: Statementstmt 100% 206 / 206 Branchbran 98.8% 83 / 84 Conditioncond 88.8% 16 / 18 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 36 / 36 Podpod 93.7% 15 / 16 totaltotal 98.3% 364 / 370 CC 54 SCAR 17.2
CC
54
Cov
99%
_load_config
16.0
files
7.0
plugin
6.0
CRAP 5.6
« lib/XT/Files/Plugin/Dirs.pm ↑ Summary corpus/dist1/xt/lib/Local/Test.pm »
1 package XT::Files;
2  
3 32 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 our $VERSION = '0.001';
8  
9 32 ▶use Class::Tiny 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 ▶use Role::Tiny::With ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 32 Role::Tiny::With::with 'XT::Files::Role::Logger';
14  
15 32 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 32 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 32 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 32 ▶use version 0.77 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 32 ▶use XT::Files::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 32 ▶use constant MODULE_NAME_RX => qr{ ^ [A-Za-z_] [0-9A-Za-z_]* (?: :: [0-9A-Za-z_]+ )* $ }xs; ## no critic (RegularExpressions::RequireLineBoundaryMatching)
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #
27 # CLASS METHODS
28 #
29  
30 sub BUILD {
31 48 ▶ my ( $self, $args ) = @_;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
32  
33 48 $self->{_excludes} = [];
34 48 $self->{_file} = {};
35  
36 48 ▶ if ( exists $args->{'-config'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'-config'}) { } 41 7 48
37 41 ▶ if ( defined $args->{'-config'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"-config"}) 11 30 41
38 11 $self->_load_config( $args->{'-config'} );
39 }
40  
41 # -config exists but is not defined, no configuration requested
42 }
43 else {
44 # We did not get "config => undef" and therefore try to load the
45 # default config file
46 7 $self->_load_default_config();
47 }
48  
49 40 return;
50 }
51  
52 {
53 # The XT::Files singleton
54 32 my $xtf;
55  
56 sub initialize {
57 5 ▶ my $class = shift;
Subroutine initialize: called CC=2 SCAR=6.9
Pod documented
58  
59 5 ▶ Carp::croak( __PACKAGE__ . q{ is already initialized} ) if $class->_is_initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_initialized 1 4 5
60  
61 4 $xtf = $class->new(@_);
62 4 return $xtf;
63 }
64  
65 sub instance {
66 4 ▶ my ($class) = @_;
Subroutine instance: called CC=2 SCAR=6.9
Pod documented
67  
68 4 ▶ if ( !$class->_is_initialized ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_initialized) 3 1 4
69  
70 # ignore args
71 3 $class->initialize;
72 }
73  
74 4 return $xtf;
75 }
76  
77 sub _is_initialized {
78 45 ▶ my ($class) = @_;
Subroutine _is_initialized: called CC=2 SCAR=6.9
79  
80 45 ▶ return 1 if defined $xtf;
Branch: 1 branch
BranchTrueFalseTotal
if defined $xtf 5 40 45
81 40 return;
82 }
83 }
84  
85 #
86 # OBJECT METHODS
87 #
88  
89 32 sub plugin {
90 28 ▶ my ( $self, $plugin_name, $plugin_version, $keyvals_ref ) = @_;
Subroutine plugin: called CC=6 SCAR=17.9
Pod documented
91  
92 28 my $plugin_pkg = $self->_expand_config_plugin_name($plugin_name);
93  
94 27 Module::Load::load($plugin_pkg);
95  
96 26 ▶ if ( defined $plugin_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plugin_version) 6 20 26
97 6 ▶ $self->log_fatal("Not a valid version '$plugin_version'") if !version::is_lax($plugin_version);
Branch: 1 branch
BranchTrueFalseTotal
unless version::is_lax($plugin_version) 1 5 6
98 5 ▶ $self->log_fatal( "$plugin_pkg version $plugin_version required--this is only version " . $plugin_pkg->VERSION ) if version->parse( $plugin_pkg->VERSION ) < version->parse($plugin_version);
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse($plugin_pkg->VERSION) < "version"->parse($plugin_version) 1 4 5
99 }
100  
101 24 ▶ $self->log_fatal("$plugin_pkg doesn't have a run method") if !$plugin_pkg->can('run');
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_pkg->can("run") 1 23 24
102 23 ▶ $self->log_fatal("$plugin_pkg doesn't have a new method") if !$plugin_pkg->can('new');
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_pkg->can("new") 1 22 23
103  
104 22 my $plugin = $plugin_pkg->new( xtf => $self );
105  
106 22 $plugin->run($keyvals_ref);
107  
108 22 return;
109 }
110  
111 sub files {
112 20 ▶ my ($self) = @_;
Subroutine files: called CC=7 SCAR=19.5
Pod documented
113  
114 20 my $exclude_regex;
115 20 my @excludes = @{ $self->{_excludes} };
116 20 ▶ if (@excludes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@excludes) 6 14 20
117 6 $exclude_regex = join q{|}, @excludes;
118 }
119  
120 20 my @result;
121 RESULT_FILE:
122 20 for my $name ( sort keys %{ $self->{_file} } ) {
123  
124 # skip ignored files
125 54 ▶ next RESULT_FILE if !defined $self->{_file}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file"}{$name} 4 50 54
126  
127 # skip excluded files
128 50 ▶ next RESULT_FILE if defined $exclude_regex && File::Basename::fileparse($name) =~ $exclude_regex;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exclude_regex and File::Basename::fileparse($name) =~ /$exclude_regex/ 7 43 50
Condition100%3/3
defined $exclude_regex and File::Basename::fileparse($name) =~ /$exclude_regex/
!ll&&!rl&&r
25187
MC/DC100%2/2
defined $exclude_regex and File::Basename::fileparse($name) =~ /$exclude_regex/
defined $exclude_regex File::Basename::fileparse($name) =~ /$exclude_regex/
Truth table100%3/3
A and B
A= defined $exclude_regexB= File::Basename::fileparse($name) =~ /$exclude_regex/
ABresult
0X0
100
111
129  
130 # skip non-existing files
131 43 ▶ next RESULT_FILE if !-e $name;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $name 1 42 43
132  
133 42 push @result, $self->{_file}->{$name};
134 }
135  
136 20 return @result;
137 }
138  
139 #
140 # File
141 #
142  
143 sub bin_file {
144 26 ▶ my ( $self, $name ) = @_;
Subroutine bin_file: called CC=1 SCAR=0.0
Pod documented
145  
146 26 my $file = XT::Files::File->new( name => $name, is_script => 1 );
147 26 $self->file( $name, $file );
148 26 return;
149 }
150  
151 sub file {
152 93 ▶ my ( $self, $name, $file ) = @_;
Subroutine file: called CC=5 SCAR=16.1
Pod documented
153  
154 93 ▶ if ( @_ > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 71 22 93
155 71 ▶ if ( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 67 4 71
156 67 ▶ $self->log_fatal(q{File is not of class 'XT::Files::File'}) if !defined Scalar::Util::blessed($file) || !$file->isa('XT::Files::File');
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($file) and $file->isa("XT::Files::File") 1 66 67
Condition66%2/3
defined builtin::blessed($file) and $file->isa("XT::Files::File")
!ll&&!rl&&r
0166
MC/DC50%1/2
defined builtin::blessed($file) and $file->isa("XT::Files::File")
defined builtin::blessed($file) $file->isa("XT::Files::File")
Truth table66%2/3
A and B
A= defined builtin::blessed($file)B= $file->isa("XT::Files::File")
ABresult
0X0
100
111
157 }
158  
159 70 $self->{_file}->{$name} = $file;
160 }
161  
162 92 return $self->{_file}->{$name};
163 }
164  
165 sub ignore_file {
166 3 ▶ my ( $self, $name ) = @_;
Subroutine ignore_file: called CC=1 SCAR=0.0
Pod documented
167  
168 3 $self->file( $name, undef );
169 3 return;
170 }
171  
172 sub module_file {
173 21 ▶ my ( $self, $name ) = @_;
Subroutine module_file: called CC=1 SCAR=0.0
Pod documented
174  
175 21 my $file = XT::Files::File->new( name => $name, is_module => 1 );
176 21 $self->file( $name, $file );
177 21 return;
178 }
179  
180 sub pod_file {
181 10 ▶ my ( $self, $name ) = @_;
Subroutine pod_file: called CC=1 SCAR=0.0
Pod documented
182  
183 10 my $file = XT::Files::File->new( name => $name, is_pod => 1 );
184 10 $self->file( $name, $file );
185 10 return;
186 }
187  
188 sub remove_file {
189 1 ▶ my ( $self, $name ) = @_;
Subroutine remove_file: called CC=1 SCAR=0.0
Pod documented
190  
191 1 delete $self->{_file}->{$name};
192 1 return;
193 }
194  
195 sub test_file {
196 8 ▶ my ( $self, $name ) = @_;
Subroutine test_file: called CC=1 SCAR=0.0
Pod documented
197  
198 8 my $file = XT::Files::File->new( name => $name, is_test => 1, is_script => 1 );
199 8 $self->file( $name, $file );
200 8 return;
201 }
202  
203 #
204 # Directory
205 #
206  
207 sub bin_dir {
208 14 ▶ my ( $self, $name ) = @_;
Subroutine bin_dir: called CC=2 SCAR=6.9
Pod documented
209  
210 14 for my $file ( $self->_find_new_files($name) ) {
211 21 $self->bin_file( $file, $name );
212 }
213  
214 14 return;
215 }
216  
217 sub module_dir {
218 9 ▶ my ( $self, $name ) = @_;
Subroutine module_dir: called CC=4 SCAR=13.9
Pod documented
219  
220 9 for my $file ( $self->_find_new_files($name) ) {
221 30 ▶ if ( $file =~ m{ [.] pm $ }xsm ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ / [.] pm $ /msx) { } 17 13 30
elsif ($file =~ / [.] pod $ /msx) { } 6 7 13
222 17 $self->module_file( $file, $name );
223 }
224 elsif ( $file =~ m{ [.] pod $ }xsm ) {
225 6 $self->pod_file( $file, $name );
226 }
227 }
228  
229 9 return;
230 }
231  
232 sub test_dir {
233 4 ▶ my ( $self, $name ) = @_;
Subroutine test_dir: called CC=3 SCAR=11.0
Pod documented
234  
235 4 for my $file ( $self->_find_new_files($name) ) {
236 6 ▶ if ( $file =~ m{ [.] t $ }xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ / [.] t $ /msx) 3 3 6
237 3 $self->test_file( $file, $name );
238 }
239 }
240  
241 4 return;
242 }
243  
244 #
245 # Excludes
246 #
247  
248 sub exclude {
249 21 ▶ my ( $self, $exclude ) = @_;
Subroutine exclude: called CC=1 SCAR=0.0
Pod documented
250  
251 21 push @{ $self->{_excludes} }, $exclude;
252 21 return;
253 }
254  
255 #
256 # PRIVATE METHODS
257 #
258  
259 sub _expand_config_plugin_name {
260 31 ▶ my ( $self, $plugin_name ) = @_;
Subroutine _expand_config_plugin_name: called CC=3 SCAR=11.0
261  
262 31 my $package_name = $plugin_name;
263 31 ▶ if ( $package_name !~ s{ ^ = }{}xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package_name =~ s/ ^ = //msx) 17 14 31
264 17 $package_name = "XT::Files::Plugin::$plugin_name";
265 }
266  
267 31 ▶ $self->log_fatal("'$plugin_name' is not a valid plugin name") if $package_name !~ MODULE_NAME_RX;
Branch: 1 branch
BranchTrueFalseTotal
unless $package_name =~ / ^ [A-Za-z_] [0-9A-Za-z_]* (?: :: [0-9A-Za-z_]+ )* $ /sx 2 29 31
268  
269 29 return $package_name;
270 }
271  
272 sub _find_new_files {
273 32 ▶ my ( $self, $dir ) = @_;
Subroutine _find_new_files: called CC=2 SCAR=6.9
274  
275 32 my @files;
276  
277 32 ▶ if ( !-d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 10 22 32
278 10 $self->log_debug("Directory $dir does not exist or is not a directory");
279 10 return;
280 }
281  
282 File::Find::find(
283 {
284 no_chdir => 1,
285 wanted => sub {
286 97 ▶ return if -l $File::Find::name || !-f _;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if -l $File::Find::name or not -f _ 33 64 97
Condition100%3/3
-l $File::Find::name or not -f _
l!l&&r!l&&!r
23164
MC/DC100%2/2
-l $File::Find::name or not -f _
-l $File::Find::name not -f _
Truth table100%3/3
A or not(B)
A= -l $File::Find::nameB= not -f _
ABresult
1X1
011
000
287 64 push @files, $File::Find::name;
288 },
289 },
290 22 $dir,
291 );
292  
293 22 @files = grep { !exists $self->{_file}->{$_} } @files;
294  
295 22 return @files;
296 }
297  
298 sub _global_keyval { ## no critic (Subroutines::RequireFinalReturn)
299 7 ▶ my ( $self, $key, $value ) = @_;
Subroutine _global_keyval: called CC=4 SCAR=13.9
300  
301 7 ▶ if ( $key eq ':version' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ":version") 6 1 7
302 6 ▶ $self->log_fatal("Not a valid version '$value'") if !version::is_lax($value);
Branch: 1 branch
BranchTrueFalseTotal
unless version::is_lax($value) 2 4 6
303 4 ▶ $self->log_fatal( __PACKAGE__ . " version $value required--this is only version " . __PACKAGE__->VERSION ) if version->parse( __PACKAGE__->VERSION ) < version->parse($value);
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse(__PACKAGE__->VERSION) < "version"->parse($value) 2 2 4
304 2 return;
305 }
306  
307 1 $self->log_fatal("Invalid entry '$key = $value'");
308 }
309  
310 sub _load_config {
311 18 ▶ my ( $self, $config ) = @_;
Subroutine _load_config: called CC=16 SCAR=27.7
312  
313 18 my $type_section = 1;
314 18 my $type_keyval = 2;
315  
316 18 ▶ open my $fh, '<', $config or $self->log_fatal("Cannot read file '$config': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $config or $self->log_fatal("Cannot read file '${config}': $!") 2 16 18
317  
318 16 my $in_global_section = 1;
319 16 my @lines;
320 16 my $line_counter = 0;
321 LINE:
322 16 ▶ while ( defined( my $line = <$fh> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 48 10 58
323 48 $line_counter++;
324  
325 # skip empty lines or comment
326 48 ▶ next LINE if $line =~ m{ ^ \s* (?: [#;] | $ ) }xsm;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / ^ \s* (?: [#;] | $ ) /msx 8 40 48
327  
328 # remove leading whitespace
329 40 $line =~ s{ ^ \s* }{}xsm;
330  
331 # remove tailing whitespace
332 40 $line =~ s{ \s* $ }{}xsm;
333  
334 40 ▶ if ( $line =~ m{ ^ \[ }xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / ^ \[ /msx) 12 28 40
335 12 ▶ $self->log_fatal("Syntax error in config on line $line_counter") if $line !~ m{ ^ \[ ( .+ ) \] $ }xsm;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ / ^ \[ ( .+ ) \] $ /msx 1 11 12
336 11 my $section = $1; ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
337 11 push @lines, [ $type_section, $line_counter, $section ];
338 11 $in_global_section = 0;
339 11 next LINE;
340 }
341  
342 28 my ( $key, $value ) = split /\s*=\s*/xsm, $line, 2;
343 28 ▶ $self->log_fatal("Syntax error in config on line $line_counter") if !defined $key || !defined $value || $key eq q{} || $value eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key && defined $value or $key eq "" or $value eq "" 2 25 27
Condition88%8/9
not defined $key && defined $value or $key eq "" or $value eq ""
l!l&&r!l&&!r
2125
not defined $key && defined $value or $key eq ""
l!l&&r!l&&!r
1126
defined $key && defined $value
!ll&&!rl&&r
0127
MC/DC75%3/4
not defined $key && defined $value or $key eq "" or $value eq ""
defined $key defined $value $key eq "" $value eq ""
Truth table80%4/5
not(A && B) or C or D
A= defined $keyB= defined $valueC= $key eq ""D= $value eq ""
ABCDresult
0XXX1
10XX1
111X1
11011
11000
344 #
345 25 ▶ if ($in_global_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_global_section) 3 22 25
346 3 $self->_global_keyval( $key, $value );
347 1 next LINE;
348 }
349  
350 22 push @lines, [ $type_keyval, $line_counter, $key, $value ];
351 }
352 10 ▶ close $fh or $self->logger->log_fatal("Cannot read file '$config': $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->logger->log_fatal("Cannot read file '${config}': $!") 0 10 10
353  
354 10 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 11 10 21
355 11 my $section = ${ $lines[0] }[2];
356 11 shift @lines;
357  
358 11 my @keyvals;
359 my $plugin_version;
360  
361 LINE_KEYVAL:
362 11 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 24 9 33
363 24 ▶ last LINE_KEYVAL if $lines[0][0] == $type_section;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[0][0] == $type_section 2 22 24
364  
365 22 ▶ if ( $lines[0][2] eq ':version' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0][2] eq ':version') { } 3 19 22
366 3 $plugin_version = $lines[0][3];
367 }
368 else {
369 19 push @keyvals, [ @{ $lines[0] }[ 2, 3 ] ];
370 }
371  
372 22 shift @lines;
373 }
374  
375 11 $self->plugin( $section, $plugin_version, \@keyvals );
376 }
377  
378 10 return;
379 }
380  
381 sub _load_default_config {
382 7 ▶ my ($self) = @_;
Subroutine _load_default_config: called CC=5 SCAR=16.1
383  
384 7 my $config;
385 FILE:
386 7 for my $file ( '.xtfilesrc', 'xtfiles.ini' ) {
387 14 ▶ next FILE if !-e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 10 4 14
388 4 ▶ $self->log_fatal("Multiple default config files found: '$config' and '$file'") if defined $config;
Branch: 1 branch
BranchTrueFalseTotal
if defined $config 1 3 4
389 3 $config = $file;
390 }
391  
392 6 ▶ if ( !defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config) 4 2 6
393 4 my $default_config = '[Default]';
394 4 $config = \$default_config;
395 }
396  
397 6 return $self->_load_config($config);
398 }
399  
400 32 1;
401  
402 __END__
« lib/XT/Files/Plugin/Dirs.pm ↑ Summary corpus/dist1/xt/lib/Local/Test.pm »