blib/lib/Punk/Command.pm

filter: Statementstmt 100% 1071 / 1087 (18 uncoverable) Branchbran 91.1% 492 / 634 (142 uncoverable) Conditioncond 89.1% 231 / 358 (116 uncoverable) MC/DCmcdc 91.2% 72 / 205 (115 uncoverable) Subroutinesub 100% 78 / 78 Podpod 100% 11 / 11 totaltotal 95.2% 1955 / 2373 (391 uncoverable) CC 505 SCAR 30.8
CC
505
Cov
95%
routes
50.5
api_sync
45.1
main
35.0
CRAP 21.8
↑ Summary blib/lib/Punk/App.pm »
1 package Punk::Command;
2  
3 69 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 69 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 69 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 68 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 67 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 67 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 67 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 67 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 67 ▶use Punk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 67 our $VERSION = '0.70';
14  
15 # The whole punk command line. bin/punk is two lines - `exit
16 # Punk::Command->main(@ARGV)` - and everything else is here: a registry of
17 # command specs, one dispatcher that owns option parsing and help generation,
18 # and the command bodies. Exit codes: 0 fine, 1 something is wrong with the
19 # application or its environment, 2 the command was used wrongly (misuse
20 # prints the generated usage to STDERR).
21 #
22 # A compiled-at-boot framework is opaque from the outside: everything is
23 # resolved and frozen inside one coderef at to_app. These commands exist to
24 # open that up - what the router actually holds, what the spec actually maps
25 # to, what the configuration actually resolved to, and which C ABIs are
26 # actually in play.
27  
28 # ---- the registry ------------------------------------------------------------
29 #
30 # A command is a spec registered by name. Help is GENERATED from the spec, so
31 # `punk help X`, `punk X --help` and every misuse path read one source and
32 # cannot drift - the invariant the old %USAGE heredocs tried to hold by hand.
33 #
34 # Punk::Command->register(greet => {
35 # abstract => 'one line for the command list',
36 # display => 'greet <what>', # the list row, when the name alone misleads
37 # usage => '[options]', # after "usage: punk serve "
38 # desc => 'a paragraph for the help page',
39 # options => [ { spec => 'port=i', arg => 'N',
40 # doc => 'listen port', default => 5000 } ],
41 # commands => { sync => \%subspec }, # nested verbs (punk api sync)
42 # code => sub { my ($opt, @args) = @_; ...; return 0 },
43 # hidden => 0,
44 # hints => [ [ qr/pattern/, 'the next move worth spelling out' ] ],
45 # }, $owner);
46 #
47 # Two owners claiming one name croak naming both (the install_kw rule).
48 # Registration order is display order. A command body signals misuse with
49 # `die { usage_error => $msg }` (message + usage + exit 2); any other die
50 # becomes `punk <cmd>: msg` + exit 1, with the spec's hints appended when
51 # their pattern matches.
52 #
53 # An unknown command X gets one shot at `require Punk::Command::<ucfirst X>`,
54 # whose load-time job is to call register - the seam a plugin dist uses to add
55 # `punk queue ...`. No namespace scanning, no cost on the common path.
56  
57 67 our ($OUT, $ERR); # the test seam: filehandles replacing STDOUT/STDERR
58  
59 67 our (%SPECS, @ORDER, %OWNER);
60 67 our @DOCTOR;
61 67 my %DOCTOR_OWNER;
62  
63 819 ▶sub register {
Subroutine register: called CC=7 SCAR=19.5
Pod documented
64 819 my ($class, $name, $spec, $owner) = @_;
65 # uncoverable condition false
66 819 ▶ $owner //= caller;
Condition100%2/3
$owner //= caller
l!l&&r!l&&!r
148050
MC/DC100%0/2
$owner //= caller
$owner caller
Truth table100%2/3
A //= B
A= $ownerB= caller
ABresult
1X1
011
000
67 # uncoverable condition left count:1
68 # uncoverable condition right count:1
69 # uncoverable condition left count:2
70 819 ▶ Carp::croak('Punk::Command: register needs a name and a spec hashref')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name and ref $spec eq "HASH" 1 818 819
Condition66%3/6
defined $name and length $name and ref $spec eq "HASH"
!ll&&!rl&&r
01818
defined $name and length $name
!ll&&!rl&&r
00819
MC/DC100%1/3
defined $name and length $name and ref $spec eq "HASH"
defined $name length $name ref $spec eq "HASH"
Truth table75%2/4
A and B and C
A= defined $nameB= length $nameC= ref $spec eq "HASH"
ABCresult
0XX0
10X0
1100
1111
71 unless defined $name && length $name && ref $spec eq 'HASH';
72 818 ▶ if (my $prev = $OWNER{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $Punk::Command::OWNER{$name}) 1 817 818
73 1 Carp::croak("Punk::Command: command '$name' is registered by both "
74 . "$prev and $owner");
75 }
76 817 $OWNER{$name} = $owner;
77 817 push @ORDER, $name;
78 # uncoverable condition left
79 # uncoverable condition false
80 817 ▶ $spec->{name} //= $name; # so a diagnostic can name the command it is about
Condition100%1/3
$spec->{"name"} //= $name
l!l&&r!l&&!r
08170
MC/DC100%0/2
$spec->{"name"} //= $name
$spec->{"name"} $name
Truth table100%1/3
A //= B
A= $spec->{"name"}B= $name
ABresult
1X1
011
000
81 817 $SPECS{$name} = $spec;
82 817 return $class;
83 }
84  
85 # A doctor row: label plus a probe returning (ok, detail). Core rows register
86 # here too, so a plugin's rows land in the same report.
87 9 ▶sub register_doctor {
Subroutine register_doctor: called CC=3 SCAR=11.0
Pod documented
88 9 my ($class, $label, $probe, $owner) = @_;
89 # uncoverable condition right
90 # uncoverable condition false
91 9 ▶ $owner //= caller;
Condition100%1/3
$owner //= caller
l!l&&r!l&&!r
900
MC/DC100%0/2
$owner //= caller
$owner caller
Truth table100%1/3
A //= B
A= $ownerB= caller
ABresult
1X1
011
000
92 9 ▶ if (my $prev = $DOCTOR_OWNER{$label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $DOCTOR_OWNER{$label}) 1 8 9
93 1 Carp::croak("Punk::Command: doctor row '$label' is registered by "
94 . "both $prev and $owner");
95 }
96 8 $DOCTOR_OWNER{$label} = $owner;
97 8 push @DOCTOR, [ $label, $probe ];
98 8 return $class;
99 }
100  
101 # every command takes these; a spec's own option with the same key wins
102 67 my @SHARED_OPTIONS = (
103 { spec => 'dir=s', arg => 'PATH',
104 doc => 'the application (default: found by walking up for app.psgi)' },
105 );
106  
107 1882 ▶sub _opt_key {
Subroutine _opt_key: called CC=1 SCAR=0.0
108 1882 my ($spec) = @_;
109 1882 my ($name) = $spec =~ /^([^=|!+\@]+)/;
110 1882 $name =~ tr/-/_/;
111 1882 return $name;
112 }
113  
114 # ---- the dispatcher ----------------------------------------------------------
115  
116 155 ▶sub main {
Subroutine main: called CC=35 SCAR=35.6
Pod documented
117 155 my ($class, @argv) = @_;
118 155 ▶ local *STDOUT = $OUT if $OUT;
Branch: 1 branch
BranchTrueFalseTotal
if $Punk::Command::OUT 106 49 155
119 155 ▶ local *STDERR = $ERR if $ERR;
Branch: 1 branch
BranchTrueFalseTotal
if $Punk::Command::ERR 106 49 155
120  
121 # uncoverable branch true
122 # uncoverable condition right count:1
123 # uncoverable condition right count:2
124 155 ▶ if (!@argv || $argv[0] eq '-h' || $argv[0] eq '--help') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @argv or $argv[0] eq "-h" or $argv[0] eq "--help") 3 152 155
Condition100%4/6
not @argv or $argv[0] eq "-h" or $argv[0] eq "--help"
l!l&&r!l&&!r
30152
not @argv or $argv[0] eq "-h"
l!l&&r!l&&!r
30152
MC/DC100%1/3
not @argv or $argv[0] eq "-h" or $argv[0] eq "--help"
not @argv $argv[0] eq "-h" $argv[0] eq "--help"
Truth table100%2/4
not(A) or B or C
A= not @argvB= $argv[0] eq "-h"C= $argv[0] eq "--help"
ABCresult
1XX1
01X1
0011
0000
125 3 _usage_all(\*STDOUT);
126 3 return 0;
127 }
128 # uncoverable condition left
129 152 ▶ if ($argv[0] eq '-v' || $argv[0] eq '--version') {
Branch: 1 branch
BranchTrueFalseTotal
if ($argv[0] eq "-v" or $argv[0] eq "--version") 1 151 152
Condition100%2/3
$argv[0] eq "-v" or $argv[0] eq "--version"
l!l&&r!l&&!r
01151
MC/DC100%1/2
$argv[0] eq "-v" or $argv[0] eq "--version"
$argv[0] eq "-v" $argv[0] eq "--version"
Truth table100%2/3
A or B
A= $argv[0] eq "-v"B= $argv[0] eq "--version"
ABresult
1X1
011
000
130 1 print "punk $Punk::VERSION\n";
131 1 return 0;
132 }
133  
134 151 my $name = shift @argv;
135 151 ▶ return _cmd_help(@argv) if $name eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "help" 14 137 151
136  
137 137 ▶ my $spec = $SPECS{$name} // _probe($name);
Condition100%3/3
$Punk::Command::SPECS{$name} // _probe($name)
l!l&&r!l&&!r
13511
MC/DC100%2/2
$Punk::Command::SPECS{$name} // _probe($name)
$Punk::Command::SPECS{$name} _probe($name)
Truth table100%3/3
A // B
A= $Punk::Command::SPECS{$name}B= _probe($name)
ABresult
1X1
011
000
138 137 ▶ unless ($spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 1 136 137
139 1 print STDERR "punk: unknown command '$name'\n\n";
140 1 _usage_all(\*STDERR);
141 1 return 2;
142 }
143  
144 # descend nested verbs (punk api sync, punk queue jobs ...)
145 136 my $path = $name;
146 136 ▶ while ($spec->{commands}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"commands"}) 46 132 178
147 # uncoverable condition right
148 46 ▶ my $tok = (@argv && $argv[0] !~ /^-/) ? $argv[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@argv && !($argv[0] =~ /^-/) ? : 45 1 46
Condition100%2/3
@argv and not $argv[0] =~ /^-/
!ll&&!rl&&r
1045
MC/DC100%1/2
@argv and not $argv[0] =~ /^-/
@argv not $argv[0] =~ /^-/
Truth table100%2/3
A and not(B)
A= @argvB= not $argv[0] =~ /^-/
ABresult
0X0
100
111
149 46 ▶ if (defined $tok && $spec->{commands}{$tok}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tok and $spec->{"commands"}{$tok}) 42 4 46
Condition100%3/3
defined $tok and $spec->{"commands"}{$tok}
!ll&&!rl&&r
1342
MC/DC100%2/2
defined $tok and $spec->{"commands"}{$tok}
defined $tok $spec->{"commands"}{$tok}
Truth table100%3/3
A and B
A= defined $tokB= $spec->{"commands"}{$tok}
ABresult
0X0
100
111
150 42 shift @argv;
151 42 $spec = $spec->{commands}{$tok};
152 42 $path .= " $tok";
153 42 next;
154 }
155 # uncoverable branch true
156 4 ▶ last if $spec->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"code"} 0 4 4
157 4 ▶ if (defined $tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tok) 3 1 4
158 3 my @verbs = sort keys %{ $spec->{commands} };
159 3 ▶ print STDERR @verbs == 1
Branch: 1 branch
BranchTrueFalseTotal
@verbs == 1 ? : 2 1 3
160 ? "punk $path: expected '$verbs[0]'\n\n"
161 : "punk $path: unknown subcommand '$tok' (expected one of: "
162 . join(', ', @verbs) . ")\n\n";
163 }
164 4 _usage_cmd($path, $spec, \*STDERR);
165 4 return 2;
166 }
167  
168 # a raw spec owns its own argv - options, help, everything - which is
169 # what an adapter over another CLI wants (punk queue X == punk-queue X)
170 132 ▶ if ($spec->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"raw"}) 3 129 132
171 3 my $code = eval { $spec->{code}->({}, @argv) };
172 3 ▶ if (my $err = $@) { return _fail($path, $err, $spec->{hints}) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 2 3
173 2 ▶ return defined $code ? int $code : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 1 1 2
174 }
175  
176 # A command may bring options it cannot know at registration time: `new`
177 # asks the kit named in argv for its own. Resolved before Getopt so the
178 # kit's options parse and show up in help like any other, rather than
179 # being scraped out of a leftover argv by the command body.
180 129 my (@extra, @options);
181 {
182 # Both halves under one eval: resolving them can croak (a kit that
183 # will not load), and so can merging them (an option the command
184 # already declares). Either is a die that has to become one line and
185 # an exit code, not a raw croak out of main.
186 129 my $ok = eval {
187 @extra = $spec->{extra_options}
188 129 ▶ ? $spec->{extra_options}->(@argv) : ();
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'extra_options'} ? : 12 117 129
189 129 @options = _options_for($spec, \@extra);
190 128 1;
191 };
192 129 ▶ return _fail($path, $@, $spec->{hints}) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 128 129
193 }
194  
195 128 my (%opt, @gspec);
196 128 for my $o (@options) {
197 756 my $k = _opt_key($o->{spec});
198 756 ▶ if ($o->{spec} =~ /\@$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{'spec'} =~ /\@$/) { } 17 739 756
199 17 ▶ $opt{$k} = [ @{ $o->{default} || [] } ];
Condition100%2/2
$o->{'default'} || []
l!l
215
MC/DC100%1/1
$o->{'default'} || []
$o->{'default'}
Truth table100%2/2
A
A= $o->{'default'}
Aresult
11
00
200 17 push @gspec, $o->{spec} => $opt{$k};
201 }
202 else {
203 739 ▶ $opt{$k} = $o->{default} if exists $o->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"default"} 97 642 739
204 739 push @gspec, $o->{spec} => \$opt{$k};
205 }
206 }
207 {
208 128 my $saved = Getopt::Long::Configure(
209 qw(no_auto_abbrev no_ignore_case bundling));
210 128 my $ok = do {
211 3 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 3 my ($w) = @_;
213 3 $w =~ s/\s+\z//;
214 3 print STDERR "punk $path: $w\n";
215 128 };
216 128 eval { Getopt::Long::GetOptionsFromArray(\@argv, @gspec) };
217 };
218 128 my $err = $@;
219 128 Getopt::Long::Configure($saved);
220 128 ▶ if ($err) { print STDERR "punk $path: $err"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 127 128
221 127 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 124 127
222 3 print STDERR "\n";
223 3 _usage_cmd($path, $spec, \*STDERR, \@extra);
224 3 return 2;
225 }
226 }
227 124 ▶ if ($opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 5 119 124
228 5 _usage_cmd($path, $spec, \*STDOUT, \@extra);
229 5 return 0;
230 }
231 119 delete $opt{help};
232  
233 119 my $code = eval { $spec->{code}->(\%opt, @argv) };
234 116 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 16 100 116
235 # uncoverable condition right
236 16 ▶ if (ref $err eq 'HASH' && defined $err->{usage_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err eq "HASH" and defined $err->{"usage_error"}) 8 8 16
Condition100%2/3
ref $err eq "HASH" and defined $err->{"usage_error"}
!ll&&!rl&&r
808
MC/DC100%1/2
ref $err eq "HASH" and defined $err->{"usage_error"}
ref $err eq "HASH" defined $err->{"usage_error"}
Truth table100%2/3
A and B
A= ref $err eq "HASH"B= defined $err->{"usage_error"}
ABresult
0X0
100
111
237 8 print STDERR "punk $path: $err->{usage_error}\n\n";
238 8 _usage_cmd($path, $spec, \*STDERR, \@extra);
239 8 return 2;
240 }
241 8 return _fail($path, $err, $spec->{hints});
242 }
243 # uncoverable branch false
244 100 ▶ return defined $code ? int $code : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 100 0 100
245 }
246  
247 # the plugin seam: Punk::Command::<Ucfirst> registers at load
248 4 ▶sub _probe {
Subroutine _probe: called CC=3 SCAR=11.0
249 4 my ($name) = @_;
250 # uncoverable branch true
251 4 ▶ return unless $name =~ /^[a-z][a-z0-9_-]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[a-z][a-z0-9_-]*$/ 0 4 4
252 4 (my $mod = ucfirst $name) =~ s/[-_](\w)/\U$1/g;
253 4 ▶ return unless eval { require "Punk/Command/$mod.pm"; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require "Punk/Command/$mod.pm"; 1 } } 3 1 4
254 1 return $SPECS{$name};
255 }
256  
257 14 ▶sub _cmd_help {
Subroutine _cmd_help: called CC=8 SCAR=20.8
258 14 my ($topic, @rest) = @_;
259 # uncoverable condition right
260 14 ▶ if (defined $topic && length $topic) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $topic and length $topic) 13 1 14
Condition100%2/3
defined $topic and length $topic
!ll&&!rl&&r
1013
MC/DC100%1/2
defined $topic and length $topic
defined $topic length $topic
Truth table100%2/3
A and B
A= defined $topicB= length $topic
ABresult
0X0
100
111
261 # uncoverable condition right
262 13 ▶ my $spec = $SPECS{$topic} // _probe($topic);
Condition100%2/3
$Punk::Command::SPECS{$topic} // _probe($topic)
l!l&&r!l&&!r
1102
MC/DC100%1/2
$Punk::Command::SPECS{$topic} // _probe($topic)
$Punk::Command::SPECS{$topic} _probe($topic)
Truth table100%2/3
A // B
A= $Punk::Command::SPECS{$topic}B= _probe($topic)
ABresult
1X1
011
000
263 13 ▶ unless ($spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 2 11 13
264 2 print STDERR "punk help: no such command '$topic'\n\n";
265 2 _usage_all(\*STDERR);
266 2 return 2;
267 }
268 11 my $path = $topic;
269 # uncoverable condition right count:1
270 # uncoverable condition left count:2
271 # uncoverable condition right count:2
272 11 ▶ while (@rest && $spec->{commands} && $spec->{commands}{$rest[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest and $spec->{"commands"} and $spec->{"commands"}{$rest[0]}) 1 11 12
Condition83%4/6
@rest and $spec->{"commands"} and $spec->{"commands"}{$rest[0]}
!ll&&!rl&&r
1101
@rest and $spec->{"commands"}
!ll&&!rl&&r
1101
MC/DC100%1/3
@rest and $spec->{"commands"} and $spec->{"commands"}{$rest[0]}
@rest $spec->{"commands"} $spec->{"commands"}{$rest[0]}
Truth table75%2/4
A and B and C
A= @restB= $spec->{"commands"}C= $spec->{"commands"}{$rest[0]}
ABCresult
0XX0
10X0
1100
1111
273 1 my $t = shift @rest;
274 1 $spec = $spec->{commands}{$t};
275 1 $path .= " $t";
276 }
277 11 _usage_cmd($path, $spec, \*STDOUT);
278 11 return 0;
279 }
280 1 _usage_all(\*STDOUT);
281 1 return 0;
282 }
283  
284 # croaks arrive with an "at FILE line N." tail that means nothing to someone
285 # running a command; one actionable line is the whole message
286 10 ▶sub _fail {
Subroutine _fail: called CC=4 SCAR=13.9
287 10 my ($path, $err, $hints) = @_;
288 10 $err = "$err";
289 10 $err =~ s/ at \S+ line \d+\.?\s*\z//;
290 10 $err =~ s/\s+\z//;
291 10 $err =~ s/\A(?:Punk::Generate|Punk::Kit::[\w:]+): //;
292 10 print STDERR "punk $path: $err\n";
293 10 ▶ for my $h (@{ $hints || [] }) {
Condition100%2/2
$hints || []
l!l
64
MC/DC100%1/1
$hints || []
$hints
Truth table100%2/2
A
A= $hints
Aresult
11
00
294 10 ▶ print STDERR " $h->[1]\n" if $err =~ $h->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /$h->[0]/ 2 8 10
295 }
296 10 return 1;
297 }
298  
299 # ---- generated help ----------------------------------------------------------
300  
301 159 ▶sub _options_for {
Subroutine _options_for: called CC=9 SCAR=22.0
302 159 my ($spec, $extra) = @_;
303 159 ▶ my @own = @{ $spec->{options} || [] };
Condition100%2/2
$spec->{'options'} || []
l!l
14415
MC/DC100%1/1
$spec->{'options'} || []
$spec->{'options'}
Truth table100%2/2
A
A= $spec->{'options'}
Aresult
11
00
304 159 my %own_key = map { _opt_key($_->{spec}) => 1 } @own;
305  
306 # An option a command brought at run time cannot quietly replace one it
307 # declared: which of the two Getopt bound would decide what the command
308 # does, and that is not a thing to leave to ordering.
309 159 my @extra;
310 # uncoverable branch true
311 159 ▶ for my $o (@{ $extra || [] }) {
Condition50%1/2
$extra || []
l!l
1590
MC/DC0%0/1
$extra || []
$extra
Truth table50%1/2
A
A= $extra
Aresult
11
00
312 6 my $k = _opt_key($o->{spec});
313 Carp::croak("Punk::Command: option '--$k' is defined by both "
314 . "'" . ($spec->{name} // '?') . "' and "
315 . ($o->{owner} // 'a kit'))
316 # uncoverable condition right count:1
317 # uncoverable condition right count:2
318 # uncoverable condition right count:3
319 6 ▶ if $own_key{$k} || $k eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if $own_key{$k} or $k eq "help" 1 5 6
Condition100%4/7
$own_key{$k} or $k eq "help"
l!l&&r!l&&!r
105
$spec->{"name"} // "?"
l!l
10
$o->{"owner"} // "a kit"
l!l
10
MC/DC100%1/2
$own_key{$k} or $k eq "help"
$own_key{$k} $k eq "help"
MC/DC100%0/1
$spec->{"name"} // "?"
$spec->{"name"}
MC/DC100%0/1
$o->{"owner"} // "a kit"
$o->{"owner"}
Truth table100%2/3
A or B
A= $own_key{$k}B= $k eq "help"
ABresult
1X1
011
000
Truth table100%1/2
A
A= $spec->{"name"}
Aresult
11
00
Truth table100%1/2
A
A= $o->{"owner"}
Aresult
11
00
320 5 push @extra, $o;
321 }
322 158 my %extra_key = map { _opt_key($_->{spec}) => 1 } @extra;
323  
324 return (
325 158 (grep { !$own_key{ _opt_key($_->{spec}) }
326 # uncoverable condition right
327 158 ▶ && !$extra_key{ _opt_key($_->{spec}) } } @SHARED_OPTIONS),
Condition100%2/3
$own_key{_opt_key($_->{'spec'})} || $extra_key{_opt_key($_->{'spec'})}
l!l&&r!l&&!r
320126
MC/DC100%1/2
$own_key{_opt_key($_->{'spec'})} || $extra_key{_opt_key($_->{'spec'})}
$own_key{_opt_key($_->{'spec'})} $extra_key{_opt_key($_->{'spec'})}
Truth table100%2/3
A || B
A= $own_key{_opt_key($_->{'spec'})}B= $extra_key{_opt_key($_->{'spec'})}
ABresult
1X1
011
000
328 @own,
329 @extra,
330 { spec => 'help|h', doc => 'this' },
331 );
332 }
333  
334 7 ▶sub _usage_all {
Subroutine _usage_all: called CC=8 SCAR=20.8
335 7 my ($fh) = @_;
336 7 print {$fh} "punk - the Punk command line\n\n"
337 . "usage: punk <command> [options]\n\ncommands:\n";
338 7 my @rows;
339 7 for my $name (@ORDER) {
340 87 my $spec = $SPECS{$name};
341 87 ▶ next if $spec->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"hidden"} 10 77 87
342 # uncoverable condition false count:1
343 # uncoverable condition right count:2
344 77 ▶ push @rows, [ $spec->{display} // $name, $spec->{abstract} // '' ];
Condition100%3/5
$spec->{"display"} // $name
l!l&&r!l&&!r
35420
$spec->{"abstract"} // ""
l!l
770
MC/DC100%0/2
$spec->{"display"} // $name
$spec->{"display"} $name
MC/DC100%0/1
$spec->{"abstract"} // ""
$spec->{"abstract"}
Truth table100%2/3
A // B
A= $spec->{"display"}B= $name
ABresult
1X1
011
000
Truth table100%1/2
A
A= $spec->{"abstract"}
Aresult
11
00
345 }
346 7 push @rows, [ 'help [command]', "this, or a command's own options" ];
347 7 my $w = 0;
348 # uncoverable condition false
349 7 ▶ length $_->[0] > $w and $w = length $_->[0] for @rows;
Branch: 1 branch
BranchTrueFalseTotal
length $_->[0] > $w and $w = length $_->[0] 14 70 84
350 7 printf {$fh} " %-*s %s\n", $w, @{$_}[0, 1] for @rows;
351 7 print {$fh} "\n punk --version\n";
352 7 return;
353 }
354  
355 36 ▶sub _usage_cmd {
Subroutine _usage_cmd: called CC=32 SCAR=34.7
356 36 my ($path, $spec, $fh, $extra) = @_;
357 36 ▶ if ($spec->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"raw"}) 1 35 36
358 # the adapted CLI owns its own help
359 1 ▶ print {$fh} 'usage: punk ' . $path . ' '
Condition50%1/2
$spec->{"usage"} // "<command> [options]"
l!l
10
MC/DC0%0/1
$spec->{"usage"} // "<command> [options]"
$spec->{"usage"}
Truth table50%1/2
A
A= $spec->{"usage"}
Aresult
11
00
360 # uncoverable condition right
361 1 . ($spec->{usage} // '<command> [options]') . "\n";
362 # uncoverable branch false
363 1 ▶ print {$fh} "\n$spec->{desc}\n" if $spec->{desc};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"desc"} 1 0 1
364 1 print {$fh} "\nsee `punk $path help` for its commands\n";
365 1 return;
366 }
367 # uncoverable condition right count:1
368 35 ▶ if ($spec->{commands} && !$spec->{code}
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"commands"} and not $spec->{"code"} and 1 == keys %{$$spec{"commands"};}) 5 30 35
Condition66%5/6
$spec->{"commands"} and not $spec->{"code"} and 1 == keys %{$$spec{"commands"};}
!ll&&!rl&&r
2825
$spec->{"commands"} and not $spec->{"code"}
!ll&&!rl&&r
2807
MC/DC66%2/3
$spec->{"commands"} and not $spec->{"code"} and 1 == keys %{$$spec{"commands"};}
$spec->{"commands"} not $spec->{"code"} 1 == keys %{$$spec{"commands"};}
Truth table50%3/4
A and not(B) and C
A= $spec->{"commands"}B= not $spec->{"code"}C= 1 == keys %{$$spec{"commands"};}
ABCresult
0XX0
10X0
1100
1111
369 7 && 1 == keys %{ $spec->{commands} }) {
370 # one mandatory verb reads better spelled out: "usage: punk api sync"
371 5 my ($verb) = keys %{ $spec->{commands} };
372 5 return _usage_cmd("$path $verb", $spec->{commands}{$verb}, $fh);
373 }
374 30 ▶ print {$fh} "usage: punk $path " . ($spec->{usage} // '[options]') . "\n";
Condition100%2/2
$spec->{"usage"} // "[options]"
l!l
1713
MC/DC100%1/1
$spec->{"usage"} // "[options]"
$spec->{"usage"}
Truth table100%2/2
A
A= $spec->{"usage"}
Aresult
11
00
375 30 ▶ print {$fh} "\n$spec->{desc}\n" if $spec->{desc};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"desc"} 25 5 30
376 30 ▶ if ($spec->{commands}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"commands"}) 2 28 30
377 2 my @names = sort keys %{ $spec->{commands} };
378 2 my $w = 0;
379 # uncoverable condition false
380 2 ▶ length $_ > $w and $w = length $_ for @names;
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $w and $w = length $_ 2 2 4
381 2 print {$fh} "\ncommands:\n";
382 4 ▶ printf {$fh} " %-*s %s\n", $w, $_,
Condition100%2/2
$spec->{"commands"}{$_}{"abstract"} // ""
l!l
22
MC/DC100%1/1
$spec->{"commands"}{$_}{"abstract"} // ""
$spec->{"commands"}{$_}{"abstract"}
Truth table100%2/2
A
A= $spec->{"commands"}{$_}{"abstract"}
Aresult
11
00
383 2 $spec->{commands}{$_}{abstract} // '' for @names;
384 }
385 # Options a kit brought get their own heading, so it is clear which of
386 # them go away when --kit does.
387 30 ▶ my @extra = @{ $extra || [] };
Condition100%2/2
$extra || []
l!l
1614
MC/DC100%1/1
$extra || []
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
388 30 my %is_extra = map { _opt_key($_->{spec}) => 1 } @extra;
389 30 my @options = _options_for($spec, \@extra);
390 30 my (@rows, @extra_rows);
391 30 for my $o (@options) {
392 179 ▶ next if $o->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"hidden"} 1 178 179
393 178 my ($long) = $o->{spec} =~ /^([^=|!+\@]+)/;
394 178 (my $shown = $long) =~ tr/_/-/;
395 my $left = "--$shown" . (defined $o->{arg} && length $o->{arg}
396 # uncoverable condition right
397 178 ▶ ? " $o->{arg}" : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $o->{'arg'} && length $o->{'arg'} ? : 104 74 178
Condition100%2/3
defined $o->{"arg"} and length $o->{"arg"}
!ll&&!rl&&r
740104
MC/DC100%1/2
defined $o->{"arg"} and length $o->{"arg"}
defined $o->{"arg"} length $o->{"arg"}
Truth table100%2/3
A and B
A= defined $o->{"arg"}B= length $o->{"arg"}
ABresult
0X0
100
111
398 # uncoverable condition right
399 178 ▶ my $doc = $o->{doc} // '';
Condition100%1/2
$o->{"doc"} // ""
l!l
1780
MC/DC100%0/1
$o->{"doc"} // ""
$o->{"doc"}
Truth table100%1/2
A
A= $o->{"doc"}
Aresult
11
00
400 $doc .= " (default: $o->{default})"
401 if defined $o->{default} && !ref $o->{default}
402 # uncoverable condition right count:2
403 178 ▶ && length $o->{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"default"} and not ref $o->{"default"} and length $o->{"default"} 35 143 178
Condition66%5/6
defined $o->{"default"} and not ref $o->{"default"} and length $o->{"default"}
!ll&&!rl&&r
143035
defined $o->{"default"} and not ref $o->{"default"}
!ll&&!rl&&r
142135
MC/DC66%2/3
defined $o->{"default"} and not ref $o->{"default"} and length $o->{"default"}
defined $o->{"default"} not ref $o->{"default"} length $o->{"default"}
Truth table50%3/4
A and not(B) and C
A= defined $o->{"default"}B= not ref $o->{"default"}C= length $o->{"default"}
ABCresult
0XX0
10X0
1100
1111
404 178 ▶ push @{ $is_extra{ _opt_key($o->{spec}) } ? \@extra_rows : \@rows },
Branch: 1 branch
BranchTrueFalseTotal
$is_extra{_opt_key($o->{'spec'})} ? : 2 176 178
405 [ $left, $doc ];
406 }
407 30 my $w = 0;
408 # uncoverable condition false
409 30 ▶ length $_->[0] > $w and $w = length $_->[0] for @rows, @extra_rows;
Branch: 1 branch
BranchTrueFalseTotal
length $_->[0] > $w and $w = length $_->[0] 59 119 178
410 30 print {$fh} "\noptions:\n";
411 30 printf {$fh} " %-*s %s\n", $w, @{$_}[0, 1] for @rows;
412 30 ▶ if (@extra_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra_rows) 2 28 30
413 # uncoverable condition right
414 2 ▶ my $title = $extra[0]{section} // 'more options';
Condition100%1/2
$extra[0]{"section"} // "more options"
l!l
20
MC/DC100%0/1
$extra[0]{"section"} // "more options"
$extra[0]{"section"}
Truth table100%1/2
A
A= $extra[0]{"section"}
Aresult
11
00
415 2 print {$fh} "\n$title:\n";
416 2 printf {$fh} " %-*s %s\n", $w, @{$_}[0, 1] for @extra_rows;
417 }
418 30 ▶ if ($spec->{examples}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"examples"}) 13 17 30
419 13 print {$fh} "\nexamples:\n";
420 13 print {$fh} " $_\n" for @{ $spec->{examples} };
421 }
422 30 return;
423 }
424  
425 # ---- legacy public surface ---------------------------------------------------
426  
427 67 our @COMMANDS = qw(routes api doctor config console dev);
428  
429 1 ▶sub commands { grep { !$SPECS{$_}{hidden} } @ORDER }
Subroutine commands: called CC=1 SCAR=0.0
Pod documented
430  
431 # ---- punk routes -------------------------------------------------------------
432  
433 # The compiled routing table. Targets come back from the coderef through B,
434 # not from the declaration, so what is printed is what the route actually
435 # resolved to - which is the question you have when a route misbehaves.
436 25 ▶sub routes {
Subroutine routes: called CC=49 SCAR=39.2
Pod documented
437 25 my (%opt) = @_;
438 25 ▶ my $app = _load_app($opt{dir}) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $app = _load_app($opt{"dir"}) or return 1 19 6 25
439  
440 19 my @rows;
441 # uncoverable condition right
442 19 ▶ my $recs = eval { $app->{registrar}{router}->records } || [];
Condition100%1/2
eval { do { $app->{'registrar'}{'router'}->records } } || []
l!l
190
MC/DC100%0/1
eval { do { $app->{'registrar'}{'router'}->records } } || []
eval { do { $app->{'registrar'}{'router'}->records } }
Truth table100%1/2
A
A= eval { do { $app->{'registrar'}{'router'}->records } }
Aresult
11
00
443 19 for my $rec (@$recs) {
444 push @rows, {
445 method => $rec->{method} // '?',
446 path => $rec->{path} // '?',
447 target => _code_name($rec->{code}),
448 # uncoverable branch true
449 54 ▶ guards => scalar @{ $rec->{guards} || [] },
Condition50%1/2
$rec->{'guards'} || []
l!l
540
MC/DC0%0/1
$rec->{'guards'} || []
$rec->{'guards'}
Truth table50%1/2
A
A= $rec->{'guards'}
Aresult
11
00
450 name => $rec->{name},
451 # uncoverable condition right count:1
452 # uncoverable condition right count:2
453 54 ▶ kind => 'route',
Condition100%2/4
$rec->{"method"} // "?"
l!l
540
$rec->{"path"} // "?"
l!l
540
MC/DC100%0/1
$rec->{"method"} // "?"
$rec->{"method"}
MC/DC100%0/1
$rec->{"path"} // "?"
$rec->{"path"}
Truth table100%1/2
A
A= $rec->{"method"}
Aresult
11
00
Truth table100%1/2
A
A= $rec->{"path"}
Aresult
11
00
454 };
455 }
456  
457 # API operations live in their mount, not the router: the spec is the
458 # routing table for everything under the mount's prefix.
459 # uncoverable branch true
460 19 ▶ for my $m (@{ $app->{registrar}{api_mounts} || [] }) {
Condition50%1/2
$app->{'registrar'}{'api_mounts'} || []
l!l
190
MC/DC0%0/1
$app->{'registrar'}{'api_mounts'} || []
$app->{'registrar'}{'api_mounts'}
Truth table50%1/2
A
A= $app->{'registrar'}{'api_mounts'}
Aresult
11
00
461 # uncoverable condition right
462 3 ▶ my $prefix = $m->{prefix} // '';
Condition100%1/2
$m->{"prefix"} // ""
l!l
30
MC/DC100%0/1
$m->{"prefix"} // ""
$m->{"prefix"}
Truth table100%1/2
A
A= $m->{"prefix"}
Aresult
11
00
463 # uncoverable branch true
464 3 ▶ my $api = $m->{api} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $api = $m->{"api"} or next 3 0 3
465 # uncoverable condition right
466 3 ▶ my $ops = eval { $api->operations } || [];
Condition100%1/2
eval { do { $api->operations } } || []
l!l
30
MC/DC100%0/1
eval { do { $api->operations } } || []
eval { do { $api->operations } }
Truth table100%1/2
A
A= eval { do { $api->operations } }
Aresult
11
00
467 # uncoverable branch false
468 3 ▶ my $by_id = ref $m->{ops} eq 'HASH' ? $m->{ops} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $m->{'ops'} eq 'HASH' ? : 3 0 3
469 3 for my $op (sort { $a->{path} cmp $b->{path}
470 # uncoverable branch true
471 9 ▶ || $a->{method} cmp $b->{method} } @$ops) {
Condition66%2/3
$Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'} || $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
l!l&&r!l&&!r
630
MC/DC0%0/2
$Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'} || $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
$Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'} $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
Truth table66%2/3
A || B
A= $Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'}B= $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
ABresult
1X1
011
000
472 9 my $rec = $by_id->{ $op->{operationId} };
473 # uncoverable branch false
474 9 ▶ my $code = ref $rec eq 'HASH' ? $rec->{code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $rec eq 'HASH' ? : 9 0 9
475 push @rows, {
476 method => uc $op->{method},
477 path => $prefix . $op->{path},
478 target => (ref $code eq 'CODE'
479 ? _code_name($code) : $op->{operationId}),
480 guards => (ref $rec eq 'HASH'
481 # uncoverable branch true
482 9 ▶ ? scalar @{ $rec->{guards} || [] } : 0),
Condition50%1/2
$rec->{'guards'} || []
l!l
90
MC/DC0%0/1
$rec->{'guards'} || []
$rec->{'guards'}
Truth table50%1/2
A
A= $rec->{'guards'}
Aresult
11
00
483 # an operationId IS the name: it is what url_for takes, and
484 # it shares the one namespace with the route names
485 name => $op->{operationId},
486 # uncoverable branch false count:1
487 # uncoverable branch false count:2
488 9 ▶ kind => 'api',
Branch: 2 branches
BranchTrueFalseTotal
ref $code eq 'CODE' ? : 9 0 9
ref $rec eq 'HASH' ? : 9 0 9
489 };
490 }
491 }
492  
493 19 ▶ for my $w (@{ $app->{registrar}{ws_routes} || [] }) {
Condition100%2/2
$app->{'registrar'}{'ws_routes'} || []
l!l
910
MC/DC100%1/1
$app->{'registrar'}{'ws_routes'} || []
$app->{'registrar'}{'ws_routes'}
Truth table100%2/2
A
A= $app->{'registrar'}{'ws_routes'}
Aresult
11
00
494 push @rows, {
495 method => 'WS',
496 path => $w->{path} // '?',
497 target => (ref $w->{target} ? _code_name($w->{target})
498 : ($w->{target} // '?')),
499 # uncoverable branch true count:1
500 # uncoverable branch false count:2
501 # uncoverable condition right count:1
502 # uncoverable condition left count:2
503 9 ▶ guards => scalar @{ $w->{guards} || [] },
Branch: 1 branch
BranchTrueFalseTotal
ref $w->{'target'} ? : 0 9 9
Condition83%3/6
$w->{"path"} // "?"
l!l
90
$w->{"target"} // "?"
l!l
09
$w->{'guards'} || []
l!l
09
MC/DC100%0/1
$w->{"path"} // "?"
$w->{"path"}
MC/DC100%0/1
$w->{"target"} // "?"
$w->{"target"}
MC/DC0%0/1
$w->{'guards'} || []
$w->{'guards'}
Truth table100%1/2
A
A= $w->{"path"}
Aresult
11
00
Truth table100%1/2
A
A= $w->{"target"}
Aresult
11
00
Truth table50%1/2
A
A= $w->{'guards'}
Aresult
11
00
504 kind => 'ws',
505 };
506 }
507  
508 # uncoverable branch true
509 19 ▶ for my $m (@{ $app->{registrar}{mounts} || [] }) {
Condition50%1/2
$app->{'registrar'}{'mounts'} || []
l!l
190
MC/DC0%0/1
$app->{'registrar'}{'mounts'} || []
$app->{'registrar'}{'mounts'}
Truth table50%1/2
A
A= $app->{'registrar'}{'mounts'}
Aresult
11
00
510 push @rows, {
511 method => 'ANY',
512 path => ($m->{prefix} // '') . '/*',
513 target => $m->{dir} ? "static $m->{dir}"
514 : $m->{md_dir} ? "markdown $m->{md_dir}"
515 : 'mounted psgi app',
516 guards => 0,
517 # uncoverable branch true count:1
518 # uncoverable branch false count:1
519 # uncoverable branch false count:2
520 # uncoverable branch true count:3
521 # uncoverable condition right
522 17 ▶ kind => $m->{md_dir} ? 'markdown' : 'mount',
Branch: 3 branches
BranchTrueFalseTotal
$m->{'dir'} ? : 17 0 17
$m->{'md_dir'} ? : 0 0 0
$m->{'md_dir'} ? : 0 17 17
Condition100%1/2
$m->{"prefix"} // ""
l!l
170
MC/DC100%0/1
$m->{"prefix"} // ""
$m->{"prefix"}
Truth table100%1/2
A
A= $m->{"prefix"}
Aresult
11
00
523 };
524 }
525  
526 19 ▶ unless (@rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rows) 1 18 19
527 1 print "no routes\n";
528 1 return 0;
529 }
530  
531 18 ▶ if (defined $opt{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"method"}) 2 16 18
532 2 my $want = uc $opt{method};
533 2 @rows = grep { $_->{method} eq $want } @rows;
534 }
535 18 ▶ if (defined $opt{kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"kind"}) 1 17 18
536 1 @rows = grep { $_->{kind} eq $opt{kind} } @rows;
537 }
538 # exact, not a glob: a name is an identifier, and the question `--name`
539 # answers is "which route is this, that my template names?"
540 18 ▶ if (defined $opt{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"name"}) 4 14 18
541 4 ▶ @rows = grep { defined $_->{name} && $_->{name} eq $opt{name} } @rows;
Condition100%3/3
defined $_->{'name'} && $_->{'name'} eq $opt{'name'}
!ll&&!rl&&r
1263
MC/DC100%2/2
defined $_->{'name'} && $_->{'name'} eq $opt{'name'}
defined $_->{'name'} $_->{'name'} eq $opt{'name'}
Truth table100%3/3
A && B
A= defined $_->{'name'}B= $_->{'name'} eq $opt{'name'}
ABresult
0X0
100
111
542 }
543 18 ▶ if (defined $opt{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"path"}) 2 16 18
544 2 my $re = _glob_re($opt{path});
545 2 @rows = grep { $_->{path} =~ $re } @rows;
546 }
547 18 ▶ unless (@rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rows) 2 16 18
548 2 print "no matching routes\n";
549 2 return 0;
550 }
551  
552 # uncoverable branch true
553 30 ▶ @rows = sort { $a->{path} cmp $b->{path} || $a->{method} cmp $b->{method} }
Condition66%2/3
$Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'} || $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
l!l&&r!l&&!r
2820
MC/DC0%0/2
$Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'} || $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
$Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'} $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
Truth table66%2/3
A || B
A= $Punk::Command::a->{'path'} cmp $Punk::Command::b->{'path'}B= $Punk::Command::a->{'method'} cmp $Punk::Command::b->{'method'}
ABresult
1X1
011
000
554 16 ▶ @rows if $opt{sort};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"sort"} 2 14 16
555  
556 16 ▶ if ($opt{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"json"}) 1 15 16
557 1 require File::Raw::JSON;
558 1 print File::Raw::JSON::file_json_encode(\@rows), "\n";
559 1 return 0;
560 }
561  
562 15 my $w_m = _widest('method', \@rows, 6);
563 15 my $w_p = _widest('path', \@rows, 4);
564 # the NAME column appears only when a route has one, so a table from an
565 # application that names nothing prints exactly as it did before
566 15 my $named = grep { defined $_->{name} } @rows;
567 15 ▶ my $w_n = $named ? _widest('name', \@rows, 4) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$named ? : 10 5 15
568 15 ▶ printf "%-*s %-*s %s%s\n", $w_m, 'METHOD', $w_p, 'PATH',
Branch: 1 branch
BranchTrueFalseTotal
$named ? : 10 5 15
569 ($named ? sprintf('%-*s ', $w_n, 'NAME') : ''), 'TARGET';
570 15 for my $r (@rows) {
571 printf "%-*s %-*s %s%s%s\n", $w_m, $r->{method}, $w_p, $r->{path},
572 ($named ? sprintf('%-*s ', $w_n, $r->{name} // '') : ''),
573 $r->{target},
574 $r->{guards} ? " [$r->{guards} guard"
575 # uncoverable branch true count:2
576 # uncoverable branch false count:2
577 # uncoverable branch true count:3
578 49 ▶ . ($r->{guards} > 1 ? 's' : '') . ']' : '';
Branch: 3 branches
BranchTrueFalseTotal
$named ? : 43 6 49
$r->{'guards'} ? : 0 49 49
$r->{'guards'} > 1 ? : 0 0 0
Condition100%2/2
$r->{"name"} // ""
l!l
1726
MC/DC100%1/1
$r->{"name"} // ""
$r->{"name"}
Truth table100%2/2
A
A= $r->{"name"}
Aresult
11
00
579 }
580 15 ▶ printf "\n%d route%s\n", scalar @rows, @rows == 1 ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
@rows == 1 ? : 7 8 15
581 15 return 0;
582 }
583  
584 # a path glob for --path: * is one segment's worth, ** crosses slashes
585 2 ▶sub _glob_re {
Subroutine _glob_re: called CC=3 SCAR=11.0
586 2 my ($glob) = @_;
587 my $re = join '',
588 4 ▶ map { $_ eq '**' ? '.*' : $_ eq '*' ? '[^/]*' : quotemeta $_ }
Branch: 2 branches
BranchTrueFalseTotal
$_ eq '**' ? : 1 3 4
$_ eq '*' ? : 1 2 3
589 2 grep { length }
590 split /(\*\*|\*)/, $glob;
591 2 return qr/^$re$/;
592 }
593  
594 # ---- punk doctor -------------------------------------------------------------
595  
596 # Punk reaches four other distributions through C ABI tables resolved at
597 # runtime, each gated on the version it was compiled against. When those drift
598 # the symptom is a croak on a request path, a long way from the cause; this
599 # reports the whole picture at once.
600 5 ▶sub doctor {
Subroutine doctor: called CC=29 SCAR=33.7
Pod documented
601 5 my (%opt) = @_;
602 5 my $bad = 0;
603  
604 5 print "punk doctor\n\n";
605  
606 5 require Punk;
607 5 _line('Punk', $Punk::VERSION, 'ok');
608 5 _line('perl', sprintf('%vd', $^V), 'ok');
609  
610 5 print "\nprerequisites\n";
611 5 for my $mod (qw(Open::API Template::Stencil File::Raw::JSON File::Raw::XML
612 JSON::Schema::Fast YAML::XS Hyperman)) {
613 35 my $v = _module_version($mod);
614 # uncoverable branch true
615 35 ▶ $bad++ unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 35 35
616 # uncoverable branch false
617 # uncoverable condition right
618 35 ▶ _line($mod, $v // '-', defined $v ? 'ok' : 'MISSING');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 35 0 35
Condition100%1/2
$v // "-"
l!l
350
MC/DC100%0/1
$v // "-"
$v
Truth table100%1/2
A
A= $v
Aresult
11
00
619 }
620  
621 5 print "\noptional\n";
622 5 for my $mod (qw(DBI Future Open::API::UI Plack)) {
623 20 my $v = _module_version($mod);
624 # uncoverable branch false
625 # uncoverable condition right
626 20 ▶ _line($mod, $v // '-', defined $v ? 'ok' : 'not installed');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 20 0 20
Condition100%1/2
$v // "-"
l!l
200
MC/DC100%0/1
$v // "-"
$v
Truth table100%1/2
A
A= $v
Aresult
11
00
627 }
628 { # a database backend is only useful with a driver behind it
629 5 my @drv = eval { require DBI; DBI->available_drivers } ;
630 # uncoverable branch false count:1
631 # uncoverable branch false count:2
632 # uncoverable branch false count:3
633 # uncoverable condition right
634 # uncoverable condition false
635 5 ▶ _line('DBD drivers', (@drv ? join(' ', sort @drv) : '-'),
Branch: 3 branches
BranchTrueFalseTotal
if @drv or not $@ 5 0 5
@drv ? : 5 0 5
@drv ? : 5 0 5
Condition100%1/3
@drv or not $@
l!l&&r!l&&!r
500
MC/DC100%0/2
@drv or not $@
@drv not $@
Truth table100%1/3
A or not(B)
A= @drvB= not $@
ABresult
1X1
011
000
636 @drv ? 'ok' : 'none') if @drv || !$@;
637 }
638  
639 # The part that cannot be read off a version number: whether each table
640 # resolved, and at which version.
641 5 print "\nC ABIs\n";
642 5 for my $abi (_abi_report()) {
643 # uncoverable branch true
644 40 ▶ $bad++ if $abi->{state} ne 'ok';
Branch: 1 branch
BranchTrueFalseTotal
if $abi->{"state"} ne "ok" 0 40 40
645 40 _line($abi->{name}, $abi->{detail}, $abi->{state});
646 }
647  
648 # Load every Punk::Command::* on @INC first.
649 #
650 # A plugin's command module registers at load, and nothing loads it until
651 # somebody types its name - which is right for `punk dev`, where the cost
652 # of scanning would be paid on every invocation, and wrong here. `doctor`
653 # is the command whose whole job is "what have I got installed", and a
654 # row that only appears when you were already running that plugin's
655 # command is a row nobody will ever see when they need it.
656 #
657 # A module that dies on load is skipped rather than fatal: doctor is what
658 # somebody runs when something is already broken.
659 5 _load_command_plugins();
660  
661 # rows other distributions registered (register_doctor) - a probe returns
662 # (ok, detail); a die is a failed row, not a failed doctor
663 # uncoverable branch false
664 5 ▶ if (@DOCTOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Punk::Command::DOCTOR) 5 0 5
665 5 print "\nplugins\n";
666 5 for my $row (@DOCTOR) {
667 6 my ($label, $probe) = @$row;
668 6 my ($ok, $detail) = eval { $probe->() };
669 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 5 6
670 1 my $e = $@; $e =~ s/ at \S+ line \d+\.?\s*\z//; $e =~ s/\s+\z//;
671 1 ($ok, $detail) = (0, $e);
672 }
673 6 ▶ $bad++ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 5 6
674 # uncoverable condition right
675 6 ▶ _line($label, $detail // '-', $ok ? 'ok' : 'FAILED');
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 5 1 6
Condition100%1/2
$detail // "-"
l!l
60
MC/DC100%0/1
$detail // "-"
$detail
Truth table100%1/2
A
A= $detail
Aresult
11
00
676 }
677 }
678  
679 5 ▶ if (my $app = _load_app($opt{dir}, quiet => 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $app = _load_app($opt{"dir"}, "quiet", 1)) 1 4 5
680 1 print "\napplication\n";
681 1 _line('class', $app->{class}, 'ok');
682 1 _line('root', $app->{root}, 'ok');
683 # uncoverable condition right
684 1 ▶ my $n = eval { scalar @{ $app->{registrar}{router}->records } } || 0;
Condition100%1/2
eval { do { scalar @{$app->{'registrar'}{'router'}->records;} } } || 0
l!l
10
MC/DC100%0/1
eval { do { scalar @{$app->{'registrar'}{'router'}->records;} } } || 0
eval { do { scalar @{$app->{'registrar'}{'router'}->records;} } }
Truth table100%1/2
A
A= eval { do { scalar @{$app->{'registrar'}{'router'}->records;} } }
Aresult
11
00
685 # uncoverable branch false
686 1 ▶ _line('routes', $n, $n ? 'ok' : 'none');
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 1 0 1
687 # uncoverable branch false
688 1 ▶ if (my $cfg = $app->{registrar}{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cfg = $app->{"registrar"}{"config"}) 1 0 1
689 1 _line('config', join(' ', @{ $cfg->files }), 'ok');
690 # uncoverable condition right
691 1 ▶ my $s = eval { $cfg->secret_paths } || [];
Condition100%1/2
eval { do { $cfg->secret_paths } } || []
l!l
10
MC/DC100%0/1
eval { do { $cfg->secret_paths } } || []
eval { do { $cfg->secret_paths } }
Truth table100%1/2
A
A= eval { do { $cfg->secret_paths } }
Aresult
11
00
692 # uncoverable branch true
693 1 ▶ _line('secrets', (@$s ? scalar(@$s) . ' resolved' : 'none'), 'ok');
Branch: 1 branch
BranchTrueFalseTotal
@$s ? : 0 1 1
694 }
695 }
696  
697 # uncoverable branch true count:1
698 5 ▶ print "\n", $bad ? "$bad problem" . ($bad > 1 ? 's' : '') . " found\n"
Branch: 2 branches
BranchTrueFalseTotal
$bad ? : 1 4 5
$bad > 1 ? : 0 1 1
699 : "no problems found\n";
700 5 ▶ return $bad ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bad ? : 1 4 5
701 }
702  
703 # ---- punk config check -------------------------------------------------------
704  
705 # Resolving configuration is fatal by design - a missing secret should stop a
706 # deployment, not produce an empty password. That makes it a thing worth
707 # testing before the deployment: this walks every layer for every secret
708 # reference and reports all of them, rather than dying on the first.
709 14 ▶sub config_check {
Subroutine config_check: called CC=24 SCAR=31.8
Pod documented
710 14 my (%opt) = @_;
711 # uncoverable condition left
712 14 ▶ my $root = $opt{root} || _find_root($opt{dir}) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $root = $opt{"root"} || _find_root($opt{"dir"}) or do { print STDERR 'punk config: no application found (looked for app.psgi upwards from ' . ($opt{"dir"} || Cwd::getcwd()) . ")\n"; return 2 } 12 2 14
Condition100%2/3
$opt{'root'} || _find_root($opt{'dir'})
l!l&&r!l&&!r
0122
MC/DC100%1/2
$opt{'root'} || _find_root($opt{'dir'})
$opt{'root'} _find_root($opt{'dir'})
Truth table100%2/3
A || B
A= $opt{'root'}B= _find_root($opt{'dir'})
ABresult
1X1
011
000
713 print STDERR "punk config: no application found "
714 . "(looked for app.psgi upwards from "
715 # uncoverable condition false
716 2 ▶ . ($opt{dir} || Cwd::getcwd()) . ")\n";
Condition100%2/3
$opt{'dir'} || Cwd::getcwd()
l!l&&r!l&&!r
110
MC/DC100%0/2
$opt{'dir'} || Cwd::getcwd()
$opt{'dir'} Cwd::getcwd()
Truth table100%2/3
A || B
A= $opt{'dir'}B= Cwd::getcwd()
ABresult
1X1
011
000
717 2 return 2;
718 };
719 # uncoverable condition left
720 # uncoverable condition false
721 12 ▶ my $file = $opt{file} || File::Spec->catfile($root, 'config', 'punk.yml');
Condition100%1/3
$opt{'file'} || 'File::Spec'->catfile($root, 'config', 'punk.yml')
l!l&&r!l&&!r
0120
MC/DC100%0/2
$opt{'file'} || 'File::Spec'->catfile($root, 'config', 'punk.yml')
$opt{'file'} 'File::Spec'->catfile($root, 'config', 'punk.yml')
Truth table100%1/3
A || B
A= $opt{'file'}B= 'File::Spec'->catfile($root, 'config', 'punk.yml')
ABresult
1X1
011
000
722 # uncoverable condition left
723 12 ▶ my $env = $opt{env} || $ENV{PUNK_ENV} || 'production';
Condition60%4/5
$opt{'env'} || $ENV{'PUNK_ENV'} || 'production'
l!l
75
$opt{"env"} or $ENV{"PUNK_ENV"}
l!l&&r!l&&!r
705
MC/DC100%1/2
$opt{'env'} || $ENV{'PUNK_ENV'} || 'production'
$opt{"env"} $ENV{"PUNK_ENV"}
Truth table66%2/3
A or B
A= $opt{"env"}B= $ENV{"PUNK_ENV"}
ABresult
1X1
011
000
724  
725 12 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 1 11 12
726 1 print STDERR "punk config: no such file: $file\n";
727 1 return 2;
728 }
729  
730 11 printf "config %s\nenv %s\n\n", $file, $env;
731  
732 11 my @layers = _layer_files($file, $env);
733 11 my @present = grep { -f $_->[1] } @layers;
734 11 print "layers\n";
735 11 for my $l (@layers) {
736 33 ▶ _line($l->[0], $l->[1], -f $l->[1] ? 'ok' : 'absent');
Branch: 1 branch
BranchTrueFalseTotal
-f $l->[1] ? : 13 20 33
737 }
738  
739 # uncoverable branch true
740 11 ▶ unless (@present) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@present) 0 11 11
741 # uncoverable statement
742 0 print STDERR "\nno layer files exist\n";
743 # uncoverable statement
744 0 return 1;
745 }
746  
747 # Every secret reference in every layer, resolved independently so one
748 # failure does not hide the rest.
749 11 my @refs;
750 11 for my $l (@present) {
751 13 my $doc = eval { _yaml_file($l->[1]) };
752 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 12 13
753 1 print "\n", _fmt('', $l->[1], 'UNPARSEABLE'), " $@";
754 1 return 1;
755 }
756 12 ▶ _walk_secrets($doc, '', sub { push @refs, { %{ $_[0] }, layer => $l->[1] } });
Subroutine __ANON__: called CC=1 SCAR=0.0
757 }
758  
759 10 my $bad = 0;
760 10 ▶ if (@refs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@refs) { } 6 4 10
761 6 print "\nsecrets\n";
762 6 for my $r (@refs) {
763 9 my ($ok, $detail) = _try_resolver($r->{kind}, $r->{value});
764 9 ▶ $bad++ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 6 3 9
765 9 ▶ _line($r->{path}, "$r->{kind}: $detail", $ok ? 'ok' : 'FAILED');
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 3 6 9
766 }
767 }
768 else {
769 4 print "\nno secret references\n";
770 }
771  
772 # Then the authoritative pass, which also applies the guardrail.
773 10 print "\nload\n";
774 10 require Punk::Config;
775 10 my $cfg = eval {
776 10 local $ENV{PUNK_ENV} = $env;
777 Punk::Config->load(file => $file, env => $env,
778 # uncoverable branch true
779 10 ▶ ($opt{secrets} ? (secrets => $opt{secrets}) : ()));
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$opt{'secrets'} ? : 0 10 10
780 };
781 10 ▶ if (!$cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cfg) 4 6 10
782 # uncoverable condition right
783 4 ▶ my $err = $@ || 'failed';
Condition100%1/2
$@ || 'failed'
l!l
40
MC/DC100%0/1
$@ || 'failed'
$@
Truth table100%1/2
A
A= $@
Aresult
11
00
784 4 $err =~ s/ at \S+ line \d+\.?\s*\z//;
785 4 _line('Punk::Config->load', $err, 'FAILED');
786 4 return 1;
787 }
788 6 _line('Punk::Config->load', 'ok', 'ok');
789  
790 6 ▶ if ($opt{dump}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"dump"}) 3 3 6
791 3 print "\nresolved (secrets redacted)\n";
792 3 require Data::Dumper;
793 3 my $d = Data::Dumper->new([ _redact_inline($cfg->config) ]);
794 3 $d->Sortkeys(1)->Indent(1)->Terse(1);
795 3 print $d->Dump;
796 }
797  
798 # uncoverable branch true count:1
799 # uncoverable branch false count:1
800 # uncoverable branch true count:2
801 6 ▶ print "\n", $bad ? "$bad secret" . ($bad > 1 ? 's' : '')
Branch: 2 branches
BranchTrueFalseTotal
$bad ? : 0 6 6
$bad > 1 ? : 0 0 0
802 . " could not be resolved\n" : "configuration is sound\n";
803 # uncoverable branch true
804 6 ▶ return $bad ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bad ? : 0 6 6
805 }
806  
807 # A copy of the resolved configuration with every value whose KEY says it is
808 # a credential replaced. A reference such as { $env: ... } is already redacted
809 # by the loader; a password somebody typed straight into the file is not, and
810 # a dump is the thing that gets pasted into a ticket.
811 39 ▶sub _redact_inline {
Subroutine _redact_inline: called CC=7 SCAR=19.5
812 39 my ($v, $hide) = @_;
813 39 ▶ if (ref $v eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 23 16 39
814 return { map {
815 23 my $k = $_;
816 # uncoverable condition left
817 # uncoverable condition false
818 34 ▶ ($k => _redact_inline($v->{$k},
Condition100%1/3
$hide || $k =~ /pass|secret|token|api_?key|private/i
l!l&&r!l&&!r
0230
MC/DC100%0/2
$hide || $k =~ /pass|secret|token|api_?key|private/i
$hide $k =~ /pass|secret|token|api_?key|private/i
Truth table100%1/3
A || B
A= $hideB= $k =~ /pass|secret|token|api_?key|private/i
ABresult
1X1
011
000
819 $hide || $k =~ /pass|secret|token|api_?key|private/i))
820 } keys %$v };
821 }
822 16 ▶ return [ map { _redact_inline($_, $hide) } @$v ] if ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 1 15 16
823 # uncoverable condition left count:1
824 # uncoverable condition right count:1
825 # uncoverable condition left count:2
826 15 ▶ return $v if ref $v || !defined $v || !$hide;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v or not defined $v or not $hide 9 6 15
Condition66%3/6
ref $v or not defined $v or not $hide
l!l&&r!l&&!r
096
ref $v or not defined $v
l!l&&r!l&&!r
0015
MC/DC100%1/3
ref $v or not defined $v or not $hide
ref $v not defined $v not $hide
Truth table75%2/4
A or not(B) or not(C)
A= ref $vB= not defined $vC= not $hide
ABCresult
1XX1
01X1
0011
0000
827 6 return '[redacted]';
828 }
829  
830 # ---- punk api sync -----------------------------------------------------------
831  
832 # A spec-first application whose spec has moved on. Adds a stub for every
833 # operation the document declares and the controllers do not implement, and
834 # never touches one that is already there - so this is the command that makes
835 # `punk new --api` a workflow rather than a one-shot.
836 15 ▶sub api_sync {
Subroutine api_sync: called CC=45 SCAR=38.1
Pod documented
837 15 my (%opt) = @_;
838 # uncoverable condition left
839 15 ▶ my $root = $opt{root} || _find_root($opt{dir}) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $root = $opt{"root"} || _find_root($opt{"dir"}) or do { print STDERR "punk api: no application found\n"; return 2 } 13 2 15
Condition100%2/3
$opt{'root'} || _find_root($opt{'dir'})
l!l&&r!l&&!r
0132
MC/DC100%1/2
$opt{'root'} || _find_root($opt{'dir'})
$opt{'root'} _find_root($opt{'dir'})
Truth table100%2/3
A || B
A= $opt{'root'}B= _find_root($opt{'dir'})
ABresult
1X1
011
000
840 2 print STDERR "punk api: no application found\n";
841 2 return 2;
842 };
843 13 ▶ my $class = _class_of($root) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $class = _class_of($root) or do { print STDERR "punk api: cannot work out the application class in $root\n"; return 2 } 12 1 13
844 1 print STDERR "punk api: cannot work out the application class in $root\n";
845 1 return 2;
846 };
847 # uncoverable condition false
848 12 ▶ my $spec = $opt{spec} || File::Spec->catfile($root, 'openapi.json');
Condition100%2/3
$opt{'spec'} || 'File::Spec'->catfile($root, 'openapi.json')
l!l&&r!l&&!r
2100
MC/DC100%0/2
$opt{'spec'} || 'File::Spec'->catfile($root, 'openapi.json')
$opt{'spec'} 'File::Spec'->catfile($root, 'openapi.json')
Truth table100%2/3
A || B
A= $opt{'spec'}B= 'File::Spec'->catfile($root, 'openapi.json')
ABresult
1X1
011
000
849 12 ▶ unless (-f $spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $spec) 1 11 12
850 1 print STDERR "punk api: no such spec: $spec\n";
851 1 return 2;
852 }
853  
854 11 require Punk::Generate;
855 11 ▶ my $plan = eval { Punk::Generate->api_plan($spec) } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $plan = eval { do { "Punk::Generate"->api_plan($spec) } } or do { my $err = $@; $err =~ s/ at \S+ line \d+\.?\s*\z//; print STDERR "punk api: $err\n"; return 1 } 10 1 11
856 1 my $err = $@; $err =~ s/ at \S+ line \d+\.?\s*\z//;
857 1 print STDERR "punk api: $err\n";
858 1 return 1;
859 };
860  
861 10 (my $path = $class) =~ s{::}{/}g;
862 10 my $ctrl_dir = File::Spec->catdir($root, 'lib', $path, 'Controller', 'API');
863 # the checker class holds security checkers, not operations - counting its
864 # subs would make every checker look like an operation the spec dropped
865 10 my $auth = $plan->{auth_class};
866 10 ▶ my %implemented = _implemented_ops($ctrl_dir,
Branch: 1 branch
BranchTrueFalseTotal
$auth ? : 6 4 10
867 ($auth ? (skip => qr/\A\Q$auth\E\.pm\z/) : ()));
868  
869 10 my (@added, @created, @orphans);
870 10 my %declared;
871  
872 10 for my $group (@{ $plan->{groups} }) {
873 22 my @new = grep { !$implemented{ $_->{id} } } @{ $group->{ops} };
874 22 $declared{ $_->{id} } = 1 for @{ $group->{ops} };
875 22 ▶ next unless @new;
Branch: 1 branch
BranchTrueFalseTotal
unless @new 14 8 22
876  
877 # Where a new operation's siblings already live wins over where a
878 # fresh generation would have put it. Adding a tag to a spec changes
879 # how untagged operations group, and a sync should not scatter an
880 # operation away from the others on its own path because of that.
881 8 my $file = File::Spec->catfile($ctrl_dir, "$group->{group}.pm");
882 # Only for operations that fell through to Default - a tag the author
883 # wrote is a placement decision, and it outranks any guess made here.
884 # uncoverable condition right count:1
885 8 ▶ if (!-f $file && !length($group->{group_label} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file or length($group->{"group_label"} // "")) 3 5 8
Condition60%4/5
-f $file or length($group->{"group_label"} // "")
l!l&&r!l&&!r
323
$group->{"group_label"} // ""
l!l
50
MC/DC50%0/2
-f $file or length($group->{"group_label"} // "")
-f $file $group->{"group_label"}
Truth table33%2/3
A or B
A= -f $fileB= $group->{"group_label"}
ABresult
1X1
011
000
886 3 my %by_home;
887 3 for my $op (@new) {
888 4 my ($seg) = grep { length } split m{/}, $op->{path};
889 # uncoverable condition right
890 4 ▶ my $home = Punk::Generate->ident($seg // '');
Condition100%1/2
$seg // ""
l!l
40
MC/DC100%0/1
$seg // ""
$seg
Truth table100%1/2
A
A= $seg
Aresult
11
00
891 # uncoverable branch false
892 4 ▶ my $f = length $home
Branch: 1 branch
BranchTrueFalseTotal
length $home ? : 4 0 4
893 ? File::Spec->catfile($ctrl_dir, "$home.pm") : '';
894 # uncoverable condition left
895 4 ▶ push @{ $by_home{ (length $f && -f $f) ? $f : $file } }, $op;
Branch: 1 branch
BranchTrueFalseTotal
length $f && -f $f ? : 3 1 4
Condition100%2/3
length $f and -f $f
!ll&&!rl&&r
013
MC/DC100%1/2
length $f and -f $f
length $f -f $f
Truth table100%2/3
A and B
A= length $fB= -f $f
ABresult
0X0
100
111
896 }
897 # uncoverable branch false
898 # uncoverable condition false
899 3 ▶ if (keys %by_home > 1 || !exists $by_home{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %by_home > 1 or not exists $by_home{$file}) 3 0 3
Condition100%2/3
keys %by_home > 1 or not exists $by_home{$file}
l!l&&r!l&&!r
120
MC/DC100%0/2
keys %by_home > 1 or not exists $by_home{$file}
keys %by_home > 1 not exists $by_home{$file}
Truth table100%2/3
A or not(B)
A= keys %by_home > 1B= not exists $by_home{$file}
ABresult
1X1
011
000
900 3 for my $f (sort keys %by_home) {
901 4 my $ops = $by_home{$f};
902 4 my $base = File::Basename::basename($f);
903 4 ▶ if (-f $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $f) { } 3 1 4
904 3 _insert_stubs($f, $ops, $opt{dry_run});
905 }
906 else {
907 Punk::Generate->write_skel('controller_api.tmpl', $f,
908 { name => $class, path => $path, api => 1,
909 # uncoverable branch false
910 1 ▶ %$group, ops => $ops }) unless $opt{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"dry_run"} 1 0 1
911 1 my $stem = $base;
912 1 $stem =~ s/\.pm\z//; # s///r is 5.14
913 1 push @created, [ $stem, scalar @$ops ];
914 }
915 4 push @added, map { [ $_->{id}, $base ] } @$ops;
916 }
917 3 next;
918 }
919 }
920 5 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 3 2 5
921 3 _insert_stubs($file, \@new, $opt{dry_run});
922 3 push @added, map { [ $_->{id}, "$group->{group}.pm" ] } @new;
923 }
924 else {
925 2 ▶ unless ($opt{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"dry_run"}) 1 1 2
926 1 Punk::Generate->write_skel('controller_api.tmpl', $file,
927 { name => $class, path => $path, api => 1,
928 %$group, ops => \@new });
929 }
930 2 push @created, [ $group->{group}, scalar @new ];
931 2 push @added, map { [ $_->{id}, "$group->{group}.pm" ] } @new;
932 }
933 }
934  
935 # Checkers for schemes the spec has started requiring since.
936 10 my $auth_file;
937 10 ▶ if (@{ $plan->{schemes} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$plan->{"schemes"};}) 6 4 10
938 6 $auth_file = File::Spec->catfile($ctrl_dir, "$plan->{auth_class}.pm");
939 6 ▶ unless (-f $auth_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $auth_file) 2 4 6
940 2 ▶ unless ($opt{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"dry_run"}) 1 1 2
941 Punk::Generate->write_skel('controller_auth.tmpl', $auth_file,
942 { name => $class, path => $path, api => 1,
943 auth_class => $plan->{auth_class},
944 1 schemes => $plan->{schemes} });
945 }
946 push @created, [ $plan->{auth_class},
947 2 scalar @{ $plan->{schemes} } ];
948 }
949 }
950  
951 10 @orphans = sort grep { !$declared{$_} } keys %implemented;
952  
953 10 ▶ my $pre = $opt{dry_run} ? 'would add' : 'added';
Branch: 1 branch
BranchTrueFalseTotal
$opt{'dry_run'} ? : 2 8 10
954 10 ▶ if (@added) {
Branch: 1 branch
BranchTrueFalseTotal
if (@added) { } 6 4 10
955 6 ▶ printf "%s %d stub%s\n", $pre, scalar @added, @added == 1 ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
@added == 1 ? : 3 3 6
956 6 printf " %-28s %s\n", $_->[0], $_->[1] for @added;
957 }
958 else {
959 4 print "every operation in the spec is implemented\n";
960 }
961 10 ▶ if (@created) {
Branch: 1 branch
BranchTrueFalseTotal
if (@created) 3 7 10
962 3 ▶ printf "\n%s %d file%s\n", ($opt{dry_run} ? 'would create' : 'created'),
Branch: 2 branches
BranchTrueFalseTotal
$opt{'dry_run'} ? : 1 2 3
@created == 1 ? : 1 2 3
963 scalar @created, @created == 1 ? '' : 's';
964 3 printf " %s.pm (%d)\n", @$_ for @created;
965 }
966 10 ▶ if (@orphans) {
Branch: 1 branch
BranchTrueFalseTotal
if (@orphans) 4 6 10
967 # Reported, never removed: a sub the spec no longer declares may still
968 # be wanted, and deleting someone's code is not this command's job.
969 # uncoverable branch false
970 4 ▶ printf "\n%d method%s with no operation in the spec (left alone)\n",
Branch: 1 branch
BranchTrueFalseTotal
@orphans == 1 ? : 4 0 4
971 scalar @orphans, @orphans == 1 ? '' : 's';
972 4 print " $_\n" for @orphans;
973 }
974 # uncoverable condition right count:2
975 10 ▶ if ($auth_file && -f $auth_file && @{ $plan->{schemes} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_file and -f $auth_file and @{$plan->{"schemes"};}) 5 5 10
Condition66%5/6
$auth_file and -f $auth_file and @{$plan->{"schemes"};}
!ll&&!rl&&r
505
$auth_file and -f $auth_file
!ll&&!rl&&r
415
MC/DC66%2/3
$auth_file and -f $auth_file and @{$plan->{"schemes"};}
$auth_file -f $auth_file @{$plan->{"schemes"};}
Truth table50%3/4
A and B and C
A= $auth_fileB= -f $auth_fileC= @{$plan->{"schemes"};}
ABCresult
0XX0
10X0
1100
1111
976 5 my %have = _implemented_ops($ctrl_dir,
977 only => qr/\A\Q$plan->{auth_class}\E\.pm\z/);
978 5 my @miss = grep { !$have{ $_->{method} } } @{ $plan->{schemes} };
979 printf "\n%d security checker%s missing from %s.pm: %s\n",
980 scalar @miss, @miss == 1 ? '' : 's', $plan->{auth_class},
981 # uncoverable statement count:2
982 # uncoverable branch true count:1
983 # uncoverable branch false count:1
984 # uncoverable branch true count:2
985 0 ▶ join(', ', map { $_->{method} } @miss) if @miss;
Branch: 2 branches
BranchTrueFalseTotal
if @miss 0 5 5
@miss == 1 ? : 0 0 0
986 }
987 10 return 0;
988 }
989  
990 # ---- punk console ------------------------------------------------------------
991  
992 5 ▶sub console {
Subroutine console: called CC=20 SCAR=30.1
Pod documented
993 5 my (%opt) = @_;
994 # uncoverable branch true
995 5 ▶ my $app = _load_app($opt{dir}) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $app = _load_app($opt{"dir"}) or return 1 5 0 5
996  
997 # Named so the evals below can see them by name rather than by closure.
998 5 our $app_ = $app->{registrar};
999 5 our $psgi_ = $app->{psgi};
1000 5 our $ctx_ = _console_context($app);
1001  
1002 # --eval: one expression, its dump, and out - the scripting form.
1003 5 ▶ if (defined $opt{eval}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"eval"}) 3 2 5
1004 3 my @out = eval _console_wrap($opt{eval});
1005 3 ▶ if ($@) { print STDERR "!! $@"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
1006 2 ▶ print _console_dump(@out) if @out;
Branch: 1 branch
BranchTrueFalseTotal
if @out 1 1 2
1007 2 return 0;
1008 }
1009  
1010 2 my $class = $app->{class};
1011 2 print "punk console - $class\n"
1012 . " \$app the Punk::App registrar\n"
1013 . " \$psgi the compiled PSGI coderef\n"
1014 . " \$c a throwaway context (\$c->model, \$c->render, ...)\n"
1015 . " q, exit or ctrl-d to leave\n\n";
1016  
1017 # Term::ReadLine::Stub reads nothing from a pipe, so it is for terminals
1018 # only; falling back to STDIN keeps the console scriptable.
1019 2 my $tty = -t STDIN;
1020 # uncoverable statement count:2
1021 # uncoverable branch true
1022 0 ▶ my $term = $tty ? eval { require Term::ReadLine;
Branch: 1 branch
BranchTrueFalseTotal
$tty ? : 0 2 2
1023 # uncoverable statement
1024 0 Term::ReadLine->new('punk') } : undef;
1025  
1026 # History survives sessions when the reader supports it - only on a
1027 # terminal, so piped input never rewrites the file.
1028 my $hist = $ENV{PUNK_HISTFILE}
1029 # uncoverable condition right count:1
1030 # uncoverable condition left count:2
1031 # uncoverable condition false count:2
1032 2 ▶ // File::Spec->catfile($ENV{HOME} || '.', '.punk_history');
Condition0%2/5
$ENV{"PUNK_HISTFILE"} // "File::Spec"->catfile($ENV{"HOME"} || ".", ".punk_history")
l!l&&r!l&&!r
020
$ENV{'HOME'} || '.'
l!l
20
MC/DC50%0/2
$ENV{"PUNK_HISTFILE"} // "File::Spec"->catfile($ENV{"HOME"} || ".", ".punk_history")
$ENV{"PUNK_HISTFILE"} "File::Spec"->catfile($ENV{"HOME"} || ".", ".punk_history")
MC/DC100%0/1
$ENV{'HOME'} || '.'
$ENV{'HOME'}
Truth table0%1/3
A // B
A= $ENV{"PUNK_HISTFILE"}B= "File::Spec"->catfile($ENV{"HOME"} || ".", ".punk_history")
ABresult
1X1
011
000
Truth table0%1/2
A
A= $ENV{'HOME'}
Aresult
11
00
1033 # uncoverable branch true
1034 # uncoverable condition right
1035 # uncoverable condition false
1036 2 ▶ if ($term && $term->can('ReadHistory')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term and $term->can("ReadHistory")) 0 2 2
Condition100%1/3
$term and $term->can("ReadHistory")
!ll&&!rl&&r
200
MC/DC100%0/2
$term and $term->can("ReadHistory")
$term $term->can("ReadHistory")
Truth table100%1/3
A and B
A= $termB= $term->can("ReadHistory")
ABresult
0X0
100
111
1037 # uncoverable statement
1038 # uncoverable branch true
1039 # uncoverable branch false
1040 0 ▶ $term->ReadHistory($hist) if -f $hist;
Branch: 1 branch
BranchTrueFalseTotal
if -f $hist 0 0 0
1041 }
1042  
1043 2 while (1) {
1044 # uncoverable branch true
1045 5 ▶ my $line = $term ? $term->readline('punk> ') : do {
Branch: 1 branch
BranchTrueFalseTotal
$term ? : 0 5 5
1046 # uncoverable branch true
1047 5 ▶ print 'punk> ' if $tty;
Branch: 1 branch
BranchTrueFalseTotal
if $tty 0 5 5
1048 5 my $l = <STDIN>;
1049 5 $l;
1050 };
1051 5 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 3 5
1052 3 chomp $line;
1053 # uncoverable branch true
1054 3 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 3 3
1055 # uncoverable branch true
1056 3 ▶ last if $line =~ /^\s*(?:q|quit|exit)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(?:q|quit|exit)\s*$/ 0 3 3
1057 3 my @out = eval _console_wrap($line);
1058 3 ▶ if ($@) { print "!! $@"; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
1059 # uncoverable branch true
1060 2 ▶ next unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 0 2 2
1061 2 print _console_dump(@out);
1062 }
1063 # uncoverable branch true
1064 # uncoverable condition right
1065 # uncoverable condition false
1066 2 ▶ if ($term && $term->can('WriteHistory')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term and $term->can("WriteHistory")) 0 2 2
Condition100%1/3
$term and $term->can("WriteHistory")
!ll&&!rl&&r
200
MC/DC100%0/2
$term and $term->can("WriteHistory")
$term $term->can("WriteHistory")
Truth table100%1/3
A and B
A= $termB= $term->can("WriteHistory")
ABresult
0X0
100
111
1067 # uncoverable statement count:1
1068 # uncoverable statement count:2
1069 0 eval { $term->WriteHistory($hist) };
1070 }
1071 2 print "\n";
1072 2 return 0;
1073 }
1074  
1075 # ---- punk dev ----------------------------------------------------------------
1076  
1077 # The application compiles at boot, which is exactly why a dev server has to
1078 # restart rather than reload: there is no live structure to patch. The server
1079 # runs in a child, the parent watches for changes and replaces it.
1080 15 ▶sub dev {
Subroutine dev: called CC=32 SCAR=34.7
Pod documented
1081 15 my (%opt) = @_;
1082 # uncoverable condition left
1083 15 ▶ my $root = $opt{root} || _find_root($opt{dir}) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $root = $opt{"root"} || _find_root($opt{"dir"}) or do { print STDERR "punk dev: no application found\n"; return 2 } 7 8 15
Condition100%2/3
$opt{'root'} || _find_root($opt{'dir'})
l!l&&r!l&&!r
078
MC/DC100%1/2
$opt{'root'} || _find_root($opt{'dir'})
$opt{'root'} _find_root($opt{'dir'})
Truth table100%2/3
A || B
A= $opt{'root'}B= _find_root($opt{'dir'})
ABresult
1X1
011
000
1084 8 print STDERR "punk dev: no application found\n";
1085 8 return 2;
1086 };
1087 7 my $psgi = File::Spec->catfile($root, 'app.psgi');
1088 # uncoverable condition right
1089 7 ▶ my $port = $opt{port} || 5000;
Condition100%1/2
$opt{'port'} || 5000
l!l
70
MC/DC100%0/1
$opt{'port'} || 5000
$opt{'port'}
Truth table100%1/2
A
A= $opt{'port'}
Aresult
11
00
1090 # uncoverable condition right
1091 7 ▶ my $host = $opt{host} || '127.0.0.1';
Condition100%1/2
$opt{'host'} || '127.0.0.1'
l!l
70
MC/DC100%0/1
$opt{'host'} || '127.0.0.1'
$opt{'host'}
Truth table100%1/2
A
A= $opt{'host'}
Aresult
11
00
1092 # uncoverable condition right
1093 7 ▶ my $workers = $opt{workers} || 1;
Condition100%1/2
$opt{'workers'} || 1
l!l
70
MC/DC100%0/1
$opt{'workers'} || 1
$opt{'workers'}
Truth table100%1/2
A
A= $opt{'workers'}
Aresult
11
00
1094  
1095 # uncoverable branch true
1096 7 ▶ unless (eval { require Hyperman; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Hyperman; 1 } }) 0 7 7
1097 # uncoverable statement
1098 0 print STDERR "punk dev: Hyperman is not installed\n";
1099 # uncoverable statement
1100 0 return 1;
1101 }
1102  
1103 # uncoverable branch true
1104 7 ▶ my @watch_rel = @{ $opt{watch} || [] };
Condition50%1/2
$opt{'watch'} || []
l!l
70
MC/DC0%0/1
$opt{'watch'} || []
$opt{'watch'}
Truth table50%1/2
A
A= $opt{'watch'}
Aresult
11
00
1105 7 ▶ @watch_rel = qw(lib config root/templates) unless @watch_rel;
Branch: 1 branch
BranchTrueFalseTotal
unless @watch_rel 5 2 7
1106 17 my @watch = grep { -d $_ }
1107 # uncoverable branch true
1108 7 ▶ map { File::Spec->file_name_is_absolute($_)
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($_) ? : 0 17 17
1109 ? $_ : File::Spec->catdir($root, $_) } @watch_rel;
1110 7 printf "punk dev http://%s:%d\n", $host, $port;
1111 5 printf "watching %s\n\n", join(' ', map { _rel($root, $_) } @watch)
1112 # uncoverable condition left
1113 7 ▶ if @watch && !$opt{no_reload};
Branch: 1 branch
BranchTrueFalseTotal
if @watch and not $opt{"no_reload"} 5 2 7
Condition100%2/3
@watch and not $opt{"no_reload"}
!ll&&!rl&&r
025
MC/DC100%1/2
@watch and not $opt{"no_reload"}
@watch not $opt{"no_reload"}
Truth table100%2/3
A and not(B)
A= @watchB= not $opt{"no_reload"}
ABresult
0X0
100
111
1114  
1115 7 my $child;
1116 7 my $stop = 0;
1117 3 ▶ local $SIG{INT} = local $SIG{TERM} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1118 3 $stop = 1;
1119 3 ▶ kill 'TERM', $child if $child;
Branch: 1 branch
BranchTrueFalseTotal
if $child 2 1 3
1120 7 };
1121  
1122 7 require POSIX;
1123 7 ▶ my %seen = $opt{no_reload} ? () : _mtimes(@watch);
Branch: 1 branch
BranchTrueFalseTotal
$opt{'no_reload'} ? : 2 5 7
1124  
1125 7 ▶ while (!$stop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stop) 9 2 11
1126 9 $child = fork();
1127 # uncoverable branch true
1128 9 ▶ defined $child or die "punk dev: cannot fork: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $child or die "punk dev: cannot fork: $!\n" 9 0 9
1129 9 ▶ unless ($child) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child) 4 5 9
1130 # uncoverable branch true
1131 4 ▶ chdir $root or die "punk dev: cannot chdir to $root: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $root or die "punk dev: cannot chdir to ${root}: $!\n" 4 0 4
1132 # punk dev IS the development opt-in: the environment defaults
1133 # to production everywhere else. --env overrides even an
1134 # inherited setting - saying it on the command line is explicit.
1135 4 ▶ $ENV{PUNK_ENV} = $opt{env} if defined $opt{env};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"env"} 1 3 4
1136 4 ▶ $ENV{PUNK_ENV} //= 'development';
Condition100%2/2
$ENV{"PUNK_ENV"} //= "development"
l!l
13
MC/DC100%1/1
$ENV{"PUNK_ENV"} //= "development"
$ENV{"PUNK_ENV"}
Truth table100%2/2
A
A= $ENV{"PUNK_ENV"}
Aresult
11
00
1137 # app.psgi reads $0 to find lib/ and its own root, exactly as a
1138 # server loading the file would
1139 4 local $0 = $psgi;
1140 4 my $app = do $psgi;
1141 # uncoverable condition left
1142 # uncoverable condition right
1143 # uncoverable condition false
1144 4 ▶ die($@ || $! || "punk dev: $psgi did not return an app\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $app eq "CODE" 1 3 4
Condition40%2/5
$@ || $! || "punk dev: $psgi did not return an app\n"
l!l
10
$@ or $!
l!l&&r!l&&!r
100
MC/DC100%0/2
$@ || $! || "punk dev: $psgi did not return an app\n"
$@ $!
Truth table66%1/3
A or B
A= $@B= $!
ABresult
1X1
011
000
1145 unless ref $app eq 'CODE';
1146 3 Hyperman->run(app => $app, host => $host, port => $port,
1147 workers => $workers);
1148 3 exit 0;
1149 }
1150  
1151 5 ▶ if ($opt{no_reload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"no_reload"}) 1 4 5
1152 1 waitpid $child, 0;
1153 1 last;
1154 }
1155  
1156 # One watch loop for both waits: until a change while the child runs,
1157 # and - after it exits on its own (a compile error, most likely) -
1158 # until a change gives any reason to try again. Respinning
1159 # immediately would just spin.
1160 4 ▶ while (!$stop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stop) 33 2 35
1161 33 select undef, undef, undef, 0.5;
1162 33 ▶ if ($child && waitpid($child, POSIX::WNOHANG()) == $child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child and waitpid($child, POSIX::WNOHANG()) == $child) 1 32 33
Condition100%3/3
$child and waitpid($child, POSIX::WNOHANG()) == $child
!ll&&!rl&&r
13191
MC/DC100%2/2
$child and waitpid($child, POSIX::WNOHANG()) == $child
$child waitpid($child, POSIX::WNOHANG()) == $child
Truth table100%3/3
A and B
A= $childB= waitpid($child, POSIX::WNOHANG()) == $child
ABresult
0X0
100
111
1163 1 $child = 0;
1164 1 print "\n-- the application exited; waiting for a change --\n";
1165 }
1166 33 my %now = _mtimes(@watch);
1167 33 ▶ next if _same(\%seen, \%now);
Branch: 1 branch
BranchTrueFalseTotal
if _same(\%seen, \%now) 31 2 33
1168 # uncoverable condition right count:1
1169 # uncoverable condition right count:2
1170 2 ▶ my ($what) = grep { ($seen{$_} // 0) != ($now{$_} // 0) }
Condition100%2/4
$seen{$_} // 0
l!l
20
$now{$_} // 0
l!l
20
MC/DC100%0/1
$seen{$_} // 0
$seen{$_}
MC/DC100%0/1
$now{$_} // 0
$now{$_}
Truth table100%1/2
A
A= $seen{$_}
Aresult
11
00
Truth table100%1/2
A
A= $now{$_}
Aresult
11
00
1171 sort keys %now;
1172 # uncoverable condition right
1173 2 ▶ printf "\n-- %s changed, restarting --\n", _rel($root, $what // '');
Condition100%1/2
$what // ""
l!l
20
MC/DC100%0/1
$what // ""
$what
Truth table100%1/2
A
A= $what
Aresult
11
00
1174 2 %seen = %now;
1175 # uncoverable branch false
1176 2 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 2 0 2
1177 2 kill 'TERM', $child;
1178 2 waitpid $child, 0;
1179 2 $child = 0;
1180 }
1181 2 last;
1182 }
1183 }
1184 3 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 2 1 3
1185 2 kill 'TERM', $child;
1186 2 waitpid $child, 0;
1187 }
1188 3 print "\n";
1189 3 return 0;
1190 }
1191  
1192 # ---- punk serve --------------------------------------------------------------
1193  
1194 # The other command that needs no application. `dev` serves an application and
1195 # refuses without one; this serves a directory of files and never looks for
1196 # one - the whole server is Punk::Static->app, which carries no framework
1197 # state at all.
1198 #
1199 # There is no watch-and-restart loop here, unlike `dev`: a file is read on the
1200 # request that asks for it, so there is nothing compiled to invalidate.
1201 5 ▶sub _cmd_serve {
Subroutine _cmd_serve: called CC=16 SCAR=27.7
1202 5 my ($opt, @args) = @_;
1203 5 my $dir = shift(@args);
1204 5 ▶ die { usage_error => "unexpected argument '$args[0]'" } if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 3 5
1205 # uncoverable branch true
1206 # uncoverable condition left
1207 # uncoverable condition right
1208 3 ▶ $dir = $opt->{dir} unless defined $dir && length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 3 3
Condition100%1/3
defined $dir and length $dir
!ll&&!rl&&r
003
MC/DC100%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table100%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
1209 # uncoverable branch true
1210 # uncoverable condition left
1211 # uncoverable condition right
1212 3 ▶ $dir = '.' unless defined $dir && length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 3 3
Condition100%1/3
defined $dir and length $dir
!ll&&!rl&&r
003
MC/DC100%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table100%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
1213  
1214 3 ▶ -d $dir or die "no such directory: $dir\n";
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or die "no such directory: $dir\n" 1 2 3
1215  
1216 # uncoverable branch true
1217 1 ▶ unless (eval { require Hyperman; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Hyperman; 1 } }) 0 1 1
1218 # uncoverable statement
1219 0 print STDERR "punk serve: Hyperman is not installed\n";
1220 # uncoverable statement
1221 0 return 1;
1222 }
1223  
1224 1 require Punk::Static;
1225 my $app = Punk::Static->app($dir,
1226 ($opt->{no_index} ? () : (index => $opt->{index})),
1227 ($opt->{list} ? (list => 1) : ()),
1228 # uncoverable branch true count:1
1229 # uncoverable branch true count:2
1230 # uncoverable branch true count:3
1231 1 ▶ (defined $opt->{max_age} ? (max_age => $opt->{max_age}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$opt->{'no_index'} ? : 0 1 1
$opt->{'list'} ? : 0 1 1
defined $opt->{'max_age'} ? : 0 1 1
1232 );
1233  
1234 # uncoverable condition right
1235 1 ▶ my $port = $opt->{port} || 8000;
Condition100%1/2
$opt->{'port'} || 8000
l!l
10
MC/DC100%0/1
$opt->{'port'} || 8000
$opt->{'port'}
Truth table100%1/2
A
A= $opt->{'port'}
Aresult
11
00
1236 # uncoverable condition right
1237 1 ▶ my $host = $opt->{host} || '127.0.0.1';
Condition100%1/2
$opt->{'host'} || '127.0.0.1'
l!l
10
MC/DC100%0/1
$opt->{'host'} || '127.0.0.1'
$opt->{'host'}
Truth table100%1/2
A
A= $opt->{'host'}
Aresult
11
00
1238 1 printf "punk serve http://%s:%d\n", $host, $port;
1239 # uncoverable condition right
1240 # uncoverable condition false
1241 1 ▶ printf "serving %s\n\n", Cwd::abs_path($dir) // $dir;
Condition100%1/3
Cwd::abs_path($dir) // $dir
l!l&&r!l&&!r
100
MC/DC100%0/2
Cwd::abs_path($dir) // $dir
Cwd::abs_path($dir) $dir
Truth table100%1/3
A // B
A= Cwd::abs_path($dir)B= $dir
ABresult
1X1
011
000
1242  
1243 # access_log takes an open handle and formats Combined Log Format in C,
1244 # buffered and flushed once per wakeup - a request log with no per-request
1245 # Perl frame, which is the only reason it is on by default.
1246 Hyperman->run(app => $app, host => $host, port => $port,
1247 workers => ($opt->{workers} || 1),
1248 # uncoverable branch false
1249 # uncoverable condition right
1250 1 ▶ ($opt->{quiet} ? () : (access_log => \*STDERR)));
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'quiet'} ? : 1 0 1
Condition100%1/2
$opt->{'workers'} || 1
l!l
10
MC/DC100%0/1
$opt->{'workers'} || 1
$opt->{'workers'}
Truth table100%1/2
A
A= $opt->{'workers'}
Aresult
11
00
1251 1 return 0;
1252 }
1253  
1254 # ---- finding and loading an application --------------------------------------
1255  
1256 # app.psgi is the marker: it is what a server is pointed at, so it is what
1257 # identifies the root. Walk up from where the command was run.
1258 94 ▶sub _find_root {
Subroutine _find_root: called CC=7 SCAR=19.5
1259 94 my ($dir) = @_;
1260 # uncoverable branch true
1261 # uncoverable condition false
1262 94 ▶ my $here = Cwd::abs_path($dir || Cwd::getcwd()) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $here = Cwd::abs_path($dir || Cwd::getcwd()) or return undef 94 0 94
Condition100%2/3
$dir || Cwd::getcwd()
l!l&&r!l&&!r
61330
MC/DC100%0/2
$dir || Cwd::getcwd()
$dir Cwd::getcwd()
Truth table100%2/3
A || B
A= $dirB= Cwd::getcwd()
ABresult
1X1
011
000
1263 94 for (1 .. 40) {
1264 124 ▶ return $here if -f File::Spec->catfile($here, 'app.psgi');
Branch: 1 branch
BranchTrueFalseTotal
if -f "File::Spec"->catfile($here, "app.psgi") 73 51 124
1265 51 my $up = Cwd::abs_path(File::Spec->catdir($here, File::Spec->updir));
1266 51 ▶ last if !$up || $up eq $here;
Branch: 1 branch
BranchTrueFalseTotal
if not $up or $up eq $here 21 30 51
Condition100%3/3
not $up or $up eq $here
l!l&&r!l&&!r
111030
MC/DC100%2/2
not $up or $up eq $here
not $up $up eq $here
Truth table100%3/3
not(A) or B
A= not $upB= $up eq $here
ABresult
1X1
011
000
1267 30 $here = $up;
1268 }
1269 21 return undef;
1270 }
1271  
1272 # The application class, from the psgi's own `Class->to_app`.
1273 51 ▶sub _class_of {
Subroutine _class_of: called CC=4 SCAR=14.0
1274 51 my ($root) = @_;
1275 51 my $psgi = File::Spec->catfile($root, 'app.psgi');
1276 # uncoverable branch true
1277 51 ▶ open my $fh, '<', $psgi or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $psgi or return undef 51 0 51
1278 51 local $/;
1279 51 my $src = <$fh>;
1280 51 close $fh;
1281 51 ▶ return $1 if $src =~ /([A-Za-z_][\w:]*)\s*->\s*to_app\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $src =~ /([A-Za-z_][\w:]*)\s*->\s*to_app\b/ 48 3 51
1282 3 ▶ return $1 if $src =~ /^\s*use\s+([A-Za-z_][\w:]*)\s*;/m;
Branch: 1 branch
BranchTrueFalseTotal
if $src =~ /^\s*use\s+([A-Za-z_][\w:]*)\s*;/m 1 2 3
1283 2 return undef;
1284 }
1285  
1286 # Load the application, with the reason out through $err rather than printed:
1287 # the two callers below want it said in two different ways.
1288 38 ▶sub _load_app_info {
Subroutine _load_app_info: called CC=11 SCAR=24.0
1289 38 my ($dir, $errref, %o) = @_;
1290 38 ▶ my $say = sub { $$errref = $_[0]; return undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
1291  
1292 38 my $root = _find_root($dir);
1293 # uncoverable condition false
1294 38 ▶ return $say->("no application found (looked for app.psgi upwards from "
Branch: 1 branch
BranchTrueFalseTotal
unless $root 7 31 38
Condition100%2/3
$dir || Cwd::getcwd()
l!l&&r!l&&!r
250
MC/DC100%0/2
$dir || Cwd::getcwd()
$dir Cwd::getcwd()
Truth table100%2/3
A || B
A= $dirB= Cwd::getcwd()
ABresult
1X1
011
000
1295 . ($dir || Cwd::getcwd()) . ")") unless $root;
1296  
1297 31 my $class = _class_of($root);
1298 31 ▶ return $say->("cannot work out the application class in $root")
Branch: 1 branch
BranchTrueFalseTotal
unless $class 1 30 31
1299 unless $class;
1300  
1301 30 my $psgi_file = File::Spec->catfile($root, 'app.psgi');
1302 30 my $cwd = Cwd::getcwd();
1303 30 my $psgi = do {
1304 30 local $0 = $psgi_file; # app.psgi finds lib/ and its root through it
1305 30 do $psgi_file;
1306 };
1307 # app.psgi chdirs to the application root and everything relative in
1308 # punk.yml is written for that. A caller that will go on to open the
1309 # database wants to be left there; one that only reads the compiled
1310 # tables wants its own directory back.
1311 30 ▶ chdir($o{chdir} ? $root : $cwd);
Branch: 1 branch
BranchTrueFalseTotal
$o{'chdir'} ? : 1 29 30
1312 30 ▶ unless (ref $psgi eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $psgi eq "CODE") 2 28 30
1313 # uncoverable condition left
1314 2 ▶ my $err = $@ || $! || 'it returned no coderef';
Condition60%4/5
$@ || $! || 'it returned no coderef'
l!l
11
$@ or $!
l!l&&r!l&&!r
101
MC/DC100%1/2
$@ || $! || 'it returned no coderef'
$@ $!
Truth table66%2/3
A or B
A= $@B= $!
ABresult
1X1
011
000
1315 # uncoverable branch false
1316 2 ▶ chdir $cwd unless $o{chdir};
Branch: 1 branch
BranchTrueFalseTotal
unless $o{"chdir"} 2 0 2
1317 2 return $say->("$psgi_file did not load: $err");
1318 }
1319 28 ▶ my $registrar = $class->can('punk_app') ? $class->punk_app : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('punk_app') ? : 27 1 28
1320 28 ▶ return $say->("$class is not a Punk application") unless $registrar;
Branch: 1 branch
BranchTrueFalseTotal
unless $registrar 1 27 28
1321  
1322 27 return { root => $root, class => $class, psgi => $psgi,
1323 registrar => $registrar };
1324 }
1325  
1326 35 ▶sub _load_app {
Subroutine _load_app: called CC=3 SCAR=11.0
1327 35 my ($dir, %o) = @_;
1328 35 my $err;
1329 35 my $app = _load_app_info($dir, \$err, %o);
1330 35 ▶ print STDERR "punk: $err\n" if !$app && !$o{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $app or $o{"quiet"} 6 29 35
Condition100%3/3
$app or $o{"quiet"}
l!l&&r!l&&!r
2546
MC/DC100%2/2
$app or $o{"quiet"}
$app $o{"quiet"}
Truth table100%3/3
A or B
A= $appB= $o{"quiet"}
ABresult
1X1
011
000
1331 35 return $app;
1332 }
1333  
1334 # The public form, for a command in another distribution: same load, but the
1335 # failure is a die, because a command body's die already becomes one prefixed
1336 # line through _fail.
1337 #
1338 # my $app = Punk::Command->load_app(dir => $opt->{dir}, chdir => 1);
1339 # my $users = $app->{registrar}->model_instance('User');
1340 #
1341 # `chdir => 1` leaves the process in the application root, which is what
1342 # anything opening a relative dsn from punk.yml needs; without it the
1343 # directory the caller started in is restored.
1344 3 ▶sub load_app {
Subroutine load_app: called CC=2 SCAR=6.9
Pod documented
1345 3 my ($class, %o) = @_;
1346 3 my $dir = delete $o{dir};
1347 3 my $err;
1348 3 my $app = _load_app_info($dir, \$err, %o);
1349 3 ▶ die "$err\n" unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 1 2 3
1350 2 return $app;
1351 }
1352  
1353 # Every Punk/Command/*.pm an @INC entry holds, loaded once. Only that exact
1354 # directory, only a plain .pm, and each in its own eval - this walks
1355 # directories a user did not ask it to walk, so it does as little as it can.
1356 5 ▶sub _load_command_plugins {
Subroutine _load_command_plugins: called CC=8 SCAR=20.9
1357 5 my %seen;
1358 5 for my $inc (@INC) {
1359 # uncoverable branch true
1360 69 ▶ next if ref $inc;
Branch: 1 branch
BranchTrueFalseTotal
if ref $inc 0 69 69
1361 69 my $dir = File::Spec->catdir($inc, 'Punk', 'Command');
1362 69 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 64 5 69
1363 # uncoverable branch true
1364 5 ▶ opendir(my $dh, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or next 5 0 5
1365 5 my @mods = grep { /\A[A-Za-z]\w*\.pm\z/ } readdir $dh;
1366 5 closedir $dh;
1367 5 for my $file (sort @mods) {
1368 5 ▶ next if $seen{$file}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$file}++ 0 5 5
1369 5 (my $mod = "Punk/Command/$file") =~ s{/}{/}g;
1370 # uncoverable branch true
1371 5 ▶ next if $INC{$mod};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$mod} 0 5 5
1372 5 eval { require $mod; 1 };
1373 }
1374 }
1375 5 return;
1376 }
1377  
1378 # ---- reporting helpers -------------------------------------------------------
1379  
1380 174 ▶sub _fmt {
Subroutine _fmt: called CC=6 SCAR=17.9
1381 174 my ($name, $detail, $state) = @_;
1382 174 ▶ my $mark = $state eq 'ok' ? ' ' : $state =~ /^(?:absent|none|not installed)$/
Branch: 2 branches
BranchTrueFalseTotal
$state eq 'ok' ? : 142 32 174
$state =~ /^(?:absent|none|not installed)$/ ? : 20 12 32
1383 ? '-' : '!';
1384 # uncoverable branch false count:1
1385 174 ▶ return sprintf "%s %-26s %s%s", $mark, $name,
Branch: 2 branches
BranchTrueFalseTotal
defined $detail ? : 174 0 174
$state eq 'ok' || $state =~ /^(?:absent|none|not installed)$/ ? : 162 12 174
Condition100%3/3
$state eq "ok" or $state =~ /^(?:absent|none|not installed)$/
l!l&&r!l&&!r
1422012
MC/DC100%2/2
$state eq "ok" or $state =~ /^(?:absent|none|not installed)$/
$state eq "ok" $state =~ /^(?:absent|none|not installed)$/
Truth table100%3/3
A or B
A= $state eq "ok"B= $state =~ /^(?:absent|none|not installed)$/
ABresult
1X1
011
000
1386 (defined $detail ? $detail : ''),
1387 ($state eq 'ok' || $state =~ /^(?:absent|none|not installed)$/
1388 ? '' : " <- $state");
1389 }
1390  
1391 173 ▶sub _line { print _fmt(@_), "\n" }
Subroutine _line: called CC=1 SCAR=0.0
1392  
1393 40 ▶sub _widest {
Subroutine _widest: called CC=4 SCAR=13.9
1394 40 my ($key, $rows, $min) = @_;
1395 40 my $w = $min;
1396 40 ▶ for (@$rows) { my $l = length($_->{$key} // ''); $w = $l if $l > $w }
Branch: 1 branch
BranchTrueFalseTotal
if $l > $w 23 118 141
Condition100%2/2
$_->{$key} // ""
l!l
11526
MC/DC100%1/1
$_->{$key} // ""
$_->{$key}
Truth table100%2/2
A
A= $_->{$key}
Aresult
11
00
1397 40 return $w;
1398 }
1399  
1400 7 ▶sub _rel {
Subroutine _rel: called CC=4 SCAR=13.9
1401 7 my ($root, $path) = @_;
1402 # uncoverable branch true
1403 # uncoverable condition left
1404 # uncoverable condition right
1405 7 ▶ return '' unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 7 7
Condition100%1/3
defined $path and length $path
!ll&&!rl&&r
007
MC/DC100%0/2
defined $path and length $path
defined $path length $path
Truth table100%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
1406 7 my $r = File::Spec->abs2rel($path, $root);
1407 # uncoverable branch true
1408 7 ▶ return $r =~ /^\.\./ ? $path : $r;
Branch: 1 branch
BranchTrueFalseTotal
$r =~ /^\.\./ ? : 0 7 7
1409 }
1410  
1411 # The name behind a coderef: a target string resolves to a real named sub, so
1412 # this reports where the request will actually land. Anonymous handlers get
1413 # their file and line, which is the equivalent answer for them.
1414 63 ▶sub _code_name {
Subroutine _code_name: called CC=13 SCAR=25.7
1415 63 my ($code) = @_;
1416 # uncoverable branch true
1417 63 ▶ return '?' unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 63 63
1418 63 require B;
1419 63 my $cv = B::svref_2object($code);
1420 # uncoverable branch true
1421 63 ▶ my $gv = eval { $cv->GV } or return 'sub {...}';
Branch: 1 branch
BranchTrueFalseTotal
my $gv = eval { do { $cv->GV } } or return "sub {...}" 63 0 63
1422 # uncoverable condition right
1423 63 ▶ my $name = eval { $gv->NAME } // '';
Condition100%1/2
eval { do { $gv->NAME } } // ""
l!l
630
MC/DC100%0/1
eval { do { $gv->NAME } } // ""
eval { do { $gv->NAME } }
Truth table100%1/2
A
A= eval { do { $gv->NAME } }
Aresult
11
00
1424 # uncoverable condition right
1425 63 ▶ my $stash = eval { $gv->STASH->NAME } // '';
Condition100%1/2
eval { do { $gv->STASH->NAME } } // ""
l!l
630
MC/DC100%0/1
eval { do { $gv->STASH->NAME } } // ""
eval { do { $gv->STASH->NAME } }
Truth table100%1/2
A
A= eval { do { $gv->STASH->NAME } }
Aresult
11
00
1426 # uncoverable condition right
1427 63 ▶ if ($name eq '__ANON__' || !length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "__ANON__" or not length $name) 46 17 63
Condition100%2/3
$name eq "__ANON__" or not length $name
l!l&&r!l&&!r
46017
MC/DC100%1/2
$name eq "__ANON__" or not length $name
$name eq "__ANON__" not length $name
Truth table100%2/3
A or not(B)
A= $name eq "__ANON__"B= not length $name
ABresult
1X1
011
000
1428 # uncoverable condition right
1429 46 ▶ my $file = eval { $cv->FILE } // '';
Condition100%1/2
eval { do { $cv->FILE } } // ""
l!l
460
MC/DC100%0/1
eval { do { $cv->FILE } } // ""
eval { do { $cv->FILE } }
Truth table100%1/2
A
A= eval { do { $cv->FILE } }
Aresult
11
00
1430 # uncoverable condition right
1431 46 ▶ my $line = eval { $gv->LINE } // 0;
Condition100%1/2
eval { do { $gv->LINE } } // 0
l!l
460
MC/DC100%0/1
eval { do { $gv->LINE } } // 0
eval { do { $gv->LINE } }
Truth table100%1/2
A
A= eval { do { $gv->LINE } }
Aresult
11
00
1432 # uncoverable branch false
1433 46 ▶ $file = File::Basename::basename($file) if length $file;
Branch: 1 branch
BranchTrueFalseTotal
if length $file 46 0 46
1434 # a magic-CV closure (the static app, a docs route) reports the C file
1435 # it was created in, which tells a reader nothing useful
1436 46 ▶ return 'native handler' if $file =~ /\.h\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.h\z/ 15 31 46
1437 # uncoverable branch false
1438 31 ▶ return length $file ? "sub {...} at $file:$line" : 'sub {...}';
Branch: 1 branch
BranchTrueFalseTotal
length $file ? : 31 0 31
1439 }
1440 # uncoverable branch false
1441 17 ▶ return length $stash ? "${stash}::${name}" : $name;
Branch: 1 branch
BranchTrueFalseTotal
length $stash ? : 17 0 17
1442 }
1443  
1444 55 ▶sub _module_version {
Subroutine _module_version: called CC=3 SCAR=11.0
1445 55 my ($mod) = @_;
1446 55 (my $file = $mod) =~ s{::}{/}g;
1447 # uncoverable branch true
1448 55 ▶ return undef unless eval { require "$file.pm"; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require "$file.pm"; 1 } } 0 55 55
1449 67 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1450 55 my $v = ${"${mod}::VERSION"};
1451 # uncoverable branch false
1452 55 ▶ return defined $v ? "$v" : 'installed';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 55 0 55
1453 }
1454  
1455 # Which ABI tables resolved, and at what version. Compiled-against comes from
1456 # Punk itself; provided comes from asking the provider.
1457 5 ▶sub _abi_report {
Subroutine _abi_report: called CC=26 SCAR=32.9
1458 5 my @out;
1459  
1460 push @out, {
1461 name => 'Open::API (oa_abi)',
1462 detail => do {
1463 5 my $want = eval { Punk::_oa_abi_version() };
1464 5 my $ok = eval { Punk::_oa_available() };
1465 # uncoverable branch false count:1
1466 # uncoverable branch false count:2
1467 5 ▶ defined $want ? "v$want" . ($ok ? ', resolved' : '') : 'unknown';
Branch: 3 branches
BranchTrueFalseTotal
defined $want ? : 5 0 5
$ok ? : 5 0 5
eval { do { Punk::_oa_available() } } ? : 5 0 5
1468 },
1469 # uncoverable branch false
1470 5 state => (eval { Punk::_oa_available() } ? 'ok' : 'NOT RESOLVED'),
1471 };
1472  
1473 5 push @out, do {
1474 5 my $ok = eval { require Punk::View::Stencil;
1475 5 Punk::View::Stencil::_abi_ok() };
1476 # uncoverable branch false count:1
1477 # uncoverable branch false count:2
1478 5 ▶ { name => 'Template::Stencil (st_abi)',
Branch: 2 branches
BranchTrueFalseTotal
$ok ? : 5 0 5
$ok ? : 5 0 5
1479 detail => $ok ? 'resolved' : 'not resolved',
1480 state => $ok ? 'ok' : 'NOT RESOLVED' };
1481 };
1482  
1483 5 push @out, do {
1484 5 my $v = eval { require Hyperman; Hyperman::_abi_version() };
1485 # uncoverable branch false
1486 5 ▶ my $d = eval { Hyperman->can('detach') } ? ', detach' : '';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'Hyperman'->can('detach') } } ? : 5 0 5
1487 # uncoverable branch false count:1
1488 # uncoverable branch true count:2
1489 # uncoverable branch false count:2
1490 # uncoverable branch false count:3
1491 # uncoverable condition left
1492 # uncoverable condition right
1493 5 ▶ { name => 'Hyperman (hm_abi)',
Branch: 3 branches
BranchTrueFalseTotal
defined $v ? : 5 0 5
defined $v && $v >= 2 ? : 5 0 5
defined $v ? : 0 0 0
Condition100%1/3
defined $v and $v >= 2
!ll&&!rl&&r
005
MC/DC100%0/2
defined $v and $v >= 2
defined $v $v >= 2
Truth table100%1/3
A and B
A= defined $vB= $v >= 2
ABresult
0X0
100
111
1494 detail => defined $v ? "v$v$d" : 'not available',
1495 state => (defined $v && $v >= 2) ? 'ok'
1496 : defined $v ? 'TOO OLD (websockets need v2)' : 'MISSING' };
1497 };
1498  
1499 5 push @out, do {
1500 5 my $p = eval { require File::Raw::JSON; File::Raw::JSON::_abi_ptr() };
1501 # uncoverable branch false count:1
1502 # uncoverable branch false count:2
1503 5 ▶ { name => 'File::Raw::JSON (frj_abi)',
Branch: 2 branches
BranchTrueFalseTotal
$p ? : 5 0 5
$p ? : 5 0 5
1504 detail => $p ? 'resolved' : 'not resolved',
1505 state => $p ? 'ok' : 'NOT RESOLVED' };
1506 };
1507  
1508 5 push @out, do {
1509 5 my $p = eval { require File::Raw::XML; File::Raw::XML::_abi_ptr() };
1510 5 my $v = eval { File::Raw::XML::_abi_version() };
1511 # uncoverable branch false count:1
1512 # uncoverable branch false count:2
1513 # uncoverable condition right
1514 5 ▶ { name => 'File::Raw::XML (frx_abi)',
Branch: 2 branches
BranchTrueFalseTotal
$p ? : 5 0 5
$p ? : 5 0 5
Condition100%1/2
$v // "?"
l!l
50
MC/DC100%0/1
$v // "?"
$v
Truth table100%1/2
A
A= $v
Aresult
11
00
1515 detail => $p ? "v" . ($v // '?') . ", resolved" : 'not resolved',
1516 state => $p ? 'ok' : 'NOT RESOLVED' };
1517 };
1518  
1519 # Frozen holds the i18n catalogues. Hard, unlike the markdown pair
1520 # below: an app with the I18n plugin cannot boot without it, so a
1521 # missing table is a fault rather than a note.
1522 5 push @out, do {
1523 5 my $ok = eval { Punk::_fz_available() };
1524 5 my $want = eval { Punk::_fz_abi_version() };
1525 5 my $have = eval { require Frozen; Frozen::_abi_version() };
1526 # uncoverable branch true count:1
1527 # uncoverable branch false count:1
1528 # uncoverable branch false count:2
1529 # uncoverable branch true count:3
1530 # uncoverable branch false count:3
1531 # uncoverable branch false count:4
1532 5 ▶ { name => 'Frozen (fz_abi)',
Branch: 4 branches
BranchTrueFalseTotal
$ok ? : 5 0 5
defined $have ? : 0 0 0
$ok ? : 5 0 5
defined $have ? : 0 0 0
1533 detail => $ok ? "v$have, resolved"
1534 : defined $have ? "v$have present, v$want wanted"
1535 : 'not available',
1536 state => $ok ? 'ok'
1537 : defined $have ? 'TOO OLD (upgrade Frozen)' : 'MISSING' };
1538 };
1539  
1540 # The markdown mount's two. Both are optional in the sense that an app
1541 # without a markdown keyword never touches them, so a missing one is
1542 # reported rather than treated as a fault.
1543 5 push @out, do {
1544 5 my $ok = eval { Punk::_mds_available() };
1545 5 my $v = eval { Punk::_mds_abi_version() };
1546 # uncoverable branch true count:1
1547 # uncoverable branch false count:1
1548 # uncoverable branch false count:2
1549 # uncoverable branch false count:3
1550 5 ▶ { name => 'Markdown::Simple (mds_abi)',
Branch: 3 branches
BranchTrueFalseTotal
$ok ? : 5 0 5
defined $v ? : 0 0 0
$ok ? : 5 0 5
1551 detail => $ok ? "v$v, resolved"
1552 : defined $v ? "v$v wanted, not resolved" : 'not available',
1553 state => $ok ? 'ok' : 'not resolved (only needed by `markdown`)' };
1554 };
1555  
1556 5 push @out, do {
1557 5 my $ok = eval { Punk::_sg_available() };
1558 5 my $v = eval { Punk::_sg_abi_version() };
1559 # uncoverable branch true count:1
1560 # uncoverable branch false count:1
1561 # uncoverable branch false count:2
1562 # uncoverable branch false count:3
1563 5 ▶ { name => 'Search::Trigram (sg_abi)',
Branch: 3 branches
BranchTrueFalseTotal
$ok ? : 5 0 5
defined $v ? : 0 0 0
$ok ? : 5 0 5
1564 detail => $ok ? "v$v, resolved"
1565 : defined $v ? "v$v wanted, not resolved" : 'not available',
1566 state => $ok ? 'ok'
1567 : 'not resolved (only needed by `markdown` search)' };
1568 };
1569  
1570 5 return @out;
1571 }
1572  
1573 # ---- config helpers ----------------------------------------------------------
1574  
1575 # The same three layers punk_config.h builds: the file, the environment's, and
1576 # the gitignored local one.
1577 11 ▶sub _layer_files {
Subroutine _layer_files: called CC=2 SCAR=6.9
1578 11 my ($file, $env) = @_;
1579 11 my ($stem, $ext) = $file =~ /\A(.*?)(\.[^.\/]*)?\z/;
1580 # uncoverable branch true
1581 11 ▶ $ext = '' unless defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 11 11
1582 11 return ( [ 'base', "$stem$ext" ],
1583 [ $env, "$stem.$env$ext" ],
1584 [ 'local', "$stem.local$ext" ] );
1585 }
1586  
1587 13 ▶sub _yaml_file {
Subroutine _yaml_file: called CC=3 SCAR=11.1
1588 13 my ($file) = @_;
1589 13 require YAML::XS;
1590 # uncoverable branch true
1591 13 ▶ open my $fh, '<', $file or die "cannot read $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "cannot read ${file}: $!\n" 13 0 13
1592 13 local $/;
1593 13 my $body = <$fh>;
1594 13 close $fh;
1595 13 my $doc = YAML::XS::Load($body);
1596 # uncoverable branch false
1597 12 ▶ return ref $doc eq 'HASH' ? $doc : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $doc eq 'HASH' ? : 12 0 12
1598 }
1599  
1600 # Every single-key { $env: ... } / { $file: ... } / { $exec: [...] } in the
1601 # document, with the dotted path it sits at.
1602 63 ▶sub _walk_secrets {
Subroutine _walk_secrets: called CC=9 SCAR=22.0
1603 63 my ($node, $path, $cb) = @_;
1604 63 ▶ if (ref $node eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'HASH') { } 38 25 63
elsif (ref $node eq 'ARRAY') { } 1 24 25
1605 38 my @k = keys %$node;
1606 38 ▶ if (@k == 1 && $k[0] =~ /\A\$(env|file|exec|literal)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k == 1 and $k[0] =~ /\A\$(env|file|exec|literal)\z/) 9 29 38
Condition100%3/3
@k == 1 and $k[0] =~ /\A\$(env|file|exec|literal)\z/
!ll&&!rl&&r
12179
MC/DC100%2/2
@k == 1 and $k[0] =~ /\A\$(env|file|exec|literal)\z/
@k == 1 $k[0] =~ /\A\$(env|file|exec|literal)\z/
Truth table100%3/3
A and B
A= @k == 1B= $k[0] =~ /\A\$(env|file|exec|literal)\z/
ABresult
0X0
100
111
1607 $cb->({ path => ($path || '(root)'), kind => $k[0],
1608 # uncoverable condition right
1609 9 ▶ value => $node->{ $k[0] } });
Condition100%1/2
$path || '(root)'
l!l
90
MC/DC100%0/1
$path || '(root)'
$path
Truth table100%1/2
A
A= $path
Aresult
11
00
1610 9 return;
1611 }
1612 _walk_secrets($node->{$_}, ($path ? "$path.$_" : $_), $cb)
1613 29 ▶ for sort keys %$node;
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 27 22 49
1614 }
1615 elsif (ref $node eq 'ARRAY') {
1616 1 _walk_secrets($node->[$_], "$path\[$_]", $cb) for 0 .. $#$node;
1617 }
1618 54 return;
1619 }
1620  
1621 9 ▶sub _try_resolver {
Subroutine _try_resolver: called CC=21 SCAR=30.4
1622 9 my ($kind, $value) = @_;
1623 9 ▶ if ($kind eq '$literal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "\$literal") 1 8 9
1624 1 return (1, 'inline value');
1625 }
1626 8 ▶ if ($kind eq '$env') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "\$env") 3 5 8
1627 # uncoverable branch true
1628 # uncoverable condition right
1629 3 ▶ my $name = ref $value ? '(not a name)' : ($value // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 3 3
Condition100%1/2
$value // ""
l!l
30
MC/DC100%0/1
$value // ""
$value
Truth table100%1/2
A
A= $value
Aresult
11
00
1630 3 ▶ return (0, "$name is not set") unless defined $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$name} 2 1 3
1631 1 return (1, "$name is set");
1632 }
1633 5 ▶ if ($kind eq '$file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "\$file") 4 1 5
1634 # uncoverable branch true
1635 # uncoverable condition right
1636 4 ▶ my $f = ref $value ? '(not a path)' : ($value // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 4 4
Condition100%1/2
$value // ""
l!l
40
MC/DC100%0/1
$value // ""
$value
Truth table100%1/2
A
A= $value
Aresult
11
00
1637 4 ▶ return (0, "$f does not exist") unless -e $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f 2 2 4
1638 # uncoverable branch true class:ignore_covered_err
1639 2 ▶ return (0, "$f is not readable") unless -r $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $f 1 1 2
1640 1 return (1, $f);
1641 }
1642 # uncoverable branch false
1643 1 ▶ if ($kind eq '$exec') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "\$exec") 1 0 1
1644 # uncoverable branch false
1645 # uncoverable condition left
1646 # uncoverable condition right
1647 1 ▶ my @cmd = ref $value eq 'ARRAY' ? @$value : ($value // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 0 1
Condition100%0/2
$value // ""
l!l
00
MC/DC100%0/1
$value // ""
$value
Truth table100%0/2
A
A= $value
Aresult
11
00
1648 # uncoverable condition right
1649 1 ▶ my $prog = $cmd[0] // '';
Condition100%1/2
$cmd[0] // ""
l!l
10
MC/DC100%0/1
$cmd[0] // ""
$cmd[0]
Truth table100%1/2
A
A= $cmd[0]
Aresult
11
00
1650 # uncoverable branch true
1651 1 ▶ return (0, 'no command given') unless length $prog;
Branch: 1 branch
BranchTrueFalseTotal
unless length $prog 0 1 1
1652 # uncoverable branch true
1653 # uncoverable condition right
1654 # uncoverable condition false
1655 1 ▶ return (1, join ' ', @cmd) if $prog =~ m{/} && -x $prog;
Branch: 1 branch
BranchTrueFalseTotal
if $prog =~ m[/] and -x $prog 0 1 1
Condition100%1/3
$prog =~ m[/] and -x $prog
!ll&&!rl&&r
100
MC/DC100%0/2
$prog =~ m[/] and -x $prog
$prog =~ m[/] -x $prog
Truth table100%1/3
A and B
A= $prog =~ m[/]B= -x $prog
ABresult
0X0
100
111
1656 # uncoverable condition right
1657 1 ▶ for my $d (split /:/, ($ENV{PATH} // '')) {
Condition100%1/2
$ENV{"PATH"} // ""
l!l
10
MC/DC100%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table100%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
1658 # uncoverable branch true
1659 10 ▶ return (1, join ' ', @cmd) if -x File::Spec->catfile($d, $prog);
Branch: 1 branch
BranchTrueFalseTotal
if -x "File::Spec"->catfile($d, $prog) 0 10 10
1660 }
1661 1 return (0, "$prog is not on PATH");
1662 }
1663 # uncoverable statement
1664 0 return (0, 'unknown resolver');
1665 }
1666  
1667 # ---- api sync helpers --------------------------------------------------------
1668  
1669 # Which operationIds already have a sub, by reading the controllers rather than
1670 # loading them: a half-written controller should not stop the command that is
1671 # meant to help you finish it.
1672 15 ▶sub _implemented_ops {
Subroutine _implemented_ops: called CC=12 SCAR=25.1
1673 15 my ($dir, %o) = @_;
1674 15 my %seen;
1675 # uncoverable branch true
1676 15 ▶ return %seen unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 15 15
1677 # uncoverable branch true
1678 15 ▶ opendir(my $dh, $dir) or return %seen;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return %seen 15 0 15
1679 15 my @files = grep { /\.pm\z/ } readdir $dh;
1680 15 closedir $dh;
1681 15 for my $f (@files) {
1682 36 ▶ next if $o{only} && $f !~ $o{only};
Branch: 1 branch
BranchTrueFalseTotal
if $o{"only"} and not $f =~ /$o{"only"}/ 12 24 36
Condition100%3/3
$o{"only"} and not $f =~ /$o{"only"}/
!ll&&!rl&&r
19512
MC/DC100%2/2
$o{"only"} and not $f =~ /$o{"only"}/
$o{"only"} not $f =~ /$o{"only"}/
Truth table100%3/3
A and not(B)
A= $o{"only"}B= not $f =~ /$o{"only"}/
ABresult
0X0
100
111
1683 24 ▶ next if $o{skip} && $f =~ $o{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $o{"skip"} and $f =~ /$o{"skip"}/ 4 20 24
Condition100%3/3
$o{"skip"} and $f =~ /$o{"skip"}/
!ll&&!rl&&r
9114
MC/DC100%2/2
$o{"skip"} and $f =~ /$o{"skip"}/
$o{"skip"} $f =~ /$o{"skip"}/
Truth table100%3/3
A and B
A= $o{"skip"}B= $f =~ /$o{"skip"}/
ABresult
0X0
100
111
1684 # uncoverable branch true
1685 20 ▶ open my $fh, '<', File::Spec->catfile($dir, $f) or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "File::Spec"->catfile($dir, $f) or next 20 0 20
1686 20 ▶ while (my $l = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fh)) 784 0 784
1687 784 ▶ last if $l =~ /\A__END__/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /\A__END__/ 20 764 784
1688 764 ▶ $seen{$1} = $f if $l =~ /\A\s*sub\s+([A-Za-z_]\w*)\s*\{/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /\A\s*sub\s+([A-Za-z_]\w*)\s*\{/ 47 717 764
1689 }
1690 20 close $fh;
1691 }
1692 15 return %seen;
1693 }
1694  
1695 # Splice new stubs in before the closing 1; of an existing controller.
1696 6 ▶sub _insert_stubs {
Subroutine _insert_stubs: called CC=6 SCAR=18.2
1697 6 my ($file, $ops, $dry) = @_;
1698 6 ▶ return if $dry;
Branch: 1 branch
BranchTrueFalseTotal
if $dry 2 4 6
1699 # uncoverable branch true
1700 4 ▶ open my $fh, '<', $file or Carp::croak("punk api: cannot read $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or Carp::croak("punk api: cannot read ${file}: $!") 4 0 4
1701 4 local $/;
1702 4 my $src = <$fh>;
1703 4 close $fh;
1704  
1705 4 require Punk::Generate;
1706 4 Punk::Generate::_op_id($_->{id}) for @$ops;
1707 my $stubs = join '', map {
1708 4 Punk::Generate->render_skel('api_stub.tmpl', { op => $_ })
1709 } @$ops;
1710  
1711 4 ▶ if ($src =~ /\n1;\s*\n/) { $src =~ s/\n1;\s*\n/\n$stubs\n1;\n/ }
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /\n1;\s*\n/) { } 3 1 4
1712 1 else { $src .= "\n$stubs" }
1713  
1714 # uncoverable branch true
1715 4 ▶ open my $out, '>', $file or Carp::croak("punk api: cannot write $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $file or Carp::croak("punk api: cannot write ${file}: $!") 4 0 4
1716 4 print $out $src;
1717 4 close $out;
1718 4 return;
1719 }
1720  
1721 # ---- console helpers ---------------------------------------------------------
1722  
1723 5 ▶sub _console_context {
Subroutine _console_context: called CC=2 SCAR=6.9
1724 5 my ($app) = @_;
1725 5 return eval {
1726 5 require Punk::Context;
1727 5 my $class = $app->{class} . '::_Context';
1728 # uncoverable branch true
1729 5 ▶ $class = 'Punk::Context' unless $class->can('req');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("req") 0 5 5
1730 $class->_build({ REQUEST_METHOD => 'GET', PATH_INFO => '/',
1731 QUERY_STRING => '', 'psgi.url_scheme' => 'http',
1732 HTTP_HOST => 'localhost', SERVER_NAME => 'localhost',
1733 SERVER_PORT => 80 },
1734 5 $app->{registrar}, {});
1735 };
1736 }
1737  
1738 3 ▶sub _console_dump {
Subroutine _console_dump: called CC=1 SCAR=0.0
1739 3 my (@out) = @_;
1740 3 require Data::Dumper;
1741 3 my $d = Data::Dumper->new(\@out);
1742 3 $d->Sortkeys(1)->Indent(1)->Terse(1);
1743 3 return $d->Dump;
1744 }
1745  
1746 6 ▶sub _console_wrap {
Subroutine _console_wrap: called CC=1 SCAR=0.0
1747 6 my ($line) = @_;
1748 6 return "package main; my \$app = \$Punk::Command::app_;"
1749 . " my \$psgi = \$Punk::Command::psgi_;"
1750 . " my \$c = \$Punk::Command::ctx_; $line";
1751 }
1752  
1753 # ---- dev helpers -------------------------------------------------------------
1754  
1755 38 ▶sub _mtimes {
Subroutine _mtimes: called CC=2 SCAR=6.9
1756 38 my (@dirs) = @_;
1757 38 my %m;
1758 38 require File::Find;
1759 59 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1760 59 ▶ return unless -f $File::Find::name;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $File::Find::name 38 21 59
1761 # uncoverable branch true
1762 21 ▶ return if $File::Find::name =~ m{/\.};
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name =~ m[/\.] 0 21 21
1763 21 $m{$File::Find::name} = (stat _)[9];
1764 # uncoverable branch false
1765 38 ▶ } }, @dirs) if @dirs;
Branch: 1 branch
BranchTrueFalseTotal
if @dirs 38 0 38
1766 38 return %m;
1767 }
1768  
1769 33 ▶sub _same {
Subroutine _same: called CC=5 SCAR=16.1
1770 33 my ($a, $b) = @_;
1771 # uncoverable branch true
1772 33 ▶ return 0 if keys %$a != keys %$b;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$a != keys %$b 0 33 33
1773 # uncoverable condition left
1774 33 ▶ for (keys %$a) { return 0 if !exists $b->{$_} || $a->{$_} != $b->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
if not exists $b->{$_} or $a->{$_} != $b->{$_} 2 16 18
Condition100%2/3
not exists $b->{$_} or $a->{$_} != $b->{$_}
l!l&&r!l&&!r
0216
MC/DC100%1/2
not exists $b->{$_} or $a->{$_} != $b->{$_}
not exists $b->{$_} $a->{$_} != $b->{$_}
Truth table100%2/3
not(A) or B
A= not exists $b->{$_}B= $a->{$_} != $b->{$_}
ABresult
1X1
011
000
1775 31 return 1;
1776 }
1777  
1778 # ---- punk new ----------------------------------------------------------------
1779 # The one command that does not need an existing application. The work lives
1780 # in Punk::Generate; this is argument shape and the next-steps note.
1781  
1782 # The kit seam, the command seam's twin: `--kit diy` loads Punk::Kit::Diy and
1783 # generates through it. Same name shape and same transform as _probe, so a
1784 # distribution shipping both a kit and a subcommand spells them alike.
1785 13 ▶sub _kit_name_ok { return $_[0] =~ /\A[a-z][a-z0-9_-]*\z/ }
Subroutine _kit_name_ok: called CC=1 SCAR=0.0
1786  
1787 11 ▶sub _kit_class {
Subroutine _kit_class: called CC=4 SCAR=13.9
1788 11 my ($kit) = @_;
1789 11 (my $mod = ucfirst $kit) =~ s/[-_](\w)/\U$1/g;
1790 11 my $class = "Punk::Kit::$mod";
1791 11 (my $file = "$class.pm") =~ s{::}{/}g;
1792 11 ▶ unless (eval { require $file; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require $file; 1 } }) 4 7 11
1793 4 my $err = $@;
1794 # Two different problems with two different answers: one is a cpanm
1795 # away, the other is a bug in something already installed.
1796 4 ▶ die "kit '$kit' is not installed (no $class on \@INC)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /\ACan't locate \Q$file\E in \@INC/ 2 2 4
1797 if $err =~ /\ACan't locate \Q$file\E in \@INC/;
1798 2 $err =~ s/\s+\z//;
1799 2 die "kit '$kit' failed to load: $err\n";
1800 }
1801 # uncoverable branch true
1802 7 ▶ die "$class is not a kit: a kit subclasses Punk::Generate and overrides "
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Punk::Generate") 0 7 7
1803 . "run\n" unless $class->isa('Punk::Generate');
1804 7 return $class;
1805 }
1806  
1807 # --kit's value, read out of argv before Getopt has run, so the kit's own
1808 # options can be added to the ones being parsed. Takes both spellings and only
1809 # the first: a second --kit is Getopt's to complain about, not this.
1810 12 ▶sub _kit_from_argv {
Subroutine _kit_from_argv: called CC=5 SCAR=16.1
1811 12 my (@argv) = @_;
1812 12 for my $i (0 .. $#argv) {
1813 27 ▶ return $1 if $argv[$i] =~ /\A--kit=(.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $argv[$i] =~ /\A--kit=(.*)\z/s 1 26 27
1814 # uncoverable condition right
1815 26 ▶ return $argv[$i + 1] if $argv[$i] eq '--kit' && $i < $#argv;
Branch: 1 branch
BranchTrueFalseTotal
if $argv[$i] eq "--kit" and $i < $#argv 7 19 26
Condition100%2/3
$argv[$i] eq "--kit" and $i < $#argv
!ll&&!rl&&r
1907
MC/DC100%1/2
$argv[$i] eq "--kit" and $i < $#argv
$argv[$i] eq "--kit" $i < $#argv
Truth table100%2/3
A and B
A= $argv[$i] eq "--kit"B= $i < $#argv
ABresult
0X0
100
111
1816 }
1817 4 return;
1818 }
1819  
1820 8 ▶sub _cmd_new {
Subroutine _cmd_new: called CC=17 SCAR=28.3
1821 8 my ($opt, @args) = @_;
1822 8 my $name = shift @args;
1823 # uncoverable condition right
1824 8 ▶ die { usage_error => 'an application name is required' }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 7 8
Condition100%2/3
defined $name and length $name
!ll&&!rl&&r
107
MC/DC100%1/2
defined $name and length $name
defined $name length $name
Truth table100%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
1825 unless defined $name && length $name;
1826 # uncoverable branch true
1827 7 ▶ die { usage_error => "unexpected argument '$args[0]'" } if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 7 7
1828 # --sqitch's engine is checked before anything is written
1829 die { usage_error => "--sqitch needs an engine: sqlite, pg or mysql, not '$opt->{sqitch}'" }
1830 7 ▶ if defined $opt->{sqitch} && $opt->{sqitch} !~ /\A(?:sqlite|pg|mysql)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"sqitch"} and not $opt->{"sqitch"} =~ /\A(?:sqlite|pg|mysql)\z/ 1 6 7
Condition100%3/3
defined $opt->{"sqitch"} and not $opt->{"sqitch"} =~ /\A(?:sqlite|pg|mysql)\z/
!ll&&!rl&&r
421
MC/DC100%2/2
defined $opt->{"sqitch"} and not $opt->{"sqitch"} =~ /\A(?:sqlite|pg|mysql)\z/
defined $opt->{"sqitch"} not $opt->{"sqitch"} =~ /\A(?:sqlite|pg|mysql)\z/
Truth table100%3/3
A and not(B)
A= defined $opt->{"sqitch"}B= not $opt->{"sqitch"} =~ /\A(?:sqlite|pg|mysql)\z/
ABresult
0X0
100
111
1831 die { usage_error => "--kit takes a name like 'diy' ([a-z][a-z0-9_-]*), "
1832 . "not '$opt->{kit}'" }
1833 6 ▶ if defined $opt->{kit} && !_kit_name_ok($opt->{kit});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"kit"} and not _kit_name_ok($opt->{"kit"}) 1 5 6
Condition100%3/3
defined $opt->{"kit"} and not _kit_name_ok($opt->{"kit"})
!ll&&!rl&&r
141
MC/DC100%2/2
defined $opt->{"kit"} and not _kit_name_ok($opt->{"kit"})
defined $opt->{"kit"} not _kit_name_ok($opt->{"kit"})
Truth table100%3/3
A and not(B)
A= defined $opt->{"kit"}B= not _kit_name_ok($opt->{"kit"})
ABresult
0X0
100
111
1834  
1835 5 require Punk::Generate;
1836 5 ▶ my $class = defined $opt->{kit} ? _kit_class($opt->{kit}) : 'Punk::Generate';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'kit'} ? : 4 1 5
1837  
1838 # Every option goes to the constructor: Punk::Generate ignores the keys it
1839 # does not know, so a kit reads its own out of the same hash rather than
1840 # needing this to know what it takes.
1841 my $gen = $class->new(
1842 %$opt,
1843 name => $name,
1844 (defined $opt->{dir} ? (dir => $opt->{dir}) : ()),
1845 (defined $opt->{api} ? (api => $opt->{api}) : ()),
1846 force => $opt->{force},
1847 # uncoverable branch false count:1
1848 # uncoverable branch true count:2
1849 3 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
defined $opt->{'dir'} ? : 3 0 3
defined $opt->{'api'} ? : 0 3 3
1850 3 my @files = $gen->run;
1851 3 my $dir = $gen->dir;
1852 3 print "Created $name in $dir\n\n";
1853 3 print " $_\n" for @files;
1854  
1855 # --sqitch ENGINE: a Sqitch project for the schema, through Punk-Sqitch.
1856 # The engine is asked for rather than read, because the generated
1857 # punk.yml ships its database block commented out. The application is
1858 # already written by now, so a missing Punk-Sqitch is reported with the
1859 # command to run once it is installed, not as a failed `new`.
1860 3 my $sq = 0;
1861 3 ▶ if (defined $opt->{sqitch}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt->{'sqitch'} and -f 'File::Spec'->catfile($dir, 'sqitch.plan')) { } 1 2 3
elsif (defined $opt->{'sqitch'}) { } 1 1 2
Condition100%3/3
defined $opt->{"sqitch"} and -f "File::Spec"->catfile($dir, "sqitch.plan")
!ll&&!rl&&r
111
MC/DC100%2/2
defined $opt->{"sqitch"} and -f "File::Spec"->catfile($dir, "sqitch.plan")
defined $opt->{"sqitch"} -f "File::Spec"->catfile($dir, "sqitch.plan")
Truth table100%3/3
A and B
A= defined $opt->{"sqitch"}B= -f "File::Spec"->catfile($dir, "sqitch.plan")
ABresult
0X0
100
111
1862 && -f File::Spec->catfile($dir, 'sqitch.plan')) {
1863 # A kit that manages its own schema has already written the project.
1864 # Sqitch's own init is a silent no-op on one, so running it here would
1865 # end a successful generation with a confusing message about a project
1866 # that is exactly as it should be.
1867 1 print "\npunk new: the kit wrote the Sqitch project; skipping init\n";
1868 }
1869 elsif (defined $opt->{sqitch}) {
1870 1 my $engine = $opt->{sqitch};
1871 # uncoverable branch false class:ignore_covered_err
1872 # uncoverable statement count:3 class:ignore_covered_err
1873 # uncoverable branch true class:ignore_covered_err
1874 0 ▶ if (eval { require Punk::Command::Sqitch; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Punk::Command::Sqitch; 1 } }) { } 0 1 1
1875 # uncoverable statement class:ignore_covered_err
1876 0 print "\n";
1877 # uncoverable statement class:ignore_covered_err
1878 0 $sq = Punk::Command::Sqitch::main('--dir', $dir, 'init', '--engine', $engine);
1879 }
1880 else {
1881 # uncoverable statement class:ignore_covered_err
1882 1 print STDERR "\npunk new: --sqitch needs Punk-Sqitch, which is not installed; "
1883 . "once it is, run\n\n punk sqitch init --engine $engine\n\n"
1884 . "inside $dir\n";
1885 # uncoverable statement class:ignore_covered_err
1886 1 $sq = 1;
1887 }
1888 }
1889 # A kit knows what its application needs before it will serve - a schema
1890 # deployed, a secret set - so it gets to say so instead of this.
1891 3 ▶ if ($gen->can('next_steps')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen->can('next_steps')) { } 1 2 3
1892 1 print $gen->next_steps;
1893 }
1894 else {
1895 2 print <<"NEXT";
1896  
1897 cd $dir
1898 plackup app.psgi
1899  
1900 Then open http://localhost:5000/
1901 NEXT
1902 }
1903 3 return $sq;
1904 }
1905  
1906 # ---- punk generate -----------------------------------------------------------
1907 # Into an EXISTING application - `new` scaffolds once; these add to it. The
1908 # class comes from app.psgi by the same scrape _load_app uses, but without
1909 # executing anything - a broken controller cannot stop you generating the fix.
1910  
1911 10 ▶sub _cmd_generate {
Subroutine _cmd_generate: called CC=13 SCAR=25.7
1912 10 my ($kind, $opt, @args) = @_;
1913 10 my $name = shift @args;
1914 # uncoverable condition right
1915 10 ▶ die { usage_error => "a $kind name is required" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 9 10
Condition100%2/3
defined $name and length $name
!ll&&!rl&&r
109
MC/DC100%1/2
defined $name and length $name
defined $name length $name
Truth table100%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
1916 unless defined $name && length $name;
1917 # uncoverable branch true
1918 9 ▶ die { usage_error => "unexpected argument '$args[0]'" } if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 9 9
1919 9 ▶ die { usage_error => "'$name' is not a valid package name" }
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[A-Z]\w*(?:::[A-Z]\w*)*$/ 1 8 9
1920 unless $name =~ /^[A-Z]\w*(?:::[A-Z]\w*)*$/;
1921  
1922 my $root = _find_root($opt->{dir})
1923 8 ▶ or die "no application found (run inside one, or pass --dir)\n";
Branch: 1 branch
BranchTrueFalseTotal
my $root = _find_root($opt->{"dir"}) or die "no application found (run inside one, or pass --dir)\n" 7 1 8
1924 # uncoverable branch true
1925 7 ▶ my $class = _class_of($root)
Branch: 1 branch
BranchTrueFalseTotal
my $class = _class_of($root) or die "could not read the application class from app.psgi\n" 7 0 7
1926 or die "could not read the application class from app.psgi\n";
1927  
1928 7 my @segs = split /::/, $name;
1929 7 my ($tmpl, $rel, %vars);
1930 7 ▶ if ($kind eq 'controller') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'controller') { } 5 2 7
1931 5 ▶ my $ns = $opt->{api} ? 'API' : 'Web';
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'api'} ? : 1 4 5
1932 5 $rel = join('/', 'lib', (split /::/, $class),
1933 'Controller', $ns, @segs) . '.pm';
1934 5 $tmpl = 'controller_gen.tmpl';
1935 %vars = (app => $class, ns => $ns, name => join('::', @segs),
1936 5 ▶ route => lc(join '/', @segs), api => $opt->{api} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'api'} ? : 1 4 5
1937 }
1938 else {
1939 2 $rel = join('/', 'lib', (split /::/, $class), 'Model', @segs) . '.pm';
1940 2 $tmpl = 'model_gen.tmpl';
1941 %vars = (app => $class, name => join('::', @segs),
1942 # uncoverable condition false
1943 2 ▶ table => $opt->{table} // lc($segs[-1]) . 's');
Condition100%2/2
$opt->{"table"} // lc($segs[-1]) . 's'
l!l
11
MC/DC100%1/1
$opt->{"table"} // lc($segs[-1]) . 's'
$opt->{"table"}
Truth table100%2/2
A
A= $opt->{"table"}
Aresult
11
00
1944 }
1945 7 my $abs = File::Spec->catfile($root, split m{/}, $rel);
1946 7 ▶ die "$rel already exists\n" if -e $abs && !$opt->{force};
Branch: 1 branch
BranchTrueFalseTotal
if -e $abs and not $opt->{"force"} 1 6 7
Condition100%3/3
-e $abs and not $opt->{"force"}
!ll&&!rl&&r
511
MC/DC100%2/2
-e $abs and not $opt->{"force"}
-e $abs not $opt->{"force"}
Truth table100%3/3
A and not(B)
A= -e $absB= not $opt->{"force"}
ABresult
0X0
100
111
1947  
1948 6 require Punk::Generate;
1949 6 Punk::Generate->write_skel($tmpl, $abs, \%vars);
1950 6 print "wrote $rel\n";
1951 6 return 0;
1952 }
1953  
1954 # ---- punk test ---------------------------------------------------------------
1955  
1956 4 ▶sub _cmd_test {
Subroutine _cmd_test: called CC=7 SCAR=19.5
1957 4 my ($opt, @args) = @_;
1958 4 ▶ my $root = _find_root($opt->{dir}) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $root = _find_root($opt->{"dir"}) or do { print STDERR "punk test: no application found\n"; return 2 } 3 1 4
1959 1 print STDERR "punk test: no application found\n";
1960 1 return 2;
1961 };
1962 3 require App::Prove; # ships with Test::Harness
1963 # uncoverable branch true
1964 3 ▶ local $ENV{PUNK_ENV} = $opt->{env} if defined $opt->{env};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"env"} 0 3 3
1965 3 my $cwd = Cwd::getcwd();
1966 # uncoverable branch true
1967 3 ▶ chdir $root or die "cannot chdir to $root: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $root or die "cannot chdir to ${root}: $!\n" 3 0 3
1968 3 my $prove = App::Prove->new;
1969 3 ▶ $prove->process_args('-l', '-r', (@args ? @args : 't'));
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 1 2 3
1970 3 my $ok = eval { $prove->run };
1971 3 my $err = $@;
1972 3 chdir $cwd;
1973 # uncoverable branch true
1974 3 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 3 3
1975 3 ▶ return $ok ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 2 1 3
1976 }
1977  
1978 # ---- punk secret -------------------------------------------------------------
1979  
1980 4 ▶sub _random_bytes {
Subroutine _random_bytes: called CC=4 SCAR=14.0
1981 4 my ($n) = @_;
1982 # uncoverable branch true
1983 4 ▶ open my $ur, '<:raw', '/dev/urandom'
Branch: 1 branch
BranchTrueFalseTotal
open my $ur, "<:raw", "/dev/urandom" or die "cannot open /dev/urandom: $!\n" 4 0 4
1984 or die "cannot open /dev/urandom: $!\n";
1985 4 my $bytes;
1986 4 my $got = read($ur, $bytes, $n);
1987 4 close $ur;
1988 # uncoverable branch true
1989 # uncoverable condition right
1990 4 ▶ die "short read from /dev/urandom\n" unless ($got // 0) == $n;
Branch: 1 branch
BranchTrueFalseTotal
unless ($got // 0) == $n 0 4 4
Condition100%1/2
$got // 0
l!l
40
MC/DC100%0/1
$got // 0
$got
Truth table100%1/2
A
A= $got
Aresult
11
00
1991 4 return $bytes;
1992 }
1993  
1994 5 ▶sub _cmd_secret {
Subroutine _cmd_secret: called CC=5 SCAR=16.4
1995 5 my ($opt) = @_;
1996 5 my $n = $opt->{bytes};
1997 # uncoverable condition left count:1
1998 # uncoverable condition right count:2
1999 5 ▶ die { usage_error => 'bytes must be between 16 and 256' }
Branch: 1 branch
BranchTrueFalseTotal
if not $n or $n < 16 or $n > 256 1 4 5
Condition33%4/6
not $n or $n < 16 or $n > 256
l!l&&r!l&&!r
104
not $n or $n < 16
l!l&&r!l&&!r
014
MC/DC66%1/3
not $n or $n < 16 or $n > 256
not $n $n < 16 $n > 256
Truth table50%2/4
not(A) or B or C
A= not $nB= $n < 16C= $n > 256
ABCresult
1XX1
01X1
0011
0000
2000 if !$n || $n < 16 || $n > 256;
2001 4 my $raw = _random_bytes($n);
2002 4 ▶ if ($opt->{hex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'hex'}) { } 1 3 4
2003 1 print unpack('H*', $raw), "\n";
2004 }
2005 else {
2006 3 require MIME::Base64;
2007 3 my $t = MIME::Base64::encode_base64($raw, '');
2008 3 $t =~ tr{+/=}{-_}d; # url-safe, no padding
2009 3 print "$t\n";
2010 }
2011 4 return 0;
2012 }
2013  
2014 # ---- the core registrations --------------------------------------------------
2015 # Registration order is the order `punk` lists them in.
2016  
2017 __PACKAGE__->register(new => {
2018 abstract => 'generate a new application',
2019 display => 'new <AppName>',
2020 usage => '<AppName> [options]',
2021 desc => 'Generate a running Punk application: routes, a controller, '
2022 . "Stencil views,\nconfig/punk.yml, a psgi entry point and a "
2023 . 'test.',
2024 options => [
2025 { spec => 'dir=s', arg => 'PATH',
2026 doc => 'where to write it (default: the name, :: replaced by -)' },
2027 { spec => 'api=s', arg => 'SPEC',
2028 doc => 'an OpenAPI 3.1 document to mount under /api, generating '
2029 . 'one controller of operation stubs per tag' },
2030 { spec => 'force',
2031 doc => 'write into a directory that is not empty' },
2032 { spec => 'sqitch=s', arg => 'ENGINE',
2033 doc => 'also initialise a Sqitch project for the schema (sqlite, pg '
2034 . 'or mysql); needs Punk-Sqitch' },
2035 { spec => 'kit=s', arg => 'NAME',
2036 doc => 'generate from an installed kit (Punk::Kit::<Name>) instead '
2037 . 'of the basic skeleton; `punk new --kit NAME --help` lists '
2038 . 'what it takes' },
2039 ],
2040 # The kit named in argv brings its own options, so they parse and appear
2041 # in help like any other rather than being scraped out of a leftover argv.
2042 12 ▶ extra_options => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
2043 12 my (@argv) = @_;
2044 12 my $kit = _kit_from_argv(@argv);
2045 12 ▶ return unless defined $kit && _kit_name_ok($kit);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kit and _kit_name_ok($kit) 5 7 12
Condition100%3/3
defined $kit and _kit_name_ok($kit)
!ll&&!rl&&r
417
MC/DC100%2/2
defined $kit and _kit_name_ok($kit)
defined $kit _kit_name_ok($kit)
Truth table100%3/3
A and B
A= defined $kitB= _kit_name_ok($kit)
ABresult
0X0
100
111
2046 7 ▶ my $class = eval { _kit_class($kit) } or return; # the body reports it
Branch: 1 branch
BranchTrueFalseTotal
my $class = eval { do { _kit_class($kit) } } or return 5 2 7
2047 5 ▶ return unless $class->can('options');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("options") 1 4 5
2048 4 return map { { %$_, owner => $class, section => "kit options ($kit)" } }
2049 $class->options;
2050 },
2051 67 examples => [
2052 'punk new MyApp',
2053 'punk new MyApp --dir ~/code/myapp',
2054 'punk new MyApp --api ./openapi.json',
2055 'punk new MyApp --sqitch pg',
2056 'punk new MyApp --kit diy',
2057 ],
2058 hints => [ [ qr/is not empty/, 'use --force to write into it anyway' ],
2059 [ qr/kit '.*' is not installed/,
2060 'kits ship in their own distributions; the diy kit is '
2061 . 'Punk-DIY' ] ],
2062 code => \&_cmd_new,
2063 });
2064  
2065 __PACKAGE__->register(generate => {
2066 abstract => 'add a controller or model to an existing application',
2067 display => 'generate <what>',
2068 commands => {
2069 controller => {
2070 abstract => 'a controller class',
2071 usage => '<Name> [options]',
2072 desc => 'Write lib/<App>/Controller/Web/<Name>.pm (or '
2073 . "Controller/API with --api) into\nthe application, "
2074 . 'ready to be named as a route target.',
2075 options => [
2076 { spec => 'api',
2077 doc => 'under Controller::API, answering JSON' },
2078 { spec => 'force', doc => 'overwrite an existing file' },
2079 ],
2080 8 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2081 8 my ($opt, @a) = @_;
2082 8 return _cmd_generate(controller => $opt, @a);
2083 },
2084 },
2085 model => {
2086 abstract => 'a Punk::Model class',
2087 usage => '<Name> [options]',
2088 desc => 'Write lib/<App>/Model/<Name>.pm with a table and a '
2089 . "field stub - auto-discovered\nby a bare `model;`, or "
2090 . "registered with model '<Name>'.",
2091 options => [
2092 { spec => 'table=s', arg => 'NAME',
2093 doc => 'the table (default: the lowercased name, '
2094 . 'pluralised)' },
2095 { spec => 'force', doc => 'overwrite an existing file' },
2096 ],
2097 2 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2098 2 my ($opt, @a) = @_;
2099 2 return _cmd_generate(model => $opt, @a);
2100 },
2101 },
2102 },
2103 67 });
2104  
2105 __PACKAGE__->register(routes => {
2106 abstract => 'the compiled routing table',
2107 desc => 'Print the compiled routing table: every route, API '
2108 . "operation, websocket\nroute and mounted application, with "
2109 . 'what it resolved to.',
2110 options => [
2111 { spec => 'sort',
2112 doc => 'order by path rather than by declaration' },
2113 { spec => 'method=s', arg => 'NAME',
2114 doc => 'only this HTTP method (or WS / ANY)' },
2115 { spec => 'path=s', arg => 'GLOB',
2116 doc => 'only paths matching the glob (* within a segment, '
2117 . '** across)' },
2118 { spec => 'kind=s', arg => 'KIND',
2119 doc => 'only route, api, ws, markdown or mount rows' },
2120 { spec => 'name=s', arg => 'NAME',
2121 doc => 'only the route with this name (exact)' },
2122 { spec => 'json',
2123 doc => 'machine output: a JSON array of row objects' },
2124 ],
2125 25 ▶ code => sub { my ($opt) = @_; return routes(%$opt) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2126 67 });
2127  
2128 __PACKAGE__->register(api => {
2129 abstract => 'add stubs for operations added to the spec',
2130 display => 'api sync',
2131 commands => {
2132 sync => {
2133 abstract => 'add stubs for new operations',
2134 desc => 'Add a stub for every operation openapi.json declares '
2135 . "that the controllers\ndo not implement. Methods with "
2136 . "no matching operation are reported, never\nremoved, "
2137 . 'and an implemented operation is never touched.',
2138 options => [
2139 { spec => 'spec=s', arg => 'PATH',
2140 doc => 'the document (default: openapi.json in the '
2141 . 'application root)' },
2142 { spec => 'dry-run',
2143 doc => 'report what would change, write nothing' },
2144 ],
2145 15 ▶ code => sub { my ($opt) = @_; return api_sync(%$opt) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2146 },
2147 },
2148 67 });
2149  
2150 __PACKAGE__->register(config => {
2151 abstract => 'resolve the configuration and every secret it references',
2152 display => 'config check',
2153 commands => {
2154 check => {
2155 abstract => 'resolve the configuration',
2156 desc => 'Resolve config/punk.yml and report every $env, '
2157 . "\$file and \$exec reference\nindependently, so one "
2158 . "missing secret does not hide the rest. Exits\n"
2159 . 'non-zero when anything failed - usable as a '
2160 . 'deployment gate.',
2161 options => [
2162 { spec => 'file=s', arg => 'PATH',
2163 doc => 'the config file (default: config/punk.yml)' },
2164 { spec => 'env=s', arg => 'NAME',
2165 doc => 'the environment layer (default: $PUNK_ENV or '
2166 . 'production)' },
2167 { spec => 'secrets=s', arg => 'MODE',
2168 doc => 'strict, warn or off, for the plaintext-secret '
2169 . 'guardrail' },
2170 { spec => 'dump',
2171 doc => 'print the resolved structure, secrets redacted' },
2172 ],
2173 14 ▶ code => sub { my ($opt) = @_; return config_check(%$opt) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2174 },
2175 },
2176 67 });
2177  
2178 __PACKAGE__->register(doctor => {
2179 abstract => 'versions, C ABIs and application health',
2180 desc => 'Report versions, which C ABI tables resolved and at what '
2181 . "version, and -\nwhen run inside an application - its routes "
2182 . 'and configuration.',
2183 5 ▶ code => sub { my ($opt) = @_; return doctor(%$opt) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2184 67 });
2185  
2186 __PACKAGE__->register(console => {
2187 abstract => 'a REPL with the application compiled',
2188 desc => 'A REPL with the application compiled: $app is the '
2189 . "registrar, \$psgi the\ncompiled coderef, \$c a throwaway "
2190 . 'context for $c->model and friends.',
2191 options => [
2192 { spec => 'eval=s', arg => 'CODE',
2193 doc => 'evaluate one expression, print its dump, exit' },
2194 ],
2195 5 ▶ code => sub { my ($opt) = @_; return console(%$opt) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2196 67 });
2197  
2198 67 __PACKAGE__->register(test => {
2199 abstract => 'run the application test suite',
2200 usage => '[prove arguments]',
2201 desc => 'Run prove -lr in the application root. Extra arguments go '
2202 . "to prove\nverbatim (a file, -v, -j4).",
2203 options => [
2204 { spec => 'env=s', arg => 'NAME', doc => 'PUNK_ENV for the run' },
2205 ],
2206 code => \&_cmd_test,
2207 });
2208  
2209 67 __PACKAGE__->register(secret => {
2210 abstract => 'random key material for a session secret',
2211 desc => 'Print one line of random key material - what punk.yml\'s '
2212 . "session secret\nwants behind its \$env reference. Nothing "
2213 . 'else goes to stdout, so it pipes.',
2214 options => [
2215 { spec => 'bytes=i', arg => 'N', doc => 'how much entropy',
2216 default => 32 },
2217 { spec => 'hex', doc => 'hex instead of url-safe base64' },
2218 ],
2219 code => \&_cmd_secret,
2220 });
2221  
2222 __PACKAGE__->register(dev => {
2223 abstract => 'run under Hyperman, restarting when files change',
2224 desc => 'Run the application under Hyperman, restarting it when '
2225 . "anything under\nlib/, config/ or root/templates/ changes.",
2226 options => [
2227 { spec => 'port=i', arg => 'N', doc => 'listen port',
2228 default => 5000 },
2229 { spec => 'host=s', arg => 'ADDR', doc => 'listen address',
2230 default => '127.0.0.1' },
2231 { spec => 'workers=i', arg => 'N', doc => 'worker processes',
2232 default => 1 },
2233 { spec => 'watch=s@', arg => 'DIR',
2234 doc => 'watch this directory, repeatable (default: lib, config, '
2235 . 'root/templates)' },
2236 { spec => 'env=s', arg => 'NAME',
2237 doc => 'PUNK_ENV for the server (default: development)' },
2238 { spec => 'no-reload', doc => 'do not watch for changes' },
2239 ],
2240 15 ▶ code => sub { my ($opt) = @_; return dev(%$opt) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2241 67 });
2242  
2243 67 __PACKAGE__->register(serve => {
2244 abstract => 'serve a directory of files over HTTP',
2245 display => 'serve [DIR]',
2246 usage => '[DIR] [options]',
2247 desc => "Serve a directory of files under Hyperman. There is no "
2248 . "application involved -\nno app.psgi, no configuration, "
2249 . "nothing to generate. A directory resolves to\nits "
2250 . 'index.html, and --list renders one for a directory that '
2251 . 'has none.',
2252 options => [
2253 # displaces the shared --dir, which is about finding an application
2254 { spec => 'dir=s', arg => 'PATH',
2255 doc => 'the directory to serve, if not given as an argument',
2256 default => '.' },
2257 { spec => 'port=i', arg => 'N', doc => 'listen port',
2258 default => 8000 },
2259 { spec => 'host=s', arg => 'ADDR', doc => 'listen address',
2260 default => '127.0.0.1' },
2261 { spec => 'workers=i', arg => 'N', doc => 'worker processes',
2262 default => 1 },
2263 { spec => 'index=s', arg => 'NAME',
2264 doc => 'the file a directory resolves to', default => 'index.html' },
2265 { spec => 'no-index',
2266 doc => 'leave a directory as a 404 instead' },
2267 { spec => 'list',
2268 doc => 'render a listing for a directory with no index file' },
2269 { spec => 'max-age=i', arg => 'N',
2270 doc => 'Cache-Control freshness in seconds (default: revalidate '
2271 . 'every time)' },
2272 { spec => 'quiet', doc => 'no access log' },
2273 ],
2274 examples => [
2275 'punk serve',
2276 'punk serve ./public --port 3000',
2277 'punk serve --host 0.0.0.0 --list',
2278 ],
2279 hints => [ [ qr/Hyperman is not installed/,
2280 'cpanm Hyperman' ] ],
2281 code => \&_cmd_serve,
2282 });
2283  
2284 __PACKAGE__->register(version => {
2285 abstract => 'print the version',
2286 hidden => 1, # the list already shows punk --version
2287 1 ▶ code => sub { print "punk $Punk::VERSION\n"; return 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
2288 67 });
2289  
2290 67 1;
2291  
2292 __END__
↑ Summary blib/lib/Punk/App.pm »