blib/lib/Punk/Generate.pm

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