39d02b05eee227473754c2f5f2855798e6794c7d
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_test_dir.pm
1 package dbixcsl_test_dir;
2
3 use strict;
4 use warnings;
5 use File::Path 'rmtree';
6 use Scalar::Util 'weaken';
7 use namespace::clean;
8 use DBI ();
9
10 our $tdir = 't/var';
11
12 use base qw/Exporter/;
13 our @EXPORT_OK = '$tdir';
14
15 die "/t does not exist, this can't be right...\n"
16   unless -d 't';
17
18 unless (-d $tdir) {
19   mkdir $tdir or die "Unable to create $tdir: $!\n";
20 }
21
22 # We need to disconnect all active DBI handles before deleting the directory,
23 # otherwise the SQLite .db files cannot be deleted on Win32 (file in use) since
24 # END does not run in any sort of order.
25
26 no warnings 'redefine';
27
28 my $connect = \&DBI::connect;
29
30 my @handles;
31
32 *DBI::connect = sub {
33     my $dbh = $connect->(@_);
34     push @handles, $dbh;
35     weaken $handles[-1];
36     return $dbh;
37 };
38
39 END {
40     if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
41         foreach my $dbh (@handles) {
42             $dbh->disconnect if $dbh;
43         }
44
45         rmtree($tdir, 1, 1)
46     }
47 }
48
49 1;