Begin the Changes for 1.0016
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
CommitLineData
09dd8113 1use 5.006_000;
2
714618f0 3use strict;
09dd8113 4use warnings FATAL => 'all';
5
3e9498a1 6use Test::More tests => 14;
acd4faf2 7use Test::Exception;
fde3db1a 8use t::common qw( new_fh );
714618f0 9
2a81bf9e 10use_ok( 'DBM::Deep' );
11
2a81bf9e 12{
3e9498a1 13 my ($fh, $filename) = new_fh();
714618f0 14
3e9498a1 15 # Create the datafile to be used
16 {
17 my $db = DBM::Deep->new( $filename );
18 $db->{hash} = { foo => [ 'a' .. 'c' ] };
19 }
714618f0 20
3e9498a1 21 {
22 open(my $fh, '<', $filename) || die("Can't open '$filename' for reading: $!\n");
714618f0 23
3e9498a1 24 # test if we can open and read a db using its filehandle
714618f0 25
888453b9 26 my $db;
27 ok( ($db = DBM::Deep->new( fh => $fh )), "open db in filehandle" );
28 ok( $db->{hash}{foo}[1] eq 'b', "and get at stuff in the database" );
3e9498a1 29 throws_ok {
30 $db->{foo} = 1;
888453b9 31 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
3e9498a1 32 ok( !$db->exists( 'foo' ), "foo doesn't exist" );
70b55428 33
6e6789b0 34 SKIP: {
35 skip( "No inode tests on Win32", 1 )
36 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' );
37 my $db_obj = $db->_get_self;
f1879fdc 38 ok( $db_obj->_engine->storage->{inode}, "The inode has been set" );
6e6789b0 39 }
3e9498a1 40
41 close($fh);
42 }
70b55428 43}
714618f0 44
45# now the same, but with an offset into the file. Use the database that's
46# embedded in the test for the DATA filehandle. First, find the database ...
3e9498a1 47{
48 my ($fh,$filename) = new_fh();
70b55428 49
3e9498a1 50 print $fh "#!$^X\n";
51 print $fh <<'__END_FH__';
52use strict;
40df5605 53use Test::More 'no_plan';
3e9498a1 54Test::More->builder->no_ending(1);
55Test::More->builder->{Curr_Test} = 12;
70b55428 56
3e9498a1 57use_ok( 'DBM::Deep' );
70b55428 58
3e9498a1 59my $db = DBM::Deep->new({
60 fh => *DATA,
61});
62is($db->{x}, 'b', "and get at stuff in the database");
63__END_FH__
345e7fd0 64
65 # The exec below prevents END blocks from doing this.
66 (my $esc_dir = $t::common::dir) =~ s/(.)/sprintf "\\x{%x}", ord $1/egg;
67 print $fh <<__END_FH_AGAIN__;
68use File::Path 'rmtree';
69rmtree "$esc_dir";
70__END_FH_AGAIN__
71
3e9498a1 72 print $fh "__DATA__\n";
73 close $fh;
74
75 my $offset = do {
76 open my $fh, '<', $filename;
77 while(my $line = <$fh>) {
78 last if($line =~ /^__DATA__/);
79 }
80 tell($fh);
81 };
82
83 {
84 my $db = DBM::Deep->new({
85 file => $filename,
86 file_offset => $offset,
2120a181 87#XXX For some reason, this is needed to make the test pass. Figure out why later.
88locking => 0,
3e9498a1 89 });
90
91 $db->{x} = 'b';
92 is( $db->{x}, 'b', 'and it was stored' );
93 }
94
3e9498a1 95 {
96 open my $fh, '<', $filename;
97 my $db = DBM::Deep->new({
98 fh => $fh,
99 file_offset => $offset,
100 });
101
102 is($db->{x}, 'b', "and get at stuff in the database");
103
104 ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
105 throws_ok {
106 $db->{foo} = 1;
107 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
108 ok( !$db->exists( 'foo' ), "foo still doesn't exist" );
109
9c87a079 110 is( $db->{x}, 'b' );
3e9498a1 111 }
112
019404df 113 exec( "$^X -Iblib/lib $filename" );
70b55428 114}