Fix building on perls with no . in @INC
[dbsrgits/DBIx-Class.git] / t / lib / ANFANG.pm
CommitLineData
c0329273 1package # hide from pauses
2 ANFANG;
3
4# load-time critical
5BEGIN {
6 if ( $ENV{RELEASE_TESTING} ) {
7 require warnings and warnings->import;
8 require strict and strict->import;
9 }
c0329273 10}
11
26710bc9 12#
13# FROM THIS POINT ONWARD EVERYTHING HAPPENS LINEARLY AT RUNTIME
14#
15our $anfang_loaded;
16
17# this allows the obscure but possible call case to behave correctly:
18#
8aae7940 19# perl -It/lib -MANFANG -e 'do "./t/lib/ANFANG.pm" or die ( $@ || $! )'
26710bc9 20#
21return 1 if $anfang_loaded;
c0329273 22
26710bc9 23# cover even more bases
24$INC{$_} ||= __FILE__ for (qw( ANFANG.pm t/lib/ANFANG.pm ./t/lib/ANFANG.pm ));
25
26{
c0329273 27 # load-me-first sanity check
28 if (
29
30 # nobody shut us off
31 ! $ENV{DBICTEST_ANFANG_DEFANG}
32
33 and
34
26710bc9 35 # if these are set - all bets are off
36 ! (
37 $ENV{PERL5OPT}
38 or
39 scalar grep { $_ =~ m| \/ sitecustomize\.pl $ |x } keys %INC
40 )
c0329273 41
42 and
43
44 # -d:Confess / -d:TraceUse and the like
45 ! $^P
46
47 and
48
64d48e19 49 # a ghetto way of recognizing cperl without loading Config.pm
50 # the $] guard is there because touching $^V on pre-5.10 loads
51 # the entire utf8 stack (wtf!!!)
52 ( "$]" < 5.010 or $^V !~ /\d+c$/ )
53
54 and
55
c0329273 56 # just don't check anything under RELEASE_TESTING
57 # a naive approach would be to simply whitelist both
58 # strict and warnings, but pre 5.10 there were even
59 # more modules loaded by these two:
60 #
61 # perlbrew exec perl -Mstrict -Mwarnings -e 'warn join "\n", sort keys %INC'
62 #
63 ! $ENV{RELEASE_TESTING}
64
65 and
66
67 my @undesirables = grep {
68
69 ($INC{$_}||'') ne __FILE__
70
71 and
72
73 # allow direct loads via -M
74 $_ !~ m{^ DBICTest (?: /Schema )? \.pm $}x
75
76 } keys %INC
77
78 ) {
79
80 my ( $fr, @frame );
81 while (@frame = caller(++$fr)) {
82 last if $frame[1] !~ m{ (?: \A | [\/\\] ) t [\/\\] lib [\/\\] }x;
83 }
84
85 die __FILE__ . " must be loaded before any other module (i.e. @{[ join ', ', map { qq('$_') } sort @undesirables ]}) at $frame[1] line $frame[2]\n";
86 }
87
88
89 if ( $ENV{DBICTEST_VERSION_WARNS_INDISCRIMINATELY} ) {
90 my $ov = UNIVERSAL->can("VERSION");
91
92 require Carp;
93
50841788 94 # in case we loaded warnings.pm / used -w
95 # ( do not do `no warnings ...` as it is also a load )
96 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ };
c0329273 97
98 *UNIVERSAL::VERSION = sub {
99 Carp::carp( 'Argument "blah bleh bloh" isn\'t numeric in subroutine entry' );
100 &$ov;
101 };
102 }
103
104
105 if (
106 $ENV{DBICTEST_ASSERT_NO_SPURIOUS_EXCEPTION_ACTION}
107 or
108 # keep it always on during CI
109 (
110 ($ENV{TRAVIS}||'') eq 'true'
111 and
112 ($ENV{TRAVIS_REPO_SLUG}||'') =~ m|\w+/dbix-class$|
113 )
114 ) {
e2741c7f 115 # two levels of if() because of taint mode tangling the %ENV-checks
116 # with the require() call, sigh...
117
118 if ( eval { require Try::Tiny } ) {
119 my $orig = \&Try::Tiny::try;
120
121 # in case we loaded warnings.pm / used -w
122 # ( do not do `no warnings ...` as it is also a load )
123 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ };
124
125 *Try::Tiny::try = sub (&;@) {
126 my ($fr, $first_pkg) = 0;
127 while( $first_pkg = caller($fr++) ) {
128 last if $first_pkg !~ /^
129 __ANON__
130 |
131 \Q(eval)\E
132 $/x;
133 }
134
135 if ($first_pkg =~ /DBIx::Class/) {
136 require Test::Builder;
137 Test::Builder->new->ok(0,
138 'Using try{} within DBIC internals is a mistake - use dbic_internal_try{} instead'
139 );
140 }
141
142 goto $orig;
143 };
144 }
c0329273 145 }
c0329273 146}
147
26710bc9 148
50841788 149unshift @INC, 't/lib';
26710bc9 150
c0329273 151
e48635f7 152# everything expects this to be there
50841788 153! -d 't/var'
154 and
155(
e48635f7 156 mkdir 't/var'
157 or
50841788 158 # creation is inherently racy
159 do {
160 my $err = $!;
161 require Errno;
162 die "Unable to create 't/var': $err\n"
163 unless $err == Errno::EEXIST();
164 }
e48635f7 165);
166
26710bc9 167
7b87b77c 168# Back in ab340f7f ribasushi stupidly introduced a "did you check your deps"
169# verification tied very tightly to Module::Install. The check went away, and
170# so eventually will M::I, but bisecting can bring all of this back from the
171# dead. In order to reduce hair-pulling make sure that ./inc/ is always there
172-f 'Makefile.PL' and mkdir 'inc' and mkdir 'inc/.author';
173
10dd5c05 174END {
175 if( my @finalest_tasks = (
176
177 ( !$ENV{DBICTEST_DIRTY_EXIT} ? () : sub {
178
179 my $exit = $?;
180 require POSIX;
181
182 # Crucial flushes in case we are piping things out (e.g. prove)
183 # Otherwise the last lines will never arrive at the receiver
2c4abbea 184 close($_) for \*STDOUT, \*STDERR;
10dd5c05 185
186 POSIX::_exit($exit);
187 } ),
188
189 )) {
190
191 # in the case of an early skip_all B may very well not have loaded
192 unless( $INC{"B.pm"} ) {
193 local ( $!, $^E, $?, $@ );
194 require B;
195 }
196
197 # Make sure we run after any cleanup in other END blocks
198 # ( push-to-end twice in a row )
199 push @{ B::end_av()->object_2svref }, sub {
200 push @{ B::end_av()->object_2svref }, @finalest_tasks;
201 }
202 }
203}
26710bc9 204
205# make absolutely sure this is last
206$anfang_loaded = 1;