Commit | Line | Data |
c213fd3d |
1 | package dbixcsl_test_dir; |
2 | |
c213fd3d |
3 | use strict; |
9643e2f1 |
4 | use warnings; |
5 | use File::Path 'rmtree'; |
48d580c8 |
6 | use File::Temp 'tempdir'; |
9643e2f1 |
7 | use Scalar::Util 'weaken'; |
8 | use namespace::clean; |
9 | use DBI (); |
c213fd3d |
10 | |
c213fd3d |
11 | use base qw/Exporter/; |
12 | our @EXPORT_OK = '$tdir'; |
13 | |
14 | die "/t does not exist, this can't be right...\n" |
494e0205 |
15 | unless -d 't'; |
c213fd3d |
16 | |
48d580c8 |
17 | my $tbdir = 't/var'; |
18 | |
19 | unless (-d $tbdir) { |
494e0205 |
20 | mkdir $tbdir or die "Unable to create $tbdir: $!\n"; |
c213fd3d |
21 | } |
22 | |
48d580c8 |
23 | our $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 | |
29 | no warnings 'redefine'; |
30 | |
31 | my $connect = \&DBI::connect; |
32 | |
33 | my @handles; |
34 | |
35 | *DBI::connect = sub { |
36 | my $dbh = $connect->(@_); |
37 | push @handles, $dbh; |
38 | weaken $handles[-1]; |
39 | return $dbh; |
40 | }; |
41 | |
42 | END { |
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 |
53 | 1; |