blib/lib/Test/Clustericious/Command.pm

filter: Statementstmt 78.7% 182 / 231 Branchbran 56.2% 36 / 64 Conditioncond 38.1% 8 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 85.7% 36 / 42 Podpod 0.0% 0 / 8 totaltotal 69.5% 263 / 378 CC 48 SCAR 24.8
CC
48
Cov
72%
requires
57.3
diag
12.0
mirror
8.2
CRAP 12.0
« blib/lib/Clustericious/Command/plackup.pm ↑ Summary blib/lib/Clustericious/Plugin/AutodataHandler.pm »
1 package Test::Clustericious::Command;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Test2::Plugin::FauxHomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use File::Glob qw( bsd_glob );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Mojo::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Path::Class qw( file dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Env qw( @PERL5LIB @PATH );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Capture::Tiny qw( capture );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use File::Which qw( which );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use File::Glob qw( bsd_glob );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Test2::API qw( context );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # ABSTRACT: Test Clustericious commands
21 13 our $VERSION = '1.29'; # VERSION
22  
23  
24 13 our @EXPORT = qw( extract_data mirror requires run_ok generate_port note_file clean_file create_symlink );
25 13 our @EXPORT_OK = @EXPORT;
26 13 our %EXPORT_TAGS = ( all => \@EXPORT );
27  
28 13 unshift @INC, dir(bsd_glob '~/lib')->stringify;
29 13 unshift @PERL5LIB, map { dir($_)->absolute->stringify } @INC;
30 13 unshift @PATH, dir(bsd_glob '~/bin')->stringify;
31  
32 sub _can_execute_in_tmp
33 11 ▶{
Subroutine _can_execute_in_tmp: called CC=1 SCAR=0.0
34 11 my $script = file( tempdir( CLEANUP => 1 ), 'mytest' );
35 11 $script->spew("#!$^X\nexit 0");
36 11 chmod 0755, "$script";
37 11 my $exit;
38 11 ▶ capture { system "$script", "okay"; $exit = $? };
Subroutine __ANON__: called CC=1 SCAR=0.0
39 11 $exit == 0;
40 }
41  
42 sub requires
43 11 ▶{
Subroutine requires: called CC=13 SCAR=40.5
Pod undocumented
44 11 my($command, $num) = @_;
45  
46 11 my $ctx = context();
47 11 ▶ $ctx->plan( 0, 'SKIP', 'test requires execute in tmp') unless __PACKAGE__->_can_execute_in_tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless __PACKAGE__->_can_execute_in_tmp 0 11 11
48  
49 11 ▶ unless(defined $command)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 8 3 11
50 {
51 8 ▶ $ctx->plan( $num ) if defined $num;
Branch: 1 branch
BranchTrueFalseTotal
if defined $num 8 0 8
52 8 $ctx->release;
53 8 return;
54 }
55  
56 3 ▶ if($command =~ /^(.*)\.conf$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /^(.*)\.conf$/) 3 0 3
57 {
58 3 my $name = $1;
59 3 ▶ if(defined $ENV{CLUSTERICIOUS_COMMAND_TEST} && -r $ENV{CLUSTERICIOUS_COMMAND_TEST})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{'CLUSTERICIOUS_COMMAND_TEST'} and -r $ENV{'CLUSTERICIOUS_COMMAND_TEST'}) { } 0 3 3
Condition33%1/3
defined $ENV{"CLUSTERICIOUS_COMMAND_TEST"} and -r $ENV{"CLUSTERICIOUS_COMMAND_TEST"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $ENV{"CLUSTERICIOUS_COMMAND_TEST"} and -r $ENV{"CLUSTERICIOUS_COMMAND_TEST"}
defined $ENV{"CLUSTERICIOUS_COMMAND_TEST"} -r $ENV{"CLUSTERICIOUS_COMMAND_TEST"}
Truth table33%1/3
A and B
A= defined $ENV{"CLUSTERICIOUS_COMMAND_TEST"}B= -r $ENV{"CLUSTERICIOUS_COMMAND_TEST"}
ABresult
0X0
100
111
60 {
61 my $config = do {
62 0 require Clustericious::Config;
63 0 my $config = Clustericious::Config->new($ENV{CLUSTERICIOUS_COMMAND_TEST});
64 0 my %config = %$config;
65 0 \%config;
66 0 }->{$name};
67 0 ▶ $ctx->plan( 0, 'SKIP', "developer test not configured" ) unless defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 0 0
68
69 0 ▶ unshift @PATH, $config->{path} if defined $config->{path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"path"} 0 0 0
70 0 unshift @PATH, dir(bsd_glob '~/bin')->stringify;
71 0 $ENV{$_} = $config->{env}->{$_} for keys %{ $config->{env} };
72 0 ▶ $command = $config->{exe} // $name;
Condition0%0/3
$config->{"exe"} // $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"exe"} // $name
$config->{"exe"} $name
Truth table0%0/3
A // B
A= $config->{"exe"}B= $name
ABresult
1X1
011
000
73 }
74 else
75 {
76 3 $ctx->plan( 0, 'SKIP', "developer only test" );
77 }
78 }
79  
80 0 ▶ if(which $command)
Branch: 1 branch
BranchTrueFalseTotal
if (which($command)) { } 0 0 0
81 {
82 0 ▶ $ctx->plan( $num ) if defined $num;
Branch: 1 branch
BranchTrueFalseTotal
if defined $num 0 0 0
83 }
84 else
85 {
86 0 $ctx->plan( 0, 'SKIP', "test requires $command to be in the PATH" );
87 }
88 0 $ctx->release;
89 }
90  
91 sub extract_data
92 7 ▶{
Subroutine extract_data: called CC=7 SCAR=19.5
Pod undocumented
93 7 my(@values) = @_;
94 7 my $caller = caller;
95 7 ▶ Mojo::Loader::load_class($caller) unless $caller eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
unless $caller eq "main" 0 7 7
96 7 my $all = Mojo::Loader::data_section $caller;
97
98 7 my $ctx = context();
99
100 7 foreach my $name (sort keys %$all)
101 {
102 18 my $file = file(bsd_glob('~'), $name);
103 18 my $dir = $file->parent;
104 18 ▶ unless(-d $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 13 5 18
105 {
106 13 $ctx->note("[extract] DIR $dir");
107 13 $dir->mkpath(0,0700);
108 }
109 18 ▶ unless(-f $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 18 0 18
110 {
111 18 ▶ $ctx->note("[extract] FILE $file@{[ $name =~ m{^bin/} ? ' (*)' : '']}");
Branch: 1 branch
BranchTrueFalseTotal
$name =~ m[^bin/] ? : 9 9 18
112
113 18 ▶ if($name =~ m{^bin/})
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[^bin/]) { } 9 9 18
114 {
115 9 my $content = $all->{$name};
116 9 $content =~ s{^#!/usr/bin/perl}{#!$^X};
117 9 $file->spew($content);
118 9 chmod 0700, "$file";
119 }
120 else
121 {
122 9 $file->spew($all->{$name});
123 }
124 }
125 }
126
127 7 $ctx->release;
128 }
129  
130 sub mirror
131 8 ▶{
Subroutine mirror: called CC=8 SCAR=21.0
Pod undocumented
132 8 ▶ my($src, $dst) = map { ref($_) ? $_ : dir($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 16 16
133
134 8 my $ctx = context();
135  
136 8 ▶ $dst = dir(bsd_glob('~'), $dst) unless $dst->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
unless $dst->is_absolute 8 0 8
137
138 8 ▶ unless(-d $dst)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dst) 3 5 8
139 {
140 3 $ctx->note("[mirror ] DIR $dst");
141 3 $dst->mkpath(0,0700);
142 }
143
144 8 foreach my $child ($src->children)
145 {
146 20 ▶ if($child->is_dir)
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_dir) { } 0 20 20
147 {
148 0 mirror($child, $dst->subdir($child->basename));
149 }
150 else
151 {
152 20 my $dst = $dst->file($child->basename);
153 20 ▶ unless(-f $dst)
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $dst) 20 0 20
154 {
155 20 ▶ if(-x $child)
Branch: 1 branch
BranchTrueFalseTotal
if (-x $child) { } 5 15 20
156 {
157 5 $ctx->note("[mirror ] FILE $dst (*)");
158 5 my $content = scalar $child->slurp;
159 5 $content =~ s{^#!/usr/bin/perl}{#!$^X};
160 5 $dst->spew($content);
161 5 chmod 0700, "$dst";
162 }
163 else
164 {
165 15 $ctx->note("[mirror ] FILE $dst");
166 15 $dst->spew(scalar $child->slurp);
167 15 chmod 0600, "$dst";
168 }
169 }
170 }
171 }
172
173 8 $ctx->release;
174 }
175  
176 sub run_ok
177 31 ▶{
Subroutine run_ok: called CC=6 SCAR=17.9
Pod undocumented
178 31 my(@cmd) = @_;
179
180 # Yath set some environment variables which confuse a subprocess
181 # for when we are testing the use of prove, etc
182 31 local %ENV = %ENV;
183 31 delete $ENV{$_} for grep /^T2_/, keys %ENV;
184
185 31 ▶ my($out, $err, $error, $exit) = capture { system @cmd; ($!,$?) };
Subroutine __ANON__: called CC=1 SCAR=0.0
186
187 31 ▶ my $ok = ($exit != -1) && ! ($exit & 128);
Condition66%2/3
$exit != -1 && !($exit & 128)
!ll&&!rl&&r
6025
MC/DC50%1/2
$exit != -1 && !($exit & 128)
$exit != -1 !($exit & 128)
Truth table66%2/3
A && not(B)
A= $exit != -1B= !($exit & 128)
ABresult
0X0
100
111
188
189 31 my $ctx = context();
190
191 31 $ctx->ok($ok, "run: @cmd");
192 31 ▶ $ctx->diag(" @cmd failed") unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 6 25 31
193 31 ▶ $ctx->diag(" - execute failed: $error") if $exit == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $exit == -1 6 25 31
194 31 ▶ $ctx->diag(" - died from signal: " . ($exit & 128)) if $exit & 128;
Branch: 1 branch
BranchTrueFalseTotal
if $exit & 128 6 25 31
195  
196 31 my $run = Test::Clustericious::Command::Run->new(
197 cmd => \@cmd,
198 out => $out, err => $err, exit => $exit >> 8,
199 );
200
201 31 $ctx->release;
202
203 31 $run;
204 }
205  
206 sub generate_port
207 0 ▶{
Subroutine generate_port: not called CC=1 SCAR=6.9
Pod undocumented
208 0 require IO::Socket::INET;
209 0 IO::Socket::INET->new(Listen => 5, LocalAddr => "127.0.0.1")->sockport;
210 }
211  
212 sub note_file
213 0 ▶{
Subroutine note_file: not called CC=2 SCAR=17.9
Pod undocumented
214 0 my $ctx = context();
215 0 foreach my $file (sort map { file $_ } map { bsd_glob "~/$_" } @_)
216 {
217 0 $ctx->note("[content] $file");
218 0 $ctx->note(scalar $file->slurp);
219 }
220 0 $ctx->release;
221 }
222  
223 sub clean_file
224 0 ▶{
Subroutine clean_file: not called CC=2 SCAR=17.9
Pod undocumented
225 0 foreach my $file (sort map { file $_ } map { bsd_glob "~/$_" } @_)
226 {
227 0 $file->remove;
228 }
229 }
230  
231 sub create_symlink
232 0 ▶{
Subroutine create_symlink: not called CC=2 SCAR=13.4
Pod undocumented
233 0 my($old,$new) = map { file(bsd_glob('~'), $_) } @_;
234 0 ▶ $new->remove if -f $new;
Branch: 1 branch
BranchTrueFalseTotal
if -f $new 0 0 0
235 0 my $ctx = context();
236 0 $ctx->note("[symlink] $old => $new");
237 0 $ctx->release;
238 13 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 0 symlink "$old", "$new";
240 0 %Clustericious::Config::singletons = ();
241 }
242  
243 package Test::Clustericious::Command::Run;
244  
245 13 ▶use Test2::API qw( context );
Subroutine BEGIN: called CC=1 SCAR=0.0
246  
247 sub new
248 31 ▶{
Subroutine new: called CC=1 SCAR=0.0
249 31 my($class, %args) = @_;
250 31 bless \%args, $class;
251 }
252  
253 38 ▶sub cmd { @{ shift->{cmd} // [] } }
Subroutine cmd: called CC=2 SCAR=7.2
Condition50%1/2
+(shift())->{"cmd"} // []
l!l
380
MC/DC0%0/1
+(shift())->{"cmd"} // []
+(shift())->{"cmd"}
Truth table50%1/2
A
A= +(shift())->{"cmd"}
Aresult
11
00
254 74 ▶sub out { shift->{out} }
Subroutine out: called CC=1 SCAR=0.0
255 100 ▶sub err { shift->{err} }
Subroutine err: called CC=1 SCAR=0.0
256 62 ▶sub exit { shift->{exit} }
Subroutine exit: called CC=1 SCAR=0.0
257  
258 sub exit_is
259 31 ▶{
Subroutine exit_is: called CC=6 SCAR=18.4
260 31 my($self, $value, $name) = @_;
261 31 ▶ $name //= "exit with $value";
Condition50%1/2
$name //= "exit with $value"
l!l
031
MC/DC0%0/1
$name //= "exit with $value"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
262 31 my $ctx = context();
263 31 $ctx->ok($self->exit eq $value, $name);
264 31 ▶ unless($self->exit == $value)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exit == $value) 19 12 31
265 {
266 19 ▶ $ctx->diag("[cmd]\n", join(' ', $self->cmd)) if $self->cmd;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cmd 19 0 19
267 19 ▶ $ctx->diag("[out]\n", $self->out) if $self->out;
Branch: 1 branch
BranchTrueFalseTotal
if $self->out 0 19 19
268 19 ▶ $ctx->diag("[err]\n", $self->err) if $self->err;
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 19 0 19
269 }
270 31 $ctx->release;
271 31 $self;
272 }
273  
274 sub note
275 28 ▶{
Subroutine note: called CC=3 SCAR=11.0
276 28 my($self) = @_;
277 28 my $ctx = context();
278 28 ▶ $ctx->note("[out]\n" . $self->out) if $self->out;
Branch: 1 branch
BranchTrueFalseTotal
if $self->out 5 23 28
279 28 ▶ $ctx->note("[err]\n" . $self->err) if $self->err;
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 28 0 28
280 28 $ctx->release;
281 28 $self;
282 }
283  
284 sub diag
285 0 ▶{
Subroutine diag: not called CC=3 SCAR=24.8
286 0 my($self) = @_;
287 0 my $ctx = context();
288 0 ▶ $ctx->diag("[out]\n" . $self->out) if $self->out;
Branch: 1 branch
BranchTrueFalseTotal
if $self->out 0 0 0
289 0 ▶ $ctx->diag("[err]\n" . $self->err) if $self->err;
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 0 0 0
290 0 $ctx->release;
291 0 $self;
292 }
293  
294 sub out_like
295 17 ▶{
Subroutine out_like: called CC=2 SCAR=7.0
296 17 my($self, $pattern, $name) = @_;
297  
298 17 my $ctx = context();
299 17 ▶ $name ||= "output matches";
Condition50%1/2
$name ||= "output matches"
l!l
017
MC/DC0%0/1
$name ||= "output matches"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
300 17 $ctx->ok($self->out =~ $pattern, $name);
301 17 $ctx->release;
302  
303 17 $self;
304 }
305  
306 sub out_unlike
307 1 ▶{
Subroutine out_unlike: called CC=2 SCAR=7.0
308 1 my($self, $pattern, $name) = @_;
309  
310 1 my $ctx = context();
311 1 ▶ $name ||= "output does not match";
Condition50%1/2
$name ||= "output does not match"
l!l
01
MC/DC0%0/1
$name ||= "output does not match"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
312 1 $ctx->ok($self->out !~ $pattern, $name);
313 1 $ctx->release;
314  
315 1 $self;
316 }
317  
318 sub err_like
319 6 ▶{
Subroutine err_like: called CC=2 SCAR=7.0
320 6 my($self, $pattern, $name) = @_;
321  
322 6 my $ctx = context();
323 6 ▶ $name ||= "error matches";
Condition50%1/2
$name ||= "error matches"
l!l
06
MC/DC0%0/1
$name ||= "error matches"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
324 6 $ctx->ok($self->err =~ $pattern, $name);
325 6 $ctx->release;
326  
327 6 $self;
328 }
329  
330 sub err_unlike
331 0 ▶{
Subroutine err_unlike: not called CC=2 SCAR=17.9
332 0 my($self, $pattern, $name) = @_;
333  
334 0 my $ctx = context();
335 0 ▶ $name ||= "error does not match";
Condition0%0/2
$name ||= "error does not match"
l!l
00
MC/DC0%0/1
$name ||= "error does not match"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
336 0 $ctx->unlike($self->err, $pattern, $name);
337 0 $ctx->release;
338
339 0 $self;
340 }
341  
342 sub tap
343 4 ▶{
Subroutine tap: called CC=1 SCAR=0.0
344 4 my($self, $sub) = @_;
345 4 $sub->($self);
346 1 $self;
347 }
348  
349 13 1;
350  
351 __END__
« blib/lib/Clustericious/Command/plackup.pm ↑ Summary blib/lib/Clustericious/Plugin/AutodataHandler.pm »