Merged with master and am ready to merge back
[dbsrgits/DBM-Deep.git] / t / common.pm
index 0103885..eda627c 100644 (file)
@@ -15,7 +15,7 @@ use File::Temp qw( tempfile tempdir );
 use Fcntl qw( :flock );
 
 my $parent = $ENV{WORK_DIR} || File::Spec->tmpdir;
-my $dir = tempdir( CLEANUP => 1, DIR => $parent );
+our $dir = tempdir( CLEANUP => 1, DIR => $parent );
 
 sub new_fh {
     my ($fh, $filename) = tempfile( 'tmpXXXX', DIR => $dir, UNLINK => 1 );
@@ -30,24 +30,48 @@ sub new_dbm {
     my @args = @_;
     my ($fh, $filename) = new_fh();
 
-    my @reset_funcs;
-    my @extra_args;
+    my (@names, @reset_funcs, @extra_args);
 
     unless ( $ENV{NO_TEST_FILE} ) {
+        push @names, 'File';
         push @reset_funcs, undef;
-        push @extra_args, (
-            [ file => $filename ],
-        );
+        push @extra_args, [
+            file => $filename,
+        ];
     }
 
-#    eval { require DBD::SQLite; };
-#    unless ( $@ ) {
-#        push @extra_args, [
-#        ];
-#    }
+    if ( $ENV{TEST_SQLITE} ) {
+        (undef, my $filename) = new_fh();
+        push @names, 'SQLite';
+        push @reset_funcs, sub {
+            require 'DBI.pm';
+            my $dbh = DBI->connect(
+                "dbi:SQLite:dbname=$filename", '', '',
+            );
+            my $sql = do {
+                my $filename = 'etc/sqlite_tables.sql';
+                open my $fh, '<', $filename
+                    or die "Cannot open '$filename' for reading: $!\n";
+                local $/;
+                <$fh>
+            };
+            foreach my $line ( split ';', $sql ) {
+                $dbh->do( "$line" ) if $line =~ /\S/;
+            }
+        };
+        push @extra_args, [
+            dbi => {
+                dsn      => "dbi:SQLite:dbname=$filename",
+                user     => '',
+                password => '',
+            },
+        ];
+    }
 
     if ( $ENV{TEST_MYSQL_DSN} ) {
+        push @names, 'MySQL';
         push @reset_funcs, sub {
+            require 'DBI.pm';
             my $dbh = DBI->connect(
                 $ENV{TEST_MYSQL_DSN},
                 $ENV{TEST_MYSQL_USER},
@@ -79,10 +103,9 @@ sub new_dbm {
         if ( my $reset = shift @reset_funcs ) {
             $reset->();
         }
+        Test::More::diag( "Testing '@{[shift @names]}'\n" ) if $ENV{TEST_VERBOSE};
         return sub {
-            DBM::Deep->new(
-                @these_args, @args, @_,
-            );
+            DBM::Deep->new( @these_args, @args, @_ )
         };
     };
 }