How to pass arguments to a Perl test when run using Module::Build

179 views Asked by At

I am building a Perl package using Module::Build. The tests t/*.t for the package have to use a program residing in a directory that a user should supply during invoking tests:

perl Build.PL
./Build
./Build test user-supplied-directory

The Module::Build documentation provides information on passing arguments to the Build script but I have not found how to pass them to a test. Is it possible?

2

There are 2 answers

0
mike_934 On BEST ANSWER

Since one cannot set an environment variable in the same command line in Windows, I decided to change Build.PL for passing an environment variable to tests by subclassing Module::Build (Adding an action).

# Build.PL
use Module::Build;
use strict;
use warnings;

my $class = Module::Build->subclass(
    class => "Module::Build::Husky",
    code => <<'SUBCLASS' );

sub ACTION_test
{
    my $self = shift;
    my $dir = ($^O =~ /Win/) ? $ARGV[2] : $ARGV[1];
    $ENV{HUSKYBINDIR} = $dir if(defined($dir) && $dir ne "" && -d $dir);
    $self->SUPER::ACTION_test;
}
SUBCLASS

my $build = $class->new
(
    ...
);
$build->create_build_script;

Now when user runs

Build test user-supplied-directory

the user-supplied-directory is passed to tests as $ENV{HUSKYBINDIR}.

Strange enough that the same command-line argument is passed as $ARGV[1] in Linux and as $ARGV[2] in Windows. I had no opportunity to test it in FreeBSD and Mac OS X but hopefully, it will be $ARGV[1] as in Linux.

0
Schwern On

Typically one uses environment variables. This allows one to change the behavior of the tests without altering the build script.

TEST_ALL_THE_THINGS=1 ./Build test