1 # This is a replacement for the old BEGIN preamble which heads (or
2 # should head) up every core test program to prepare it for running:
9 # Its primary purpose is to clear @INC so core tests don't pick up
10 # modules from an installed Perl.
12 # t/TEST and t/harness will invoke each test script with
13 # perl -I. -MTestInit[=arg,arg,..] some/test.t
14 # You may "use TestInit" in the test # programs but it is not required.
16 # TestInit will completely empty the current @INC and replace it with
17 # new entries based on the args:
19 # U2T: adds ../../lib and ../../t;
21 # T: adds lib and chdir's to the top-level directory.
23 # In the absence of any of the above options, it chdir's to
24 # t/ or cpan/Foo-Bar/ etc as appropriate and correspondingly
25 # sets @INC to (../lib) or ( ../../lib, ../../t)
29 # A: converts any added @INC entries to absolute paths;
30 # NC: unsets $ENV{PERL_CORE};
31 # DOT: unconditionally appends '.' to @INC.
33 # Any trailing '.' in @INC present on entry will be preserved.
35 # P.S. This documentation is not in POD format in order to avoid
36 # problems when there are fundamental bugs in perl.
42 # Let tests know they're running in the perl core. Useful for modules
43 # which live dual lives on CPAN.
44 # Don't interfere with the taintedness of %ENV, this could perturbate tests.
45 # This feels like a better solution than the original, from
47 # https://www.nntp.perl.org/group/perl.perl5.porters/2003/07/msg77533.html
48 $ENV{PERL_CORE} = $^X;
50 $0 =~ s/\.dp$//; # for the test.deparse make target
52 my $add_dot = (@INC && $INC[-1] eq '.'); # preserve existing,
56 my @up_2_t = ('../../lib', '../../t');
57 my ($abs, $chdir, $setopt);
62 } elsif ($_ eq 'U1') {
65 } elsif ($_ eq 'NC') {
66 delete $ENV{PERL_CORE}
71 unless -f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext';
74 } elsif ($_ eq 'DOT') {
77 die "Unknown option '$_'";
81 # Need to default. This behaviour is consistent with previous behaviour,
82 # as the equivalent of this code used to be run at the top level, hence
83 # would happen (unconditionally) before import() was called.
85 if (-f 't/TEST' && -f 'MANIFEST' && -d 'lib' && -d 'ext') {
86 # We're being run from the top level. Try to change directory, and
87 # set things up correctly. This is a 90% solution, but for
88 # hand-running tests, that's good enough
89 if ($0 =~ s!^((?:ext|dist|cpan)[\\/][^\\/]+)[\\/](.*\.t)$!$2!) {
90 # Looks like a test in ext.
94 $^X =~ s!^\.([\\/])!..$1..$1!;
98 $setopt = $0 =~ m!^lib/!;
101 # (likely) we're being run by t/TEST or t/harness, and we're a test
103 if (defined &DynaLoader::boot_DynaLoader) {
108 # t/TEST does not supply -I../lib, so buildcustomize.pl is
109 # not automatically included.
110 unshift @INC, '../lib';
111 do "../lib/buildcustomize.pl";
116 if (defined $chdir) {
117 chdir $chdir or die "Can't chdir '$chdir': $!";
121 require File::Spec::Functions;
122 # Forcibly untaint this.
123 @INC = map { $_ = File::Spec::Functions::rel2abs($_); /(.*)/; $1 } @INC;
124 $^X = File::Spec::Functions::rel2abs($^X);
131 } elsif ($^O eq 'MSWin32') {
137 my $lib = join $sep, @INC;
138 if (exists $ENV{PERL5LIB}) {
139 $ENV{PERL5LIB} = $lib . substr $ENV{PERL5LIB}, 0, 0;
141 $ENV{PERL5LIB} = $lib;
145 push @INC, '.' if $add_dot;