blib/lib/Punk/Generate.pm

filter: Statementstmt 100% 261 / 261 Branchbran 83.9% 83 / 106 (24 uncoverable) Conditioncond 87.2% 58 / 86 (23 uncoverable) MC/DCmcdc 89.5% 19 / 48 (24 uncoverable) Subroutinesub 100% 43 / 43 Podpod 100% 12 / 12 totaltotal 94.0% 476 / 556 (71 uncoverable) CC 98 SCAR 21.0
CC
98
Cov
94%
_read_api
21.3
_schemes
13.0
run
11.0
CRAP 8.1
« blib/lib/Punk/Plugin/I18n.pm ↑ Summary blib/lib/Punk/Plugin/RequestId.pm »
1 package Punk::Generate;
2  
3 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Template::Stencil;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 our $VERSION = '0.70';
14  
15 46 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
16 46 my ($class, %args) = @_;
17  
18 46 my $name = $args{name};
19 46 ▶ Carp::croak('Punk::Generate: an application name is required')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 2 44 46
Condition100%3/3
defined $name and length $name
!ll&&!rl&&r
1144
MC/DC100%2/2
defined $name and length $name
defined $name length $name
Truth table100%3/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
20 unless defined $name && length $name;
21 44 ▶ Carp::croak("Punk::Generate: '$name' is not a legal Perl package name")
Branch: 1 branch
BranchTrueFalseTotal
unless _pkg_ok($name) 3 41 44
22 unless _pkg_ok($name);
23  
24 my $self = bless {
25 name => $name,
26 api => $args{api},
27 force => $args{force} ? 1 : 0,
28 dir => $args{dir},
29 41 ▶ written => [],
Branch: 1 branch
BranchTrueFalseTotal
$args{'force'} ? : 1 40 41
30 }, $class;
31  
32 41 ▶ $self->{dir} = _dist_dir($name) unless defined $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"dir"} 1 40 41
33 41 return $self;
34 }
35  
36 # The operation plan for a spec - groups, security schemes, the lot - without
37 # generating anything. `punk api sync` reuses it so the grouping rules have one
38 # implementation, not two that drift.
39 11 ▶sub api_plan {
Subroutine api_plan: called CC=1 SCAR=0.0
Pod documented
40 11 my ($class, $spec) = @_;
41 11 return _read_api(undef, $spec);
42 }
43  
44 # The package-name-safe identifier for a tag or a path segment, so a caller
45 # grouping operations reaches the same answer this does.
46 4 ▶sub ident {
Subroutine ident: called CC=1 SCAR=0.0
Pod documented
47 4 my ($class, $raw) = @_;
48 4 return _ident($raw);
49 }
50  
51 # Render one skeleton template to a string, or straight to a file. Same engine,
52 # same options, so a stub added later is identical to one generated up front.
53 # Class methods, and the search path below is the invocant's: called on
54 # Punk::Generate they see Punk's templates, called on a kit they see the kit's
55 # first and Punk's behind them.
56 249 ▶sub render_skel {
Subroutine render_skel: called CC=2 SCAR=6.9
Pod documented
57 249 my ($class, $template, $vars) = @_;
58 249 my ($dir) = $class->_find_skel($template);
59 # uncoverable condition right
60 248 ▶ return _engine_for($dir)->render($template, $vars || {});
Condition100%1/2
$vars || {}
l!l
2480
MC/DC100%0/1
$vars || {}
$vars
Truth table100%1/2
A
A= $vars
Aresult
11
00
61 }
62  
63 9 ▶sub write_skel {
Subroutine write_skel: called CC=3 SCAR=12.7
Pod documented
64 9 my ($class, $template, $abs, $vars) = @_;
65 9 File::Path::make_path(File::Basename::dirname($abs));
66 # uncoverable branch true
67 9 ▶ open my $fh, '>:encoding(UTF-8)', $abs
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $abs or Carp::croak("Punk::Generate: cannot write ${abs}: $!") 9 0 9
68 or Carp::croak("Punk::Generate: cannot write $abs: $!");
69 9 print $fh $class->render_skel($template, $vars);
70 # uncoverable branch true
71 9 ▶ close $fh or Carp::croak("Punk::Generate: cannot close $abs: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or Carp::croak("Punk::Generate: cannot close ${abs}: $!") 9 0 9
72 9 return $abs;
73 }
74  
75 # The variables every skeleton template is rendered with. A kit adds its own
76 # here rather than re-rendering the base tree with a second set: the templates
77 # it overrides by name are found through the search path and want the same
78 # hash the rest do.
79 31 ▶sub vars { return $_[1] }
Subroutine vars: called CC=1 SCAR=0.0
Pod documented
80  
81 1 ▶sub name { $_[0]{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
82 6 ▶sub dir { $_[0]{dir} }
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
83 34 ▶sub written { @{ $_[0]{written} } }
Subroutine written: called CC=1 SCAR=0.0
Pod documented
84  
85 39 ▶sub run {
Subroutine run: called CC=11 SCAR=24.0
Pod documented
86 39 my ($self) = @_;
87 39 my $dir = $self->{dir};
88  
89 39 ▶ if (-e $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) 4 35 39
90 # uncoverable branch true
91 4 ▶ Carp::croak("Punk::Generate: '$dir' exists and is not a directory")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 4 4
92 unless -d $dir;
93 Carp::croak("Punk::Generate: '$dir' is not empty")
94 4 ▶ if !$self->{force} && _has_entries($dir);
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"force"} and _has_entries($dir) 1 3 4
Condition100%3/3
not $self->{"force"} and _has_entries($dir)
!ll&&!rl&&r
121
MC/DC100%2/2
not $self->{"force"} and _has_entries($dir)
not $self->{"force"} _has_entries($dir)
Truth table100%3/3
not(A) and B
A= not $self->{"force"}B= _has_entries($dir)
ABresult
0X0
100
111
95 }
96  
97 # Everything that can fail on bad input fails before the first write: an
98 # unreadable or invalid spec should not leave half a tree behind.
99 38 ▶ my $api = $self->{api} ? $self->_read_api($self->{api}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'api'} ? : 18 20 38
100  
101 31 my $path = $self->{name};
102 31 $path =~ s{::}{/}g;
103  
104 my %vars = (
105 name => $self->{name},
106 path => $path,
107 lc_name => lc(($self->{name} =~ /([^:]+)\z/)[0]),
108 31 ▶ uc_name => uc(($self->{name} =~ /([^:]+)\z/)[0]),
Branch: 1 branch
BranchTrueFalseTotal
$api ? : 11 20 31
109 api => $api ? 1 : 0,
110 );
111 31 ▶ if ($api) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api) 11 20 31
112 11 $vars{first_op} = $api->{first_op};
113 11 $vars{schemes} = $api->{schemes};
114 11 $vars{auth_class} = $api->{auth_class};
115 }
116 31 $self->vars(\%vars); # a kit's own, for the templates it overrides
117  
118 31 $self->_render('app_psgi.tmpl', 'app.psgi', \%vars);
119 31 $self->_render('punk_yml.tmpl', 'config/punk.yml', \%vars);
120 31 $self->_render('app_class.tmpl', "lib/$path.pm", \%vars);
121 31 $self->_render('controller_web_root.tmpl',
122 "lib/$path/Controller/Web/Root.pm", \%vars);
123 31 $self->_render('basic_t.tmpl', 't/01-basic.t', \%vars);
124 31 $self->_render('readme_md.tmpl', 'README.md', \%vars);
125 31 $self->_render('gitignore.tmpl', '.gitignore', \%vars);
126  
127 31 $self->_copy('view/layout.tmpl', 'root/templates/layout.tmpl');
128 31 $self->_copy('view/welcome.tmpl', 'root/templates/welcome.tmpl');
129 31 $self->_copy('view/style.css', 'root/static/style.css');
130  
131 31 ▶ if ($api) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api) 11 20 31
132 11 $self->_copy_file($self->{api}, 'openapi.json');
133 11 for my $group (@{ $api->{groups} }) {
134 13 $self->_render('controller_api.tmpl',
135 "lib/$path/Controller/API/$group->{group}.pm",
136 { %vars, %$group });
137 }
138 $self->_render('controller_auth.tmpl',
139 "lib/$path/Controller/API/$api->{auth_class}.pm", \%vars)
140 11 ▶ if @{ $api->{schemes} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$api->{"schemes"};} 3 8 11
141 }
142  
143 31 chmod 0755, File::Spec->catfile($dir, 'app.psgi');
144 31 return $self->written;
145 }
146  
147 # ---- names -------------------------------------------------------------------
148  
149 # MyApp -> MyApp; My::App -> My-App, the CPAN convention for a distribution
150 # directory holding a nested package.
151 1 ▶sub _dist_dir {
Subroutine _dist_dir: called CC=1 SCAR=0.0
152 1 my ($name) = @_;
153 1 (my $dir = $name) =~ s/::/-/g;
154 1 return $dir;
155 }
156  
157 44 ▶sub _pkg_ok {
Subroutine _pkg_ok: called CC=1 SCAR=0.0
158 44 my ($name) = @_;
159 44 return $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
160 }
161  
162 # An operationId becomes `sub NAME {` in a generated controller, written
163 # with nothing escaped, so it has to BE a name. A specification is a document
164 # somebody else may have written, and anything else in that position is their
165 # Perl in your application.
166 107 ▶sub _op_id {
Subroutine _op_id: called CC=4 SCAR=16.1
167 107 my ($id) = @_;
168 # uncoverable condition right count:1
169 # uncoverable condition left count:2
170 107 ▶ Carp::croak("punk: operationId '" . ($id // '') . "' is not a Perl "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and $id =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ 6 101 107
Condition20%3/5
defined $id and $id =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
!ll&&!rl&&r
06101
$id // ""
l!l
60
MC/DC50%1/2
defined $id and $id =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
defined $id $id =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
MC/DC100%0/1
$id // ""
$id
Truth table33%2/3
A and B
A= defined $idB= $id =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
ABresult
0X0
100
111
Truth table0%1/2
A
A= $id
Aresult
11
00
171 . "identifier (a letter or underscore, then letters, digits "
172 . "and underscores), so no handler can be generated for it")
173 unless defined $id && $id =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/;
174 101 return $id;
175 }
176  
177 # A tag, a path segment or an operationId turned into something that can sit
178 # in a package name: word characters kept, everything else a boundary, each
179 # resulting piece capitalised. "books & authors" -> BooksAuthors.
180 87 ▶sub _ident {
Subroutine _ident: called CC=4 SCAR=13.9
181 87 my ($raw) = @_;
182 # uncoverable branch true
183 87 ▶ return '' unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 87 87
184 87 my @parts = grep { length } split /[^A-Za-z0-9]+/, $raw;
185 87 ▶ return '' unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 12 75 87
186 75 my $id = join '', map { ucfirst $_ } @parts;
187 # uncoverable branch true
188 75 ▶ $id = "N$id" if $id =~ /\A[0-9]/; # a package name cannot start with a digit
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /\A[0-9]/ 0 75 75
189 75 return $id;
190 }
191  
192 # ---- writing -----------------------------------------------------------------
193  
194 # Where a class keeps its templates: lib/<class path>/skel, beside the module.
195 # Punk::Generate -> Punk/Generate/skel, Punk::Kit::Diy -> Punk/Kit/Diy/skel.
196 # Read from %INC rather than computed from @INC, so a blib and an installed
197 # copy in the same @INC cannot disagree.
198 377 ▶sub skel_dir {
Subroutine skel_dir: called CC=3 SCAR=11.2
Pod documented
199 377 my ($class) = @_;
200 # uncoverable condition left
201 # uncoverable condition false
202 377 ▶ $class = ref $class || $class;
Condition100%1/3
ref $class || $class
l!l&&r!l&&!r
03770
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
203 377 (my $file = "$class.pm") =~ s{::}{/}g;
204 # uncoverable branch true
205 377 ▶ my $pm = $INC{$file}
Branch: 1 branch
BranchTrueFalseTotal
my $pm = $INC{$file} or Carp::croak("${class}: cannot locate my own installation") 377 0 377
206 or Carp::croak("$class: cannot locate my own installation");
207 377 (my $dir = $pm) =~ s/\.pm\z//;
208 377 return "$dir/skel";
209 }
210  
211 # The search path: every class in the inheritance chain that ships templates,
212 # most derived first. That is what lets a kit override one of Punk's templates
213 # by name and inherit the rest, rather than copying the whole skeleton to
214 # change a line of it.
215 343 ▶sub skel_dirs {
Subroutine skel_dirs: called CC=3 SCAR=11.0
Pod documented
216 343 my ($class) = @_;
217 # uncoverable condition false
218 343 ▶ $class = ref $class || $class;
Condition100%2/3
ref $class || $class
l!l&&r!l&&!r
329140
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
219 343 require mro;
220 377 return grep { -d }
221 # uncoverable branch false
222 377 ▶ map { $_->can('skel_dir') ? $_->skel_dir : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->can('skel_dir') ? : 377 0 377
223 343 @{ mro::get_linear_isa($class) };
224 }
225  
226 # The first directory on the path holding $rel, and the file inside it.
227 342 ▶sub _find_skel {
Subroutine _find_skel: called CC=4 SCAR=13.9
228 342 my ($class, $rel) = @_;
229 342 my @dirs = $class->skel_dirs;
230 342 for my $d (@dirs) {
231 361 my $abs = File::Spec->catfile($d, split m{/}, $rel);
232 361 ▶ return ($d, $abs) if -f $abs;
Branch: 1 branch
BranchTrueFalseTotal
if -f $abs 341 20 361
233 }
234 # uncoverable branch false
235 1 ▶ Carp::croak("Punk::Generate: no skeleton template '$rel' in "
Branch: 1 branch
BranchTrueFalseTotal
@dirs ? : 1 0 1
236 . (@dirs ? join(', ', @dirs) : '(no skeleton directories)'));
237 }
238  
239 # The engines. auto_escape => 0 because this generates Perl, YAML and psgi -
240 # escaping would turn every quote, & and < in the output into an entity.
241 # One per directory rather than one per process: Stencil takes a single
242 # template_dir, and it caches compiled templates, so a second generator over
243 # the same directory should not compile them again.
244 17 my %ENGINE;
245 248 ▶sub _engine_for {
Subroutine _engine_for: called CC=2 SCAR=6.9
246 248 my ($dir) = @_;
247 # uncoverable condition false
248 248 ▶ return $ENGINE{$dir} ||= Template::Stencil->new(
Condition100%2/3
$ENGINE{$dir} ||= "Template::Stencil"->new("template_dir", $dir, "auto_escape", 0, "chars", 1)
l!l&&r!l&&!r
233150
MC/DC100%0/2
$ENGINE{$dir} ||= "Template::Stencil"->new("template_dir", $dir, "auto_escape", 0, "chars", 1)
$ENGINE{$dir} "Template::Stencil"->new("template_dir", $dir, "auto_escape", 0, "chars", 1)
Truth table100%2/3
A ||= B
A= $ENGINE{$dir}B= "Template::Stencil"->new("template_dir", $dir, "auto_escape", 0, "chars", 1)
ABresult
1X1
011
000
249 template_dir => $dir,
250 auto_escape => 0,
251 chars => 1, # decoded text; the write below encodes once
252 );
253 }
254  
255 3 ▶sub _has_entries {
Subroutine _has_entries: called CC=3 SCAR=11.2
256 3 my ($dir) = @_;
257 # uncoverable branch true
258 3 ▶ opendir(my $dh, $dir) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return 0 3 0 3
259 3 ▶ my @e = grep { $_ ne '.' && $_ ne '..' } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
337
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
260 3 closedir $dh;
261 3 return scalar @e;
262 }
263  
264 340 ▶sub _dest {
Subroutine _dest: called CC=1 SCAR=0.0
265 340 my ($self, $rel) = @_;
266 340 my $abs = File::Spec->catfile($self->{dir}, split m{/}, $rel);
267 340 File::Path::make_path(File::Basename::dirname($abs));
268 340 return $abs;
269 }
270  
271 236 ▶sub _render {
Subroutine _render: called CC=3 SCAR=12.3
272 236 my ($self, $template, $rel, $vars) = @_;
273 236 my $abs = $self->_dest($rel);
274 # uncoverable branch true
275 236 ▶ open my $fh, '>:encoding(UTF-8)', $abs
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $abs or Carp::croak("Punk::Generate: cannot write ${abs}: $!") 236 0 236
276 or Carp::croak("Punk::Generate: cannot write $abs: $!");
277 236 print $fh $self->render_skel($template, $vars);
278 # uncoverable branch true
279 235 ▶ close $fh or Carp::croak("Punk::Generate: cannot close $abs: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or Carp::croak("Punk::Generate: cannot close ${abs}: $!") 235 0 235
280 235 $self->_wrote($rel);
281 235 return $abs;
282 }
283  
284 93 ▶sub _copy {
Subroutine _copy: called CC=1 SCAR=0.0
285 93 my ($self, $skel_rel, $rel) = @_;
286 93 my (undef, $src) = $self->_find_skel($skel_rel);
287 93 return $self->_copy_file($src, $rel);
288 }
289  
290 104 ▶sub _copy_file {
Subroutine _copy_file: called CC=2 SCAR=7.4
291 104 my ($self, $src, $rel) = @_;
292 104 my $abs = $self->_dest($rel);
293 # uncoverable branch true
294 104 ▶ File::Copy::copy($src, $abs)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($src, $abs) or Carp::croak("Punk::Generate: cannot copy $src to ${abs}: $!") 104 0 104
295 or Carp::croak("Punk::Generate: cannot copy $src to $abs: $!");
296 104 $self->_wrote($rel);
297 104 return $abs;
298 }
299  
300 # One entry per path, in the order it was first written. A kit that renders
301 # over one of the base skeleton's files - the point of the search path - must
302 # not have it listed twice.
303 339 ▶sub _wrote {
Subroutine _wrote: called CC=2 SCAR=6.9
304 339 my ($self, $rel) = @_;
305 339 push @{ $self->{written} }, $rel
306 # uncoverable branch false
307 339 ▶ unless grep { $_ eq $rel } @{ $self->{written} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $rel;} @{$$self{"written"};} 339 0 339
308 339 return;
309 }
310  
311 # ---- the OpenAPI side --------------------------------------------------------
312  
313 # Parse the spec through Open::API itself, so a duplicate or missing
314 # operationId, or an unresolvable $ref, is caught here - by the same code that
315 # will mount it - rather than surfacing at the application's first boot.
316 29 ▶sub _read_api {
Subroutine _read_api: called CC=21 SCAR=30.6
317 29 my ($self, $spec) = @_; # $self is unused, so api_plan can pass undef
318 29 ▶ Carp::croak("Punk::Generate: no such spec file: $spec") unless -f $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $spec 1 28 29
319  
320 28 require Open::API;
321 # uncoverable branch true
322 28 ▶ my $api = eval { Open::API->new(spec => $spec) }
Branch: 1 branch
BranchTrueFalseTotal
my $api = eval { do { "Open::API"->new("spec", $spec) } } or Carp::croak("Punk::Generate: $spec is not a usable OpenAPI " . "document: $@") 28 0 28
323 or Carp::croak("Punk::Generate: $spec is not a usable OpenAPI "
324 . "document: $@");
325  
326 28 my $ops = $api->operations;
327 # uncoverable condition left
328 28 ▶ Carp::croak("Punk::Generate: $spec declares no operations")
Branch: 1 branch
BranchTrueFalseTotal
unless $ops and @$ops 1 27 28
Condition100%2/3
$ops and @$ops
!ll&&!rl&&r
0127
MC/DC100%1/2
$ops and @$ops
$ops @$ops
Truth table100%2/3
A and B
A= $opsB= @$ops
ABresult
0X0
100
111
329 unless $ops && @$ops;
330  
331 # operations comes back in hash order, which perl randomises per process.
332 # Left alone that makes the generator non-deterministic - regenerating the
333 # same spec would shuffle subs between and within files, and every diff
334 # would be noise. Sort by path, then by method in the order a reader
335 # expects to meet them.
336 27 my %rank = (GET => 0, HEAD => 1, POST => 2, PUT => 3,
337 PATCH => 4, DELETE => 5, OPTIONS => 6, TRACE => 7);
338 my @sorted = sort {
339 27 $a->{path} cmp $b->{path}
340 || ($rank{ uc $a->{method} } // 99) <=> ($rank{ uc $b->{method} } // 99)
341 || $a->{method} cmp $b->{method}
342 # uncoverable branch true
343 # uncoverable condition right count:1
344 # uncoverable condition right count:2
345 # uncoverable condition false count:3
346 91 ▶ } @$ops;
Condition50%5/10
$Punk::Generate::a->{'path'} cmp $Punk::Generate::b->{'path'} || ($rank{uc $Punk::Generate::a->{'method'}} // 99) <=> ($rank{uc $Punk::Generate::b->{'method'}} // 99) || $Punk::Generate::a->{'method'} cmp $Punk::Generate::b->{'method'}
l!l&&r!l&&!r
9100
$Punk::Generate::a->{"path"} cmp $Punk::Generate::b->{"path"} or ($rank{uc $Punk::Generate::a->{"method"}} // 99) <=> ($rank{uc $Punk::Generate::b->{"method"}} // 99)
l!l&&r!l&&!r
75160
$rank{uc $Punk::Generate::a->{"method"}} // 99
l!l
160
$rank{uc $Punk::Generate::b->{"method"}} // 99
l!l
160
MC/DC66%0/3
$Punk::Generate::a->{'path'} cmp $Punk::Generate::b->{'path'} || ($rank{uc $Punk::Generate::a->{'method'}} // 99) <=> ($rank{uc $Punk::Generate::b->{'method'}} // 99) || $Punk::Generate::a->{'method'} cmp $Punk::Generate::b->{'method'}
$Punk::Generate::a->{"path"} cmp $Punk::Generate::b->{"path"} $rank{uc $Punk::Generate::a->{"method"}} $Punk::Generate::a->{'method'} cmp $Punk::Generate::b->{'method'}
MC/DC0%0/1
$rank{uc $Punk::Generate::b->{"method"}} // 99
$rank{uc $Punk::Generate::b->{"method"}}
Truth table50%2/4
A or B || C
A= $Punk::Generate::a->{"path"} cmp $Punk::Generate::b->{"path"}B= $rank{uc $Punk::Generate::a->{"method"}}C= $Punk::Generate::a->{'method'} cmp $Punk::Generate::b->{'method'}
ABCresult
1XX1
01X1
0011
0000
Truth table50%1/2
A
A= $rank{uc $Punk::Generate::b->{"method"}}
Aresult
11
00
347 27 $ops = \@sorted;
348  
349 27 my $doc = $api->spec;
350 27 my (%by_group, @order);
351  
352 # Group by tag when the document uses tags at all, by first path segment
353 # when it does not - a spec with no tags still deserves more than one
354 # bucket, and one with tags has already told us how its author groups it.
355 27 my $tagged = 0;
356 27 for my $op (@$ops) {
357 49 ▶ next unless @{ _tags_for($doc, $op) };
Branch: 1 branch
BranchTrueFalseTotal
unless @{_tags_for($doc, $op);} 40 9 49
358 9 $tagged = 1;
359 9 last;
360 }
361  
362 27 for my $op (@$ops) {
363 my $label = $tagged ? (_tags_for($doc, $op)->[0] // '')
364 # uncoverable condition right count:2
365 82 ▶ : (grep { length } split m{/}, $op->{path})[0] // '';
Branch: 1 branch
BranchTrueFalseTotal
$tagged ? : 42 40 82
Condition100%3/4
_tags_for($doc, $op)->[0] // ""
l!l
3012
(grep {length $_;} split(m[/], $op->{"path"}, 0))[0] // ""
l!l
400
MC/DC100%1/1
_tags_for($doc, $op)->[0] // ""
_tags_for($doc, $op)->[0]
MC/DC100%0/1
(grep {length $_;} split(m[/], $op->{"path"}, 0))[0] // ""
(grep {length $_;} split(m[/], $op->{"path"}, 0))[0]
Truth table100%2/2
A
A= _tags_for($doc, $op)->[0]
Aresult
11
00
Truth table100%1/2
A
A= (grep {length $_;} split(m[/], $op->{"path"}, 0))[0]
Aresult
11
00
366 82 ▶ my $group = _ident($label) || 'Default';
Condition100%2/2
_ident($label) || 'Default'
l!l
7012
MC/DC100%1/1
_ident($label) || 'Default'
_ident($label)
Truth table100%2/2
A
A= _ident($label)
Aresult
11
00
367 82 ▶ push @order, $group unless $by_group{$group};
Branch: 1 branch
BranchTrueFalseTotal
unless $by_group{$group} 41 41 82
368 82 my $node = _op_node($doc, $op);
369 82 push @{ $by_group{$group}{ops} }, {
370 id => _op_id($op->{operationId}),
371 method => uc $op->{method},
372 82 path => _one_line($op->{path}),
373 summary => _summary($node),
374 };
375 76 $by_group{$group}{group} = $group;
376 76 $by_group{$group}{group_label} = _one_line($label);
377 76 ▶ $by_group{$group}{tagged} = $tagged ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tagged ? : 42 34 76
378 }
379  
380 # Every securityScheme the document actually requires needs a checker in
381 # the mount's security option, or Punk croaks at boot naming it. So the
382 # scaffolder owes one stub per scheme - an application generated from a
383 # secured spec has to start.
384 21 my @schemes = _schemes($doc, $ops);
385 21 my $auth_class;
386 21 ▶ if (@schemes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@schemes) 9 12 21
387 # A tag called Auth would already own that file; take the first free
388 # name rather than clobbering a controller full of operations.
389 9 my %taken = map { $_ => 1 } @order;
390 9 ($auth_class) = grep { !$taken{$_} } qw(Auth Security Authentication);
391 # uncoverable condition right
392 9 ▶ $auth_class ||= 'AuthSchemes';
Condition100%1/2
$auth_class ||= "AuthSchemes"
l!l
90
MC/DC100%0/1
$auth_class ||= "AuthSchemes"
$auth_class
Truth table100%1/2
A
A= $auth_class
Aresult
11
00
393 9 $_->{class} = $auth_class for @schemes;
394 }
395  
396 21 my $first = $ops->[0];
397 21 _op_id($first->{operationId});
398 return {
399 # by name, for the same reason the operations are sorted: which class
400 # is written first should not depend on hash order
401 35 groups => [ map { $by_group{$_} } sort @order ],
402 schemes => \@schemes,
403 auth_class => $auth_class,
404 # The generated test needs a route it can prove is mounted. A path
405 # template's placeholders get a filler so it is a real request path.
406 first_op => {
407 id => $first->{operationId},
408 method => _one_line(uc $first->{method}),
409 path => _one_line($first->{path}),
410 # it sits inside single quotes in the generated test
411 21 path_literal => _perl_q(_fill_path($first->{path})),
412 },
413 };
414 }
415  
416 # The securityScheme names the document actually requires - the union of the
417 # global `security` and every operation's own - paired with their definitions.
418 # A scheme defined in components but never required needs no checker, and
419 # generating one would only be noise.
420 21 ▶sub _schemes {
Subroutine _schemes: called CC=13 SCAR=25.6
421 21 my ($doc, $ops) = @_;
422 21 ▶ my $defs = ($doc->{components} || {})->{securitySchemes} || {};
Condition100%4/4
+($doc->{'components'} || {})->{'securitySchemes'} || {}
l!l
1011
$doc->{'components'} || {}
l!l
192
MC/DC100%1/1
+($doc->{'components'} || {})->{'securitySchemes'} || {}
$doc->{'components'}
Truth table100%2/2
A
A= $doc->{'components'}
Aresult
11
00
423 21 my (%want, @order);
424  
425 97 ▶ my $collect = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
426 97 my ($req) = @_;
427 97 ▶ return unless ref $req eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $req eq "ARRAY" 87 10 97
428 10 for my $alt (@$req) {
429 # uncoverable branch true
430 10 ▶ next unless ref $alt eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $alt eq "HASH" 0 10 10
431 10 for my $name (sort keys %$alt) {
432 # uncoverable branch false
433 10 ▶ push @order, $name unless $want{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $want{$name}++ 10 0 10
434 }
435 }
436 21 };
437 21 $collect->($doc->{security});
438 21 for my $op (@$ops) {
439 76 my $node = _op_node($doc, $op);
440 # uncoverable branch false
441 76 ▶ $collect->($node->{security}) if $node;
Branch: 1 branch
BranchTrueFalseTotal
if $node 76 0 76
442 }
443  
444 21 my @out;
445 21 for my $name (@order) {
446 # uncoverable branch false
447 10 ▶ my $d = ref $defs->{$name} eq 'HASH' ? $defs->{$name} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $defs->{$name} eq 'HASH' ? : 10 0 10
448 push @out, {
449 # Spec strings land in comment lines, so none may carry a
450 # newline; the one that lands inside single quotes (the mount
451 # key in app_class.tmpl) is name_q, escaped for that spot.
452 name => _one_line($name),
453 name_q => _perl_q($name),
454 # The mount key stays the spec's name. The sub keeps it too when
455 # it is already a legal identifier - which is almost always - and
456 # is only reshaped when the spec used something like 'api-key'.
457 method => ($name =~ /\A[A-Za-z_]\w*\z/
458 ? $name : (_ident($name) || 'check')),
459 type => _one_line($d->{type} || ''),
460 in => _one_line($d->{in} || ''),
461 key => _one_line($d->{name} || ''),
462 # uncoverable condition right count:1
463 # uncoverable condition right count:2
464 10 ▶ scheme => _one_line(lc($d->{scheme} || '')),
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_]\w*\z/ ? : 9 1 10
Condition100%8/10
_ident($name) or "check"
l!l
10
$d->{'type'} || ''
l!l
100
$d->{'in'} || ''
l!l
82
$d->{'name'} || ''
l!l
82
$d->{'scheme'} || ''
l!l
28
MC/DC100%0/1
_ident($name) or "check"
_ident($name)
MC/DC100%0/1
$d->{'type'} || ''
$d->{'type'}
MC/DC100%1/1
$d->{'in'} || ''
$d->{'in'}
MC/DC100%1/1
$d->{'name'} || ''
$d->{'name'}
MC/DC100%1/1
$d->{'scheme'} || ''
$d->{'scheme'}
Truth table100%1/2
A
A= _ident($name)
Aresult
11
00
Truth table100%1/2
A
A= $d->{'type'}
Aresult
11
00
Truth table100%2/2
A
A= $d->{'in'}
Aresult
11
00
Truth table100%2/2
A
A= $d->{'name'}
Aresult
11
00
Truth table100%2/2
A
A= $d->{'scheme'}
Aresult
11
00
465 description => _summary($d),
466 credential => _one_line(_credential_of($d)),
467 };
468 }
469 21 return @out;
470 }
471  
472 # What arrives as the checker's first argument, per scheme shape - the one
473 # thing someone filling in a stub most needs to know.
474 14 ▶sub _credential_of {
Subroutine _credential_of: called CC=11 SCAR=24.0
475 14 my ($d) = @_;
476 # uncoverable condition right
477 14 ▶ my $type = $d->{type} || '';
Condition100%1/2
$d->{'type'} || ''
l!l
140
MC/DC100%0/1
$d->{'type'} || ''
$d->{'type'}
Truth table100%1/2
A
A= $d->{'type'}
Aresult
11
00
478 14 ▶ if ($type eq 'apiKey') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "apiKey") 8 6 14
479 # uncoverable condition right
480 8 ▶ my $where = $d->{in} || 'header';
Condition100%1/2
$d->{'in'} || 'header'
l!l
80
MC/DC100%0/1
$d->{'in'} || 'header'
$d->{'in'}
Truth table100%1/2
A
A= $d->{'in'}
Aresult
11
00
481 # uncoverable condition right
482 8 ▶ my $key = $d->{name} // '';
Condition100%1/2
$d->{"name"} // ""
l!l
80
MC/DC100%0/1
$d->{"name"} // ""
$d->{"name"}
Truth table100%1/2
A
A= $d->{"name"}
Aresult
11
00
483 8 return "the $where '$key'";
484 }
485 6 ▶ if ($type eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "http") 3 3 6
486 # uncoverable condition right
487 3 ▶ my $s = lc($d->{scheme} || '');
Condition100%1/2
$d->{'scheme'} || ''
l!l
30
MC/DC100%0/1
$d->{'scheme'} || ''
$d->{'scheme'}
Truth table100%1/2
A
A= $d->{'scheme'}
Aresult
11
00
488 # uncoverable branch true
489 3 ▶ return 'the base64-decoded "user:password" from the Authorization '
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "basic" 0 3 3
490 . 'header' if $s eq 'basic';
491 3 ▶ return 'the token from the Authorization header, without the '
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "bearer" 2 1 3
492 . '"Bearer " prefix' if $s eq 'bearer';
493 1 return "the Authorization header's credential";
494 }
495 3 ▶ return 'the access token' if $type eq 'oauth2' || $type eq 'openIdConnect';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "oauth2" or $type eq "openIdConnect" 2 1 3
Condition100%3/3
$type eq "oauth2" or $type eq "openIdConnect"
l!l&&r!l&&!r
111
MC/DC100%2/2
$type eq "oauth2" or $type eq "openIdConnect"
$type eq "oauth2" $type eq "openIdConnect"
Truth table100%3/3
A or B
A= $type eq "oauth2"B= $type eq "openIdConnect"
ABresult
1X1
011
000
496 1 return 'the credential';
497 }
498  
499 249 ▶sub _op_node {
Subroutine _op_node: called CC=4 SCAR=13.9
500 249 my ($doc, $op) = @_;
501 # uncoverable condition left
502 # uncoverable condition right
503 249 ▶ my $p = $doc->{paths} && $doc->{paths}{ $op->{path} };
Condition100%1/3
$doc->{'paths'} && $doc->{'paths'}{$op->{'path'}}
!ll&&!rl&&r
00249
MC/DC100%0/2
$doc->{'paths'} && $doc->{'paths'}{$op->{'path'}}
$doc->{'paths'} $doc->{'paths'}{$op->{'path'}}
Truth table100%1/3
A && B
A= $doc->{'paths'}B= $doc->{'paths'}{$op->{'path'}}
ABresult
0X0
100
111
504 # uncoverable branch true
505 249 ▶ return undef unless ref $p eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" 0 249 249
506 249 my $node = $p->{ lc $op->{method} };
507 # uncoverable branch false
508 249 ▶ return ref $node eq 'HASH' ? $node : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $node eq 'HASH' ? : 249 0 249
509 }
510  
511 91 ▶sub _tags_for {
Subroutine _tags_for: called CC=4 SCAR=14.3
512 91 my ($doc, $op) = @_;
513 91 my $node = _op_node($doc, $op);
514 # uncoverable condition left
515 91 ▶ my $tags = $node && $node->{tags};
Condition100%2/3
$node && $node->{'tags'}
!ll&&!rl&&r
05239
MC/DC100%1/2
$node && $node->{'tags'}
$node $node->{'tags'}
Truth table100%2/3
A && B
A= $nodeB= $node->{'tags'}
ABresult
0X0
100
111
516 # uncoverable branch false count:1
517 91 ▶ return ref $tags eq 'ARRAY' ? [ grep { defined && length } @$tags ] : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $tags eq 'ARRAY' ? : 39 52 91
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0039
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
518 }
519  
520 86 ▶sub _summary {
Subroutine _summary: called CC=6 SCAR=17.9
521 86 my ($node) = @_;
522 # uncoverable branch true
523 86 ▶ return '' unless ref $node eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 86 86
524 86 my $s = $node->{summary};
525 # uncoverable condition right
526 86 ▶ $s = $node->{description} unless defined $s && length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 56 30 86
Condition100%2/3
defined $s and length $s
!ll&&!rl&&r
56030
MC/DC100%1/2
defined $s and length $s
defined $s length $s
Truth table100%2/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
527 # uncoverable condition right
528 86 ▶ return '' unless defined $s && length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 56 30 86
Condition100%2/3
defined $s and length $s
!ll&&!rl&&r
56030
MC/DC100%1/2
defined $s and length $s
defined $s length $s
Truth table100%2/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
529 30 $s =~ s/\s+/ /g; # one line, for a comment and a POD para
530 30 $s =~ s/\A\s+|\s+\z//g;
531 30 $s =~ s/"/'/g; # it lands inside a double-quoted comment
532 30 return $s;
533 }
534  
535 # A spec string that lands on a comment line of generated Perl: a newline in
536 # it would end the comment, and a control byte would hide what follows.
537 287 ▶sub _one_line {
Subroutine _one_line: called CC=2 SCAR=6.9
538 287 my ($s) = @_;
539 # uncoverable branch true
540 287 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 287 287
541 287 $s =~ s/[\x00-\x1f\x7f]+/ /g;
542 287 return $s;
543 }
544  
545 # A spec string that lands inside single quotes in generated Perl.
546 32 ▶sub _perl_q {
Subroutine _perl_q: called CC=1 SCAR=0.0
547 32 my ($s) = @_;
548 32 $s = _one_line($s);
549 32 $s =~ s/([\\'])/\\$1/g;
550 32 return $s;
551 }
552  
553 # /books/{id} -> /books/1, so the generated test can request it.
554 21 ▶sub _fill_path {
Subroutine _fill_path: called CC=1 SCAR=0.0
555 21 my ($path) = @_;
556 21 $path =~ s/\{[^}]*\}/1/g;
557 21 return $path;
558 }
559  
560 17 1;
561  
562 __END__
« blib/lib/Punk/Plugin/I18n.pm ↑ Summary blib/lib/Punk/Plugin/RequestId.pm »