Commit | Line | Data |
c213fd3d |
1 | package dbixcsl_test_dir; |
2 | |
c213fd3d |
3 | use strict; |
9643e2f1 |
4 | use warnings; |
5 | use File::Path 'rmtree'; |
6 | use Scalar::Util 'weaken'; |
7 | use namespace::clean; |
8 | use DBI (); |
c213fd3d |
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 | |
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 | |
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 | |
c213fd3d |
49 | 1; |