"chdir 't' if -d 't'" is evil?

Hi,

I'm working on a release of PathTools, including File::Spec and Cwd.  
I've been told (though I never quite grokked it) to put the following 
code in the test scripts:

   BEGIN {
     chdir 't' if -d 't';
     if ($ENV{PERL_CORE}) {
       @INC = '../lib';
     }
   }
   use Cwd;

However, I've just realized that this messes up @INC, because the 
Cwd.pm we need to test is in blib/lib/, which is added to the path by 
-Mblib, but it won't be found if we chdir('t').

I would really like to remove that whole block and make Test::Harness 
handle the @INC-setting.  Short of that happening, can anyone suggest a 
better BEGIN block to use in the core case?

  -Ken

0
ken
11/23/2004 2:46:18 AM
perl.perl5.porters 48287 articles. 1 followers. Follow

2 Replies
1050 Views

Similar Articles

[PageSpeed] 39
Get it on Google Play
Get it on Apple App Store

On Mon, Nov 22, 2004 at 08:46:18PM -0600, Ken Williams wrote:
> I'm working on a release of PathTools, including File::Spec and Cwd.  
> I've been told (though I never quite grokked it) to put the following 
> code in the test scripts:
> 
>   BEGIN {
>     chdir 't' if -d 't';
>     if ($ENV{PERL_CORE}) {
>       @INC = '../lib';
>     }
>   }
>   use Cwd;

There's a couple issues.

1)  The core chdir's to t/ before running tests so if the tests goes bad
    it won't scribble all over the build.  For historical reasons MakeMaker
    does not.

2)  @INC being unset prevents libraries from a previously installed Perl from
    being picked up.

3)  There is a thing which this for you.  t/TestInit.pm.  However...

4)  Humans often run tests by hand.  They usually forget to cd into t/
    first and will certainly forget to -MTestInit.  So its mostly out of
    making it easy for humans not intimiate with the core to rerun tests
    and report bugs that we leave the BEGIN blocks there.


> However, I've just realized that this messes up @INC, because the 
> Cwd.pm we need to test is in blib/lib/, which is added to the path by 
> -Mblib, but it won't be found if we chdir('t').

You put the chdir 't' inside the if($ENV{PERL_CORE}).


> I would really like to remove that whole block and make Test::Harness 
> handle the @INC-setting.  Short of that happening, can anyone suggest a 
> better BEGIN block to use in the core case?

BEGIN {
    if( $ENV{PERL_CORE} ) {
        chdir 't';
        @INC = '../lib';
    }
}

The only problem here is now your CPAN version and your core version
are running in different locations.  CPAN version is in src/, core version
is in src/t/.  So I sometimes do this.

BEGIN {
    if( $ENV{PERL_CORE} ) {
        chdir 't';
        @INC = '../lib';
    }
}
chdir 't';

This will work as long as I only use (ie. load at compile-time) and do not
require.  If I try to load something at run-time its possible something in
@INC will be relative (as with older MakeMakers) and not be able to find
blib.


-- 
Michael G Schwern        schwern@pobox.com  http://www.pobox.com/~schwern/
Now if you'll excuse me, I've got about a hundred hours of memories to 
erase!!
        -- http://www.angryflower.com/allrigh.gif
0
schwern
11/23/2004 3:51:26 AM
On Nov 22, 2004, at 9:51 PM, Michael G Schwern wrote:
>
> BEGIN {
>     if( $ENV{PERL_CORE} ) {
>         chdir 't';
>         @INC = '../lib';
>     }
> }
> chdir 't';

Yeah, that oughta work.  Thanks.

  -Ken

0
ken
11/23/2004 4:49:20 AM
Reply:

Web resources about - "chdir 't' if -d 't'" is evil? - perl.perl5.porters

Resources last updated: 12/18/2015 12:42:07 PM