This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In TestInit, inline new_inc() and set_opt() into import(), their only caller.
[perl5.git] / TestInit.pm
CommitLineData
4343e7c3 1# This is a replacement for the old BEGIN preamble which heads (or
db7c4376 2# should head) up every core test program to prepare it for running.
4343e7c3
MS
3# Now instead of:
4#
5# BEGIN {
6# chdir 't' if -d 't';
7# @INC = '../lib';
8# }
9#
c1b78184
MS
10# Its primary purpose is to clear @INC so core tests don't pick up
11# modules from an installed Perl.
12#
db7c4376
PG
13# t/TEST will use -MTestInit. You may "use TestInit" in the test
14# programs but it is not required.
4343e7c3 15#
db7c4376
PG
16# P.S. This documentation is not in POD format in order to avoid
17# problems when there are fundamental bugs in perl.
4343e7c3 18
18fc9488
DM
19package TestInit;
20
5c9e8bc6 21$VERSION = 1.04;
2af1ab88 22
5ed59b83
NC
23# Let tests know they're running in the perl core. Useful for modules
24# which live dual lives on CPAN.
e447daf9
NC
25# Don't interfere with the taintedness of %ENV, this could perturbate tests.
26# This feels like a better solution than the original, from
27# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2003-07/msg00154.html
28$ENV{PERL_CORE} = $^X;
2adbc9b6 29
2adbc9b6
NC
30sub import {
31 my $self = shift;
b6800926
NC
32 my @up_2_t = ('../../lib', '../../t');
33 my @new_inc;
34 my ($abs, $chdir, $setopt);
2adbc9b6
NC
35 foreach (@_) {
36 if ($_ eq 'U2T') {
8fcfece7 37 @new_inc = @up_2_t;
b6800926 38 $setopt = 1;
76cc22ec
NC
39 } elsif ($_ eq 'U1') {
40 @new_inc = '../lib';
b6800926 41 $setopt = 1;
2adbc9b6
NC
42 } elsif ($_ eq 'NC') {
43 delete $ENV{PERL_CORE}
44 } elsif ($_ eq 'A') {
45 $abs = 1;
a14453b9
NC
46 } elsif ($_ eq 'T') {
47 $chdir = '..'
48 unless -f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext';
49 @new_inc = 'lib';
50 $setopt = 1;
2adbc9b6
NC
51 } else {
52 die "Unknown option '$_'";
53 }
54 }
55
b6800926
NC
56 # Need to default. This behaviour is consistent with previous behaviour,
57 # as the equivalent of this code used to be run at the top level, hence
58 # would happen (unconditionally) before import() was called.
59 unless (@new_inc) {
60 if (-f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext') {
61 # We're being run from the top level. Try to change directory, and
62 # set things up correctly. This is a 90% solution, but for
63 # hand-running tests, that's good enough
64 if ($0 =~ s!^((?:ext|dist|cpan)[\\/][^\\/]+)[\//](.*\.t)$!$2!) {
65 # Looks like a test in ext.
66 $chdir = $1;
67 @new_inc = @up_2_t;
68 $setopt = 1;
69 $^X =~ s!^\.([/\\])!..$1..$1!;
70 } else {
71 $chdir = 't';
72 @new_inc = '../lib';
73 $setopt = $0 =~ m!^lib/!;
74 }
75 } else {
76 # (likely) we're being run by t/TEST or t/harness, and we're a test
77 # in t/
2adbc9b6
NC
78 @new_inc = '../lib';
79 }
b6800926
NC
80 }
81
82 if (defined $chdir) {
83 chdir $chdir or die "Can't chdir '$chdir': $!";
84 }
85
86 if ($abs) {
2adbc9b6
NC
87 @INC = @new_inc;
88 require File::Spec::Functions;
89 # Forcibly untaint this.
90 @new_inc = map { $_ = File::Spec::Functions::rel2abs($_); /(.*)/; $1 }
91 @new_inc;
92 $^X = File::Spec::Functions::rel2abs($^X);
93 }
94
db6ebcbf
NC
95 @INC = @new_inc;
96 push @INC, '.' unless ${^TAINT};
97
98 if ($setopt) {
99 my $sep;
100 if ($^O eq 'VMS') {
101 $sep = '|';
102 } elsif ($^O eq 'MSWin32') {
103 $sep = ';';
104 } else {
105 $sep = ':';
106 }
107
108 my $lib = join $sep, @new_inc;
109 if (exists $ENV{PERL5LIB}) {
110 $ENV{PERL5LIB} = $lib . substr $ENV{PERL5LIB}, 0, 0;
111 } else {
112 $ENV{PERL5LIB} = $lib;
113 }
114 }
5ed59b83 115}
a1910616 116
7a315204 117$0 =~ s/\.dp$//; # for the test.deparse make target
ec5f1610 1181;