blib/lib/Perinci/CmdLine/Plugin/Run/Normal.pm

filter: Statementstmt 81.3% 100 / 123 Branchbran 62.5% 35 / 56 Conditioncond 48.2% 27 / 56 MC/DCmcdc 26.4% 9 / 34 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 64.2% 180 / 280 CC 49 SCAR 48.2
CC
49
Cov
69%
on_run
144.3
BEGIN
1.0
BEGIN
1.0
CRAP 124.2
↑ Summary blib/lib/Perinci/CmdLine/Base.pm »
1 package Perinci::CmdLine::Plugin::Run::Normal;
2  
3 # put pragmas + Log::ger here
4 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 86 ▶use parent 'Perinci::CmdLine::PluginBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # put other modules alphabetically here
10 86 ▶use IO::Interactive qw(is_interactive);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # put global variables alphabetically here
13 86 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 86 our $DATE = '2023-11-23'; # DATE
15 86 our $DIST = 'Perinci-CmdLine-Lite'; # DIST
16 86 our $VERSION = '1.929'; # VERSION
17  
18 85 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
19 return {
20 85 summary => 'Normal run',
21 description => <<'_',
22  
23 This plugin is included by default, run at the `run` event for a normal run.
24  
25 _
26 conf => {
27 },
28 prio => 50, # normal
29 tags => ['category:run-handler'],
30 };
31 }
32  
33 82 ▶sub on_run {
Subroutine on_run: called CC=49 SCAR=49.7
Pod undocumented
34 82 my ($self, $r) = @_;
35  
36 82 my $co = $r->{common_opts};
37  
38 # set default from common options
39 82 ▶ $r->{naked_res} = $co->{naked_res}{default} if $co->{naked_res};
Branch: 1 branch
BranchTrueFalseTotal
if $co->{"naked_res"} 82 0 82
40 82 ▶ $r->{format} = $co->{format}{default} if $co->{format};
Branch: 1 branch
BranchTrueFalseTotal
if $co->{"format"} 82 0 82
41  
42 # EXPERIMENTAL, set default format to json if we are running in a pipeline
43 # and the right side of the pipe is the 'td' program
44 {
45 82 ▶ last if is_interactive(*STDOUT) || $r->{format};
Branch: 1 branch
BranchTrueFalseTotal
if is_interactive(*STDOUT) or $r->{"format"} 0 82 82
Condition33%1/3
is_interactive(*STDOUT) or $r->{"format"}
l!l&&r!l&&!r
0082
MC/DC0%0/2
is_interactive(*STDOUT) or $r->{"format"}
is_interactive(*STDOUT) $r->{"format"}
Truth table33%1/3
A or B
A= is_interactive(*STDOUT)B= $r->{"format"}
ABresult
1X1
011
000
46 82 ▶ last unless eval { require Pipe::Find; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Pipe::Find; 1 } } 0 82 82
47 82 my $pipeinfo = Pipe::Find::get_stdout_pipe_process();
48 82 ▶ last unless $pipeinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $pipeinfo 0 82 82
49 last unless $pipeinfo->{exe} =~ m![/\\]td\z! ||
50 82 ▶ $pipeinfo->{cmdline} =~ m!\A([^\0]*[/\\])?perl\0([^\0]*[/\\])?td\0!;
Branch: 1 branch
BranchTrueFalseTotal
unless $pipeinfo->{"exe"} =~ m([/\\]td\z) or $pipeinfo->{"cmdline"} =~ m(\A([^\0]*[/\\])?perl\0([^\0]*[/\\])?td\0) 82 0 82
Condition33%1/3
$pipeinfo->{"exe"} =~ m([/\\]td\z) or $pipeinfo->{"cmdline"} =~ m(\A([^\0]*[/\\])?perl\0([^\0]*[/\\])?td\0)
l!l&&r!l&&!r
0082
MC/DC0%0/2
$pipeinfo->{"exe"} =~ m([/\\]td\z) or $pipeinfo->{"cmdline"} =~ m(\A([^\0]*[/\\])?perl\0([^\0]*[/\\])?td\0)
$pipeinfo->{"exe"} =~ m([/\\]td\z) $pipeinfo->{"cmdline"} =~ m(\A([^\0]*[/\\])?perl\0([^\0]*[/\\])?td\0)
Truth table33%1/3
A or B
A= $pipeinfo->{"exe"} =~ m([/\\]td\z)B= $pipeinfo->{"cmdline"} =~ m(\A([^\0]*[/\\])?perl\0([^\0]*[/\\])?td\0)
ABresult
1X1
011
000
51 0 $r->{format} = 'json';
52 }
53  
54 82 ▶ $r->{format} //= $self->cmdline->default_format;
Condition33%1/3
$r->{"format"} //= $self->cmdline->default_format
l!l&&r!l&&!r
0082
MC/DC0%0/2
$r->{"format"} //= $self->cmdline->default_format
$r->{"format"} $self->cmdline->default_format
Truth table33%1/3
A //= B
A= $r->{"format"}B= $self->cmdline->default_format
ABresult
1X1
011
000
55  
56 82 ▶ if ($self->cmdline->read_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cmdline->read_config) 13 69 82
57 # note that we will be reading config file
58 13 $r->{read_config} = 1;
59 }
60  
61 82 ▶ if ($self->cmdline->read_env) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cmdline->read_env) 4 78 82
62 # note that we will be reading env for default options
63 4 $r->{read_env} = 1;
64 }
65  
66 82 eval {
67 82 log_trace("[pericmd] Running hook_before_run ...");
68 82 $self->cmdline->hook_before_run($r);
69  
70 82 log_trace("[pericmd] Running hook_before_parse_argv ...");
71 82 $self->cmdline->hook_before_parse_argv($r);
72  
73 82 my $parse_res = $self->cmdline->parse_argv($r);
74 81 ▶ if ($parse_res->[0] == 501) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parse_res->[0] == 501) { } 0 81 81
elsif ($parse_res->[0] != 200) { } 4 77 81
75 # we'll need to send ARGV to the server, because it's impossible to
76 # get args from ARGV (e.g. there's a cmdline_alias with CODE, which
77 # has been transformed into string when crossing network boundary)
78 0 $r->{send_argv} = 1;
79 } elsif ($parse_res->[0] != 200) {
80 4 die $parse_res;
81 }
82 77 $r->{parse_argv_res} = $parse_res;
83 77 ▶ $r->{args} = $parse_res->[2] // {};
Condition100%2/2
$parse_res->[2] // {}
l!l
716
MC/DC100%1/1
$parse_res->[2] // {}
$parse_res->[2]
Truth table100%2/2
A
A= $parse_res->[2]
Aresult
11
00
84  
85 # set defaults
86 77 ▶ $r->{action} //= 'call';
Condition100%2/2
$r->{"action"} //= "call"
l!l
671
MC/DC100%1/1
$r->{"action"} //= "call"
$r->{"action"}
Truth table100%2/2
A
A= $r->{"action"}
Aresult
11
00
87  
88 # init logging
89 77 ▶ if ($self->cmdline->log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cmdline->log) 0 77 77
90 0 require Log::ger::App;
91 0 my $default_level = do {
92 0 ▶ my $dry_run = $r->{dry_run} // $self->cmdline->default_dry_run;
Condition0%0/3
$r->{"dry_run"} // $self->cmdline->default_dry_run
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"dry_run"} // $self->cmdline->default_dry_run
$r->{"dry_run"} $self->cmdline->default_dry_run
Truth table0%0/3
A // B
A= $r->{"dry_run"}B= $self->cmdline->default_dry_run
ABresult
1X1
011
000
93 0 ▶ $dry_run ? 'info' : 'warn';
Branch: 1 branch
BranchTrueFalseTotal
$dry_run ? : 0 0 0
94 };
95 Log::ger::App->import(
96 0 ▶ level => $r->{log_level} // $self->cmdline->log_level // $default_level,
Condition0%0/6
($r->{"log_level"} // $self->cmdline->log_level) // $default_level
l!l&&r!l&&!r
000
$r->{"log_level"} // $self->cmdline->log_level
l!l&&r!l&&!r
000
MC/DC0%0/3
($r->{"log_level"} // $self->cmdline->log_level) // $default_level
$r->{"log_level"} $self->cmdline->log_level $default_level
Truth table0%0/4
A // B // C
A= $r->{"log_level"}B= $self->cmdline->log_levelC= $default_level
ABCresult
1XX1
01X1
0011
0000
97 name => $self->cmdline->program_name,
98 );
99 }
100  
101 77 log_trace("[pericmd] Running hook_after_parse_argv ...");
102 77 $self->cmdline->hook_after_parse_argv($r);
103  
104 77 $self->cmdline->parse_cmdline_src($r);
105  
106 #log_trace("TMP: parse_res: %s", $parse_res);
107  
108 69 my $missing = $parse_res->[3]{"func.missing_args"};
109 69 ▶ die [400, "Missing required argument(s): ".join(", ", @$missing)]
Branch: 1 branch
BranchTrueFalseTotal
if $missing and @$missing 2 67 69
Condition100%3/3
$missing and @$missing
!ll&&!rl&&r
6612
MC/DC100%2/2
$missing and @$missing
$missing @$missing
Truth table100%3/3
A and B
A= $missingB= @$missing
ABresult
0X0
100
111
110 if $missing && @$missing;
111  
112 67 my $scd = $r->{subcommand_data};
113 67 ▶ if ($scd->{pass_cmdline_object} // $self->cmdline->pass_cmdline_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scd->{"pass_cmdline_object"} // $self->cmdline->pass_cmdline_object) 0 67 67
Condition66%2/3
$scd->{"pass_cmdline_object"} // $self->cmdline->pass_cmdline_object
l!l&&r!l&&!r
6250
MC/DC0%0/2
$scd->{"pass_cmdline_object"} // $self->cmdline->pass_cmdline_object
$scd->{"pass_cmdline_object"} $self->cmdline->pass_cmdline_object
Truth table66%2/3
A // B
A= $scd->{"pass_cmdline_object"}B= $self->cmdline->pass_cmdline_object
ABresult
1X1
011
000
114 0 $r->{args}{-cmdline} = $self->cmdline;
115 0 $r->{args}{-cmdline_r} = $r;
116 }
117  
118 67 log_trace("[pericmd] Running hook_before_action ...");
119 67 $self->cmdline->hook_before_action($r);
120  
121 67 my $meth = "action_$r->{action}";
122 67 ▶ die [500, "Unknown action $r->{action}"] unless $self->cmdline->can($meth);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cmdline->can($meth) 0 67 67
123 67 log_trace("[pericmd] Running %s() ...", $meth);
124 $self->cmdline->_plugin_run_event(
125 name => 'action',
126 67 ▶ on_success => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 67 $r->{res} = $self->cmdline->$meth($r);
128 },
129 67 );
130  
131 67 log_trace("[pericmd] Running hook_after_action ...");
132 67 $self->cmdline->hook_after_action($r);
133 };
134  
135 82 my $err = $@;
136 82 ▶ if ($err || !$r->{res}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err or not $r->{'res'}) { } 15 67 82
elsif (ref $r->{'res'} ne 'ARRAY') { } 0 67 67
Condition66%2/3
$err or not $r->{"res"}
l!l&&r!l&&!r
15067
MC/DC50%1/2
$err or not $r->{"res"}
$err not $r->{"res"}
Truth table66%2/3
A or not(B)
A= $errB= not $r->{"res"}
ABresult
1X1
011
000
137 15 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 15 0 15
138 15 ▶ $err = [500, "Died: $err"] unless ref($err) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err eq "ARRAY" 0 15 15
139 15 ▶ if (%Devel::Confess::) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Devel::Confess::) 15 0 15
140 86 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 15 require Scalar::Util;
142 15 my $id = Scalar::Util::refaddr($err);
143 15 my $stack_trace = $Devel::Confess::MESSAGES{$id};
144 15 ▶ $err->[1] .= "\n$stack_trace" if $stack_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $stack_trace 0 15 15
145 }
146 15 $err->[1] =~ s/\n+$//;
147 15 $r->{res} = $err;
148 } else {
149 0 $r->{res} = [500, "Bug: no response produced"];
150 }
151 } elsif (ref($r->{res}) ne 'ARRAY') {
152 0 log_trace("[pericmd] res=%s", $r->{res}); #2
153 0 $r->{res} = [500, "Bug in program: result not an array"];
154 }
155  
156 82 ▶ if (!$r->{res}[0] || $r->{res}[0] < 200 || $r->{res}[0] > 555) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $r->{"res"}[0] or $r->{"res"}[0] < 200 or $r->{"res"}[0] > 555) 0 82 82
Condition33%2/6
not $r->{"res"}[0] or $r->{"res"}[0] < 200 or $r->{"res"}[0] > 555
l!l&&r!l&&!r
0082
not $r->{"res"}[0] or $r->{"res"}[0] < 200
l!l&&r!l&&!r
0082
MC/DC0%0/3
not $r->{"res"}[0] or $r->{"res"}[0] < 200 or $r->{"res"}[0] > 555
not $r->{"res"}[0] $r->{"res"}[0] < 200 $r->{"res"}[0] > 555
Truth table25%1/4
not(A) or B or C
A= not $r->{"res"}[0]B= $r->{"res"}[0] < 200C= $r->{"res"}[0] > 555
ABCresult
1XX1
01X1
0011
0000
157 0 $r->{res}[3]{'x.orig_status'} = $r->{res}[0];
158 0 $r->{res}[0] = 555;
159 }
160  
161 $r->{res}[3]{title} //= join(
162 " ",
163 $self->cmdline->program_name,
164 82 ▶ @{ $r->{orig_argv} // \@ARGV },
Condition50%2/4
$r->{"res"}[3]{"title"} //= join(" ", $self->cmdline->program_name, @{$r->{"orig_argv"} // \@ARGV;})
l!l
082
$r->{"orig_argv"} // \@ARGV
l!l
820
MC/DC0%0/1
$r->{"res"}[3]{"title"} //= join(" ", $self->cmdline->program_name, @{$r->{"orig_argv"} // \@ARGV;})
$r->{"res"}[3]{"title"}
MC/DC0%0/1
$r->{"orig_argv"} // \@ARGV
$r->{"orig_argv"}
Truth table50%1/2
A
A= $r->{"res"}[3]{"title"}
Aresult
11
00
Truth table50%1/2
A
A= $r->{"orig_argv"}
Aresult
11
00
165 );
166  
167 82 ▶ $r->{format} //= $r->{res}[3]{'cmdline.default_format'};
Condition100%3/3
$r->{"format"} //= $r->{"res"}[3]{"cmdline.default_format"}
l!l&&r!l&&!r
13168
MC/DC100%2/2
$r->{"format"} //= $r->{"res"}[3]{"cmdline.default_format"}
$r->{"format"} $r->{"res"}[3]{"cmdline.default_format"}
Truth table100%3/3
A //= B
A= $r->{"format"}B= $r->{"res"}[3]{"cmdline.default_format"}
ABresult
1X1
011
000
168 82 ▶ $r->{format} //= $r->{meta}{'cmdline.default_format'};
Condition66%2/3
$r->{"format"} //= $r->{"meta"}{"cmdline.default_format"}
l!l&&r!l&&!r
14068
MC/DC50%1/2
$r->{"format"} //= $r->{"meta"}{"cmdline.default_format"}
$r->{"format"} $r->{"meta"}{"cmdline.default_format"}
Truth table66%2/3
A //= B
A= $r->{"format"}B= $r->{"meta"}{"cmdline.default_format"}
ABresult
1X1
011
000
169 82 my $restore_orig_result;
170 my $orig_result;
171 82 ▶ if (exists $r->{res}[3]{'cmdline.result.noninteractive'} && !is_interactive(*STDOUT)) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $r->{'res'}[3]{'cmdline.result.noninteractive'} and not is_interactive(*STDOUT)) { } 0 82 82
elsif (exists $r->{'res'}[3]{'cmdline.result.interactive'} and is_interactive(*STDOUT)) { } 0 82 82
elsif (exists $r->{'res'}[3]{'cmdline.result'}) { } 1 81 82
Condition33%1/3
exists $r->{"res"}[3]{"cmdline.result.noninteractive"} and not is_interactive(*STDOUT)
!ll&&!rl&&r
8200
MC/DC0%0/2
exists $r->{"res"}[3]{"cmdline.result.noninteractive"} and not is_interactive(*STDOUT)
exists $r->{"res"}[3]{"cmdline.result.noninteractive"} not is_interactive(*STDOUT)
Truth table33%1/3
A and not(B)
A= exists $r->{"res"}[3]{"cmdline.result.noninteractive"}B= not is_interactive(*STDOUT)
ABresult
0X0
100
111
172 0 $restore_orig_result = 1;
173 0 $orig_result = $r->{res}[2];
174 0 $r->{res}[2] = $r->{res}[3]{'cmdline.result.noninteractive'};
175 ▶ } elsif (exists $r->{res}[3]{'cmdline.result.interactive'} && is_interactive(*STDOUT)) {
Condition33%1/3
exists $r->{"res"}[3]{"cmdline.result.interactive"} and is_interactive(*STDOUT)
!ll&&!rl&&r
8200
MC/DC0%0/2
exists $r->{"res"}[3]{"cmdline.result.interactive"} and is_interactive(*STDOUT)
exists $r->{"res"}[3]{"cmdline.result.interactive"} is_interactive(*STDOUT)
Truth table33%1/3
A and B
A= exists $r->{"res"}[3]{"cmdline.result.interactive"}B= is_interactive(*STDOUT)
ABresult
0X0
100
111
176 0 $restore_orig_result = 1;
177 0 $orig_result = $r->{res}[2];
178 0 $r->{res}[2] = $r->{res}[3]{'cmdline.result.interactive'};
179 } elsif (exists $r->{res}[3]{'cmdline.result'}) {
180 1 $restore_orig_result = 1;
181 1 $orig_result = $r->{res}[2];
182 1 $r->{res}[2] = $r->{res}[3]{'cmdline.result'};
183 }
184  
185 FORMAT:
186 82 $self->cmdline->_format($r);
187  
188 82 ▶ if ($restore_orig_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restore_orig_result) 1 81 82
189 1 $r->{res}[2] = $orig_result;
190 }
191  
192 82 my $exitcode;
193 82 ▶ if ($r->{res}[3] && defined($r->{res}[3]{'cmdline.exit_code'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'res'}[3] and defined $r->{'res'}[3]{'cmdline.exit_code'}) { } 1 81 82
Condition66%2/3
$r->{"res"}[3] and defined $r->{"res"}[3]{"cmdline.exit_code"}
!ll&&!rl&&r
0811
MC/DC50%1/2
$r->{"res"}[3] and defined $r->{"res"}[3]{"cmdline.exit_code"}
$r->{"res"}[3] defined $r->{"res"}[3]{"cmdline.exit_code"}
Truth table66%2/3
A and B
A= $r->{"res"}[3]B= defined $r->{"res"}[3]{"cmdline.exit_code"}
ABresult
0X0
100
111
194 1 $exitcode = $r->{res}[3]{'cmdline.exit_code'};
195 } else {
196 81 $exitcode = $self->cmdline->status2exitcode($r->{res}[0]);
197 }
198 82 ▶ if ($self->cmdline->exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cmdline->exit) { } 82 0 82
199 82 log_trace("[pericmd] exit(%s)", $exitcode);
200 82 exit $exitcode;
201 } else {
202 # so this can be tested
203 0 $r->{res}[3]{'x.perinci.cmdline.base.exit_code'} = $exitcode;
204 }
205  
206 0 $self->cmdline->_unsetup_progress_output;
207  
208 0 [201];
209 }
210  
211 86 1;
212 # ABSTRACT: Normal run
213  
214 __END__
↑ Summary blib/lib/Perinci/CmdLine/Base.pm »