blib/lib/Langertha/Raider/Detect.pm

filter: Statementstmt 100% 222 / 222 Branchbran 94.1% 128 / 136 Conditioncond 90.0% 54 / 60 MC/DCmcdc 82.3% 28 / 34 Subroutinesub 100% 28 / 28 Podpod 100% 3 / 3 totaltotal 95.8% 463 / 483 CC 103 SCAR 26.4
CC
103
Cov
97%
_walk
25.2
validate_rule
25.0
evaluate
16.0
CRAP 14.1
« blib/lib/Langertha/Raider/ToolArgs.pm ↑ Summary blib/lib/Langertha/Raider/Provider/Activation.pm »
1 package Langertha::Raider::Detect;
2 # ABSTRACT: Internal evaluator for declarative pack detection rules
3 80 our $VERSION = '0.503';
4 80 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 80 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 80 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 80 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 80 my @CLAUSES = qw( must may must_not );
11 80 my @COND_KEYS = qw( file dir contains matches );
12  
13  
14 80 has root => (
15 is => 'ro',
16 isa => 'Str',
17 required => 1,
18 );
19  
20  
21 80 has max_depth => (
22 is => 'ro',
23 isa => 'Int',
24 default => 8,
25 );
26  
27  
28 80 has max_entries => (
29 is => 'ro',
30 isa => 'Int',
31 default => 5000,
32 );
33  
34  
35 80 has max_bytes => (
36 is => 'ro',
37 isa => 'Int',
38 default => 65536,
39 );
40  
41 80 has _real_root => (
42 is => 'ro',
43 lazy_build => 1,
44 );
45  
46 332 ▶sub _build__real_root {
Subroutine _build__real_root: called CC=2 SCAR=6.9
47 332 my ( $self ) = @_;
48 332 my $root = path($self->root);
49 332 ▶ return unless -d $root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root 2 330 332
50 330 return $root->realpath;
51 }
52  
53  
54 792 ▶sub validate_rule {
Subroutine validate_rule: called CC=25 SCAR=32.2
Pod documented
55 792 my ( $self, $rule, $label ) = @_;
56 792 ▶ $label //= 'rule';
Condition100%2/2
$label //= "rule"
l!l
74547
MC/DC100%1/1
$label //= "rule"
$label
Truth table100%2/2
A
A= $label
Aresult
11
00
57 792 ▶ my $fail = sub { croak 'Invalid detect rule '.$_[0].': '.$_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
58 792 ▶ $fail->($label, 'a rule must be a map') unless ref $rule eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" 1 791 792
59 791 my %clause = map { $_ => 1 } @CLAUSES;
60 791 for my $key (sort keys %$rule) {
61 802 ▶ $fail->($label, "unknown key '".$key."' (allowed: ".join(', ', @CLAUSES).')') unless $clause{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $clause{$key} 1 801 802
62 }
63 790 my %cond_key = map { $_ => 1 } @COND_KEYS;
64 790 for my $clause (@CLAUSES) {
65 2341 ▶ next unless exists $rule->{$clause};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$rule{$clause} 1540 801 2341
66 801 my $list = $rule->{$clause};
67 801 ▶ $fail->($label.'.'.$clause, 'must be a list of conditions') unless ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 2 799 801
68 799 for my $i (0 .. $#$list) {
69 2707 my $at = $label.'.'.$clause.'['.$i.']';
70 2707 my $cond = $list->[$i];
71 2707 ▶ $fail->($at, 'a condition must be a map') unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 1 2706 2707
72 2706 for my $key (sort keys %$cond) {
73 2723 ▶ $fail->($at, "unknown key '".$key."' (allowed: ".join(', ', @COND_KEYS).')') unless $cond_key{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $cond_key{$key} 3 2720 2723
74 }
75 2703 ▶ $fail->($at, 'needs file or dir') unless exists $cond->{file} || exists $cond->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$cond{"file"} or exists $$cond{"dir"} 1 2702 2703
Condition100%3/3
exists $$cond{"file"} or exists $$cond{"dir"}
l!l&&r!l&&!r
269661
MC/DC100%2/2
exists $$cond{"file"} or exists $$cond{"dir"}
exists $$cond{"file"} exists $$cond{"dir"}
Truth table100%3/3
A or B
A= exists $$cond{"file"}B= exists $$cond{"dir"}
ABresult
1X1
011
000
76 2702 for my $key (qw( contains matches )) {
77 5402 ▶ next unless exists $cond->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$cond{$key} 5386 16 5402
78 16 ▶ $fail->($at, $key.' needs file') unless exists $cond->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$cond{"file"} 2 14 16
79 14 ▶ $fail->($at, $key.' must be a non-empty string') unless $self->_is_text($cond->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_text($$cond{$key}) 1 13 14
80 }
81 2699 for my $key (qw( file dir )) {
82 5395 ▶ next unless exists $cond->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$cond{$key} 2694 2701 5395
83 2701 my $glob = $cond->{$key};
84 2701 ▶ $fail->($at, $key.' must be a non-empty string') unless $self->_is_text($glob);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_text($glob) 2 2699 2701
85 2699 ▶ $fail->($at, $key.' must be relative to the workspace') if $glob =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
if $glob =~ m[\A/] 1 2698 2699
86 2698 ▶ $fail->($at, $key." must not contain '..'") if grep { $_ eq '..' } split m{/}, $glob;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "..";} split(m[/], $glob, 0) 2 2696 2698
87 }
88 2694 ▶ if (exists $cond->{matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cond{"matches"}) 4 2690 2694
89 4 ▶ eval { qr/$cond->{matches}/; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { qr/$$cond{"matches"}/; 1 } } or &$fail($at, 'matches is not a valid regex: ' . (split(/\n/, $@, 0))[0]) 3 1 4
90 or $fail->($at, 'matches is not a valid regex: '.( split /\n/, $@ )[0]);
91 }
92 }
93 }
94 774 return 1;
95 }
96  
97 2715 ▶sub _is_text {
Subroutine _is_text: called CC=3 SCAR=11.0
98 2715 my ( $self, $value ) = @_;
99 2715 ▶ return defined $value && !ref $value && length $value;
Condition83%5/6
defined $value && !ref($value) && length $value
!ll&&!rl&&r
122712
defined $value and not ref $value
!ll&&!rl&&r
012714
MC/DC66%2/3
defined $value && !ref($value) && length $value
defined $value not ref $value length $value
Truth table75%3/4
A and not(B) && C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
100 }
101  
102  
103 2425 ▶sub describe_condition {
Subroutine describe_condition: called CC=1 SCAR=0.0
Pod documented
104 2425 my ( $self, $cond ) = @_;
105 my %fmt = (
106 2423 ▶ file => sub { 'file='.$_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 5 ▶ dir => sub { 'dir='.$_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 10 ▶ contains => sub { 'contains="'.$_[0].'"' },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 4 ▶ matches => sub { 'matches=/'.$_[0].'/' },
Subroutine __ANON__: called CC=1 SCAR=0.0
110 2425 );
111 2425 return join ' ', map { $fmt{$_}->($cond->{$_}) } grep { exists $cond->{$_} } @COND_KEYS;
112 }
113  
114  
115 395 ▶sub evaluate {
Subroutine evaluate: called CC=16 SCAR=27.7
Pod documented
116 395 my ( $self, $rule, $label ) = @_;
117 395 $self->validate_rule($rule, $label);
118 394 my ( @checks, @notes, @why );
119 394 ▶ my $done = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
120 394 my ( $matched, $reason ) = @_;
121 return {
122 394 ▶ matched => $matched ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$matched ? : 64 330 394
123 reason => $reason,
124 checks => \@checks,
125 notes => \@notes,
126 };
127 394 };
128 394 ▶ my @present = grep { @{ $rule->{$_} // [] } } @CLAUSES;
Condition100%2/2
$$rule{$_} // []
l!l
404778
MC/DC100%1/1
$$rule{$_} // []
$$rule{$_}
Truth table100%2/2
A
A= $$rule{$_}
Aresult
11
00
129 394 ▶ return $done->(0, 'no clauses') unless @present;
Branch: 1 branch
BranchTrueFalseTotal
unless @present 2 392 394
130 392 ▶ return $done->(0, 'workspace root not found') unless defined $self->_real_root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_real_root 2 390 392
131  
132 1270 ▶ my $check = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
133 1270 my ( $clause, $cond ) = @_;
134 1270 my $desc = $self->describe_condition($cond);
135 1270 my $hit = $self->_condition($cond, $clause.' '.$desc);
136 push @checks, {
137 clause => $clause,
138 condition => $desc,
139 held => $hit->{held},
140 1270 ▶ ( defined $hit->{path} ? ( path => $hit->{path} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $$hit{'path'} ? : 67 1203 1270
141 };
142 1270 ▶ push @notes, @{ $hit->{notes} } unless $hit->{held};
Branch: 1 branch
BranchTrueFalseTotal
unless $$hit{"held"} 1203 67 1270
143 1270 return ( $hit, $desc );
144 390 };
145  
146 390 ▶ for my $cond (@{ $rule->{must} // [] }) {
Condition100%2/2
$$rule{"must"} // []
l!l
76314
MC/DC100%1/1
$$rule{"must"} // []
$$rule{"must"}
Truth table100%2/2
A
A= $$rule{"must"}
Aresult
11
00
147 76 my ( $hit, $desc ) = $check->(must => $cond);
148 76 ▶ return $done->(0, 'must '.$desc.': no match') unless $hit->{held};
Branch: 1 branch
BranchTrueFalseTotal
unless $$hit{"held"} 37 39 76
149 39 push @why, 'must '.$desc.' ('.$hit->{path}.')';
150 }
151 353 ▶ if (my @list = @{ $rule->{must_not} // [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@list) = @{$$rule{"must_not"} // [];}) 4 349 353
Condition100%2/2
$$rule{"must_not"} // []
l!l
4349
MC/DC100%1/1
$$rule{"must_not"} // []
$$rule{"must_not"}
Truth table100%2/2
A
A= $$rule{"must_not"}
Aresult
11
00
152 4 for my $cond (@list) {
153 4 my ( $hit, $desc ) = $check->(must_not => $cond);
154 4 ▶ return $done->(0, 'must_not '.$desc.': found '.$hit->{path}) if $hit->{held};
Branch: 1 branch
BranchTrueFalseTotal
if $$hit{"held"} 1 3 4
155 }
156 3 push @why, 'must_not '.join(', ', map { $self->describe_condition($_) } @list).': none';
157 }
158 352 ▶ if (my @list = @{ $rule->{may} // [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@list) = @{$$rule{"may"} // [];}) 315 37 352
Condition100%2/2
$$rule{"may"} // []
l!l
31636
MC/DC100%1/1
$$rule{"may"} // []
$$rule{"may"}
Truth table100%2/2
A
A= $$rule{"may"}
Aresult
11
00
159 315 my $held;
160 315 for my $cond (@list) {
161 1190 my ( $hit, $desc ) = $check->(may => $cond);
162 1190 ▶ next unless $hit->{held};
Branch: 1 branch
BranchTrueFalseTotal
unless $$hit{"held"} 1163 27 1190
163 27 $held = 'may '.$desc.' ('.$hit->{path}.')';
164 27 last;
165 }
166 315 ▶ return $done->(0, 'may: none of '.join(', ', map { $self->describe_condition($_) } @list).' held')
Branch: 1 branch
BranchTrueFalseTotal
unless $held 288 27 315
167 unless $held;
168 27 push @why, $held;
169 }
170 64 return $done->(1, join '; ', @why);
171 }
172  
173 # One condition: { held, path, notes }. file and dir are looked up
174 # independently; the content checks apply to the file candidates.
175 1270 ▶sub _condition {
Subroutine _condition: called CC=5 SCAR=16.1
176 1270 my ( $self, $cond, $label ) = @_;
177 1270 my $state = { entries => 0, notes => [], seen => {}, label => $label };
178 1270 my @paths;
179 1270 ▶ if (exists $cond->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cond{"file"}) 1268 2 1270
180 1268 ▶ my $path = $self->_find($cond->{file}, 'file', $state, sub { $self->_content_ok($cond, @_, $state) });
Subroutine __ANON__: called CC=1 SCAR=0.0
181 1268 ▶ return { held => 0, notes => $state->{notes} } unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1201 67 1268
182 67 push @paths, $path;
183 }
184 69 ▶ if (exists $cond->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cond{"dir"}) 4 65 69
185 4 ▶ my $path = $self->_find($cond->{dir}, 'dir', $state, sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
186 4 ▶ return { held => 0, notes => $state->{notes} } unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 2 4
187 2 push @paths, $path;
188 }
189 67 return { held => 1, path => join(', ', @paths), notes => [] };
190 }
191  
192 6 ▶sub _note {
Subroutine _note: called CC=2 SCAR=7.0
193 6 my ( $self, $state, $text ) = @_;
194 6 ▶ push @{ $state->{notes} }, $state->{label}.': '.$text unless $state->{seen}{$text}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $$state{"seen"}{$text}++ 6 0 6
195 6 return;
196 }
197  
198 # The segments of a glob: '.' and empty ones dropped, a trailing ** made
199 # to match everything below.
200 1272 ▶sub _segments {
Subroutine _segments: called CC=7 SCAR=19.5
201 1272 my ( $self, $glob ) = @_;
202 1272 ▶ my @segs = grep { length && $_ ne '.' } split m{/}, $glob;
Condition66%2/3
length $_ && $_ ne '.'
!ll&&!rl&&r
011878
MC/DC50%1/2
length $_ && $_ ne '.'
length $_ $_ ne '.'
Truth table66%2/3
A && B
A= length $_B= $_ ne '.'
ABresult
0X0
100
111
203 1272 ▶ push @segs, '*' if @segs && $segs[-1] eq '**';
Branch: 1 branch
BranchTrueFalseTotal
if @segs and $segs[-1] eq "**" 1 1271 1272
Condition66%2/3
@segs and $segs[-1] eq "**"
!ll&&!rl&&r
012711
MC/DC50%1/2
@segs and $segs[-1] eq "**"
@segs $segs[-1] eq "**"
Truth table66%2/3
A and B
A= @segsB= $segs[-1] eq "**"
ABresult
0X0
100
111
204 return map {
205 1272 ▶ $_ eq '**' ? { globstar => 1 }
Branch: 3 branches
BranchTrueFalseTotal
$_ eq '**' ? : 299 1580 1879
/[*?]/ ? : 306 1274 1580
/\A\./ ? : 1 305 306
206 : /[*?]/ ? { re => $self->_segment_re($_), dot => /\A\./ ? 1 : 0 }
207 : { literal => $_ }
208 } @segs;
209 }
210  
211 306 ▶sub _segment_re {
Subroutine _segment_re: called CC=3 SCAR=11.0
212 306 my ( $self, $seg ) = @_;
213 306 ▶ my $re = join '', map { $_ eq '*' ? '[^/]*' : $_ eq '?' ? '[^/]' : quotemeta } split /([*?])/, $seg;
Branch: 2 branches
BranchTrueFalseTotal
$_ eq '*' ? : 305 614 919
$_ eq '?' ? : 3 611 614
214 306 return qr/\A$re\z/;
215 }
216  
217 # First path (relative to the root) of type $want matching $glob for which
218 # $accept returns true, or undef.
219 1272 ▶sub _find {
Subroutine _find: called CC=2 SCAR=6.9
220 1272 my ( $self, $glob, $want, $state, $accept ) = @_;
221 1272 my @segs = $self->_segments($glob);
222 1272 ▶ return unless @segs;
Branch: 1 branch
BranchTrueFalseTotal
unless @segs 0 1272 1272
223 1272 my $found;
224 75 ▶ $self->_walk($self->_real_root, [], \@segs, 0, 0, $want, $state, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
225 75 my ( $rel, $real ) = @_;
226 75 ▶ return 0 unless $accept->($real, $rel);
Branch: 1 branch
BranchTrueFalseTotal
unless &$accept($real, $rel) 6 69 75
227 69 $found = $rel;
228 69 return 1;
229 1272 });
230 1272 return $found;
231 }
232  
233 1346 ▶sub _walk {
Subroutine _walk: called CC=25 SCAR=32.2
234 1346 my ( $self, $dir, $rel, $segs, $i, $depth, $want, $state, $cb ) = @_;
235 1346 ▶ return 0 if $state->{capped};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"capped"} 0 1346 1346
236 1346 my $seg = $segs->[$i];
237 1346 my $last = $i == $#$segs;
238  
239 1346 ▶ if ($seg->{globstar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seg{"globstar"}) 34 1312 1346
240 34 ▶ return 1 if $self->_walk($dir, $rel, $segs, $i + 1, $depth, $want, $state, $cb);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_walk($dir, $rel, $segs, $i + 1, $depth, $want, $state, $cb) 7 27 34
241 27 for my $name ($self->_names($dir, $state)) {
242 34 ▶ last if $state->{capped};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"capped"} 0 34 34
243 34 ▶ next if $name =~ /\A\./;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A\./ 2 32 34
244 32 my $child = $dir->child($name);
245 32 ▶ next if -l $child || !-d $child;
Branch: 1 branch
BranchTrueFalseTotal
if -l $child or not -d $child 10 22 32
Condition100%3/3
-l $child or not -d $child
l!l&&r!l&&!r
5522
MC/DC100%2/2
-l $child or not -d $child
-l $child not -d $child
Truth table100%3/3
A or not(B)
A= -l $childB= not -d $child
ABresult
1X1
011
000
246 22 ▶ if ($depth >= $self->max_depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= $self->max_depth) 1 21 22
247 1 $self->_note($state, '** depth capped at '.$self->max_depth);
248 1 next;
249 }
250 21 ▶ return 1 if $self->_walk($child, [ @$rel, $name ], $segs, $i, $depth + 1, $want, $state, $cb);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_walk($child, [@$rel, $name], $segs, $i, $depth + 1, $want, $state, $cb) 16 5 21
251 }
252 11 return 0;
253 }
254  
255 1312 my @names;
256 1312 ▶ if (defined $seg->{literal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$seg{'literal'}) { } 1286 26 1312
257 1286 ▶ @names = ( $seg->{literal} ) if -e $dir->child($seg->{literal}) || -l $dir->child($seg->{literal});
Branch: 1 branch
BranchTrueFalseTotal
if -e $dir->child($$seg{"literal"}) or -l $dir->child($$seg{"literal"}) 83 1203 1286
Condition66%2/3
-e $dir->child($$seg{"literal"}) or -l $dir->child($$seg{"literal"})
l!l&&r!l&&!r
8301203
MC/DC50%1/2
-e $dir->child($$seg{"literal"}) or -l $dir->child($$seg{"literal"})
-e $dir->child($$seg{"literal"}) -l $dir->child($$seg{"literal"})
Truth table66%2/3
A or B
A= -e $dir->child($$seg{"literal"})B= -l $dir->child($$seg{"literal"})
ABresult
1X1
011
000
258 }
259 else {
260 26 ▶ @names = grep { ( $seg->{dot} || !/\A\./ ) && $_ =~ $seg->{re} } $self->_names($dir, $state);
Condition100%6/6
($$seg{'dot'} || !/\A\./) && $_ =~ /$$seg{'re'}/
!ll&&!rl&&r
45021
$$seg{"dot"} or not /\A\./
l!l&&r!l&&!r
2694
MC/DC100%3/3
($$seg{'dot'} || !/\A\./) && $_ =~ /$$seg{'re'}/
$$seg{"dot"} not /\A\./ $_ =~ /$$seg{'re'}/
Truth table80%4/5
A or not(B) && C
A= $$seg{"dot"}B= not /\A\./C= $_ =~ /$$seg{'re'}/
ABCresult
00X0
1X00
0100
1X11
0111
261 }
262 1312 for my $name (@names) {
263 103 ▶ last if $state->{capped};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"capped"} 0 103 103
264 103 my @child_rel = ( @$rel, $name );
265 103 ▶ my $real = $self->_inside($dir->child($name), join('/', @child_rel), $state) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $real = $self->_inside($dir->child($name), join("/", @child_rel), $state) or next 100 3 103
266 100 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 81 19 100
267 81 ▶ next unless $want eq 'dir' ? -d $real : -f $real;
Branch: 2 branches
BranchTrueFalseTotal
unless $want eq "dir" ? -d $real : -f $real 6 75 81
$want eq 'dir' ? : 3 78 81
268 75 ▶ return 1 if $cb->(join('/', @child_rel), $real);
Branch: 1 branch
BranchTrueFalseTotal
if &$cb(join("/", @child_rel), $real) 69 6 75
269 6 next;
270 }
271 19 ▶ next unless -d $real;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $real 0 19 19
272 19 ▶ return 1 if $self->_walk($real, \@child_rel, $segs, $i + 1, $depth, $want, $state, $cb);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_walk($real, \@child_rel, $segs, $i + 1, $depth, $want, $state, $cb) 13 6 19
273 }
274 1230 return 0;
275 }
276  
277 # Directory entries, sorted; counts against max_entries.
278 53 ▶sub _names {
Subroutine _names: called CC=5 SCAR=16.1
279 53 my ( $self, $dir, $state ) = @_;
280 53 ▶ opendir(my $dh, "$dir") or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, "$dir" or return 53 0 53
281 53 ▶ my @names = sort grep { $_ ne '.' && $_ ne '..' } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
5353131
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
282 53 closedir $dh;
283 53 my @out;
284 53 for my $name (@names) {
285 111 ▶ if (++$state->{entries} > $self->max_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$$state{"entries"} > $self->max_entries) 1 110 111
286 1 $state->{capped} = 1;
287 1 $self->_note($state, 'stopped after '.$self->max_entries.' entries');
288 1 last;
289 }
290 110 push @out, $name;
291 }
292 53 return @out;
293 }
294  
295 # The path itself when it is no symlink, else its target when that lies
296 # inside the root; undef (reported) when it leaves the workspace.
297 103 ▶sub _inside {
Subroutine _inside: called CC=4 SCAR=13.9
298 103 my ( $self, $path, $rel, $state ) = @_;
299 103 ▶ return $path unless -l $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -l $path 98 5 103
300 5 my $real = eval { $path->realpath };
301 5 ▶ return $real if $real && $self->_real_root->subsumes($real);
Branch: 1 branch
BranchTrueFalseTotal
if $real and $self->_real_root->subsumes($real) 2 3 5
Condition66%2/3
$real and $self->_real_root->subsumes($real)
!ll&&!rl&&r
032
MC/DC50%1/2
$real and $self->_real_root->subsumes($real)
$real $self->_real_root->subsumes($real)
Truth table66%2/3
A and B
A= $realB= $self->_real_root->subsumes($real)
ABresult
0X0
100
111
302 3 $self->_note($state, $rel.': symlink leaves the workspace');
303 3 return;
304 }
305  
306 73 ▶sub _content_ok {
Subroutine _content_ok: called CC=12 SCAR=24.9
307 73 my ( $self, $cond, $real, $rel, $state ) = @_;
308 73 ▶ return 1 unless exists $cond->{contains} || exists $cond->{matches};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$cond{"contains"} or exists $$cond{"matches"} 60 13 73
Condition100%3/3
exists $$cond{"contains"} or exists $$cond{"matches"}
l!l&&r!l&&!r
10360
MC/DC100%2/2
exists $$cond{"contains"} or exists $$cond{"matches"}
exists $$cond{"contains"} exists $$cond{"matches"}
Truth table100%3/3
A or B
A= exists $$cond{"contains"}B= exists $$cond{"matches"}
ABresult
1X1
011
000
309 13 ▶ my $fh = eval { $real->openr_raw } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = eval { do { $real->openr_raw } } or return 0 13 0 13
310 13 my $max = $self->max_bytes;
311 13 ▶ my $got = read($fh, my $buf, $max + 1) // 0;
Condition50%1/2
read($fh, my $buf, $max + 1) // 0
l!l
130
MC/DC0%0/1
read($fh, my $buf, $max + 1) // 0
read($fh, my $buf, $max + 1)
Truth table50%1/2
A
A= read($fh, my $buf, $max + 1)
Aresult
11
00
312 13 close $fh;
313 13 my $cut = $got > $max;
314 13 ▶ substr($buf, $max) = '' if $cut;
Branch: 1 branch
BranchTrueFalseTotal
if $cut 2 11 13
315 13 my $text = $buf;
316 13 utf8::decode($text);
317 my $ok = ( !exists $cond->{contains} || index($text, $cond->{contains}) >= 0 )
318 13 ▶ && ( !exists $cond->{matches} || $text =~ /$cond->{matches}/m );
Condition100%9/9
(!exists($$cond{'contains'}) || index($text, $$cond{'contains'}) != -1) && (!exists($$cond{'matches'}) || $text =~ /$$cond{'matches'}/m)
!ll&&!rl&&r
427
not exists $$cond{"contains"} or index($text, $$cond{"contains"}) != -1
l!l&&r!l&&!r
364
not exists $$cond{"matches"} or $text =~ /$$cond{"matches"}/m
l!l&&r!l&&!r
522
MC/DC100%4/4
(!exists($$cond{'contains'}) || index($text, $$cond{'contains'}) != -1) && (!exists($$cond{'matches'}) || $text =~ /$$cond{'matches'}/m)
not exists $$cond{"contains"} index($text, $$cond{"contains"}) != -1 not exists $$cond{"matches"} $text =~ /$$cond{"matches"}/m
Truth table71%5/7
not(A) or B && not(C) or D
A= not exists $$cond{"contains"}B= index($text, $$cond{"contains"}) != -1C= not exists $$cond{"matches"}D= $text =~ /$$cond{"matches"}/m
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
319 13 ▶ $self->_note($state, $rel.': only the first '.$max.' bytes were read') if $cut && !$ok;
Branch: 1 branch
BranchTrueFalseTotal
if $cut and not $ok 1 12 13
Condition100%3/3
$cut and not $ok
!ll&&!rl&&r
1111
MC/DC100%2/2
$cut and not $ok
$cut not $ok
Truth table100%3/3
A and not(B)
A= $cutB= not $ok
ABresult
0X0
100
111
320 13 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 7 6 13
321 }
322  
323 80 __PACKAGE__->meta->make_immutable;
324  
325 80 1;
326  
327 __END__
328  
329 =pod
330  
331 =encoding UTF-8
332  
333 =head1 NAME
334  
335 Langertha::Raider::Detect - Internal evaluator for declarative pack detection rules
336  
337 =head1 VERSION
338  
339 version 0.503
340  
341 =head1 SYNOPSIS
342  
343 # Internal to Langertha-Raider -- no API promise.
344 my $detect = Langertha::Raider::Detect->new( root => $workspace );
345  
346 Langertha::Raider::Detect->validate_rule($rule, 'perl'); # croaks when invalid
347  
348 my $result = $detect->evaluate({
349 must => [ { file => 'cpanfile' } ],
350 may => [ { file => 'dist.ini' }, { file => 'lib/**/*.pm' } ],
351 must_not => [ { file => '.raider/no-perl' } ],
352 }, 'perl');
353  
354 print $result->{matched} ? 'perl: '.$result->{reason} : 'no perl';
355  
356 =head1 DESCRIPTION
357  
358 B<Internal module.> Its interface may change without notice.
359  
360 Evaluates one detection rule of ADR 0012 against a workspace root. A rule
361 is a map of up to three clause lists:
362  
363 =over
364  
365 =item C<must> -- every condition holds.
366  
367 =item C<may> -- when the list is non-empty, at least one condition holds.
368  
369 =item C<must_not> -- no condition holds.
370  
371 =back
372  
373 A rule without any condition never matches. The clauses are evaluated in
374 the order C<must>, C<must_not>, C<may>, and evaluation stops as soon as the
375 outcome is decided.
376  
377 A condition is a map; every key in it must hold:
378  
379 =over
380  
381 =item C<file: GLOB> -- a file matching the glob exists.
382  
383 =item C<dir: GLOB> -- a directory matching the glob exists.
384  
385 =item C<contains: STRING> -- with C<file>, a matching file contains the
386 literal string.
387  
388 =item C<matches: REGEX> -- with C<file>, a matching file matches the regex
389 (multi-line: C<^> and C<$> anchor at line ends). With C<contains> as well,
390 both must hold in the same file.
391  
392 =back
393  
394 Globs are relative to the workspace root and know C<*> and C<?> (within one
395 path segment) and C<**> (any number of directories, including none; a
396 trailing C<**> matches everything below). Wildcards do not match names that
397 start with a dot unless the pattern segment does, so C<**> never walks
398 F<.git>. C<..> and absolute globs are invalid.
399  
400 Evaluation is bounded: symlinks are followed only when they resolve inside
401 the root, and C<**> never descends into a symlinked directory; a C<**>
402 descends at most L</max_depth> directories; one condition looks at no more
403 than L</max_entries> directory entries; content checks read at most the
404 first L</max_bytes> of a file. Hitting a limit makes the condition false and
405 is reported in C<notes>, it never stops the run. Content is matched as
406 UTF-8 text when it decodes, as bytes otherwise.
407  
408 =head2 root
409  
410 The workspace root the globs are relative to. Required.
411  
412 =head2 max_depth
413  
414 How many directories one C<**> descends at most. Defaults to C<8>.
415  
416 =head2 max_entries
417  
418 How many directory entries one condition looks at before it gives up.
419 Defaults to C<5000>.
420  
421 =head2 max_bytes
422  
423 How many bytes of a file a content check reads. Defaults to C<65536>
424 (64 KiB).
425  
426 =head2 validate_rule
427  
428 Langertha::Raider::Detect->validate_rule($rule, $label);
429  
430 Croaks with C<Invalid detect rule LABEL...: problem> when C<$rule> is not a
431 valid rule: not a map, an unknown clause or condition key, a clause that is
432 not a list, a condition without C<file> or C<dir>, C<contains> or
433 C<matches> without C<file>, an empty, absolute or C<..> glob, or a regex
434 that does not compile. C<$label> (e.g. the pack name) prefixes the
435 location; it defaults to C<rule>. Returns true. Callable on the class.
436  
437 =head2 describe_condition
438  
439 my $text = Langertha::Raider::Detect->describe_condition({ file => 'dist.ini', contains => 'GETTY' });
440 # 'file=dist.ini contains="GETTY"'
441  
442 One-line form of a condition, as used in C<reason> and C<checks>.
443 Callable on the class.
444  
445 =head2 evaluate
446  
447 my $result = $detect->evaluate($rule, $label);
448  
449 Validates the rule (see L</validate_rule>) and evaluates it against
450 L</root>:
451  
452 {
453 matched => 1,
454 reason => 'must file=cpanfile (cpanfile); may file=dist.ini (dist.ini)',
455 checks => [
456 { clause => 'must', condition => 'file=cpanfile', held => 1, path => 'cpanfile' },
457 { clause => 'may', condition => 'file=dist.ini', held => 1, path => 'dist.ini' },
458 ],
459 notes => [],
460 }
461  
462 C<reason> names the conditions that decided the outcome -- what matched
463 and where for a match, the condition that failed (C<no match>, C<found
464 PATH>, C<none of ... held>) otherwise; C<no clauses> for an empty rule.
465 C<checks> lists every condition that was evaluated, C<notes> every limit
466 that was hit by a condition that did not hold.
467  
468 =head1 SEE ALSO
469  
470 =over
471  
472 =item * L<Langertha::Raider::Packs>
473  
474 =back
475  
476 =head1 SUPPORT
477  
478 =head2 Issues
479  
480 Please report bugs and feature requests on GitHub at
481 L<https://github.com/Getty/langertha-raider/issues>.
482  
483 =head2 IRC
484  
485 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
486  
487 =head1 CONTRIBUTING
488  
489 Contributions are welcome! Please fork the repository and submit a pull request.
490  
491 =head1 AUTHOR
492  
493 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
494  
495 =head1 COPYRIGHT AND LICENSE
496  
497 This software is copyright (c) 2026 by Torsten Raudssus.
498  
499 This is free software; you can redistribute it and/or modify it under
500 the same terms as the Perl 5 programming language system itself.
501  
502 =cut
« blib/lib/Langertha/Raider/ToolArgs.pm ↑ Summary blib/lib/Langertha/Raider/Provider/Activation.pm »