better cleanup of t/var
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / backcompat / 0.04006 / lib / dbixcsl_test_dir.pm
CommitLineData
c213fd3d 1package dbixcsl_test_dir;
2
c213fd3d 3use strict;
9643e2f1 4use warnings;
5use File::Path 'rmtree';
6use Scalar::Util 'weaken';
7use namespace::clean;
8use DBI ();
c213fd3d 9
10our $tdir = 't/var';
11
12use base qw/Exporter/;
13our @EXPORT_OK = '$tdir';
14
15die "/t does not exist, this can't be right...\n"
16 unless -d 't';
17
18unless (-d $tdir) {
19 mkdir $tdir or die "Unable to create $tdir: $!\n";
20}
21
9643e2f1 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
26no warnings 'redefine';
27
28my $connect = \&DBI::connect;
29
30my @handles;
31
32*DBI::connect = sub {
33 my $dbh = $connect->(@_);
34 push @handles, $dbh;
35 weaken $handles[-1];
36 return $dbh;
37};
38
39END {
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
c213fd3d 491;