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