johanvdb has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm stuck with the next problem...
I want to make a test in the ./t directory that accesses a config file. Using an absolute path from out of the test script is impossible as the config files live in the ./t directory and I don't know which directory will be used to build the package from.

It also seems impossible to use a relative path, as the script is executed in Test::Harness, or at least I don't see it on how to track down this ...

So, I was thinking on building the path to the config file dynamically, using something like 'FindBin'....

Does any of the enlightened monks has an idea on how to approach this?

use Test::More qw( no_plan ); use MyPackage; # try relative ( good idea, but does not work ) my $p1 = MyPackage->new( -config => './test.conf' ); # try absolute ( bad idea, does not *always* work ) my $p2 = MyPackage->new( -config => '/tmp/MyPackage/t/test.conf' ); # help !

J.

Replies are listed 'Best First'.
Re: Accessing config files under 'make test'
by IlyaM (Parson) on Feb 26, 2002 at 20:53 UTC
    When test scripts are run from make test current directory is top directory of distribution. So it is actually possible to use relative paths:
    use Test::More qw( no_plan ); use MyPackage; # relative path my $p1 = MyPackage->new( -config => 't/test.conf' ); # more portable variation use File::Spec; my $p1 = MyPackage->new( -config => File::Spec->catfile('t', 'test.con +f') );

    --
    Ilya Martynov (http://martynov.org/)

      Hi,

      Thanks for the answer!

      I was not aware of this, but then again, I could have tried to find it out myself ... oohh boy what a bad attidude I have lately!

      Thanks!

      J.