Remove use of Try::Tiny entirely (the missing part of ddcc02d1)
[dbsrgits/DBIx-Class.git] / t / lib / ANFANG.pm
index 354bc01..e5e6035 100644 (file)
@@ -46,6 +46,13 @@ $INC{$_} ||= __FILE__ for (qw( ANFANG.pm t/lib/ANFANG.pm ./t/lib/ANFANG.pm ));
 
       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
@@ -84,8 +91,9 @@ $INC{$_} ||= __FILE__ for (qw( ANFANG.pm t/lib/ANFANG.pm ./t/lib/ANFANG.pm ));
 
     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' );
@@ -104,44 +112,56 @@ $INC{$_} ||= __FILE__ for (qw( ANFANG.pm t/lib/ANFANG.pm ./t/lib/ANFANG.pm ));
       ($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;
+      };
+    }
   }
 }
 
 
-require lib;
-lib->import('t/lib');
+unshift @INC, 't/lib';
 
 
 # everything expects this to be there
-! -d 't/var' and (
+! -d 't/var'
+  and
+(
   mkdir 't/var'
     or
-  die "Unable to create 't/var': $!\n"
+  # creation is inherently racy
+  do {
+    my $err = $!;
+    require Errno;
+    die "Unable to create 't/var': $err\n"
+      unless $err == Errno::EEXIST();
+  }
 );
 
 
@@ -151,6 +171,36 @@ lib->import('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';
 
+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;