blib/lib/Zuzu/Web/PSGI/CLI.pm

filter: Statementstmt 90.4% 104 / 115 Branchbran 83.3% 30 / 36 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 1 totaltotal 83.7% 160 / 191 CC 26 SCAR 18.0
CC
26
Cov
86%
run
12.7
_parse_options
5.1
_split_args
5.0
CRAP 6.1
« blib/lib/Zuzu/Value/Dict.pm ↑ Summary blib/lib/Zuzu/Test/ZPathFacelessPortDiagnostics.pm »
1 package Zuzu::Web::PSGI::CLI;
2  
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.007002';
8  
9 1 ▶use Getopt::Long qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 Configure
11 GetOptionsFromArray
12 1 );
13 1 ▶use Plack::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Zuzu::Web::PSGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶sub run {
Subroutine run: called CC=12 SCAR=25.4
Pod undocumented
17 7 my ( @argv ) = @_;
18 7 ▶ @argv = @ARGV if !@argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv 1 6 7
19  
20 7 binmode( *STDOUT, ':utf8' );
21 7 binmode( *STDERR, ':utf8' );
22  
23 7 my ( $zuzu_argv, $plack_args ) = _split_args(@argv);
24 7 my ( $options, $args, $usage_error ) = _parse_options($zuzu_argv);
25 7 ▶ if ( defined $usage_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $usage_error) 1 6 7
26 1 _print_usage($usage_error);
27 1 return 2;
28 }
29  
30 6 my ( $deny, $deny_modules, $value_error ) = _normalize_option_lists($options);
31 6 ▶ if ( defined $value_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_error) 0 6 6
32 0 _print_usage($value_error);
33 0 return 2;
34 }
35  
36 6 ▶ if ( $options->{show_help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"show_help"}) 1 5 6
37 1 _print_usage('');
38 1 return 0;
39 }
40  
41 5 my $script = shift @$args;
42 5 ▶ if ( !defined $script or $script eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $script or $script eq "") 1 4 5
Condition66%2/3
not defined $script or $script eq ""
l!l&&r!l&&!r
104
MC/DC50%1/2
not defined $script or $script eq ""
not defined $script $script eq ""
Truth table66%2/3
not(A) or B
A= not defined $scriptB= $script eq ""
ABresult
1X1
011
000
43 1 _print_usage('Missing ZuzuScript web application path');
44 1 return 2;
45 }
46 4 ▶ if ( @$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 0 4 4
47 0 _print_usage('Unexpected arguments before --');
48 0 return 2;
49 }
50  
51 4 my $app;
52 4 my $ok = eval {
53 $app = Zuzu::Web::PSGI->app(
54 script => $script,
55 lib => $options->{include_dirs},
56 deny => $deny,
57 deny_modules => $deny_modules,
58 debug_level => $options->{debug_level},
59 4 );
60 3 1;
61 };
62 4 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 3 4
63 1 my $error = $@;
64 1 print STDERR "$error";
65 1 ▶ print STDERR "\n" if defined $error and $error !~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error and not $error =~ /\n\z/ 0 1 1
Condition33%1/3
defined $error and not $error =~ /\n\z/
!ll&&!rl&&r
010
MC/DC0%0/2
defined $error and not $error =~ /\n\z/
defined $error not $error =~ /\n\z/
Truth table33%1/3
A and not(B)
A= defined $errorB= not $error =~ /\n\z/
ABresult
0X0
100
111
66 1 return 1;
67 }
68  
69 3 ▶ return 0 if $options->{check};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"check"} 1 2 3
70  
71 2 _run_plack( $app, $plack_args );
72 2 return 0;
73 }
74  
75 7 ▶sub _split_args {
Subroutine _split_args: called CC=5 SCAR=16.1
76 7 my ( @argv ) = @_;
77 7 my @zuzu_argv;
78 my @plack_args;
79 7 my $seen_separator = 0;
80  
81 7 for my $arg ( @argv ) {
82 18 ▶ if ( !$seen_separator and $arg eq '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $seen_separator and $arg eq "--") 1 17 18
Condition100%3/3
not $seen_separator and $arg eq "--"
!ll&&!rl&&r
4131
MC/DC100%2/2
not $seen_separator and $arg eq "--"
not $seen_separator $arg eq "--"
Truth table100%3/3
not(A) and B
A= not $seen_separatorB= $arg eq "--"
ABresult
0X0
100
111
83 1 $seen_separator = 1;
84 1 next;
85 }
86 17 ▶ if ($seen_separator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_separator) { } 4 13 17
87 4 push @plack_args, $arg;
88 }
89 else {
90 13 push @zuzu_argv, $arg;
91 }
92 }
93  
94 7 return ( \@zuzu_argv, \@plack_args );
95 }
96  
97 7 ▶sub _parse_options {
Subroutine _parse_options: called CC=5 SCAR=16.2
98 7 my ( $argv ) = @_;
99 7 @$argv = _normalize_debug_args(@$argv);
100  
101 7 my $options = {
102 debug_level => 0,
103 include_dirs => [],
104 deny_capabilities => [],
105 deny_modules => [],
106 check => 0,
107 show_help => 0,
108 };
109 7 my $debug_opt;
110  
111 7 Configure(
112 'no_ignore_case',
113 'bundling',
114 'require_order',
115 );
116 my $ok = GetOptionsFromArray(
117 $argv,
118 'd:s' => \$debug_opt,
119 'I=s@' => $options->{include_dirs},
120 'deny=s@' => $options->{deny_capabilities},
121 'denymodule=s@' => $options->{deny_modules},
122 'check' => \$options->{check},
123 'h|help' => \$options->{show_help},
124 7 );
125 7 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 7 7
126 0 return ( undef, undef, undef );
127 }
128  
129 7 ▶ if ( defined $debug_opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug_opt) 2 5 7
130 2 $debug_opt =~ s/\A=//;
131 2 ▶ $debug_opt = 1 if $debug_opt eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $debug_opt eq "" 0 2 2
132 2 ▶ return ( undef, undef, 'Debug level must be a non-negative integer' )
Branch: 1 branch
BranchTrueFalseTotal
unless $debug_opt =~ /\A(?:0|[1-9][0-9]*)\z/ 1 1 2
133 if $debug_opt !~ /\A(?:0|[1-9][0-9]*)\z/;
134 1 $options->{debug_level} = 0 + $debug_opt;
135 }
136  
137 6 return ( $options, $argv, undef );
138 }
139  
140 7 ▶sub _normalize_debug_args {
Subroutine _normalize_debug_args: called CC=2 SCAR=6.9
141 7 my ( @argv ) = @_;
142  
143 7 ▶ return map { $_ eq '-d' ? '-d=1' : $_ } @argv;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '-d' ? : 1 12 13
144 }
145  
146 6 ▶sub _normalize_option_lists {
Subroutine _normalize_option_lists: called CC=3 SCAR=11.2
147 6 my ( $options ) = @_;
148  
149 6 my @deny = _flatten_trimmed_csv( @{ $options->{deny_capabilities} } );
150 6 my @deny_modules = _flatten_trimmed_csv( @{ $options->{deny_modules} } );
151  
152 6 for my $entry ( @deny, @deny_modules ) {
153 4 ▶ if ( $entry =~ /\A\s*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A\s*\z/) 0 4 4
154 0 return ( undef, undef, 'Option values may not contain whitespace only' );
155 }
156 }
157  
158 6 return ( \@deny, \@deny_modules, undef );
159 }
160  
161 12 ▶sub _flatten_trimmed_csv {
Subroutine _flatten_trimmed_csv: called CC=2 SCAR=7.1
162 12 my ( @raw ) = @_;
163  
164 my @values = map {
165 4 s/^\s+//r =~ s/\s+$//r
166 } grep {
167 4 ▶ defined $_ and $_ ne ''
Condition33%1/3
defined $_ && $_ ne ''
!ll&&!rl&&r
004
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table33%1/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
168 } map {
169 12 split /,/
170 } @raw;
171  
172 12 return @values;
173 }
174  
175 0 ▶sub _run_plack {
Subroutine _run_plack: not called CC=1 SCAR=6.9
176 0 my ( $app, $plack_args ) = @_;
177  
178 0 my $runner = Plack::Runner->new;
179 0 $runner->parse_options( @$plack_args );
180 0 $runner->run($app);
181  
182 0 return;
183 }
184  
185 3 ▶sub _print_usage {
Subroutine _print_usage: called CC=3 SCAR=11.0
186 3 my ( $message ) = @_;
187  
188 3 ▶ if ( defined $message and $message ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and $message ne "") 2 1 3
Condition66%2/3
defined $message and $message ne ""
!ll&&!rl&&r
012
MC/DC50%1/2
defined $message and $message ne ""
defined $message $message ne ""
Truth table66%2/3
A and B
A= defined $messageB= $message ne ""
ABresult
0X0
100
111
189 2 print STDERR $message, "\n";
190 }
191 3 print STDERR
192 "Usage: zuzu-plackup.pl [options] path/to/app.zzs -- [plackup options]\n";
193 3 print STDERR "Options:\n";
194 3 print STDERR
195 " -d[=N] set debug level (default: 1 if omitted)\n";
196 3 print STDERR " -I/path/to/lib add module include directory\n";
197 3 print STDERR " --deny=CAP deny runtime capability (repeatable)\n";
198 3 print STDERR " --denymodule=MODULE deny a specific module (repeatable)\n";
199 3 print STDERR " --check load and validate the app, then exit\n";
200 3 print STDERR " -h, --help show this help\n";
201  
202 3 return;
203 }
204  
205 =pod
206  
207 =head1 NAME
208  
209 Zuzu::Web::PSGI::CLI - command-line runner for ZuzuScript PSGI apps
210  
211 =head1 DESCRIPTION
212  
213 Implements C<zuzu-plackup.pl>, a small convenience wrapper around
214 C<Zuzu::Web::PSGI> and C<Plack::Runner>.
215  
216 =head1 COPYRIGHT AND LICENCE
217  
218 B<< Zuzu::Web::PSGI::CLI >> is copyright Toby Inkster.
219  
220 It is free software; you may redistribute it and/or modify it under
221 the terms of either the Artistic License 1.0 or the GNU General Public
222 License version 2.
223  
224 =cut
225  
226 1 1;
« blib/lib/Zuzu/Value/Dict.pm ↑ Summary blib/lib/Zuzu/Test/ZPathFacelessPortDiagnostics.pm »