From: rkinyon Date: Wed, 1 Mar 2006 21:36:35 +0000 (+0000) Subject: Converted all tests to use File::Temp instead of t/test.db X-Git-Tag: 0-99_01~94 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2a81bf9ee4b16d5e7e4f1e267b74cd0b79fa13a9;p=dbsrgits%2FDBM-Deep.git Converted all tests to use File::Temp instead of t/test.db --- diff --git a/MANIFEST b/MANIFEST index 527ac48..bb4c9c6 100644 --- a/MANIFEST +++ b/MANIFEST @@ -35,6 +35,5 @@ t/24_autobless.t t/25_tie_return_value.t t/26_scalar_ref.t t/27_filehandle.t -t/27_filehandle.t.db t/28_DATA.t t/29_freespace_manager.t diff --git a/t/01_basic.t b/t/01_basic.t index 8d3bff0..b28e389 100644 --- a/t/01_basic.t +++ b/t/01_basic.t @@ -3,6 +3,7 @@ ## use strict; use Test::More tests => 3; +use File::Temp qw( tempfile tempdir ); diag "Testing DBM::Deep against Perl $] located at $^X"; @@ -11,8 +12,9 @@ use_ok( 'DBM::Deep' ); ## # basic file open ## -unlink "t/test.db"; -my $db = eval { DBM::Deep->new( "t/test.db" ) }; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); +my $db = eval { DBM::Deep->new( $filename ) }; if ( $@ ) { diag "ERROR: $@"; Test::More->builder->BAIL_OUT( "Opening a new file fails" ); diff --git a/t/02_hash.t b/t/02_hash.t index 6632b80..143fc95 100644 --- a/t/02_hash.t +++ b/t/02_hash.t @@ -4,11 +4,13 @@ use strict; use Test::More tests => 29; use Test::Exception; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( "t/test.db" ); +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); +my $db = DBM::Deep->new( $filename ); ## # put/get key @@ -91,7 +93,7 @@ is( $db->get("key1"), "value222222222222222222222222", "We set a value before cl # Make sure DB still works after closing / opening ## undef $db; -$db = DBM::Deep->new( "t/test.db" ); +$db = DBM::Deep->new( $filename ); is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" ); ## diff --git a/t/03_bighash.t b/t/03_bighash.t index d9092fb..03d8298 100644 --- a/t/03_bighash.t +++ b/t/03_bighash.t @@ -3,12 +3,14 @@ ## use strict; use Test::More tests => 2; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, type => DBM::Deep->TYPE_HASH ); diff --git a/t/04_array.t b/t/04_array.t index 157f1a8..f652ded 100644 --- a/t/04_array.t +++ b/t/04_array.t @@ -4,15 +4,14 @@ use strict; use Test::More tests => 107; use Test::Exception; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -## -# basic file open -## -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, type => DBM::Deep->TYPE_ARRAY ); diff --git a/t/05_bigarray.t b/t/05_bigarray.t index f2999c3..e088bac 100644 --- a/t/05_bigarray.t +++ b/t/05_bigarray.t @@ -3,12 +3,14 @@ ## use strict; use Test::More tests => 2; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, type => DBM::Deep->TYPE_ARRAY ); diff --git a/t/06_error.t b/t/06_error.t index 2385710..f36db8a 100644 --- a/t/06_error.t +++ b/t/06_error.t @@ -5,47 +5,51 @@ $|++; use strict; use Test::More tests => 6; use Test::Exception; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); + ## # test a corrupted file ## -open FH, '>t/test.db'; +open FH, ">$filename"; print FH 'DPDB'; close FH; throws_ok { - DBM::Deep->new( "t/test.db" ); + DBM::Deep->new( $filename ); } qr/DBM::Deep: Corrupted file, no master index record/, "Fail if there's no master index record"; { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my %hash; - tie %hash, 'DBM::Deep', 't/test.db'; + tie %hash, 'DBM::Deep', $filename; undef %hash; my @array; throws_ok { - tie @array, 'DBM::Deep', 't/test.db'; + tie @array, 'DBM::Deep', $filename; } qr/DBM::Deep: File type mismatch/, "Fail if we try and tie a hash file with an array"; throws_ok { - DBM::Deep->new( file => 't/test.db', type => DBM::Deep->TYPE_ARRAY ) + DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_ARRAY ) } qr/DBM::Deep: File type mismatch/, "Fail if we try and open a hash file with an array"; } { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my @array; - tie @array, 'DBM::Deep', 't/test.db'; + tie @array, 'DBM::Deep', $filename; undef @array; my %hash; throws_ok { - tie %hash, 'DBM::Deep', 't/test.db'; + tie %hash, 'DBM::Deep', $filename; } qr/DBM::Deep: File type mismatch/, "Fail if we try and tie an array file with a hash"; throws_ok { - DBM::Deep->new( file => 't/test.db', type => DBM::Deep->TYPE_HASH ) + DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_HASH ) } qr/DBM::Deep: File type mismatch/, "Fail if we try and open an array file with a hash"; } diff --git a/t/07_locking.t b/t/07_locking.t index e2d9df7..c6760ea 100644 --- a/t/07_locking.t +++ b/t/07_locking.t @@ -3,16 +3,14 @@ ## use strict; use Test::More tests => 4; -$|=1; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -## -# basic file open -## -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, locking => 1, ); diff --git a/t/08_deephash.t b/t/08_deephash.t index 6e349d8..59eb641 100644 --- a/t/08_deephash.t +++ b/t/08_deephash.t @@ -2,56 +2,60 @@ # DBM::Deep Test ## use strict; -use Test::More; - -my $max_levels = 1000; - -plan tests => 5; +use Test::More tests => 5; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( - file => "t/test.db", -); - -## -# basic deep hash -## -$db->{company} = {}; -$db->{company}->{name} = "My Co."; -$db->{company}->{employees} = {}; -$db->{company}->{employees}->{"Henry Higgins"} = {}; -$db->{company}->{employees}->{"Henry Higgins"}->{salary} = 90000; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); -is( $db->{company}->{name}, "My Co.", "Set and retrieved a second-level value" ); -is( $db->{company}->{employees}->{"Henry Higgins"}->{salary}, 90000, "Set and retrieved a fourth-level value" ); - -## -# super deep hash -## -$db->{base_level} = {}; -my $temp_db = $db->{base_level}; +my $max_levels = 1000; -for my $k ( 0 .. $max_levels ) { - $temp_db->{"level$k"} = {}; - $temp_db = $temp_db->{"level$k"}; +{ + my $db = DBM::Deep->new( + file => $filename, + type => DBM::Deep->TYPE_HASH, + ); + + ## + # basic deep hash + ## + $db->{company} = {}; + $db->{company}->{name} = "My Co."; + $db->{company}->{employees} = {}; + $db->{company}->{employees}->{"Henry Higgins"} = {}; + $db->{company}->{employees}->{"Henry Higgins"}->{salary} = 90000; + + is( $db->{company}->{name}, "My Co.", "Set and retrieved a second-level value" ); + is( $db->{company}->{employees}->{"Henry Higgins"}->{salary}, 90000, "Set and retrieved a fourth-level value" ); + + ## + # super deep hash + ## + $db->{base_level} = {}; + my $temp_db = $db->{base_level}; + + for my $k ( 0 .. $max_levels ) { + $temp_db->{"level$k"} = {}; + $temp_db = $temp_db->{"level$k"}; + } + $temp_db->{deepkey} = "deepvalue"; } -$temp_db->{deepkey} = "deepvalue"; -undef $temp_db; - -undef $db; -$db = DBM::Deep->new( - file => "t/test.db", - type => DBM::Deep->TYPE_HASH, -); - -my $cur_level = -1; -$temp_db = $db->{base_level}; -for my $k ( 0 .. $max_levels ) { - $cur_level = $k; - $temp_db = $temp_db->{"level$k"}; - eval { $temp_db->isa( 'DBM::Deep' ) } or last; + +{ + my $db = DBM::Deep->new( + file => $filename, + type => DBM::Deep->TYPE_HASH, + ); + + my $cur_level = -1; + my $temp_db = $db->{base_level}; + for my $k ( 0 .. $max_levels ) { + $cur_level = $k; + $temp_db = $temp_db->{"level$k"}; + eval { $temp_db->isa( 'DBM::Deep' ) } or last; + } + is( $cur_level, $max_levels, "We read all the way down to level $cur_level" ); + is( $temp_db->{deepkey}, "deepvalue", "And we retrieved the value at the bottom of the ocean" ); } -is( $cur_level, $max_levels, "We read all the way down to level $cur_level" ); -is( $temp_db->{deepkey}, "deepvalue", "And we retrieved the value at the bottom of the ocean" ); diff --git a/t/09_deeparray.t b/t/09_deeparray.t index 7b8dab8..d9ba676 100644 --- a/t/09_deeparray.t +++ b/t/09_deeparray.t @@ -1,43 +1,45 @@ ## # DBM::Deep Test ## -$|++; use strict; -use Test::More; +use Test::More tests => 3; +use File::Temp qw( tempfile tempdir ); -my $max_levels = 1000; +use_ok( 'DBM::Deep' ); -plan tests => 3; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); -use_ok( 'DBM::Deep' ); +my $max_levels = 1000; -unlink "t/test.db"; -my $db = DBM::Deep->new( - file => "t/test.db", - type => DBM::Deep->TYPE_ARRAY, -); +{ + my $db = DBM::Deep->new( + file => $filename, + type => DBM::Deep->TYPE_ARRAY, + ); -$db->[0] = []; -my $temp_db = $db->[0]; -for my $k ( 0 .. $max_levels ) { - $temp_db->[$k] = []; - $temp_db = $temp_db->[$k]; + $db->[0] = []; + my $temp_db = $db->[0]; + for my $k ( 0 .. $max_levels ) { + $temp_db->[$k] = []; + $temp_db = $temp_db->[$k]; + } + $temp_db->[0] = "deepvalue"; } -$temp_db->[0] = "deepvalue"; -undef $temp_db; -undef $db; -$db = DBM::Deep->new( - file => "t/test.db", - type => DBM::Deep->TYPE_ARRAY, -); +{ + my $db = DBM::Deep->new( + file => $filename, + type => DBM::Deep->TYPE_ARRAY, + ); -my $cur_level = -1; -$temp_db = $db->[0]; -for my $k ( 0 .. $max_levels ) { - $cur_level = $k; - $temp_db = $temp_db->[$k]; - eval { $temp_db->isa( 'DBM::Deep' ) } or last; + my $cur_level = -1; + my $temp_db = $db->[0]; + for my $k ( 0 .. $max_levels ) { + $cur_level = $k; + $temp_db = $temp_db->[$k]; + eval { $temp_db->isa( 'DBM::Deep' ) } or last; + } + is( $cur_level, $max_levels, "We read all the way down to level $cur_level" ); + is( $temp_db->[0], "deepvalue", "And we retrieved the value at the bottom of the ocean" ); } -is( $cur_level, $max_levels, "We read all the way down to level $cur_level" ); -is( $temp_db->[0], "deepvalue", "And we retrieved the value at the bottom of the ocean" ); diff --git a/t/10_largekeys.t b/t/10_largekeys.t index 5fe52c5..a6964a7 100644 --- a/t/10_largekeys.t +++ b/t/10_largekeys.t @@ -3,12 +3,14 @@ ## use strict; use Test::More tests => 14; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db" + file => $filename, ); ## diff --git a/t/11_optimize.t b/t/11_optimize.t index cbc5910..b5876a5 100644 --- a/t/11_optimize.t +++ b/t/11_optimize.t @@ -3,12 +3,13 @@ ## use strict; use Test::More tests => 9; +use File::Temp qw( tmpnam ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; +my $filename = tmpnam(); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, autoflush => 1, ); @@ -79,7 +80,7 @@ SKIP: { # re-open db $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, autoflush => 1, locking => 1 ); @@ -95,7 +96,7 @@ SKIP: { # re-open db $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, autoflush => 1, locking => 1 ); @@ -108,7 +109,6 @@ SKIP: { # see if it was stored successfully is( $db->{parentfork}, "hello", "stored key while optimize took place" ); - # ok(1); # now check some existing values from before is( $db->{key1}, 'value1', "key1's value is still there after optimize" ); diff --git a/t/12_clone.t b/t/12_clone.t index 10f1d3c..42515dd 100644 --- a/t/12_clone.t +++ b/t/12_clone.t @@ -3,46 +3,53 @@ ## use strict; use Test::More tests => 14; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( - file => "t/test.db", -); +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); -$db->{key1} = "value1"; +{ + my $clone; -## -# clone db handle, make sure both are usable -## -my $clone = $db->clone(); + { + my $db = DBM::Deep->new( + file => $filename, + ); -is($clone->{key1}, "value1"); + $db->{key1} = "value1"; -$clone->{key2} = "value2"; -$db->{key3} = "value3"; + ## + # clone db handle, make sure both are usable + ## + $clone = $db->clone(); -is($db->{key1}, "value1"); -is($db->{key2}, "value2"); -is($db->{key3}, "value3"); + is($clone->{key1}, "value1"); -is($clone->{key1}, "value1"); -is($clone->{key2}, "value2"); -is($clone->{key3}, "value3"); + $clone->{key2} = "value2"; + $db->{key3} = "value3"; -undef $db; + is($db->{key1}, "value1"); + is($db->{key2}, "value2"); + is($db->{key3}, "value3"); -is($clone->{key1}, "value1"); -is($clone->{key2}, "value2"); -is($clone->{key3}, "value3"); + is($clone->{key1}, "value1"); + is($clone->{key2}, "value2"); + is($clone->{key3}, "value3"); + } -undef $clone; + is($clone->{key1}, "value1"); + is($clone->{key2}, "value2"); + is($clone->{key3}, "value3"); +} -$db = DBM::Deep->new( - file => "t/test.db", -); +{ + my $db = DBM::Deep->new( + file => $filename, + ); -is($db->{key1}, "value1"); -is($db->{key2}, "value2"); -is($db->{key3}, "value3"); + is($db->{key1}, "value1"); + is($db->{key2}, "value2"); + is($db->{key3}, "value3"); +} diff --git a/t/13_setpack.t b/t/13_setpack.t index 1197388..b0650ab 100644 --- a/t/13_setpack.t +++ b/t/13_setpack.t @@ -3,15 +3,18 @@ ## use strict; use Test::More tests => 2; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); +my $dir = tempdir( CLEANUP => 1 ); + my ($before, $after); { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, autoflush => 1 ); $db->{key1} = "value1"; @@ -20,9 +23,9 @@ my ($before, $after); } { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, autoflush => 1 ); diff --git a/t/14_filter.t b/t/14_filter.t index f6af52f..afb556c 100644 --- a/t/14_filter.t +++ b/t/14_filter.t @@ -3,12 +3,14 @@ ## use strict; use Test::More tests => 17; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, ); ok( !$db->set_filter( 'floober', sub {} ), "floober isn't a value filter key" ); diff --git a/t/15_digest.t b/t/15_digest.t index 5bc2333..ee2853c 100644 --- a/t/15_digest.t +++ b/t/15_digest.t @@ -2,18 +2,18 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 13; +use Test::More tests => 14; +use File::Temp qw( tempfile tempdir ); -use DBM::Deep; +use_ok( 'DBM::Deep' ); + +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $salt = 38473827; -## -# basic file open -## -unlink "t/test.db"; my $db = new DBM::Deep( - file => "t/test.db" + file => $filename, ); ## diff --git a/t/16_circular.t b/t/16_circular.t index 24594dc..0456a38 100644 --- a/t/16_circular.t +++ b/t/16_circular.t @@ -3,11 +3,13 @@ ## use strict; use Test::More tests => 13; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( "t/test.db" ); +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); +my $db = DBM::Deep->new( $filename ); ## # put/get simple keys diff --git a/t/17_import.t b/t/17_import.t index 34fd75a..e39dd08 100644 --- a/t/17_import.t +++ b/t/17_import.t @@ -3,11 +3,13 @@ ## use strict; use Test::More tests => 2; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( "t/test.db" ); +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); +my $db = DBM::Deep->new( $filename ); ## # Create structure in memory diff --git a/t/18_export.t b/t/18_export.t index c76a747..77d9e4f 100644 --- a/t/18_export.t +++ b/t/18_export.t @@ -3,35 +3,35 @@ ## use strict; use Test::More tests => 2; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( "t/test.db" ); +my $dir = tempdir( CLEANUP => 1 ); -## -# Create structure in DB -## -$db->import( - key1 => "value1", - key2 => "value2", - array1 => [ "elem0", "elem1", "elem2", { foo => 'bar' }, [ 5 ] ], - hash1 => { - subkey1 => "subvalue1", - subkey2 => "subvalue2", - } -); +my $struct; +{ + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); + my $db = DBM::Deep->new( $filename ); -## -# Export entire thing -## -my $struct = $db->export(); + ## + # Create structure in DB + ## + $db->import( + key1 => "value1", + key2 => "value2", + array1 => [ "elem0", "elem1", "elem2", { foo => 'bar' }, [ 5 ] ], + hash1 => { + subkey1 => "subvalue1", + subkey2 => "subvalue2", + } + ); -## -# close, delete file -## -undef $db; -unlink "t/test.db"; + ## + # Export entire thing + ## + $struct = $db->export(); +} ## # Make sure everything is here, outside DB diff --git a/t/20_tie.t b/t/20_tie.t index 7fff003..4ad4cb9 100644 --- a/t/20_tie.t +++ b/t/20_tie.t @@ -4,34 +4,37 @@ use strict; use Test::More tests => 11; use Test::Exception; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); +my $dir = tempdir( CLEANUP => 1 ); + ## # testing the various modes of opening a file ## { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my %hash; - my $db = tie %hash, 'DBM::Deep', 't/test.db'; + my $db = tie %hash, 'DBM::Deep', $filename; ok(1, "Tied an hash with an array for params" ); } { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my %hash; my $db = tie %hash, 'DBM::Deep', { - file => 't/test.db', + file => $filename, }; ok(1, "Tied a hash with a hashref for params" ); } { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my @array; - my $db = tie @array, 'DBM::Deep', 't/test.db'; + my $db = tie @array, 'DBM::Deep', $filename; ok(1, "Tied an array with an array for params" ); @@ -39,10 +42,10 @@ use_ok( 'DBM::Deep' ); } { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my @array; my $db = tie @array, 'DBM::Deep', { - file => 't/test.db', + file => $filename, }; ok(1, "Tied an array with a hashref for params" ); @@ -50,22 +53,22 @@ use_ok( 'DBM::Deep' ); is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" ); } -unlink "t/test.db"; +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); throws_ok { - tie my %hash, 'DBM::Deep', [ file => 't/test.db' ]; + tie my %hash, 'DBM::Deep', [ file => $filename ]; } qr/Not a hashref/, "Passing an arrayref to TIEHASH fails"; unlink "t/test.db"; throws_ok { - tie my @array, 'DBM::Deep', [ file => 't/test.db' ]; + tie my @array, 'DBM::Deep', [ file => $filename ]; } qr/Not a hashref/, "Passing an arrayref to TIEARRAY fails"; unlink "t/test.db"; throws_ok { - tie my %hash, 'DBM::Deep', undef, file => 't/test.db'; + tie my %hash, 'DBM::Deep', undef, file => $filename; } qr/Odd number of parameters/, "Odd number of params to TIEHASH fails"; unlink "t/test.db"; throws_ok { - tie my @array, 'DBM::Deep', undef, file => 't/test.db'; + tie my @array, 'DBM::Deep', undef, file => $filename; } qr/Odd number of parameters/, "Odd number of params to TIEARRAY fails"; diff --git a/t/21_tie_access.t b/t/21_tie_access.t index f8eae18..e8cce37 100644 --- a/t/21_tie_access.t +++ b/t/21_tie_access.t @@ -2,20 +2,18 @@ # DBM::Deep Test ## use strict; -use Test::More; +use Test::More tests => 7; use Test::Exception; - -plan tests => 7; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -# How should one test for creation failure with the tie mechanism? - -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); { my %hash; - tie %hash, 'DBM::Deep', "t/test.db"; + tie %hash, 'DBM::Deep', $filename; $hash{key1} = 'value'; is( $hash{key1}, 'value', 'Set and retrieved key1' ); @@ -23,7 +21,7 @@ unlink "t/test.db"; { my %hash; - tie %hash, 'DBM::Deep', "t/test.db"; + tie %hash, 'DBM::Deep', $filename; is( $hash{key1}, 'value', 'Set and retrieved key1' ); @@ -33,17 +31,19 @@ unlink "t/test.db"; throws_ok { tie my @array, 'DBM::Deep', { - file => 't/test.db', + file => $filename, type => DBM::Deep->TYPE_ARRAY, }; } qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type"; -unlink "t/test2.db"; -DBM::Deep->new( file => 't/test2.db', type => DBM::Deep->TYPE_ARRAY ); - -throws_ok { - tie my %hash, 'DBM::Deep', { - file => 't/test2.db', - type => DBM::Deep->TYPE_HASH, - }; -} qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type"; +{ + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); + DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_ARRAY ); + + throws_ok { + tie my %hash, 'DBM::Deep', { + file => $filename, + type => DBM::Deep->TYPE_HASH, + }; + } qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type"; +} diff --git a/t/23_misc.t b/t/23_misc.t index 59e4aca..e25ef57 100644 --- a/t/23_misc.t +++ b/t/23_misc.t @@ -2,15 +2,15 @@ # DBM::Deep Test ## use strict; -use Test::More; +use Test::More tests => 7; use Test::Exception; - -plan tests => 7; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( "t/test.db" ); +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); +my $db = DBM::Deep->new( $filename ); $db->{key1} = "value1"; is( $db->{key1}, "value1", "Value set correctly" ); @@ -30,7 +30,7 @@ throws_ok { { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, locking => 1, ); $db->_get_self->{engine}->close_fh( $db->_get_self ); @@ -39,7 +39,7 @@ throws_ok { { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, locking => 1, ); $db->lock; diff --git a/t/24_autobless.t b/t/24_autobless.t index f2d4e2b..6f1aeb6 100644 --- a/t/24_autobless.t +++ b/t/24_autobless.t @@ -8,13 +8,15 @@ use strict; } use Test::More tests => 54; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink 't/test.db'; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); { my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, autobless => 1, ); @@ -41,7 +43,7 @@ unlink 't/test.db'; { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, autobless => 1, ); @@ -74,7 +76,7 @@ unlink 't/test.db'; { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, autobless => 1, ); is( $db->{blessed}{c}, 'new' ); @@ -107,7 +109,7 @@ unlink 't/test.db'; { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, ); my $obj = $db->{blessed}; @@ -134,10 +136,10 @@ unlink 't/test.db'; is( $db->{unblessed}{b}[2], 3 ); } +my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); { - unlink 't/test2.db'; my $db = DBM::Deep->new( - file => "t/test2.db", + file => $filename2, autobless => 1, ); my $obj = bless { @@ -146,11 +148,11 @@ unlink 't/test.db'; }, 'Foo'; $db->import( { blessed => $obj } ); +} - undef $db; - - $db = DBM::Deep->new( - file => "t/test2.db", +{ + my $db = DBM::Deep->new( + file => $filename2, autobless => 1, ); @@ -165,9 +167,9 @@ unlink 't/test.db'; # longer named class (FooFoo) and replacing key in db file, then validating # content after that point in file to check for corruption. ## - unlink 't/test3.db'; + my ($fh3, $filename3) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my $db = DBM::Deep->new( - file => "t/test3.db", + file => $filename3, autobless => 1, ); @@ -182,4 +184,3 @@ unlink 't/test.db'; is( $db->{after}, "hello" ); } - diff --git a/t/25_tie_return_value.t b/t/25_tie_return_value.t index 9e76b7b..4e3c676 100644 --- a/t/25_tie_return_value.t +++ b/t/25_tie_return_value.t @@ -1,25 +1,28 @@ use strict; use Test::More tests => 5; - -use Scalar::Util qw( reftype ); +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); +my $dir = tempdir( CLEANUP => 1 ); + +use Scalar::Util qw( reftype ); + { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my %hash; - my $obj = tie %hash, 'DBM::Deep', 't/test.db'; + my $obj = tie %hash, 'DBM::Deep', $filename; isa_ok( $obj, 'DBM::Deep' ); is( reftype( $obj ), 'HASH', "... and its underlying representation is an HASH" ); } { - unlink "t/test.db"; + my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); my @array; - my $obj = tie @array, 'DBM::Deep', 't/test.db'; + my $obj = tie @array, 'DBM::Deep', $filename; isa_ok( $obj, 'DBM::Deep' ); is( reftype( $obj ), 'HASH', "... and its underlying representation is an HASH" ); } diff --git a/t/26_scalar_ref.t b/t/26_scalar_ref.t index 4ba6bea..405c6fe 100644 --- a/t/26_scalar_ref.t +++ b/t/26_scalar_ref.t @@ -1,12 +1,15 @@ use strict; use Test::More tests => 7; +use File::Temp qw( tempfile tempdir ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); + { - my $db = DBM::Deep->new( "t/test.db" ); + my $db = DBM::Deep->new( $filename ); my $x = 25; my $y = 30; @@ -23,7 +26,7 @@ unlink "t/test.db"; } { - my $db = DBM::Deep->new( "t/test.db" ); + my $db = DBM::Deep->new( $filename ); my $x = 25; my $y = 30; diff --git a/t/27_filehandle.t b/t/27_filehandle.t index 762a05c..5902879 100644 --- a/t/27_filehandle.t +++ b/t/27_filehandle.t @@ -2,13 +2,23 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 10; +use Test::More tests => 11; use Test::Exception; +use File::Temp qw( tempfile tempdir ); -use DBM::Deep; +use_ok( 'DBM::Deep' ); + +my $dir = tempdir( CLEANUP => 1 ); +my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); + +# Create the datafile to be used +{ + my $db = DBM::Deep->new( $filename ); + $db->{hash} = { foo => [ 'a' .. 'c' ] }; +} { - open(FILE, "t/27_filehandle.t.db") || die("Can't open t/27_filehandle.t.db\n"); + open(FILE, $filename) || die("Can't open '$filename' for reading: $!\n"); my $db; diff --git a/t/27_filehandle.t.db b/t/27_filehandle.t.db deleted file mode 100644 index 46f58c7..0000000 Binary files a/t/27_filehandle.t.db and /dev/null differ diff --git a/t/28_DATA.t b/t/28_DATA.t index a80d780..4caa75c 100644 Binary files a/t/28_DATA.t and b/t/28_DATA.t differ