.github/dzil-binary/pp-args.pl

filter: Statementstmt 93.2% 83 / 89 Branchbran 73.8% 31 / 42 Conditioncond 46.1% 6 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 80.8% 131 / 162 CC 28 SCAR 20.6
CC
28
Cov
83%
argv
11.4
run_in
10.7
run_in_win32
8.1
CRAP 7.8
« blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Docker.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/Author/GETTY/InstallCheck.pm »
1 #!/usr/bin/env perl
2 # The full pp argv of one binary, NUL-terminated entries.
3 # pp-args.pl <META.json> <name> <out> <dist_root>
4 package DzilBinary::PPArgs;
5 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 45 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 45 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Same line rule as the bundle's local check: split on "\n" only, strip one
12 # trailing "\r", drop empty lines; one line is one argument.
13 4 ▶sub pp_args_lines {
Subroutine pp_args_lines: called CC=1 SCAR=0.0
14 4 my ($output) = @_;
15 4 return grep { length } map { s/\r\z//r } split /\n/, $output;
16 }
17  
18 # Same -M rule as the bundle's local check: each module as -M, a trailing ::*
19 # as ::**, every module below at any depth (pp takes one level for ::*), the
20 # namespace require-gate.pl checks for Glob::*.
21 499 ▶sub pp_module_args {
Subroutine pp_module_args: called CC=1 SCAR=0.0
22 499 my (@modules) = @_;
23 499 return map { ('-M', s/::\*\z/::**/r) } @modules;
24 }
25  
26 # Runs `sh -c <cmd>` in $dir without an argument; dies if it fails.
27 47 ▶sub run_in {
Subroutine run_in: called CC=9 SCAR=23.7
28 47 my ($dir, $cmd) = @_;
29 47 ▶ return run_in_win32($dir, $cmd) if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 27 20 47
30 20 ▶ pipe(my $r, my $w) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $r, my $w or die "pipe: $!\n" 20 0 20
31 20 ▶ my $pid = fork // die "fork: $!\n";
Condition50%1/2
fork // die("fork: $!\n")
l!l
200
MC/DC0%0/1
fork // die("fork: $!\n")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
32 20 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 8 12 20
33 8 close $r;
34 0 ▶ open STDOUT, '>&', $w or do { print STDERR "cannot redirect stdout: $!\n"; POSIX::_exit(127) };
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $w or do { print STDERR "cannot redirect stdout: $!\n"; POSIX::_exit(127) } 8 0 8
35 0 ▶ chdir $dir or do { print STDERR "cannot chdir to $dir: $!\n"; POSIX::_exit(127) };
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or do { print STDERR "cannot chdir to ${dir}: $!\n"; POSIX::_exit(127) } 8 0 8
36 8 { exec 'sh', '-c', $cmd }
37 0 print STDERR "cannot exec sh: $!\n"; POSIX::_exit(127);
38 }
39 12 close $w;
40 12 my $out = do { local $/; <$r> };
41 12 close $r;
42 12 waitpid $pid, 0;
43 12 ▶ die "binary_pp_args failed (exit " . ($? >> 8) . "): $cmd\n" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 3 9 12
44 9 ▶ return $out // '';
Condition50%1/2
$out // ""
l!l
90
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
45 }
46  
47 # Win32: fork is emulated there and pipe handles are not duplicated reliably
48 # into the pseudo-process (perlfork, BUGS), so the pipe + fork + exec above never
49 # sees EOF (the same fix as run_tool_win32 in lib-gate.pl). sh is started with a
50 # list-form pipe open from $dir; the parent's cwd is restored right after.
51 # $cmd does not travel on the command line: Perl joins the list into one
52 # CreateProcess command line without escaping an embedded " (win32.c,
53 # create_command_line), and the MSYS2 sh splits it again by rules of its own
54 # (' quotes too, \\ becomes \, words are globbed). It goes in the environment;
55 # the fixed script holds neither " nor \, and with IFS empty and globbing off
56 # the variable expands to exactly one word: a fresh sh -c <cmd>, as on Unix.
57 23 ▶sub run_in_win32 {
Subroutine run_in_win32: called CC=8 SCAR=20.9
58 23 my ($dir, $cmd) = @_;
59 23 ▶ return '' unless length $cmd; # sh -c '' prints nothing; exec sh -c without a word fails
Branch: 1 branch
BranchTrueFalseTotal
unless length $cmd 2 21 23
60 21 ▶ my $back = Cwd::getcwd() // die "cannot read the current directory: $!\n";
Condition50%1/2
Cwd::getcwd() // die("cannot read the current directory: $!\n")
l!l
210
MC/DC0%0/1
Cwd::getcwd() // die("cannot read the current directory: $!\n")
Cwd::getcwd()
Truth table50%1/2
A
A= Cwd::getcwd()
Aresult
11
00
61 21 ▶ chdir $dir or die "cannot chdir to $dir: $!\nbinary_pp_args failed: $cmd\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or die "cannot chdir to ${dir}: $!\nbinary_pp_args failed: $cmd\n" 19 2 21
62 19 my ($fh, $why);
63 19 my $started = do {
64 19 local $ENV{DZIL_BINARY_PP_ARGS} = $cmd;
65 19 my $ok = open $fh, '-|', 'sh', '-c', 'IFS=; set -f; exec sh -c $DZIL_BINARY_PP_ARGS';
66 19 $why = "$!"; # before the local is undone: restoring %ENV may clobber errno
67 19 $ok;
68 };
69 19 ▶ chdir $back or die "cannot chdir back to $back: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $back or die "cannot chdir back to ${back}: $!\n" 19 0 19
70 19 ▶ die "cannot run sh: $why\nbinary_pp_args failed: $cmd\n" unless $started;
Branch: 1 branch
BranchTrueFalseTotal
unless $started 2 17 19
71 17 my $out = do { local $/; <$fh> };
72 17 close $fh; # false on a non-zero exit; $? holds the status either way
73 17 ▶ die "binary_pp_args failed (exit " . ($? >> 8) . "): $cmd\n" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 2 15 17
74 15 ▶ return $out // '';
Condition50%1/2
$out // ""
l!l
150
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
75 }
76  
77 45 ▶sub argv {
Subroutine argv: called CC=10 SCAR=24.4
78 45 my ($meta_file, $name, $out, $dist_root) = @_;
79 45 ▶ open my $fh, '<:raw', $meta_file or die "cannot read $meta_file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $meta_file or die "cannot read ${meta_file}: $!\n" 45 0 45
80 45 my $meta = eval { JSON::PP->new->decode(do { local $/; <$fh> }) };
81 45 ▶ die "$meta_file is not valid JSON: $@" unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 0 45 45
82 45 ▶ my $bins = ref $meta->{x_binaries} eq 'HASH' ? $meta->{x_binaries}{binaries} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $meta->{'x_binaries'} eq 'HASH' ? : 45 0 45
83 45 ▶ my $b = ref $bins eq 'HASH' ? $bins->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $bins eq 'HASH' ? : 45 0 45
84 45 ▶ die "no binary '$name' in x_binaries of $meta_file\n" unless ref $b eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq "HASH" 1 44 45
85 44 ▶ die "binary '$name' has no script\n" unless defined $b->{script} && length $b->{script};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b->{"script"} and length $b->{"script"} 0 44 44
Condition33%1/3
defined $b->{"script"} and length $b->{"script"}
!ll&&!rl&&r
0044
MC/DC0%0/2
defined $b->{"script"} and length $b->{"script"}
defined $b->{"script"} length $b->{"script"}
Truth table33%1/3
A and B
A= defined $b->{"script"}B= length $b->{"script"}
ABresult
0X0
100
111
86  
87 44 ▶ my @extra = defined $b->{pp_args} ? pp_args_lines(run_in($dist_root, $b->{pp_args})) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $b->{'pp_args'} ? : 13 31 44
88 35 ▶ return ('-o', $out, '-I', 'lib', pp_module_args(@{ $b->{modules} || [] }), @extra, $b->{script});
Condition50%1/2
$b->{'modules'} || []
l!l
350
MC/DC0%0/1
$b->{'modules'} || []
$b->{'modules'}
Truth table50%1/2
A
A= $b->{'modules'}
Aresult
11
00
89 }
90  
91 16 ▶sub main {
Subroutine main: called CC=4 SCAR=13.9
92 16 my (@args) = @_;
93 16 ▶ die "usage: pp-args.pl <META.json> <name> <out> <dist_root>\n" unless @args == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 4 0 16 16
94 16 my @argv = eval { argv(@args) };
95 10 ▶ if ($@) { print STDERR $@; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 6 10
96 6 binmode STDOUT;
97 6 print "$_\0" for @argv;
98 6 return 0;
99 }
100  
101 16 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 16 0 16
102 1;
« blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Docker.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/Author/GETTY/InstallCheck.pm »