Fix silent failures on autoinc PK without an is_auto_increment attribute
[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#
19# perl -Mt::lib::ANFANG -e 'do "./t/lib/ANFANG.pm" or die ( $@ || $! )'
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 ) {
115 require Try::Tiny;
116 my $orig = \&Try::Tiny::try;
117
50841788 118 # in case we loaded warnings.pm / used -w
119 # ( do not do `no warnings ...` as it is also a load )
120 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ };
c0329273 121
122 *Try::Tiny::try = sub (&;@) {
123 my ($fr, $first_pkg) = 0;
124 while( $first_pkg = caller($fr++) ) {
125 last if $first_pkg !~ /^
126 __ANON__
127 |
128 \Q(eval)\E
129 $/x;
130 }
131
132 if ($first_pkg =~ /DBIx::Class/) {
133 require Test::Builder;
134 Test::Builder->new->ok(0,
135 'Using try{} within DBIC internals is a mistake - use dbic_internal_try{} instead'
136 );
137 }
138
139 goto $orig;
140 };
141 }
c0329273 142}
143
26710bc9 144
50841788 145unshift @INC, 't/lib';
26710bc9 146
c0329273 147
e48635f7 148# everything expects this to be there
50841788 149! -d 't/var'
150 and
151(
e48635f7 152 mkdir 't/var'
153 or
50841788 154 # creation is inherently racy
155 do {
156 my $err = $!;
157 require Errno;
158 die "Unable to create 't/var': $err\n"
159 unless $err == Errno::EEXIST();
160 }
e48635f7 161);
162
26710bc9 163
7b87b77c 164# Back in ab340f7f ribasushi stupidly introduced a "did you check your deps"
165# verification tied very tightly to Module::Install. The check went away, and
166# so eventually will M::I, but bisecting can bring all of this back from the
167# dead. In order to reduce hair-pulling make sure that ./inc/ is always there
168-f 'Makefile.PL' and mkdir 'inc' and mkdir 'inc/.author';
169
10dd5c05 170END {
171 if( my @finalest_tasks = (
172
173 ( !$ENV{DBICTEST_DIRTY_EXIT} ? () : sub {
174
175 my $exit = $?;
176 require POSIX;
177
178 # Crucial flushes in case we are piping things out (e.g. prove)
179 # Otherwise the last lines will never arrive at the receiver
2c4abbea 180 close($_) for \*STDOUT, \*STDERR;
10dd5c05 181
182 POSIX::_exit($exit);
183 } ),
184
185 )) {
186
187 # in the case of an early skip_all B may very well not have loaded
188 unless( $INC{"B.pm"} ) {
189 local ( $!, $^E, $?, $@ );
190 require B;
191 }
192
193 # Make sure we run after any cleanup in other END blocks
194 # ( push-to-end twice in a row )
195 push @{ B::end_av()->object_2svref }, sub {
196 push @{ B::end_av()->object_2svref }, @finalest_tasks;
197 }
198 }
199}
26710bc9 200
201# make absolutely sure this is last
202$anfang_loaded = 1;