blib/lib/Test/Script/Run.pm

filter: Statementstmt 89.8% 168 / 187 Branchbran 70.0% 49 / 70 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 30 / 30 Podpod 100% 10 / 10 totaltotal 84.3% 269 / 319 CC 47 SCAR 19.5
CC
47
Cov
83%
run_output_matches_unordered
12.9
get_perl_cmd
12.7
_generate_test_name
10.5
CRAP 7.0
↑ Summary
1 package Test::Script::Run;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Test::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.08';
12 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 our @EXPORT =
14 qw/run_ok run_not_ok run_script run_output_matches run_output_matches_unordered/;
15 2 our @EXPORT_OK = qw/is_script_output last_script_stdout last_script_stderr
16 last_script_exit_code get_perl_cmd/;
17 2 our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] );
18 my (
19 2 $last_script_stdout, $last_script_stderr,
20 $last_script_exit_code,
21 );
22  
23 2 our @BIN_DIRS = ('bin','sbin','script', '.');
24  
25 =head1 NAME
26  
27 Test::Script::Run - test scripts with run
28  
29 =head1 SYNOPSIS
30  
31 use Test::Script::Run;
32 # customized names of bin dirs, default is qw/bin sbin script ./;
33 @Test::Script::Run::BIN_DIRS = qw/bin/;
34 run_ok( 'app_name', [ app's args ], 'you_app runs ok' );
35 my ( $return, $stdout, $stderr ) = run_script( 'app_name', [ app's args ] );
36 run_output_matches(
37 'app_name', [app's args],
38 [ 'out line 1', 'out line 2' ],
39 [ 'err line 1', 'err line 2' ],
40 'run_output_matches'
41 );
42 run_output_matches_unordered(
43 'app_name', [ app's args ],
44 [ 'out line 2', 'out line 1' ],
45 [ 'err line 2', 'err line 1' ],
46 'run_output_matches_unordered'
47 );
48  
49 =head1 DESCRIPTION
50  
51 This module exports some subs to help test and run scripts in your dist's
52 script directory( bin, sbin, script, etc ), if the script path is not absolute.
53  
54 Nearly all the essential code is stolen from Prophet::Test, we think subs like
55 those should live below C<Test::> namespace, that's why we packed them and
56 created this module.
57  
58 =head1 FUNCTIONS
59  
60 =head2 run_script($script, $args, $stdout, $stderr)
61  
62 Runs the script $script as a perl script, setting the @INC to the same as
63 our caller.
64  
65 $script is the name of the script to be run (such as 'prophet'). $args is a
66 reference to an array of arguments to pass to the script. $stdout and $stderr
67 are both optional; if passed in, they will be passed to L<IPC::Run3>'s run3
68 subroutine as its $stdout and $stderr args. Otherwise, this subroutine will
69 create scalar references to pass to run3 instead (which are treated as strings
70 for STDOUT/STDERR to be written to).
71  
72 Returns run3's return value and, if no $stdout and $stderr were passed in, the
73 STDOUT and STDERR of the script that was run.
74  
75 =cut
76  
77 15 ▶sub run_script {
Subroutine run_script: called CC=8 SCAR=20.8
Pod documented
78 15 my $script = shift;
79 15 ▶ my $args = shift || [];
Condition100%2/2
shift() || []
l!l
78
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
80 15 my ( $stdout, $stderr ) = @_;
81 15 my ( $new_stdout, $new_stderr, $return_stdouterr );
82 15 ▶ if ( !ref($stdout) && !ref($stderr) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $stdout or ref $stderr) 10 5 15
Condition66%2/3
ref $stdout or ref $stderr
l!l&&r!l&&!r
5010
MC/DC50%1/2
ref $stdout or ref $stderr
ref $stdout ref $stderr
Truth table66%2/3
A or B
A= ref $stdoutB= ref $stderr
ABresult
1X1
011
000
83 10 ( $stdout, $stderr, $return_stdouterr ) =
84 ( \$new_stdout, \$new_stderr, 1 );
85 }
86 15 my @cmd = get_perl_cmd($script);
87  
88 15 ▶ if (@cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cmd) { } 13 2 15
89 13 my $ret = run3 [ @cmd, @$args ], undef, $stdout, $stderr;
90 13 $last_script_exit_code = $? >> 8;
91 13 ▶ if ( ref $stdout eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stdout eq "SCALAR") 9 4 13
92 9 $last_script_stdout = $$stdout;
93 }
94  
95 13 ▶ if ( ref $stderr eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stderr eq "SCALAR") 9 4 13
96 9 $last_script_stderr = $$stderr;
97 }
98  
99 13 ▶ return $return_stdouterr
Branch: 1 branch
BranchTrueFalseTotal
$return_stdouterr ? : 8 5 13
100 ? ( $ret, $last_script_stdout, $last_script_stderr )
101 : $ret;
102 }
103 else {
104 # usually people use 127 to show error about the command can't be found
105 2 $last_script_exit_code = 127;
106 2 return;
107 }
108 }
109  
110 =head2 run_ok($script, $args, $msg)
111  
112 Runs the script, checking that it didn't error out.
113  
114 $script is the name of the script to be run (e.g. 'prophet'). $args
115 is an optional reference to an array of arguments to pass to the
116 script when it is run. $msg is an optional message to print with
117 the test. If $args is not specified, you can still pass in
118 a $msg.
119  
120 Returns nothing of interest.
121  
122 =cut
123  
124 3 ▶sub run_ok {
Subroutine run_ok: called CC=1 SCAR=0.0
Pod documented
125 3 return _run_ok( '==', @_ );
126 }
127  
128 =head2 run_not_ok($script, $args, $msg)
129  
130 opposite of run_ok
131  
132 =cut
133  
134 3 ▶sub run_not_ok {
Subroutine run_not_ok: called CC=1 SCAR=0.0
Pod documented
135 3 return _run_ok( '!=', @_ );
136 }
137  
138 6 ▶sub _run_ok {
Subroutine _run_ok: called CC=4 SCAR=14.1
139 6 ▶ my $cmp = shift || '=='; # the exit code
Condition50%1/2
shift() || '=='
l!l
60
MC/DC0%0/1
shift() || '=='
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
140 6 my $script = shift;
141 6 my $args;
142 6 ▶ $args = shift if ( ref $_[0] eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" 0 6 6
143 6 ▶ my $msg = (@_) ? shift : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
144  
145 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
146  
147 6 ▶ lives_and {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
149 6 my ( $ret, $stdout, $stderr ) = run_script( $script, $args );
150 6 cmp_ok( $last_script_exit_code, $cmp, 0, _generate_test_name($msg, $script, @$args) );
151 6 };
152 }
153  
154 # _updir( $path )
155 #
156 # Strips off the filename in the given path and returns the absolute
157 # path of the remaining directory.
158  
159 34 ▶sub _updir {
Subroutine _updir: called CC=1 SCAR=0.0
160 34 my $path = shift;
161 34 my ( $file, $dir, undef ) = fileparse( File::Spec->rel2abs($path) );
162 34 return $dir;
163 }
164  
165 2 our $RUNCNT;
166  
167  
168 =head2 get_perl_cmd($script, @ARGS)
169  
170 Returns a list suitable for passing to C<system>, C<exec>, etc. If you pass
171 C<$script> then we will search upwards for it in C<@BIN_DIRS>
172  
173 =cut
174  
175 15 ▶sub get_perl_cmd {
Subroutine get_perl_cmd: called CC=12 SCAR=25.5
Pod documented
176 15 my $script = shift;
177 15 my $base_dir;
178  
179 15 ▶ if (defined $script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script) 15 0 15
180 15 my $fail = 0;
181 15 ▶ if ( File::Spec->file_name_is_absolute($script) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($script)) { } 2 13 15
182 2 ▶ unless ( -f $script ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $script) 0 2 2
183 0 warn "couldn't find the script $script";
184 0 $fail = 1;
185 }
186 }
187 else {
188 13 my ( $tmp, $i ) = ( _updir($0), 0 );
189 13 my $found;
190 LOOP:
191 13 ▶ while ( $i++ < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i++ < 10) 31 2 33
192 31 for my $bin ( @BIN_DIRS ) {
193 93 ▶ if ( -f File::Spec->catfile( $tmp, $bin, $script ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "File::Spec"->catfile($tmp, $bin, $script)) 11 82 93
194 11 $script = File::Spec->catfile( $tmp, $bin, $script );
195 11 $found = 1;
196 11 last LOOP;
197 }
198 }
199 20 $tmp = _updir($tmp);
200 }
201  
202 13 ▶ unless ( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 2 11 13
203 2 warn "couldn't find the script $script";
204 2 $fail = 1;
205 }
206 }
207 15 ▶ return if $fail;
Branch: 1 branch
BranchTrueFalseTotal
if $fail 2 13 15
208 }
209  
210 # We grep out references because of INC-hooks like Jifty::ClassLoader
211 13 my @cmd = ( $^X, ( map { "-I$_" } grep {!ref($_)} @INC ) );
212  
213 13 ▶ push @cmd, '-MDevel::Cover' if $INC{'Devel/Cover.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Devel/Cover.pm"} 13 0 13
214 13 ▶ if ( $INC{'Devel/DProf.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Devel/DProf.pm"}) 0 13 13
215 0 push @cmd, '-d:DProf';
216 0 $ENV{'PERL_DPROF_OUT_FILE_NAME'} = 'tmon.out.' . $$ . '.' . $RUNCNT++;
217 }
218  
219 13 ▶ if (defined $script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script) 13 0 13
220 13 push @cmd, $script;
221 13 push @cmd, @_;
222 }
223  
224 13 return @cmd;
225 }
226  
227 # back-compat
228 2 *_get_perl_cmd = \&get_perl_cmd;
229  
230 =head2 is_script_output($scriptname \@args, \@stdout_match, \@stderr_match, $msg)
231  
232 Runs $scriptname, checking to see that its output matches.
233  
234 $args is an array reference of args to pass to the script. $stdout_match and
235 $stderr_match are references to arrays of expected lines. $msg is a string
236 message to display with the test. $stderr_match and $msg are optional. (As is
237 $stdout_match if for some reason you expect your script to have no output at
238 all. But that would be silly, wouldn't it?)
239  
240 Allows regex matches as well as string equality (lines in $stdout_match and
241 $stderr_match may be Regexp objects).
242  
243 =cut
244  
245 4 ▶sub is_script_output {
Subroutine is_script_output: called CC=2 SCAR=7.0
Pod documented
246 4 my ( $script, $args, $exp_stdout, $exp_stderr, $msg ) = @_;
247 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
248  
249 4 my $stdout_err = [];
250 4 ▶ $exp_stderr ||= [];
Condition50%1/2
$exp_stderr ||= []
l!l
40
MC/DC0%0/1
$exp_stderr ||= []
$exp_stderr
Truth table50%1/2
A
A= $exp_stderr
Aresult
11
00
251  
252 4 my $ret = run_script(
253 $script, $args,
254 _mk_cmp_closure( 'stdout', $exp_stdout, $stdout_err ), # stdout
255 _mk_cmp_closure( 'stderr', $exp_stderr, $stdout_err ), # stderr
256 );
257  
258 4 _check_cmp_closure_output( $script, $msg, $args, $exp_stdout, $stdout_err );
259 }
260  
261 # =head2 _mk_cmp_closure($expected, $error)
262 # $expected is a reference to an array of expected output lines, and
263 # $error is an array reference for storing error messages.
264 #
265 # Returns a subroutine that takes a line of output and compares it
266 # to the next line in $expected. You can, for example, pass this
267 # subroutine to L<IPC::Run3>::run3 and it will compare the output
268 # of the script being run to the expected output. After the script
269 # is done running, errors will be in $error.
270 #
271 # If a line in $expected is a Regexp reference (made with e.g.
272 # qr/foo/), the subroutine will check for a regexp match rather
273 # than string equality.
274  
275 8 ▶sub _mk_cmp_closure {
Subroutine _mk_cmp_closure: called CC=2 SCAR=6.9
276 8 my ( $type, $exp, $err ) = @_;
277  
278 8 ▶ if ( $type eq 'stderr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'stderr') { } 4 4 8
279 4 $last_script_stderr = '';
280 4 my $line = 0;
281 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 6 my $output = shift;
283 6 ++$line;
284 6 $last_script_stderr .= $output;
285 6 __mk_cmp_closure()->( $exp, $err, $line, $output );
286 }
287 4 }
288 else {
289 4 $last_script_stdout = '';
290 4 my $line = 0;
291 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
292 6 my $output = shift;
293 6 ++$line;
294 6 $last_script_stdout .= $output;
295 6 __mk_cmp_closure()->( $exp, $err, $line, $output );
296 }
297 4 }
298 }
299  
300 12 ▶sub __mk_cmp_closure {
Subroutine __mk_cmp_closure: called CC=1 SCAR=0.4
301 12 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.9
302 12 my ( $exp, $err, $line, $output ) = @_;
303 12 chomp $output;
304 12 ▶ unless (@$exp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$exp) 0 12 12
305 0 push @$err, "$line: got $output";
306 0 return;
307 }
308 12 my $item = shift @$exp;
309 12 ▶ push @$err, "$line: got ($output), expect ($item)\n"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $item eq "Regexp" ? $output =~ /$item/ : $output eq $item 0 12 12
ref $item eq 'Regexp' ? : 0 12 12
310 unless ref($item) eq 'Regexp'
311 ? ( $output =~ m/$item/ )
312 : ( $output eq $item );
313 }
314 12 }
315  
316 # XXX note that this sub doesn't check to make sure we got
317 # all the errors we were expecting (there can be more lines
318 # in the expected stderr than the received stderr as long
319 # as they match up until the end of the received stderr --
320 # the same isn't true of stdout)
321 4 ▶sub _check_cmp_closure_output {
Subroutine _check_cmp_closure_output: called CC=4 SCAR=17.9
322 4 my ( $script, $msg, $args, $exp_stdout, $stdout_err ) = @_;
323  
324 4 for my $line (@$exp_stdout) {
325 0 ▶ next if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
326 0 push @$stdout_err, "got nothing, expected: $line";
327 }
328  
329 4 my $test_name = _generate_test_name( $msg, $script, @$args );
330 4 is( scalar(@$stdout_err), 0, $test_name );
331  
332 4 ▶ if (@$stdout_err) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stdout_err) 0 4 4
333 0 diag( "Different in line: " . join( "\n", @$stdout_err ) );
334 }
335 }
336  
337 =head2 run_output_matches($script, $args, $exp_stdout, $exp_stderr, $msg)
338  
339 A wrapper around L<is_script_output> that also checks to make sure
340 the test runs without throwing an exception.
341  
342 =cut
343  
344 1 ▶sub run_output_matches {
Subroutine run_output_matches: called CC=1 SCAR=0.0
Pod documented
345 1 my ( $script, $args, $expected, $stderr, $msg ) = @_;
346 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
347 1 ▶ lives_and {
Subroutine __ANON__: called CC=1 SCAR=0.0
348 1 local $Test::Builder::Level = $Test::Builder::Level + 5;
349 1 is_script_output( $script, $args, $expected, $stderr, $msg );
350 1 };
351 }
352  
353 =head2 run_output_matches_unordered($script, $args, $exp_stdout, $exp_stderr, $msg)
354  
355 This subroutine has exactly the same functionality as run_output_matches, but
356 doesn't impose a line ordering when comparing the expected and received
357 outputs.
358  
359 =cut
360  
361 1 ▶sub run_output_matches_unordered {
Subroutine run_output_matches_unordered: called CC=12 SCAR=25.6
Pod documented
362 1 my ( $cmd, $args, $stdout, $stderr, $msg ) = @_;
363 1 ▶ $stderr ||= [];
Condition50%1/2
$stderr ||= []
l!l
10
MC/DC0%0/1
$stderr ||= []
$stderr
Truth table50%1/2
A
A= $stderr
Aresult
11
00
364  
365 1 my ( $val, $out, $err ) = run_script( $cmd, $args );
366  
367 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
368  
369 # Check if each line matches a line in the expected output and
370 # delete that line if we have a match. If no match is found,
371 # add an error.
372 1 my $errors = [];
373 1 my @lines = split /\n/, $out;
374 1 ▶ OUTPUT: while ( my $line = shift @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 2 1 3
375 2 for my $exp_line (@$stdout) {
376 3 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (ref $exp_line eq "Regexp" ? $line =~ /$exp_line/ : $line eq $exp_line) 2 1 3
ref $exp_line eq 'Regexp' ? : 0 3 3
377 (
378 ref($exp_line) eq 'Regexp'
379 ? ( $line =~ m/$exp_line/ )
380 : ( $line eq $exp_line )
381 )
382 )
383 {
384  
385 # remove the found element from the array of expected output
386 2 $stdout = [ grep { $_ ne $exp_line } @$stdout ];
387 2 next OUTPUT;
388 }
389 }
390  
391 # we didn't find a match
392 0 push @$errors, "couldn't find match for ($line)\n";
393 }
394  
395 # do the same for STDERR
396 1 @lines = split /\n/, $err;
397 1 ▶ ERROR: while ( my $line = shift @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 2 1 3
398 2 for my $exp_line (@$stderr) {
399 3 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (ref $exp_line eq "Regexp" ? $line =~ /$exp_line/ : $line eq $exp_line) 2 1 3
ref $exp_line eq 'Regexp' ? : 0 3 3
400 (
401 ref($exp_line) eq 'Regexp'
402 ? ( $line =~ m/$exp_line/ )
403 : ( $line eq $exp_line )
404 )
405 )
406 {
407  
408 # remove the found element from the array of expected output
409 2 $stderr = [ grep { $_ ne $exp_line } @$stderr ];
410 2 next ERROR;
411 }
412 }
413  
414 # we didn't find a match
415 0 push @$errors, "couldn't find match for ($line)\n";
416 }
417  
418 # add any expected lines that we didn't find to the errors
419 1 for my $exp_line ( @$stdout, @$stderr ) {
420 0 push @$errors, "got nothing, expected: $exp_line";
421 }
422  
423 1 my $test_name = _generate_test_name( $msg, $cmd, @$args );
424 1 is( scalar(@$errors), 0, $test_name );
425  
426 1 ▶ if (@$errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$errors) 0 1 1
427 0 diag( "Errors: " . join( "\n", @$errors ) );
428 }
429 }
430  
431 11 ▶sub _is_windows {
Subroutine _is_windows: called CC=1 SCAR=0.0
432 11 return $^O =~ /MSWin/;
433 }
434  
435 11 ▶sub _generate_test_name {
Subroutine _generate_test_name: called CC=7 SCAR=23.5
436 11 my $msg = shift;
437 11 my $script = shift;
438 11 my @args = @_;
439 11 my $args;
440 11 ▶ if ( _is_windows() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_windows()) { } 0 11 11
441 0 eval { require Win32::ShellQuote };
442 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
443 0 $args = join ' ', @_;
444 }
445 else {
446 0 $args = Win32::ShellQuote::quote_system_string(@_);
447 }
448 }
449 else {
450 11 eval { require String::ShellQuote };
451 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 11 11
452 0 $args = join ' ', @_;
453 }
454 else {
455 11 $args = String::ShellQuote::shell_quote(@_);
456 }
457 }
458 11 ▶ return join( ' ', $msg ? "$msg:" : (), $script, defined $args && length $args ? $args : () );
Branch: 2 branches
BranchTrueFalseTotal
$msg ? : 11 0 11
defined $args && length $args ? : 2 9 11
Condition66%2/3
defined $args and length $args
!ll&&!rl&&r
092
MC/DC50%1/2
defined $args and length $args
defined $args length $args
Truth table66%2/3
A and B
A= defined $argsB= length $args
ABresult
0X0
100
111
459 }
460  
461 =head2 last_script_stdout
462  
463 return last script's stdout
464  
465 =cut
466  
467 4 ▶sub last_script_stdout { $last_script_stdout }
Subroutine last_script_stdout: called CC=1 SCAR=0.0
Pod documented
468  
469 =head2 last_script_stderr
470  
471 return last script's stderr
472  
473 =cut
474  
475 4 ▶sub last_script_stderr { $last_script_stderr }
Subroutine last_script_stderr: called CC=1 SCAR=0.0
Pod documented
476  
477 =head2 last_script_exit_code
478  
479 return last script's exit code
480  
481 =cut
482  
483 5 ▶sub last_script_exit_code { $last_script_exit_code }
Subroutine last_script_exit_code: called CC=1 SCAR=0.0
Pod documented
484  
485  
486 2 1;
487  
488 __END__
↑ Summary