Command.pm

filter: Statementstmt 100% 106 / 106 Branchbran 98.2% 55 / 56 Conditioncond 79.3% 23 / 29 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 94.0% 207 / 220 CC 41 SCAR 30.3
CC
41
Cov
96%
command
30.0
is_exit
12.1
BEGIN
1.0
CRAP 20.7
↑ Summary Command/ConfigData.pm »
1 # -*- Perl -*-
2 #
3 # run simple unix commands for expected results given particular inputs
4  
5 package Test2::Tools::Command;
6 3 our $VERSION = '0.20';
7  
8 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::chdir; # $CWD
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IPC::Open3 'open3';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Test2::API 'context';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 our @EXPORT = qw(command is_exit);
18  
19 3 our @command; # prefixed on each run, followed by any ->{args}
20 3 our $timeout = 30; # seconds, for alarm()
21  
22 9 ▶sub command ($) {
Subroutine command: called CC=30 SCAR=34.0
Pod documented
23 9 ▶ local $CWD = $_[0]->{chdir} if defined $_[0]->{chdir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"chdir"} 2 7 9
24 8 local @ENV{ keys %{ $_[0]->{env} } } = values %{ $_[0]->{env} };
25  
26 # what to run, and possibly also a string to include in the test name
27 8 ▶ my @cmd = ( @command, exists $_[0]->{args} ? @{ $_[0]->{args} } : () );
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'args'} ? : 7 1 8
28  
29 8 my ( $stdout, $stderr );
30 8 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"ALRM"} = sub { die "timeout\n"; } ; alarm($_[0]{"timeout"} || $Test2::Tools::Command::timeout); my $pid = open3(my $in, my $out, my $err = &gensym(), @cmd); if (defined $_[0]{"binmode"}) { foreach my $fh () { binmode $fh, $_[0]{"binmode"}; } ; } ; if (exists $_[0]{"stdin"}) { print $in $_[0]{"stdin"}; close $in; } ; $stdout = do { local $/; readline $out }; $stderr = do { local $/; readline $err }; waitpid $pid, 0; alarm 0; 1 } } or die $@ 7 1 8
31 8 ▶ local $SIG{ALRM} = sub { die "timeout\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 8 ▶ alarm( $_[0]->{timeout} || $timeout );
Condition66%2/3
$_[0]{'timeout'} || $Test2::Tools::Command::timeout
l!l&&r!l&&!r
170
MC/DC0%0/2
$_[0]{'timeout'} || $Test2::Tools::Command::timeout
$_[0]{'timeout'} $Test2::Tools::Command::timeout
Truth table66%2/3
A || B
A= $_[0]{'timeout'}B= $Test2::Tools::Command::timeout
ABresult
1X1
011
000
33  
34 8 my $pid = open3( my $in, my $out, my $err = gensym, @cmd );
35 8 ▶ if ( defined $_[0]->{binmode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{"binmode"}) 1 7 8
36 1 for my $fh ( $in, $out, $err ) { binmode $fh, $_[0]->{binmode} }
37 }
38 8 ▶ if ( exists $_[0]->{stdin} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{"stdin"}) 1 7 8
39 1 print $in $_[0]->{stdin};
40 1 close $in;
41 }
42 # this may be bad if the utility produces too much output
43 8 $stdout = do { local $/; readline $out };
44 7 $stderr = do { local $/; readline $err };
45 7 waitpid $pid, 0;
46 7 alarm 0;
47 7 1;
48 } or die $@;
49 7 my $orig_status = $?;
50 # the exit status is broken out into a hashref for exact tests on
51 # the various components of the 16-bit word (an alternative might be
52 # to mangle it into a number like the shell does)
53 7 ▶ my $status = {
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 7 7
54 code => $? >> 8,
55 signal => $? & 127,
56 iscore => $? & 128 ? 1 : 0
57 };
58 # the munge are for when the code or signal vary (for portability or
59 # for reasons out of your control) and you only want to know if the
60 # value was 0 or not. lots of CPAN Tester systems did not set the
61 # iscore flag following a CORE::dump by a test program...
62 7 ▶ $status->{code} = $status->{code} ? 1 : 0 if $_[0]->{munge_status};
Branch: 2 branches
BranchTrueFalseTotal
if $_[0]{"munge_status"} 2 5 7
$status->{'code'} ? : 1 1 2
63 7 ▶ $status->{signal} = $status->{signal} ? 1 : 0 if $_[0]->{munge_signal};
Branch: 2 branches
BranchTrueFalseTotal
if $_[0]{"munge_signal"} 2 5 7
$status->{'signal'} ? : 1 1 2
64  
65 # default exit status word is 0, but need it in hashref form
66 7 ▶ if ( exists $_[0]->{status} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{'status'}) { } 3 4 7
67 3 ▶ if ( !defined $_[0]->{status} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]{'status'}) { } 1 2 3
elsif (ref $_[0]{'status'} eq '') { } 1 1 2
68 1 $_[0]->{status} = { code => 0, signal => 0, iscore => 0 };
69 } elsif ( ref $_[0]->{status} eq '' ) {
70 1 $_[0]->{status} = { code => $_[0]->{status}, signal => 0, iscore => 0 };
71 }
72 # assume that ->{status} is a hashref
73 } else {
74 4 $_[0]->{status} = { code => 0, signal => 0, iscore => 0 };
75 }
76  
77 7 ▶ my ( $ctx, $name, $result ) = ( context(), $_[0]->{name} // "@cmd", 1 );
Condition100%2/2
$_[0]{"name"} // join($", @cmd)
l!l
25
MC/DC100%1/1
$_[0]{"name"} // join($", @cmd)
$_[0]{"name"}
Truth table100%2/2
A
A= $_[0]{"name"}
Aresult
11
00
78  
79 7 ▶ if ( $_[0]->{status}{code} == $status->{code}
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{'status'}{'code'} == $status->{'code'} and $_[0]{'status'}{'signal'} == $status->{'signal'} and $_[0]{'status'}{'iscore'} == $status->{'iscore'}) { } 5 2 7
Condition66%4/6
$_[0]{"status"}{"code"} == $status->{"code"} and $_[0]{"status"}{"signal"} == $status->{"signal"} and $_[0]{"status"}{"iscore"} == $status->{"iscore"}
!ll&&!rl&&r
205
$_[0]{"status"}{"code"} == $status->{"code"} and $_[0]{"status"}{"signal"} == $status->{"signal"}
!ll&&!rl&&r
205
MC/DC33%1/3
$_[0]{"status"}{"code"} == $status->{"code"} and $_[0]{"status"}{"signal"} == $status->{"signal"} and $_[0]{"status"}{"iscore"} == $status->{"iscore"}
$_[0]{"status"}{"code"} == $status->{"code"} $_[0]{"status"}{"signal"} == $status->{"signal"} $_[0]{"status"}{"iscore"} == $status->{"iscore"}
Truth table50%2/4
A and B and C
A= $_[0]{"status"}{"code"} == $status->{"code"}B= $_[0]{"status"}{"signal"} == $status->{"signal"}C= $_[0]{"status"}{"iscore"} == $status->{"iscore"}
ABCresult
0XX0
10X0
1100
1111
80 and $_[0]->{status}{signal} == $status->{signal}
81 and $_[0]->{status}{iscore} == $status->{iscore} ) {
82 5 $ctx->pass("exit - $name");
83 } else {
84 $ctx->fail(
85 "exit - $name",
86 sprintf(
87 "code\t%d\tsignal\t%d\tiscore\t%d want",
88 $_[0]->{status}{code},
89 $_[0]->{status}{signal},
90 $_[0]->{status}{iscore}
91 ),
92 sprintf(
93 "code\t%d\tsignal\t%d\tiscore\t%d got",
94 $status->{code}, $status->{signal}, $status->{iscore}
95 )
96 2 );
97 2 $result = 0;
98 }
99 # qr// or assume it's a string
100 7 ▶ if ( defined $_[0]->{stdout} and ref $_[0]->{stdout} eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{'stdout'} and ref $_[0]{'stdout'} eq 'Regexp') { } 2 5 7
Condition100%3/3
defined $_[0]{"stdout"} and ref $_[0]{"stdout"} eq "Regexp"
!ll&&!rl&&r
322
MC/DC100%2/2
defined $_[0]{"stdout"} and ref $_[0]{"stdout"} eq "Regexp"
defined $_[0]{"stdout"} ref $_[0]{"stdout"} eq "Regexp"
Truth table100%3/3
A and B
A= defined $_[0]{"stdout"}B= ref $_[0]{"stdout"} eq "Regexp"
ABresult
0X0
100
111
101 2 ▶ if ( $stdout =~ m/$_[0]->{stdout}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout =~ /$_[0]{'stdout'}/) { } 1 1 2
102 1 $ctx->pass("stdout - $name");
103 } else {
104 1 $ctx->fail( "stdout - $name", "expected match on $_[0]->{stdout}" );
105 1 $result = 0;
106 }
107 } else {
108 5 ▶ my $want = $_[0]->{stdout} // '';
Condition100%2/2
$_[0]{"stdout"} // ""
l!l
23
MC/DC100%1/1
$_[0]{"stdout"} // ""
$_[0]{"stdout"}
Truth table100%2/2
A
A= $_[0]{"stdout"}
Aresult
11
00
109 5 ▶ if ( $stdout eq $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout eq $want) { } 4 1 5
110 4 $ctx->pass("stdout - $name");
111 } else {
112 1 $ctx->fail( "stdout - $name", "expected equality with q{$want}" );
113 1 $result = 0;
114 }
115 }
116 7 ▶ if ( defined $_[0]->{stderr} and ref $_[0]->{stderr} eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{'stderr'} and ref $_[0]{'stderr'} eq 'Regexp') { } 2 5 7
Condition100%3/3
defined $_[0]{"stderr"} and ref $_[0]{"stderr"} eq "Regexp"
!ll&&!rl&&r
322
MC/DC100%2/2
defined $_[0]{"stderr"} and ref $_[0]{"stderr"} eq "Regexp"
defined $_[0]{"stderr"} ref $_[0]{"stderr"} eq "Regexp"
Truth table100%3/3
A and B
A= defined $_[0]{"stderr"}B= ref $_[0]{"stderr"} eq "Regexp"
ABresult
0X0
100
111
117 2 ▶ if ( $stderr =~ m/$_[0]->{stderr}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr =~ /$_[0]{'stderr'}/) { } 1 1 2
118 1 $ctx->pass("stderr - $name");
119 } else {
120 1 $ctx->fail( "stderr - $name", "expected match on $_[0]->{stderr}" );
121 1 $result = 0;
122 }
123 } else {
124 5 ▶ my $want = $_[0]->{stderr} // '';
Condition100%2/2
$_[0]{"stderr"} // ""
l!l
23
MC/DC100%1/1
$_[0]{"stderr"} // ""
$_[0]{"stderr"}
Truth table100%2/2
A
A= $_[0]{"stderr"}
Aresult
11
00
125 5 ▶ if ( $stderr eq $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr eq $want) { } 4 1 5
126 4 $ctx->pass("stderr - $name");
127 } else {
128 1 $ctx->fail( "stderr - $name", "expected equality with q{$want}" );
129 1 $result = 0;
130 }
131 }
132 7 $ctx->release;
133 7 return $result, $orig_status, \$stdout, \$stderr;
134 }
135  
136 9 ▶sub is_exit ($;$$) {
Subroutine is_exit: called CC=12 SCAR=24.9
Pod documented
137 9 my ( $exit, $expect, $name ) = @_;
138  
139 9 ▶ $name //= 'exit status';
Condition100%2/2
$name //= "exit status"
l!l
18
MC/DC100%1/1
$name //= "exit status"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
140  
141 9 ▶ if ( !defined $expect ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $expect) { } 1 8 9
elsif (ref $expect eq '') { } 1 7 8
142 1 $expect = { code => 0, signal => 0, iscore => 0 };
143 } elsif ( ref $expect eq '' ) {
144 1 $expect = { code => $expect, signal => 0, iscore => 0 };
145 }
146  
147 9 ▶ my $status = {
Branch: 1 branch
BranchTrueFalseTotal
$exit & 128 ? : 2 7 9
148 code => $exit >> 8,
149 signal => $exit & 127,
150 iscore => $exit & 128 ? 1 : 0
151 };
152 9 ▶ $status->{code} = $status->{code} ? 1 : 0 if $expect->{munge_status};
Branch: 2 branches
BranchTrueFalseTotal
if $expect->{"munge_status"} 2 7 9
$status->{'code'} ? : 1 1 2
153 9 ▶ $status->{signal} = $status->{signal} ? 1 : 0 if $expect->{munge_signal};
Branch: 2 branches
BranchTrueFalseTotal
if $expect->{"munge_signal"} 2 7 9
$status->{'signal'} ? : 1 1 2
154  
155 9 my ( $ctx, $result ) = ( context(), 1 );
156  
157 9 ▶ if ( $expect->{code} == $status->{code}
Branch: 1 branch
BranchTrueFalseTotal
if ($expect->{'code'} == $status->{'code'} and $expect->{'signal'} == $status->{'signal'} and $expect->{'iscore'} == $status->{'iscore'}) { } 8 1 9
Condition50%3/6
$expect->{"code"} == $status->{"code"} and $expect->{"signal"} == $status->{"signal"} and $expect->{"iscore"} == $status->{"iscore"}
!ll&&!rl&&r
018
$expect->{"code"} == $status->{"code"} and $expect->{"signal"} == $status->{"signal"}
!ll&&!rl&&r
009
MC/DC33%1/3
$expect->{"code"} == $status->{"code"} and $expect->{"signal"} == $status->{"signal"} and $expect->{"iscore"} == $status->{"iscore"}
$expect->{"code"} == $status->{"code"} $expect->{"signal"} == $status->{"signal"} $expect->{"iscore"} == $status->{"iscore"}
Truth table50%2/4
A and B and C
A= $expect->{"code"} == $status->{"code"}B= $expect->{"signal"} == $status->{"signal"}C= $expect->{"iscore"} == $status->{"iscore"}
ABCresult
0XX0
10X0
1100
1111
158 and $expect->{signal} == $status->{signal}
159 and $expect->{iscore} == $status->{iscore} ) {
160 8 $ctx->pass($name);
161 } else {
162 $ctx->fail(
163 $name,
164 sprintf(
165 "code\t%d\tsignal\t%d\tiscore\t%d want",
166 $expect->{code}, $expect->{signal},
167 $expect->{iscore}
168 ),
169 sprintf(
170 "code\t%d\tsignal\t%d\tiscore\t%d got",
171 $status->{code}, $status->{signal}, $status->{iscore}
172 )
173 1 );
174 1 $result = 0;
175 }
176  
177 9 $ctx->release;
178 9 return $result;
179 }
180  
181 3 1;
182 __END__
↑ Summary Command/ConfigData.pm »