lib/Serge/FindFiles.pm

filter: Statementstmt 75.0% 63 / 84 Branchbran 41.3% 19 / 46 Conditioncond 28.2% 11 / 39 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 4 totaltotal 51.2% 107 / 209 SCAR 52.7
CC
41
Cov
55%
check_path
188.8
find
15.4
process_subdir
14.8
CRAP 193.9
« lib/Serge/Engine/Plugin/parse_strings.pm ↑ Summary lib/Serge/Command/help.pm »
1 package Serge::FindFiles;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Config::Neat::Util qw(is_array is_neat_array);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec::Functions qw(rel2abs catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 6 ▶ my ($class, $self) = @_;
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
12  
13 6 ▶ $self = {} unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 6 6
14  
15 # Exclude `.svn', `.git', `.hg' (Mercurial), '.bzr' (Bazaar) and `CVS' dirs (case-sensitive) from processing by default
16 6 ▶ $self->{dir_exclude} = ['^(\.svn|\.git|\.hg|\.bzr|CVS)$'] unless exists $self->{dir_exclude};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"dir_exclude"} 6 0 6
17  
18 6 bless $self, $class;
19 6 return $self;
20 }
21  
22 sub find {
23 6 ▶ my ($self, $dir) = @_;
Subroutine find: called CC=9 SCAR=27.3
Pod undocumented
24  
25 # Perform sanity checks
26 6 ▶ die "'prefix' property must be a scalar value" if exists $self->{prefix} && !(defined $self->{prefix} && (ref(\$self->{prefix}) eq 'SCALAR'));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prefix"} and not defined $self->{"prefix"} && ref \$self->{"prefix"} eq "SCALAR" 0 6 6
Condition16%1/6
exists $self->{"prefix"} and not defined $self->{"prefix"} && ref \$self->{"prefix"} eq "SCALAR"
!ll&&!rl&&r
600
defined $self->{'prefix'} && ref \$self->{'prefix'} eq 'SCALAR'
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{"prefix"} and not defined $self->{"prefix"} && ref \$self->{"prefix"} eq "SCALAR"
exists $self->{"prefix"} defined $self->{'prefix'} ref \$self->{'prefix'} eq 'SCALAR'
Truth table25%1/4
A and not(B && C)
A= exists $self->{"prefix"}B= defined $self->{'prefix'}C= ref \$self->{'prefix'} eq 'SCALAR'
ABCresult
0XX0
1110
10X1
1101
27 6 ▶ die "'postcheck_callback' property must be a code reference" if exists $self->{postcheck_callback} && (ref($self->{postcheck_callback}) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"postcheck_callback"} and ref $self->{"postcheck_callback"} ne "CODE" 0 6 6
Condition33%1/3
exists $self->{"postcheck_callback"} and ref $self->{"postcheck_callback"} ne "CODE"
!ll&&!rl&&r
060
MC/DC0%0/2
exists $self->{"postcheck_callback"} and ref $self->{"postcheck_callback"} ne "CODE"
exists $self->{"postcheck_callback"} ref $self->{"postcheck_callback"} ne "CODE"
Truth table33%1/3
A and B
A= exists $self->{"postcheck_callback"}B= ref $self->{"postcheck_callback"} ne "CODE"
ABresult
0X0
100
111
28  
29 map {
30 6 ▶ die "'$_' property must be an array reference" if exists $self->{$_} && !(is_array($self->{$_}) || is_neat_array($self->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$_} and not is_array($self->{$_}) || is_neat_array($self->{$_}) 0 24 24
Condition66%4/6
exists $self->{$_} and not is_array($self->{$_}) || is_neat_array($self->{$_})
!ll&&!rl&&r
12120
is_array($self->{$_}) || is_neat_array($self->{$_})
l!l&&r!l&&!r
660
MC/DC0%0/3
exists $self->{$_} and not is_array($self->{$_}) || is_neat_array($self->{$_})
exists $self->{$_} is_array($self->{$_}) is_neat_array($self->{$_})
Truth table0%0/4
A and not(B || C)
A= exists $self->{$_}B= is_array($self->{$_})C= is_neat_array($self->{$_})
ABCresult
0XX0
11X0
1010
1001
31 } qw(match dir_match dir_exclude exclude);
32  
33 # Initialize the output hash and process the dir recursively
34  
35 6 $self->{found_files} = {};
36 6 $self->process_subdir(rel2abs($dir), '');
37 }
38  
39 sub process_subdir {
40 6 ▶ my ($self, $dir, $relpath) = @_;
Subroutine process_subdir: called CC=11 SCAR=27.0
Pod undocumented
41  
42 6 opendir(my $dh, $dir);
43 6 ▶ while (my $name = readdir $dh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = readdir $dh)) 20 6 26
44 20 ▶ next if $name eq '.' || $name eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "." or $name eq ".." 12 8 20
Condition100%3/3
$name eq "." or $name eq ".."
l!l&&r!l&&!r
668
MC/DC100%2/2
$name eq "." or $name eq ".."
$name eq "." $name eq ".."
Truth table100%3/3
A or B
A= $name eq "."B= $name eq ".."
ABresult
1X1
011
000
45 8 ▶ my $subpath = $relpath ne '' ? $relpath.'/'.$name : $name; # relative path is always delimited by a forward slash (relative paths are platform-independent)
Branch: 1 branch
BranchTrueFalseTotal
$relpath ne '' ? : 0 8 8
46 8 my $fullpath = catfile($dir, $name);
47 8 my $file_rel = $self->{prefix}.$subpath;
48  
49 8 ▶ if (-d $fullpath) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $fullpath) { } 0 8 8
elsif (-f $fullpath) { } 8 0 8
50 0 ▶ if ($self->{process_subdirs} && $self->check_path($file_rel, $name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"process_subdirs"} and $self->check_path($file_rel, $name)) 0 0 0
Condition0%0/3
$self->{"process_subdirs"} and $self->check_path($file_rel, $name)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"process_subdirs"} and $self->check_path($file_rel, $name)
$self->{"process_subdirs"} $self->check_path($file_rel, $name)
Truth table0%0/3
A and B
A= $self->{"process_subdirs"}B= $self->check_path($file_rel, $name)
ABresult
0X0
100
111
51 0 $self->process_subdir($fullpath, $subpath);
52 }
53 } elsif (-f $fullpath) {
54 8 ▶ if ($self->check_path($file_rel, $name, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_path($file_rel, $name, 1)) 8 0 8
55 8 ▶ if (exists $self->{postcheck_callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"postcheck_callback"}) 8 0 8
56 8 $file_rel = &{$self->{postcheck_callback}}($file_rel, $fullpath);
57 }
58 8 $self->{found_files}->{$file_rel} = 1;
59 }
60 }
61 }
62 6 closedir $dh;
63 }
64  
65 sub check_path {
66 8 ▶ my ($self, $path, $name, $file_mode) = @_;
Subroutine check_path: called CC=21 SCAR=52.4
Pod undocumented
67  
68 8 my $ok = undef;
69  
70 8 ▶ if ($file_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_mode) { } 8 0 8
71 # Test if relative file path (or file name) matches the mask
72 8 ▶ if (exists $self->{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'match'}) { } 8 0 8
73 8 foreach my $rule (@{$self->{match}}) {
74 8 ▶ if (($name =~ m/$rule/) || ($path =~ m/$rule/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$rule/ or $path =~ /$rule/) 8 0 8
Condition33%1/3
$name =~ /$rule/ or $path =~ /$rule/
l!l&&r!l&&!r
800
MC/DC0%0/2
$name =~ /$rule/ or $path =~ /$rule/
$name =~ /$rule/ $path =~ /$rule/
Truth table33%1/3
A or B
A= $name =~ /$rule/B= $path =~ /$rule/
ABresult
1X1
011
000
75 8 $ok = 1;
76 8 last;
77 }
78 }
79 } else {
80 # If 'match' not provided, gather all files
81 0 $ok = 1;
82 }
83 } else {
84 # Test if relative directory path (or directory name) matches the mask
85 0 ▶ if (exists $self->{dir_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'dir_match'}) { } 0 0 0
86 0 foreach my $rule (@{$self->{dir_match}}) {
87 0 ▶ if (($name =~ m/$rule/) || ($path =~ m/$rule/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$rule/ or $path =~ /$rule/) 0 0 0
Condition0%0/3
$name =~ /$rule/ or $path =~ /$rule/
l!l&&r!l&&!r
000
MC/DC0%0/2
$name =~ /$rule/ or $path =~ /$rule/
$name =~ /$rule/ $path =~ /$rule/
Truth table0%0/3
A or B
A= $name =~ /$rule/B= $path =~ /$rule/
ABresult
1X1
011
000
88 0 $ok = 1;
89 0 last;
90 }
91 }
92 } else {
93 # If 'dir_match' not provided, go through all directories
94 0 $ok = 1;
95 }
96 }
97  
98 8 ▶ if (!$file_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_mode) 0 8 8
99 # Test if relative directory path (or directory name) does not match the directory-specific exclusion mask
100 0 ▶ if ($ok && exists $self->{dir_exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and exists $self->{"dir_exclude"}) 0 0 0
Condition0%0/3
$ok and exists $self->{"dir_exclude"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and exists $self->{"dir_exclude"}
$ok exists $self->{"dir_exclude"}
Truth table0%0/3
A and B
A= $okB= exists $self->{"dir_exclude"}
ABresult
0X0
100
111
101 0 foreach my $rule (@{$self->{dir_exclude}}) {
102 0 ▶ if (($name =~ m/$rule/) || ($path =~ m/$rule/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$rule/ or $path =~ /$rule/) 0 0 0
Condition0%0/3
$name =~ /$rule/ or $path =~ /$rule/
l!l&&r!l&&!r
000
MC/DC0%0/2
$name =~ /$rule/ or $path =~ /$rule/
$name =~ /$rule/ $path =~ /$rule/
Truth table0%0/3
A or B
A= $name =~ /$rule/B= $path =~ /$rule/
ABresult
1X1
011
000
103 0 $ok = undef;
104 0 last;
105 }
106 }
107 }
108 }
109  
110 # Test if relative file/directory path (or file/directory name) does not match the exclusion mask
111 8 ▶ if ($ok && exists $self->{exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and exists $self->{"exclude"}) 0 8 8
Condition33%1/3
$ok and exists $self->{"exclude"}
!ll&&!rl&&r
080
MC/DC0%0/2
$ok and exists $self->{"exclude"}
$ok exists $self->{"exclude"}
Truth table33%1/3
A and B
A= $okB= exists $self->{"exclude"}
ABresult
0X0
100
111
112 0 foreach my $rule (@{$self->{exclude}}) {
113 0 ▶ if (($name =~ m/$rule/) || ($path =~ m/$rule/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$rule/ or $path =~ /$rule/) 0 0 0
Condition0%0/3
$name =~ /$rule/ or $path =~ /$rule/
l!l&&r!l&&!r
000
MC/DC0%0/2
$name =~ /$rule/ or $path =~ /$rule/
$name =~ /$rule/ $path =~ /$rule/
Truth table0%0/3
A or B
A= $name =~ /$rule/B= $path =~ /$rule/
ABresult
1X1
011
000
114 0 $ok = undef;
115 0 last;
116 }
117 }
118 }
119  
120 8 return $ok;
121 }
122  
123 2 1;
« lib/Serge/Engine/Plugin/parse_strings.pm ↑ Summary lib/Serge/Command/help.pm »