Fix building on perls with no . in @INC
[dbsrgits/DBIx-Class.git] / t / lib / ANFANG.pm
index d66322a..c429d74 100644 (file)
@@ -7,15 +7,23 @@ BEGIN {
     require warnings and warnings->import;
     require strict and strict->import;
   }
-
-  # allow 'use ANFANG' to work after it's been do()ne
-  $INC{"ANFANG.pm"} ||= __FILE__;
-  $INC{"t/lib/ANFANG.pm"} ||= __FILE__;
-  $INC{"./t/lib/ANFANG.pm"} ||= __FILE__;
 }
 
-BEGIN {
+#
+# FROM THIS POINT ONWARD EVERYTHING HAPPENS LINEARLY AT RUNTIME
+#
+our $anfang_loaded;
+
+# this allows the obscure but possible call case to behave correctly:
+#
+#   perl -It/lib -MANFANG -e 'do "./t/lib/ANFANG.pm" or die ( $@ || $! )'
+#
+return 1 if $anfang_loaded;
+
+# cover even more bases
+$INC{$_} ||= __FILE__ for (qw( ANFANG.pm t/lib/ANFANG.pm ./t/lib/ANFANG.pm ));
 
+{
   # load-me-first sanity check
   if (
 
@@ -24,8 +32,12 @@ BEGIN {
 
       and
 
-    # if this is set - all bets are off
-    ! $ENV{PERL5OPT}
+    # if these are set - all bets are off
+    ! (
+      $ENV{PERL5OPT}
+        or
+      scalar grep { $_ =~ m| \/ sitecustomize\.pl $ |x } keys %INC
+    )
 
       and
 
@@ -34,6 +46,13 @@ BEGIN {
 
       and
 
+    # a ghetto way of recognizing cperl without loading Config.pm
+    # the $] guard is there because touching $^V on pre-5.10 loads
+    # the entire utf8 stack (wtf!!!)
+    ( "$]" < 5.010 or $^V !~ /\d+c$/ )
+
+      and
+
     # just don't check anything under RELEASE_TESTING
     # a naive approach would be to simply whitelist both
     # strict and warnings, but pre 5.10 there were even
@@ -72,8 +91,9 @@ BEGIN {
 
     require Carp;
 
-    # not loading warnings.pm
-    local $^W = 0;
+    # in case we loaded warnings.pm / used -w
+    # ( do not do `no warnings ...` as it is also a load )
+    local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ };
 
     *UNIVERSAL::VERSION = sub {
       Carp::carp( 'Argument "blah bleh bloh" isn\'t numeric in subroutine entry' );
@@ -92,36 +112,58 @@ BEGIN {
       ($ENV{TRAVIS_REPO_SLUG}||'') =~ m|\w+/dbix-class$|
     )
   ) {
-    require Try::Tiny;
-    my $orig = \&Try::Tiny::try;
-
-    # not loading warnings.pm
-    local $^W = 0;
-
-    *Try::Tiny::try = sub (&;@) {
-      my ($fr, $first_pkg) = 0;
-      while( $first_pkg = caller($fr++) ) {
-        last if $first_pkg !~ /^
-          __ANON__
-            |
-          \Q(eval)\E
-        $/x;
-      }
-
-      if ($first_pkg =~ /DBIx::Class/) {
-        require Test::Builder;
-        Test::Builder->new->ok(0,
-          'Using try{} within DBIC internals is a mistake - use dbic_internal_try{} instead'
-        );
-      }
-
-      goto $orig;
-    };
+    # two levels of if() because of taint mode tangling the %ENV-checks
+    # with the require() call, sigh...
+
+    if ( eval { require Try::Tiny } ) {
+      my $orig = \&Try::Tiny::try;
+
+      # in case we loaded warnings.pm / used -w
+      # ( do not do `no warnings ...` as it is also a load )
+      local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ };
+
+      *Try::Tiny::try = sub (&;@) {
+        my ($fr, $first_pkg) = 0;
+        while( $first_pkg = caller($fr++) ) {
+          last if $first_pkg !~ /^
+            __ANON__
+              |
+            \Q(eval)\E
+          $/x;
+        }
+
+        if ($first_pkg =~ /DBIx::Class/) {
+          require Test::Builder;
+          Test::Builder->new->ok(0,
+            'Using try{} within DBIC internals is a mistake - use dbic_internal_try{} instead'
+          );
+        }
+
+        goto $orig;
+      };
+    }
   }
-
 }
 
-use lib 't/lib';
+
+unshift @INC, 't/lib';
+
+
+# everything expects this to be there
+! -d 't/var'
+  and
+(
+  mkdir 't/var'
+    or
+  # creation is inherently racy
+  do {
+    my $err = $!;
+    require Errno;
+    die "Unable to create 't/var': $err\n"
+      unless $err == Errno::EEXIST();
+  }
+);
+
 
 # Back in ab340f7f ribasushi stupidly introduced a "did you check your deps"
 # verification tied very tightly to Module::Install. The check went away, and
@@ -129,4 +171,36 @@ use lib 't/lib';
 # dead. In order to reduce hair-pulling make sure that ./inc/ is always there
 -f 'Makefile.PL' and mkdir 'inc' and mkdir 'inc/.author';
 
-1;
+END {
+  if( my @finalest_tasks = (
+
+    ( !$ENV{DBICTEST_DIRTY_EXIT} ? () : sub {
+
+      my $exit = $?;
+      require POSIX;
+
+      # Crucial flushes in case we are piping things out (e.g. prove)
+      # Otherwise the last lines will never arrive at the receiver
+      close($_) for \*STDOUT, \*STDERR;
+
+      POSIX::_exit($exit);
+    } ),
+
+  )) {
+
+    # in the case of an early skip_all B may very well not have loaded
+    unless( $INC{"B.pm"} ) {
+      local ( $!, $^E, $?, $@ );
+      require B;
+    }
+
+    # Make sure we run after any cleanup in other END blocks
+    # ( push-to-end twice in a row )
+    push @{ B::end_av()->object_2svref }, sub {
+      push @{ B::end_av()->object_2svref }, @finalest_tasks;
+    }
+  }
+}
+
+# make absolutely sure this is last
+$anfang_loaded = 1;