perlmodule-build

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


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?


Solution

  • 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.