This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Wording tweak to clarify that perlbug submits the patch for you via email
[perl5.git] / TestInit.pm
index 73bd8cf..16eb318 100644 (file)
@@ -18,7 +18,7 @@
 
 package TestInit;
 
-$VERSION = 1.02;
+$VERSION = 1.04;
 
 # Let tests know they're running in the perl core.  Useful for modules
 # which live dual lives on CPAN.
@@ -27,86 +27,89 @@ $VERSION = 1.02;
 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2003-07/msg00154.html
 $ENV{PERL_CORE} = $^X;
 
-sub new_inc {
-    if (${^TAINT}) {
-       @INC = @_;
-    } else {
-       @INC = (@_, '.');
-    }
-}
-
-sub set_opt {
-    my $sep;
-    if ($^O eq 'VMS') {
-       $sep = '|';
-    } elsif ($^O eq 'MSWin32') {
-       $sep = ';';
-    } else {
-       $sep = ':';
-    }
-
-    my $lib = join $sep, @_;
-    if (exists $ENV{PERL5LIB}) {
-       $ENV{PERL5LIB} = $lib . substr $ENV{PERL5LIB}, 0, 0;
-    } else {
-       $ENV{PERL5LIB} = $lib;
-    }
-}
-
-my @up_2_t = ('../../lib', '../../t');
-# This is incompatible with the import options.
-if (-f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext') {
-    # We're being run from the top level. Try to change directory, and set
-    # things up correctly. This is a 90% solution, but for hand-running tests,
-    # that's good enough
-    if ($0 =~ s!^(ext[\\/][^\\/]+)[\//](.*\.t)$!$2!) {
-       # Looks like a test in ext.
-       chdir $1 or die "Can't chdir '$1': $!";
-       new_inc(@up_2_t);
-       set_opt(@up_2_t);
-       $^X =~ s!^\./!../../!;
-       $^X =~ s!^\.\\!..\\..\\!;
-    } else {
-       chdir 't' or die "Can't chdir 't': $!";
-       new_inc('../lib');
-    }
-} else {
-    new_inc('../lib');
-}
+$0 =~ s/\.dp$//; # for the test.deparse make target
 
 sub import {
     my $self = shift;
-    my $abs;
+    my @up_2_t = ('../../lib', '../../t');
+    my ($abs, $chdir, $setopt);
     foreach (@_) {
        if ($_ eq 'U2T') {
-           @new_inc = @up_2_t;
+           @INC = @up_2_t;
+           $setopt = 1;
+       } elsif ($_ eq 'U1') {
+           @INC = '../lib';
+           $setopt = 1;
        } elsif ($_ eq 'NC') {
            delete $ENV{PERL_CORE}
        } elsif ($_ eq 'A') {
            $abs = 1;
+       } elsif ($_ eq 'T') {
+           $chdir = '..'
+               unless -f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext';
+           @INC = 'lib';
+           $setopt = 1;
        } else {
            die "Unknown option '$_'";
        }
     }
 
-    if ($abs) {
-       if(!@new_inc) {
-           @new_inc = '../lib';
+    # Need to default. This behaviour is consistent with previous behaviour,
+    # as the equivalent of this code used to be run at the top level, hence
+    # would happen (unconditionally) before import() was called.
+    unless ($setopt) {
+       if (-f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext') {
+           # We're being run from the top level. Try to change directory, and
+           # set things up correctly. This is a 90% solution, but for
+           # hand-running tests, that's good enough
+           if ($0 =~ s!^((?:ext|dist|cpan)[\\/][^\\/]+)[\\/](.*\.t)$!$2!) {
+               # Looks like a test in ext.
+               $chdir = $1;
+               @INC = @up_2_t;
+               $setopt = 1;
+               $^X =~ s!^\.([\\/])!..$1..$1!;
+           } else {
+               $chdir = 't';
+               @INC = '../lib';
+               $setopt = $0 =~ m!^lib/!;
+           }
+       } else {
+           # (likely) we're being run by t/TEST or t/harness, and we're a test
+           # in t/
+           @INC = '../lib';
        }
-       @INC = @new_inc;
+    }
+
+    if (defined $chdir) {
+       chdir $chdir or die "Can't chdir '$chdir': $!";
+    }
+
+    if ($abs) {
        require File::Spec::Functions;
        # Forcibly untaint this.
-       @new_inc = map { $_ = File::Spec::Functions::rel2abs($_); /(.*)/; $1 }
-           @new_inc;
+       @INC = map { $_ = File::Spec::Functions::rel2abs($_); /(.*)/; $1 } @INC;
        $^X = File::Spec::Functions::rel2abs($^X);
     }
 
-    if (@new_inc) {
-       new_inc(@new_inc);
-       set_opt(@new_inc);
+    if ($setopt) {
+       my $sep;
+       if ($^O eq 'VMS') {
+           $sep = '|';
+       } elsif ($^O eq 'MSWin32') {
+           $sep = ';';
+       } else {
+           $sep = ':';
+       }
+
+       my $lib = join $sep, @INC;
+       if (exists $ENV{PERL5LIB}) {
+           $ENV{PERL5LIB} = $lib . substr $ENV{PERL5LIB}, 0, 0;
+       } else {
+           $ENV{PERL5LIB} = $lib;
+       }
     }
+
+    push @INC, '.' unless ${^TAINT};
 }
 
-$0 =~ s/\.dp$//; # for the test.deparse make target
 1;
-