lib/App/GitFind/Actions.pm

filter: Statementstmt 75.0% 57 / 76 Branchbran 37.5% 6 / 16 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 65.2% 15 / 23 Podpod 25.0% 2 / 8 totaltotal 66.6% 90 / 135 CC 13 SCAR 18.1
CC
13
Cov
70%
_validate_exec
30.0
_validate_ok
6.0
_t
3.0
CRAP 6.1
« lib/App/GitFind/Entry/GitIndex.pm ↑ Summary lib/App/GitFind/Searcher.pm »
1 package App::GitFind::Actions;
2  
3 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.000002';
8  
9 4 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use vars::i '@EXPORT_OK' => qw(ARGTEST argdetails);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use vars::i '%EXPORT_TAGS' => { all => [@EXPORT_OK] };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Imports
14 4 ▶use App::GitFind::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Math::Cartesian::Product;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # === Documentation === {{{1
18  
19 =head1 NAME
20  
21 App::GitFind::Actions - Worker functions for App::GitFind
22  
23 =head1 SYNOPSIS
24  
25 TODO
26  
27 =head1 FUNCTIONS
28  
29 =cut
30  
31 # }}}1
32 # Definitions of supported command-line arguments {{{1
33  
34 # NOTE: import() also adds data to these hashrefs.
35  
36 # Helpers for defining these
37 60 ▶sub _a { ($_[0] => { token => 'ACTION', nparam => ($_[1]||0) }) }
Subroutine _a: called CC=2 SCAR=6.9
Condition100%2/2
$_[1] || 0
l!l
3624
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
38 248 ▶sub _t { ($_[0] => { token => 'TEST', nparam => ($_[1]||0), index => ($_[2]||false) }) }
Subroutine _t: called CC=3 SCAR=11.0
Condition100%4/4
$_[1] || 0
l!l
21632
$_[2] || 0
l!l
120128
MC/DC100%1/1
$_[1] || 0
$_[1]
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
39  
40 # A map from argument name to a details hashref. Valid keys in the hashref are:
41 # token: The token type
42 # nparam: - if a regex, the argument ends with an @ARGV element matching
43 # that regex.
44 # - if an integer, the argument takes that many parameters (>=0).
45 # index: (for tests only) Whether that test can be evaluated using only
46 # information from the index
47 # code: A coderef --- the do_*() function that implements that test.
48 # The {code} field is added by _inflate(), called by import().
49  
50 my %ARGS=(
51 # TODO find(1) positional options, global options?
52  
53 # No-argument tests -- all happen to be index tests
54 32 map( { _t $_, 0, true }
55 qw(empty executable false nogroup nouser readable true writeable) ),
56  
57 # No-argument actions
58 24 map( { _a $_ } qw(delete ls print print0 prune quit) ),
59  
60 # One-argument index tests
61 88 map( { _t $_, 1, true } qw(
62 cmin cnewer ctime
63 gid group ilname iname inum ipath iregex iwholename level
64 mmin mtime name
65 path
66 regex
67 size type uid
68 user wholename
69 ) ),
70  
71 # One-argument detailed tests
72 48 map( { _t $_, 1 } _qwc <<'EOT' ),
73 amin anewer atime fstype
74 links lname # Actually index tests?
75 newer
76 perm # Actually index test?
77 ref rev # Maybe not detailed tests - TODO investigate this
78 samefile # Actually index test?
79 used
80 EOT
81  
82 # -newerXY forms - all are detailed tests
83 80 map( { _t('newer' . join('', @$_), 1) }
84 80 ▶ cartesian {1} [qw(a B c m)], [qw(a B c m t)] ),
Subroutine __ANON__: called CC=1 SCAR=0.0
85  
86 # -amin n
87 # -anewer file
88 # -atime n
89 # -cmin n
90 # -cnewer file
91 # -ctime n
92 # -fstype type
93 # -gid n
94 # -group gname
95 # -ilname pattern
96 # -iname pattern
97 # -inum n
98 # -ipath pattern
99 # -iregex pattern
100 # -iwholename pattern
101 # -level n # not in find(1) - succeed if the item is at level n
102 # -links n
103 # -lname pattern
104 # -mmin n
105 # -mtime n
106 # -name pattern
107 # -newer file
108 # -newerXY reference
109 # -path pattern
110 # -perm [-/+]?mode
111 # -ref revspec # not in find(1) - specify a git ref OR REV
112 # (identical to -rev so you don't have to
113 # remember which)
114 # -regex pattern
115 # -rev revspec # not in find(1) - specify a git rev OR REF
116 # (identical to -ref)
117 # -samefile name
118 # -size n
119 # -type c
120 # -uid n
121 # -used n
122 # -user uname
123 # -wholename pattern
124 # -xtype c # Not supported for now
125 # -context pattern # Not supported for now
126  
127 # Actions with a fixed number of arguments
128 16 map( { _a $_, 1 } qw(fls fprint fprint0 printf) ),
129 4 map( { _a $_, 2 } qw(fprintf) ),
130  
131 # -fls file
132 # -fprint file
133 # -fprint0 file
134 # -fprintf file format
135 # -printf format
136  
137 # Actions with a delimited argument list
138 # -exec command [;+]
139 # -execdir command [;+]
140 # -ok command ;
141 # -okdir command ;
142 8 map( { _a $_, qr/^[;+]$/ } qw(exec execdir) ),
143 4 map( { _a $_, qr/^;$/ } qw(ok okdir) ),
144 );
145  
146 # }}}1
147 # === Argument-validation functions === {{{1
148 # Special validators for ok, okdir, exec, and execdir.
149 # Validators return undefined if validation passes, and an error message
150 # otherwise. Validators take the command and the located parameters
151 # in @_.
152  
153 0 ▶sub _validate_exec {
Subroutine _validate_exec: not called CC=5 SCAR=34.0
154 0 ▶ return "need at least a command name" unless $#_>1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ > 1 0 0 0
155 0 ▶ if($_[$#_] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$#_] eq "+") 0 0 0
156 0 ▶ return "need a {}" unless grep { $_ eq '{}' } @_;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "{}";} @_ 0 0 0
157 0 ▶ return "{} can't be the first argument to $_[0]" if $_[1] eq '{}';
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "{}" 0 0 0
158 }
159 0 return undef;
160 }
161  
162 0 ▶sub _validate_ok {
Subroutine _validate_ok: not called CC=2 SCAR=17.9
163 0 ▶ return "need at least a command name" unless $#_>1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ > 1 0 0 0
164 0 return undef;
165 }
166  
167 # }}}1
168 # === Accessors for argument information === {{{1
169  
170 =head2 ARGTEST
171  
172 Returns a regex that will match any arg, with C<-> or C<--> prefix. The arg
173 is captured into $1. Prototyped as C<()>.
174  
175 =cut
176  
177 sub ARGTEST ()
178 4 ▶{ # Make a regex that will match any arg, with - or --.
Subroutine ARGTEST: called CC=1 SCAR=0.0
Pod documented
179 4 my $x = join '|', map { quotemeta } keys %ARGS;
180 4 return qr{^--?($x)$};
181 } #ARGTEST
182  
183 =head2 argdetails
184  
185 Returns a hashref of details about the arg, or undef. Example:
186  
187 my $hr = argdetails('true');
188  
189 =cut
190  
191 20 ▶sub argdetails {
Subroutine argdetails: called CC=2 SCAR=7.6
Pod documented
192 20 ▶ return $ARGS{$_[0]//''};
Condition50%1/2
$_[0] // ""
l!l
200
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
193 }
194  
195 # }}}1
196 # === Tests/actions === {{{1
197 # The order matches that in App::GitFind::Actions
198  
199 # No-argument tests {{{2
200  
201 # empty
202 # executable
203  
204 0 ▶sub do_false { false }
Subroutine do_false: not called CC=1 SCAR=6.9
Pod undocumented
205  
206 # nogroup
207 # nouser
208 # readable
209  
210 0 ▶sub do_true { true }
Subroutine do_true: not called CC=1 SCAR=6.9
Pod undocumented
211  
212 # writeable
213  
214 # }}}2
215 # No-argument actions {{{2
216  
217 # delete
218  
219 0 ▶sub do_ls {
Subroutine do_ls: not called CC=1 SCAR=6.9
Pod undocumented
220 0 state $loaded = (require App::GitFind::FileStatLs, true);
221 0 print App::GitFind::FileStatLs::ls_stat($_[1]->path);
222 0 true
223 }
224 # TODO optimization? Pull the stat() results from $_[1] rather than
225 # re-statting. May not be an issue.
226  
227 0 ▶sub do_print {
Subroutine do_print: not called CC=1 SCAR=6.9
Pod undocumented
228 0 say $_[0]->dot_relative_path($_[1]);
229 0 true
230 }
231  
232 0 ▶sub do_print0 { print $_[0]->dot_relative_path($_[1]), "\0"; true }
Subroutine do_print0: not called CC=1 SCAR=6.9
Pod undocumented
233  
234 # prune
235  
236 # quit
237 # This appears to be a GNU extension. It should:
238 # - Finish any child processes
239 # (empirical): do not kill -9 ---
240 # find . -name LICENSE -exec sh -c 'sleep 2' {} + -o -name README -quit
241 # does not terminate the `sleep` early.
242 # - Run any queued -execdir {} + commands
243 # - (empirical) Do not run any queued -exec {} + commands?
244 # E.g., GNU
245 # find . \( -name LICENSE -quit -o -name README \) -exec ls -l {} +
246 # prints nothing. However, POSIX
247 # (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html)
248 # says that "The utility ... shall be invoked ... after the last
249 # pathname in the set is aggregated, and shall be completed
250 # **before the find utility exits**" (emphasis added).
251  
252  
253 # }}}2
254 # One-argument index tests
255 # TODO
256  
257 # }}}2
258 # One-argument detailed tests
259 # TODO
260  
261 # }}}2
262 # -newerXY forms (all are one-argument detailed tests)
263 # TODO
264  
265 # }}}2
266 # -newerXY forms (all are one-argument detailed tests)
267 # TODO
268  
269 # }}}2
270 # Actions with a fixed number of arguments
271  
272 # fls file
273 # fprint file
274 # fprint0 file
275 # fprintf file format
276  
277 0 ▶sub do_printf { # -printf format. No newline at the end.
Subroutine do_printf: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my ($self, %args) = getparameters('self',[qw(entry format)], @_);
279 0 print "printf($args{format}, $args{entry})"; # TODO
280 } #do_printf()
281  
282 # }}}2
283 # Actions with a delimited argument list
284  
285 # exec
286 # execdir
287 # ok
288 # okdir
289  
290 # }}}2
291  
292 # }}}1
293  
294 # === Import === {{{1
295 7 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
296 7 state $inflated = false;
297 7 my $package = $_[0];
298 7 $package->export_to_level(1, @_);
299  
300 7 ▶ if(!$inflated) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $inflated) 4 3 7
301 4 $inflated = true;
302 4 _inflate($package);
303  
304 }
305 } #import()
306  
307 # Inflate %ARGS. Arg is the package providing the actions; no return.
308 4 ▶sub _inflate {
Subroutine _inflate: called CC=3 SCAR=11.0
309 4 my $package = $_[0];
310 # Hook the validators into %ARGS
311 4 $ARGS{exec}->{validator} = \&_validate_exec;
312 4 $ARGS{execdir}->{validator} = $ARGS{exec}->{validator};
313 4 $ARGS{ok}->{validator} = \&_validate_ok;
314 4 $ARGS{okdir}->{validator} = $ARGS{ok}->{validator};
315  
316 # Hook the actions into %ARGS
317 4 ▶ while (my ($key, $hrValue) = each %ARGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ARGS) 308 4 312
318 308 my $fn = $package->can("do_$key");
319 308 ▶ next unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 284 24 308
320 24 $hrValue->{code} = $fn;
321 }
322 } #_inflate()
323  
324 # }}}1
325  
326 4 1; # End of App::GitFind::Actions
327 __END__
« lib/App/GitFind/Entry/GitIndex.pm ↑ Summary lib/App/GitFind/Searcher.pm »