blib/lib/App/FileSync/Args.pm

filter: Statementstmt 97.5% 117 / 120 Branchbran 89.1% 41 / 46 Conditioncond 70.7% 29 / 41 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 95.6% 22 / 23 Podpod 100% 3 / 3 totaltotal 86.7% 223 / 257 CC 40 SCAR 30.4
CC
40
Cov
90%
_explain
36.6
__ANON__
6.0
__ANON__
2.1
CRAP 20.8
« blib/lib/App/FileSync/CLI.pm ↑ Summary blib/lib/App/FileSync/Process.pm »
1 package App::FileSync::Args;
2  
3 # One place that turns a routine's @_ into a validated hashref, so every public
4 # entry point reads its arguments the same way and reports a bad one with a
5 # catalogued message instead of the validator's own text.
6  
7 439 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 439 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 439 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 439 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 439 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 439 ▶use Params::Get qw(get_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 439 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 439 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 439 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 439 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 439 ▶use App::FileSync::I18N qw(message throw strip_location);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 439 ▶use version; our $VERSION = version->declare('0.002.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 439 our @EXPORT_OK = qw(named positional with_options);
25  
26 # The schema types whose min and max bound a value; true for whole numbers.
27 439 Readonly my %NUMERIC => (integer => 1, number => 0);
28  
29 # The schema types whose min and max bound a size, and how to measure it.
30 # A value of the wrong kind has no size: the validator's own text explains it.
31 Readonly my %SIZE_OF => (
32 12 ▶ string => sub { ref $_[0] ? undef : length $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 12 12
33 5 ▶ arrayref => sub { ref $_[0] eq 'ARRAY' ? scalar @{ $_[0] } : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 3 2 5
34 0 ▶ hashref => sub { ref $_[0] eq 'HASH' ? scalar keys %{ $_[0] } : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
35 439 );
36  
37 # A whole number as the validator accepts one.
38 439 Readonly my $INTEGER => qr/\A[-+]?\d+\z/;
39  
40 # Parameter name => the command-line option it came from, while
41 # with_options() is running. Not part of the API: set it through
42 # with_options, which restores it.
43 439 our %OPTION;
44  
45 # Errors should point at the caller of the routine whose arguments were bad.
46 439 $Carp::Internal{ (__PACKAGE__) }++;
47  
48 =encoding utf8
49  
50 =head1 NAME
51  
52 App::FileSync::Args - validated arguments for App::FileSync's public routines
53  
54 =head1 SYNOPSIS
55  
56 use App::FileSync::Args qw(named positional);
57  
58 sub repo_create {
59 my $self = shift;
60 my $args = named(\@_, {
61 name => { type => 'string', min => 1 },
62 trash_days => { type => 'integer', min => 0, optional => 1 },
63 }, 'repo_create');
64 ...
65 }
66  
67 sub load_repo {
68 my $args = positional(\@_, [qw(root repo)], {
69 root => { type => 'string', min => 1 },
70 repo => { type => 'string', min => 1 },
71 }, 'load_repo');
72 }
73  
74 =head1 FUNCTIONS
75  
76 =head2 named
77  
78 Purpose: validate C<key =E<gt> value> arguments (or a single hashref).
79  
80 Args: C<\@args>, C<\%schema> (L<Params::Validate::Strict> format), C<$routine>
81 (the name used in an error).
82  
83 Returns: a hashref of validated (and coerced) arguments. An empty argument
84 list is an empty hash.
85  
86 Side Effects: raises a catalogued C<args.*> message naming the parameter
87 (with the routine, or as the command-line option given to L</with_options>)
88 and the problem.
89  
90 Usage:
91  
92 my $args = named(\@_, $SCHEMA, 'verify');
93  
94 =head3 EXAMPLE
95  
96 my $args = named([ deep => 1 ], { deep => { type => 'boolean', optional => 1 } }, 'verify');
97  
98 =head3 API SPECIFICATION
99  
100 =head4 INPUT
101  
102 [
103 { name => 'args', type => 'arrayref' },
104 { name => 'schema', type => 'hashref' },
105 { name => 'routine', type => 'string', min => 1 },
106 ]
107  
108 =head4 OUTPUT
109  
110 { type => 'hashref' }
111  
112 =head3 MESSAGES
113  
114 Key Meaning Resolution
115 args.unknown an argument the schema does not name remove it
116 args.missing a required argument is absent supply it
117 args.not_number a number argument is not numeric pass a number
118 args.not_integer an integer argument is not whole pass a whole number
119 args.too_small a number below the schema's min raise it
120 args.too_large a number above the schema's max lower it
121 args.too_short a string shorter than min characters lengthen it
122 args.too_long a string longer than max characters shorten it
123 args.too_few a list or hash with fewer than min add entries
124 args.too_many a list or hash with more than max remove entries
125 args.not_one_of a value outside memberof use a listed value
126 args.bad_form a string that fails matches fix its form
127 args.invalid any other failure, in the validator's fix the call
128 own words
129  
130 =head3 FORMAL SPECIFICATION
131  
132 named : seq VALUE × SCHEMA × NAME ↛ (KEY ⇸ VALUE)
133 pre pairs(args) satisfies schema
134 post result = coerce(pairs(args))
135  
136 =cut
137  
138 8600 ▶sub named {
Subroutine named: called CC=2 SCAR=6.9
Pod documented
139 8600 my ($args, $schema, $routine) = @_;
140  
141 8600 ▶ my $input = @{$args} ? get_params(undef, $args) : {};
Branch: 1 branch
BranchTrueFalseTotal
@{$args;} ? : 7279 1321 8600
142 8600 return _validate($input, $schema, $routine);
143 }
144  
145 =head2 positional
146  
147 Purpose: validate positional arguments by naming them.
148  
149 Args: C<\@args>, C<\@names> (one per position), C<\%schema>, C<$routine>.
150  
151 Returns: a hashref keyed by C<@names>; missing trailing arguments are
152 C<undef> and must be C<optional> in the schema.
153  
154 Side Effects: raises C<args.invalid>.
155  
156 Usage:
157  
158 my $args = positional(\@_, [qw(root repo)], $SCHEMA, 'load_repo');
159  
160 =head3 EXAMPLE
161  
162 my $args = positional([ '/srv', 'docs' ], [qw(root repo)],
163 { root => { type => 'string' }, repo => { type => 'string' } }, 'load_repo');
164  
165 =head3 API SPECIFICATION
166  
167 =head4 INPUT
168  
169 [
170 { name => 'args', type => 'arrayref' },
171 { name => 'names', type => 'arrayref', min => 1 },
172 { name => 'schema', type => 'hashref' },
173 { name => 'routine', type => 'string', min => 1 },
174 ]
175  
176 =head4 OUTPUT
177  
178 { type => 'hashref' }
179  
180 =head3 MESSAGES
181  
182 Key Meaning Resolution
183 args.invalid an argument is missing or invalid fix the call
184  
185 =head3 FORMAL SPECIFICATION
186  
187 positional(a, n, s, r) = named(zip(n, a), s, r)
188 pre #a ≤ #n
189  
190 =cut
191  
192 1358 ▶sub positional {
Subroutine positional: called CC=1 SCAR=0.0
Pod documented
193 1358 my ($args, $names, $schema, $routine) = @_;
194  
195 # Map by position directly: get_params treats a lone hashref argument as
196 # named pairs, which is wrong for a positional routine that takes one.
197 1358 my %input;
198 1358 @input{ @{$names} } = @{$args};
199 1358 delete @input{ grep { !defined $input{$_} } keys %input };
200  
201 1358 return _validate(\%input, $schema, $routine);
202 }
203  
204 =head2 with_options
205  
206 Purpose: run code with argument errors naming command-line options.
207  
208 Args: C<\%options> (parameter name =E<gt> option, such as
209 C<max_delete_percent =E<gt> '--max-delete-percent'>), C<$code>.
210  
211 Returns: whatever C<$code> returns.
212  
213 Side Effects: while C<$code> runs, an invalid argument with one of these
214 names is reported by its option (C<--max-delete-percent must be no more
215 than 100, not 150>) instead of by the routine and parameter
216 (C<Admin::repo_create: 'max_delete_percent' must be ...>). The mapping is
217 restored when C<$code> returns or dies.
218  
219 Usage:
220  
221 my $made = with_options({ trash_days => '--trash-days' }, sub {
222 $admin->repo_create(name => $name, trash_days => $days);
223 });
224  
225 =head3 EXAMPLE
226  
227 with_options({ role => '--role' }, sub { $admin->client_allow(%args) });
228  
229 =head3 API SPECIFICATION
230  
231 =head4 INPUT
232  
233 [
234 { name => 'options', type => 'hashref' },
235 { name => 'code', type => 'coderef' },
236 ]
237  
238 =head4 OUTPUT
239  
240 { type => 'any' }
241  
242 =head3 MESSAGES
243  
244 None of its own; see L</named>.
245  
246 =head3 FORMAL SPECIFICATION
247  
248 with_options(o, c) = c() where during c, label(p) = o(p) for p ∈ dom o
249 post OPTION' = OPTION
250  
251 =cut
252  
253 4 ▶sub with_options {
Subroutine with_options: called CC=1 SCAR=0.0
Pod documented
254 4 my ($options, $code) = @_;
255  
256 4 local %OPTION = (%OPTION, %{$options});
257 4 return $code->();
258 }
259  
260 # _validate
261 # Purpose: run Params::Validate::Strict and translate its failure.
262 # Entry: $input is a hashref; $schema a Params::Validate::Strict schema.
263 # Exit: returns the validated hashref, or raises a catalogued args.*
264 # message (args.invalid with the validator's text when the cause
265 # cannot be named).
266 # Side effects: none beyond the raise.
267 9958 ▶sub _validate :Private {
Subroutine _validate: called CC=2 SCAR=6.9
268 9958 my ($input, $schema, $routine) = @_;
269  
270 9958 my $valid = eval { validate_strict(schema => $schema, input => $input) };
271 9958 ▶ unless ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 21 9937 9958
272 21 my $error = $@;
273 21 _explain($input, $schema, $routine);
274  
275 # Drop the validator's own prefix and location; the routine name says
276 # where, and the rest says what.
277 2 my $reason = strip_location($error);
278 2 $reason =~ s/\AParams::Validate::Strict line \d+: (?:validate_strict: )?//;
279 2 $reason =~ s/ at \S+ line \d+\.?\z//;
280 2 throw('args.invalid', { routine => $routine, reason => $reason });
281 }
282  
283 9937 return $valid;
284 439 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 # _explain
287 # Purpose: name the cause of a validation failure in catalogue terms.
288 # Entry: the input, schema and routine name of a call the validator
289 # refused. The validator reports only English text, so the cause
290 # is found again from the schema rather than parsed from it.
291 # Exit: raises args.unknown, args.missing, args.not_number,
292 # args.not_integer, args.too_small, args.too_large,
293 # args.too_short, args.too_long, args.too_few, args.too_many,
294 # args.not_one_of or args.bad_form; returns when none applies
295 # (a rule this does not check, such as isa).
296 # Side effects: none beyond the raise.
297 21 ▶sub _explain :Private {
Subroutine _explain: called CC=34 SCAR=36.0
298 21 my ($input, $schema, $routine) = @_;
299  
300 33 ▶ my $what = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
301 33 my $param = shift;
302 33 ▶ return $OPTION{$param} // message('args.param', { routine => $routine, param => $param });
Condition66%2/3
$App::FileSync::Args::OPTION{$param} // message("args.param", {"routine", $routine, "param", $param})
l!l&&r!l&&!r
3300
MC/DC0%0/2
$App::FileSync::Args::OPTION{$param} // message("args.param", {"routine", $routine, "param", $param})
$App::FileSync::Args::OPTION{$param} message("args.param", {"routine", $routine, "param", $param})
Truth table66%2/3
A // B
A= $App::FileSync::Args::OPTION{$param}B= message("args.param", {"routine", $routine, "param", $param})
ABresult
1X1
011
000
303 21 };
304  
305 21 for my $param (sort keys %{$input}) {
306 49 ▶ throw('args.unknown', { what => $what->($param) }) unless exists $schema->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{$param} 1 48 49
307 }
308  
309 20 for my $param (sort keys %{$schema}) {
310 70 my $rule = $schema->{$param};
311 70 ▶ next unless ref $rule eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" 0 70 70
312 70 ▶ if (!exists $input->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $input->{$param}) 37 33 70
313 37 ▶ throw('args.missing', { what => $what->($param) }) unless $rule->{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"optional"} 1 36 37
314 36 next;
315 }
316  
317 33 my $value = $input->{$param};
318 33 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 31 33
319 31 ▶ my $type = $rule->{type} // '';
Condition50%1/2
$rule->{"type"} // ""
l!l
310
MC/DC0%0/1
$rule->{"type"} // ""
$rule->{"type"}
Truth table50%1/2
A
A= $rule->{"type"}
Aresult
11
00
320 31 ▶ my %args = (what => $what->($param), value => ref $value ? ref $value : $value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 3 28 31
321  
322 31 ▶ if (exists $NUMERIC{$type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $NUMERIC{$type}) { } 14 17 31
elsif (exists $SIZE_OF{$type} and defined(my $size = $SIZE_OF{$type}->($value))) { } 15 2 17
323 14 ▶ throw('args.not_integer', \%args) if $NUMERIC{$type} && (ref $value || $value !~ $INTEGER);
Branch: 1 branch
BranchTrueFalseTotal
if $NUMERIC{$type} and ref $value || !($value =~ /$INTEGER/) 2 12 14
Condition83%5/6
$NUMERIC{$type} and ref $value || !($value =~ /$INTEGER/)
!ll&&!rl&&r
2102
ref $value or not $value =~ /$INTEGER/
l!l&&r!l&&!r
0210
MC/DC66%2/3
$NUMERIC{$type} and ref $value || !($value =~ /$INTEGER/)
$NUMERIC{$type} ref $value not $value =~ /$INTEGER/
Truth table75%3/4
A and B or not(C)
A= $NUMERIC{$type}B= ref $valueC= not $value =~ /$INTEGER/
ABCresult
0XX0
1000
11X1
1011
324 12 ▶ throw('args.not_number', \%args) if ref $value || !looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or not &looks_like_number($value) 1 11 12
Condition66%2/3
ref $value or not &looks_like_number($value)
l!l&&r!l&&!r
0111
MC/DC50%1/2
ref $value or not &looks_like_number($value)
ref $value not &looks_like_number($value)
Truth table66%2/3
A or not(B)
A= ref $valueB= not &looks_like_number($value)
ABresult
1X1
011
000
325 11 ▶ throw('args.too_small', { %args, min => $rule->{min} }) if defined $rule->{min} && $value < $rule->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->{"min"} and $value < $rule->{"min"} 2 9 11
Condition66%2/3
defined $rule->{"min"} and $value < $rule->{"min"}
!ll&&!rl&&r
092
MC/DC50%1/2
defined $rule->{"min"} and $value < $rule->{"min"}
defined $rule->{"min"} $value < $rule->{"min"}
Truth table66%2/3
A and B
A= defined $rule->{"min"}B= $value < $rule->{"min"}
ABresult
0X0
100
111
326 9 ▶ throw('args.too_large', { %args, max => $rule->{max} }) if defined $rule->{max} && $value > $rule->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->{"max"} and $value > $rule->{"max"} 7 2 9
Condition66%2/3
defined $rule->{"max"} and $value > $rule->{"max"}
!ll&&!rl&&r
207
MC/DC50%1/2
defined $rule->{"max"} and $value > $rule->{"max"}
defined $rule->{"max"} $value > $rule->{"max"}
Truth table66%2/3
A and B
A= defined $rule->{"max"}B= $value > $rule->{"max"}
ABresult
0X0
100
111
327 ▶ } elsif (exists $SIZE_OF{$type} && defined(my $size = $SIZE_OF{$type}->($value))) {
Condition66%2/3
exists $SIZE_OF{$type} and defined(my $size = $SIZE_OF{$type}->($value))
!ll&&!rl&&r
0215
MC/DC50%1/2
exists $SIZE_OF{$type} and defined(my $size = $SIZE_OF{$type}->($value))
exists $SIZE_OF{$type} defined(my $size = $SIZE_OF{$type}->($value))
Truth table66%2/3
A and B
A= exists $SIZE_OF{$type}B= defined(my $size = $SIZE_OF{$type}->($value))
ABresult
0X0
100
111
328 15 my $string = !ref $value;
329 15 ▶ if (defined $rule->{min} && $size < $rule->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{"min"} and $size < $rule->{"min"}) 2 13 15
Condition100%3/3
defined $rule->{"min"} and $size < $rule->{"min"}
!ll&&!rl&&r
1212
MC/DC100%2/2
defined $rule->{"min"} and $size < $rule->{"min"}
defined $rule->{"min"} $size < $rule->{"min"}
Truth table100%3/3
A and B
A= defined $rule->{"min"}B= $size < $rule->{"min"}
ABresult
0X0
100
111
330 2 ▶ throw('args.too_short', { %args, count => $rule->{min} }) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 1 1 2
331 1 throw('args.too_few', { %args, count => $rule->{min} });
332 }
333 13 ▶ if (defined $rule->{max} && $size > $rule->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{"max"} and $size > $rule->{"max"}) 1 12 13
Condition66%2/3
defined $rule->{"max"} and $size > $rule->{"max"}
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $rule->{"max"} and $size > $rule->{"max"}
defined $rule->{"max"} $size > $rule->{"max"}
Truth table66%2/3
A and B
A= defined $rule->{"max"}B= $size > $rule->{"max"}
ABresult
0X0
100
111
334 1 ▶ throw('args.too_long', { %args, count => $rule->{max} }) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 1 0 1
335 0 throw('args.too_many', { %args, count => $rule->{max} });
336 }
337 }
338  
339 16 ▶ if (ref $rule->{memberof} eq 'ARRAY' && !ref $value && !grep { $_ eq $value } @{ $rule->{memberof} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule->{"memberof"} eq "ARRAY" and not ref $value and not grep({$_ eq $value;} @{$$rule{"memberof"};})) 1 15 16
Condition66%4/6
ref $rule->{"memberof"} eq "ARRAY" and not ref $value and not grep({$_ eq $value;} @{$$rule{"memberof"};})
!ll&&!rl&&r
1501
ref $rule->{"memberof"} eq "ARRAY" and not ref $value
!ll&&!rl&&r
1501
MC/DC33%1/3
ref $rule->{"memberof"} eq "ARRAY" and not ref $value and not grep({$_ eq $value;} @{$$rule{"memberof"};})
ref $rule->{"memberof"} eq "ARRAY" not ref $value not grep({$_ eq $value;} @{$$rule{"memberof"};})
Truth table50%2/4
A and not(B) and not(C)
A= ref $rule->{"memberof"} eq "ARRAY"B= not ref $valueC= not grep({$_ eq $value;} @{$$rule{"memberof"};})
ABCresult
0XX0
10X0
1100
1111
340 1 throw('args.not_one_of', { %args, list => join(', ', @{ $rule->{memberof} }) });
341 }
342 15 ▶ if (defined $rule->{matches} && !ref $value && $value !~ $rule->{matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{"matches"} and not ref $value and not $value =~ /$rule->{"matches"}/) 1 14 15
Condition66%4/6
defined $rule->{"matches"} and not ref $value and not $value =~ /$rule->{"matches"}/
!ll&&!rl&&r
1401
defined $rule->{"matches"} and not ref $value
!ll&&!rl&&r
1401
MC/DC33%1/3
defined $rule->{"matches"} and not ref $value and not $value =~ /$rule->{"matches"}/
defined $rule->{"matches"} not ref $value not $value =~ /$rule->{"matches"}/
Truth table50%2/4
A and not(B) and not(C)
A= defined $rule->{"matches"}B= not ref $valueC= not $value =~ /$rule->{"matches"}/
ABCresult
0XX0
10X0
1100
1111
343 1 throw('args.bad_form', \%args);
344 }
345 }
346  
347 2 return;
348 439 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
349  
350 439 1;
351  
352 __END__
« blib/lib/App/FileSync/CLI.pm ↑ Summary blib/lib/App/FileSync/Process.pm »