Add author test for whitespace errors and make whitespace more consistent
[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 File::Temp 'tempdir';
7 use Scalar::Util 'weaken';
8 use namespace::clean;
9 use DBI ();
10
11 use base qw/Exporter/;
12 our @EXPORT_OK = '$tdir';
13
14 die "/t does not exist, this can't be right...\n"
15     unless -d 't';
16
17 my $tbdir = 't/var';
18
19 unless (-d $tbdir) {
20     mkdir $tbdir or die "Unable to create $tbdir: $!\n";
21 }
22
23 our $tdir = tempdir(DIR => $tbdir);
24
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
48         rmtree($tdir, 1, 1);
49         rmdir($tbdir); # remove if empty, ignore otherwise
50     }
51 }
52
53 1;