I am trying to write a unit test case in perl where i need to mock output of command run via backticks and its return code twice.
Consider code flow like below
sub foo {
my $out = `ls -R`;
my $ret = $?;
if( $ret == 0 ) {
$out = `ls -R > foo.txt`;
} elsif {
# some retry logic
# i want to cover this code path
}
return ($ret, $out);
}
Now i need to mock non zero return codes. How can i acheive that?
I have something like below, but this only mocks output but return code is always 0
BEGIN {
*CORE::GLOBAL::readpipe = sub($) {
my $var = $_[0];
return 1;
}
};
I am using Perl 5.10. and i cannot change using backticks to execute the command.