blib/lib/File/Globstar/ListMatch.pm

filter: Statementstmt 100% 100 / 100 Branchbran 88.1% 37 / 42 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 19 / 19 Podpod 100% 5 / 5 totaltotal 97.0% 166 / 171 SCAR 32.6
CC
26
Cov
97%
__match
10.0
_readString
7.0
new
5.0
CRAP 26.0
« blib/lib/File/Globstar.pm ↑ Summary Build »
1 # Copyright (C) 2016-2019 Guido Flohr <guido.flohr@cantanea.com>,
2 # all rights reserved.
3  
4 # This file is distributed under the same terms and conditions as
5 # Perl itself.
6  
7 # This next lines is here to make Dist::Zilla happy.
8 # ABSTRACT: Perl Globstar (double asterisk globbing) and utils
9  
10 package File::Globstar::ListMatch;
11 3 $File::Globstar::ListMatch::VERSION = '0.6';
12 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Locale::TextDomain qw(File-Globstar);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util 1.21 qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use File::Globstar qw(translatestar pnmatchstar);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use constant RE_NONE => File::Globstar::RE_NONE();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant RE_NEGATED => File::Globstar::RE_NEGATED();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use constant RE_FULL_MATCH => File::Globstar::RE_FULL_MATCH;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use constant RE_DIRECTORY => File::Globstar::RE_DIRECTORY;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 sub new {
26 40 ▶ my ($class, $input, %options) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
27  
28 40 my $self = {};
29 40 bless $self, $class;
30 40 $self->{__ignore_case} = delete $options{ignoreCase};
31 40 $self->{__filename} = delete $options{filename};
32  
33 40 ▶ if (ref $input) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input) { } 38 2 40
elsif ('GLOB' eq builtin::reftype(\$input)) { } 1 1 2
34 38 my $type = reftype $input;
35 38 ▶ if ('SCALAR' eq $type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('SCALAR' eq $type) { } 36 2 38
elsif ('ARRAY' eq $type) { } 1 1 2
36 36 $self->_readString($$input);
37 } elsif ('ARRAY' eq $type) {
38 1 $self->_readArray($input);
39 } else {
40 1 $self->_readFileHandle($input);
41 }
42 } elsif ("GLOB" eq reftype \$input) {
43 1 $self->_readFileHandle(\$input, );
44 } else {
45 1 $self->_readFile($input);
46 }
47  
48 40 return $self;
49 }
50  
51 sub __match {
52 89 ▶ my ($self, $imode, $path, $is_directory) = @_;
Subroutine __match: called CC=10 SCAR=23.0
53  
54 89 my $match;
55 89 foreach my $pattern ($self->patterns) {
56 136 my $type = ref $pattern;
57 136 my $negated;
58 136 ▶ if ($type & RE_NEGATED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type & 1) { } 47 89 136
59 47 ▶ next if !$match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 3 44 47
60 44 $negated = 1;
61 } else {
62 89 ▶ next if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 0 89 89
63 }
64  
65 133 $match = pnmatchstar $pattern, $path, isDirectory => $is_directory;
66 }
67  
68 89 ▶ return 1 if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 36 53 89
69  
70 # Check that none of its parent directories has been ignored.
71 53 ▶ if (!$imode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($imode) 35 18 53
72 35 $path =~ s{/$}{};
73  
74 35 ▶ while ($path =~ s{/[^/]*$}{} && length $path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[/[^/]*$][]u and length $path) 15 32 47
Condition100%3/3
$path =~ s[/[^/]*$][]u and length $path
!ll&&!rl&&r
31115
MC/DC100%2/2
$path =~ s[/[^/]*$][]u and length $path
$path =~ s[/[^/]*$][]u length $path
Truth table100%3/3
A and B
A= $path =~ s[/[^/]*$][]uB= length $path
ABresult
0X0
100
111
75 15 ▶ return 1 if $self->__match(undef, $path, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->__match(undef, $path, 1) 3 12 15
76 }
77 }
78  
79 50 return;
80 }
81  
82 sub match {
83 43 ▶ my ($self) = shift @_;
Subroutine match: called CC=1 SCAR=0.0
Pod documented
84  
85 43 return $self->__match(undef, @_);
86 }
87  
88 sub matchExclude {
89 2 ▶ &match;
Subroutine matchExclude: called CC=1 SCAR=0.0
Pod documented
90 }
91  
92 sub matchInclude {
93 31 ▶ my ($self) = shift @_;
Subroutine matchInclude: called CC=1 SCAR=0.0
Pod documented
94  
95 31 return $self->__match(1, @_);
96 }
97  
98 sub patterns {
99 100 ▶ return @{shift->{__patterns}};
Subroutine patterns: called CC=1 SCAR=0.0
Pod documented
100 }
101  
102 sub _readArray {
103 40 ▶ my ($self, $lines) = @_;
Subroutine _readArray: called CC=3 SCAR=11.0
104  
105 40 my @patterns;
106 40 $self->{__patterns} = \@patterns;
107  
108 40 my $ignore_case = $self->{__ignore_case};
109 40 foreach my $line (@$lines) {
110 78 my $transpiled = eval { translatestar $line,
111 ignoreCase => $ignore_case,
112 pathMode => 1 };
113  
114 # Why a slash? When matching, we discard a trailing slash from the
115 # string to match. The regex '/$' can therefore never match. And the
116 # leading caret is there in order to save Perl at least reading the
117 # string to the end.
118 78 ▶ $transpiled = qr{^/$} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 77 78
119 78 push @patterns, $transpiled;
120 }
121  
122 40 return $self;
123 }
124  
125 sub _readString {
126 39 ▶ my ($self, $string) = @_;
Subroutine _readString: called CC=7 SCAR=19.5
127  
128 39 my @lines;
129 39 foreach my $line (split /\n/, $string) {
130 92 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 6 86 92
131  
132 # If the string contains trailing whitespace we have to count the
133 # number of backslashes in front of the first whitespace character.
134 86 ▶ if ($line =~ s/(\\*)([\x{9}-\x{13} ])[\x{9}-\x{13} ]*$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/(\\*)([\x{9}-\x{13} ])[\x{9}-\x{13} ]*$//u) 12 74 86
135 12 my ($bs, $first) = ($1, $2);
136 12 ▶ if ($bs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bs) 4 8 12
137 4 $line .= $bs;
138  
139 4 my $num_bs = $bs =~ y/\\/\\/;
140  
141 # If the number of backslashes is odd, the last space was
142 # escaped.
143 4 ▶ $line .= $first if $num_bs & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_bs & 1 3 1 4
144 }
145 }
146 86 ▶ next if '' eq $line;
Branch: 1 branch
BranchTrueFalseTotal
if "" eq $line 11 75 86
147  
148 75 push @lines, $line;
149 }
150  
151 39 return $self->_readArray(\@lines);
152 }
153  
154 sub _readFileHandle {
155 3 ▶ my ($self, $fh) = @_;
Subroutine _readFileHandle: called CC=3 SCAR=11.2
156  
157 3 my $filename = $self->{__filename};
158 3 ▶ $filename = __["in memory string"] if File::Globstar::empty($filename);
Branch: 1 branch
BranchTrueFalseTotal
if &File::Globstar::empty($filename) 0 3 3
159  
160 3 $fh->clearerr;
161 3 my @lines = $fh->getlines;
162  
163 3 ▶ die __x("Error reading '{filename}': {error}!\n",
Branch: 1 branch
BranchTrueFalseTotal
if $fh->error 0 3 3
164 filename => $filename, error => $!) if $fh->error;
165  
166 3 return $self->_readString(join '', @lines);
167 }
168  
169 sub _readFile {
170 1 ▶ my ($self, $filename) = @_;
Subroutine _readFile: called CC=3 SCAR=11.4
171  
172 $self->{__filename} = $filename
173 1 ▶ if File::Globstar::empty($self->{__filename});
Branch: 1 branch
BranchTrueFalseTotal
if &File::Globstar::empty($self->{"__filename"}) 1 0 1
174  
175 1 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die &__x("Error reading '{filename}': {error}!\n", "filename", $filename, "error", $!) 0 1 1
176 or die __x("Error reading '{filename}': {error}!\n",
177 filename => $filename, error => $!);
178  
179 1 return $self->_readFileHandle($fh);
180 }
181  
182 3 1;
« blib/lib/File/Globstar.pm ↑ Summary Build »