1use strict; 2use warnings; 3use Test::More; 4use FindBin qw($Bin); 5use Cwd qw(abs_path); 6 7use IPC::ShellCmd; 8use IPC::ShellCmd::Generic; 9 10my ($isc, $stdin, $stdout, $stderr, $status); 11 12# Check that stdin can be passed in from a variable 13 14$isc = IPC::ShellCmd->new(["/bin/sh", '-c', 'cat']) 15 ->stdin('testing 1, 2, 3...') 16 ->run(); 17 18$stdout = $isc->stdout(); 19$stderr = $isc->stderr(); 20$status = $isc->status(); 21 22is ($stdout, "testing 1, 2, 3...", 'string passed into shell command on stdin and picked up on stdout'); 23is ($stderr, '', 'nothing leaked out on stderr'); 24is ($status, 0, 'check exit status'); 25 26 27# Check that output from stderr can be picked up 28 29$isc = IPC::ShellCmd->new(["/bin/sh", '-c', 'cat >&2']) 30 ->stdin('testing 1, 2, 3...') 31 ->run(); 32 33$stdout = $isc->stdout(); 34$stderr = $isc->stderr(); 35$status = $isc->status(); 36 37is ($stderr, "testing 1, 2, 3...", 'string passed into shell command on stdin and output picked up on stderr'); 38is ($stdout, '', 'nothing leaked out on stdout'); 39is ($status, 0, 'check exit status'); 40 41 42# Check that environment variables are passed through 43 44$isc = IPC::ShellCmd->new(["/bin/sh", '-c', 'echo $ANSWER1-$ANSWER2']) 45 ->add_envs(ANSWER1 => '42', ANSWER2 => '22') 46 ->run(); 47 48$stdout = $isc->stdout(); 49$status = $isc->status(); 50 51like ($stdout, qr{42-22}, 'environment variables picked up'); 52is ($status, 0, 'check exit status'); 53 54# Check that working directory is acted upon 55# (use a directory that exists on Linux, BSD, and MacOS) 56 57my $testdir = abs_path("$Bin/../lib"); 58if (-d $testdir) { 59 $isc = IPC::ShellCmd->new(["/bin/sh", '-c', 'pwd']) 60 ->working_dir($testdir) 61 ->run(); 62 63 $stdout = $isc->stdout(); 64 $status = $isc->status(); 65 66 like ($stdout, qr{^$testdir\s*$}, 'working directory set'); 67 is ($status, 0, 'check exit status'); 68} 69 70 71# Check that umask is acted upon 72 73$isc = IPC::ShellCmd->new(["/bin/sh", '-c', 'umask']) 74 ->set_umask(oct(321)) 75 ->run(); 76 77$stdout = $isc->stdout(); 78$status = $isc->status(); 79 80like ($stdout, qr{^0321\s*$}, 'umask set'); 81is ($status, 0, 'check exit status'); 82 83# Check that environment variables are passed through 84 85$isc = IPC::ShellCmd->new(["/bin/sh", '-c', 'echo $ANSWER1-$ANSWER2']) 86 ->chain_prog(IPC::ShellCmd::Generic->new( Prog => 'time' ), 87 '-include-stdout' => 1) 88 ->add_envs(ANSWER1 => '42', ANSWER2 => '22') 89 ->run(); 90 91$stdout = $isc->stdout(); 92$stderr = $isc->stderr(); 93$status = $isc->status(); 94 95like ($stdout, qr{42-22}, 'environment variables picked up'); 96like ($stderr, qr{\d+\.\d+\s*user.*\d+\.\d+\s*sys}, 'stderr contains timing info'); 97is ($status, 0, 'check exit status'); 98 99 100 101done_testing; 102