views:

97

answers:

3

I'm writing Perl t/*.t tests for an existing project. During development, I'd like to use 'prove' to run select tests at arbitrary depths in the module directory hierarchy. I've created some t/ directories at the same depth as the *.pm files I'd like to test. Unfortunately, the lib/ at the base of the project's code is not in @INC.

What's the best way to automatically detect and add the path to @INC? I've looked at the -b option to prove and blib in general but they don't seem to work (they take the pwd and append blib/lib to it and add that to @INC). Ideally, the solution would not require an environment variable or installing the modules and would accommodate a number of working copies of the same code.

Is my approach wrong? Is there something obvious I'm missing? CPAN modules seem to solve this through installation or copied code blocks at the top of *.t files (yuck).

Edit:

Structure looks like:

/home/user/proj/branch\_foo/lib/First/Second/Third.pm
/home/user/proj/branch\_foo/lib/First/Second/t/third.t

This works:

~/proj/branch\_foo/lib/First/Second$ prove -I../..

But I'd like:

~/proj/branch\_foo/lib/First/Second$ prove -r

Or:

~/proj/branch\_foo/lib/First/Second$ prove t/third.t

So I can do:

~/proj/branch\_foo/lib$ prove -r

Instead of:

~/proj/branch\_foo/lib$ prove -I. -r

Maybe I just have to use -I and deal with it.

+2  A: 

prove has an -I command line option:

-I Library paths to include.

Can you specify the paths using that option?

Update:

Thanks for providing the layout. I cannot think of a quick solution right now, but maybe you can use .proverc files in the appropriate directories to cut down on the typing required. I am not sure how multiple .proverc files interact with the -r option but this is something to look into.

Sinan Ünür
That's what I'm currently doing. Do I need to manually recalculate and specify this for the depth that I'm at? There's no magic option?
sOpen
@sOpen I am sorry, I must have misunderstood. Can you explain the layout better? Maybe show some paths? Do using relative paths for the -I switch not work?
Sinan Ünür
The relative paths work... and that may be the best option. I will edit the question to clarify. Thanks.
sOpen
.proverc is a great answer. Thanks!
sOpen
@sOpen Glad it worked. You are welcome.
Sinan Ünür
A: 

You can set the environment variable PERL5LIB (as described in perldoc perlrun) which will cause perl to prepend the colon-separated paths to your @INC:

% export PERL5LIB=/absolute/path/to/your/libraries
% cd into/part/of/your/test/suite
% prove -r
Brian Phillips
Note the OP's request: "Ideally, the solution would not require an environment variable or installing the modules and would accommodate a number of working copies of the same code."
Sinan Ünür
+2  A: 

This is exactly why the blib module exist. It looks in the current directory then its parent directories looking for blib/lib to add to @INC. It's part of the Perl standard library, so you should already have it.

From somewhere under t/ you can use the -M switch to load blib:

% perl -Mblib test.t

If you want to use it with prove

% perl -Mblib prove test.t

I think that's supposed to be the same thing as the -b switch in prove, but that last time I tried it (long, long ago) it didn't work. Maybe they've fixed it to work like blib.pm:

% prove -b test.t

Note that this requires you to build your distro first rather than test from lib/, but you should be doing that anyway. :)

brian d foy
I guess I never noticed the "working back up to five levels of '..'" part: http://perldoc.perl.org/blib.htmlThanks for pointing it out.
Sinan Ünür