Fix test suite to work again with DBICTEST_SQLITE_USE_FILE
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest.pm
index 49df060..fa92a95 100644 (file)
@@ -5,8 +5,9 @@ use strict;
 use warnings;
 
 use DBICTest::Util 'local_umask';
-use DBICTest::RunMode;
+use DBICTest::Schema;
 use DBICTest::Util::LeakTracer qw/populate_weakregistry assert_empty_weakregistry/;
+use DBIx::Class::_Util 'detected_reinvoked_destructor';
 use Carp;
 use Path::Class::File ();
 use File::Spec;
@@ -142,6 +143,7 @@ END {
 
 $SIG{INT} = sub { _cleanup_dbfile(); exit 1 };
 
+my $need_global_cleanup;
 sub _cleanup_dbfile {
     # cleanup if this is us
     if (
@@ -151,6 +153,10 @@ sub _cleanup_dbfile {
         or
       $ENV{DBICTEST_LOCK_HOLDER} == $$
     ) {
+        if ($need_global_cleanup and my $dbh = DBICTest->schema->storage->_dbh) {
+          $dbh->disconnect;
+        }
+
         my $db_file = _sqlite_dbfilename();
         unlink $_ for ($db_file, "${db_file}-journal");
     }
@@ -217,7 +223,7 @@ sub _database {
           $dbh->{Callbacks} = {
             connect => sub { $guard_cb->('connect') },
             disconnect => sub { $guard_cb->('disconnect') },
-            DESTROY => sub { $guard_cb->('DESTROY') },
+            DESTROY => sub { &detected_reinvoked_destructor; $guard_cb->('DESTROY') },
           };
         }
       },
@@ -314,9 +320,8 @@ sub init_schema {
 
     my $schema;
 
-    require DBICTest::Schema;
-
     if ($args{compose_connection}) {
+      $need_global_cleanup = 1;
       $schema = DBICTest::Schema->compose_connection(
                   'DBICTest', $self->_database(%args)
                 );
@@ -346,7 +351,6 @@ sub init_schema {
 
 END {
   # Make sure we run after any cleanup in other END blocks
-  require B;
   push @{ B::end_av()->object_2svref }, sub {
     assert_empty_weakregistry($weak_registry, 'quiet');
   };
@@ -576,10 +580,4 @@ sub populate_schema {
     ]);
 }
 
-sub connect_schema {
-  my $self = shift;
-  require DBICTest::Schema;
-  return DBICTest::Schema->connect(@_);
-}
-
 1;