Overriding a module that is used by a program I'm testing
I am revising a Perl program and I wanted a test harness that could run the original version of the program (call it launch_rockets.pl
) and collect the standard output, but somehow skip the system
calls that occur inside launch_rockets.pl
. The following code successfully overrides system
inside launch_rockets.pl
:
use subs qw(system);
my $SYSTEM_SUCCESS = 0;
sub system {
print "***\n";
print "system @_\n";
print "***\n\n";
return $SYSTEM_SUC开发者_如何转开发CESS;
}
local @ARGV = @test_args;
do 'launch_rockets.pl';
So far so good. But launch_rockets.pl
also contains
use Proc::Background;
and later
Proc::Background->new('perl', 'launch_missiles.pl');
I could copy launch_rockets.pl
into a sandbox where Proc::Background
is replaced by a stub, but I was wondering if there was any override strategy that would be effective inside a do FILE
call in the file's original environment.
use lib '/my/test/library/path';
lib prepends the directory to @INC
, so /my/test/library/path/Proc/Background.pm
will be the file that gets loaded. Put whatever code you want in there.
Another alternative would be:
{
package Proc::Background;
... # Put stub code here
} # end of package Proc::Background
$INC{'Proc/Background.pm'} = 1; # Make Perl think Proc::Background is loaded
精彩评论