--- t/fs.t 2007/07/09 08:39:58 11 +++ t/fs.t 2007/07/09 10:35:02 12 @@ -4,8 +4,9 @@ my $debug = shift @ARGV; -use Test::More tests => 115; +use Test::More tests => 227; use File::Slurp; +use IO::File; my ( $from, $to, $tmp ) = ( '/tmp/comp', '/tmp/no-comp', '/dev/shm/comp' ); @@ -13,34 +14,64 @@ ok( -e $to, 'to' ); ok( -e $tmp, 'tmp' ); +ok( (system "touch $to/.debug") == 0, 'debug on' ); + +sub dump_debug { + my $msg = shift; + ok( open(my $d, '<', "$to/.debug"), 'open debug' ); + local $/; + my $dump = <$d>; + diag "DEBUG: $msg\n$dump\n"; + ok( close($d), 'close debug' ); +} + sub file { my ( $op, $path, $content ) = @_; + my $orig_size = -s "$to/$path"; ok( open( my $fh, $op, "$to/$path" ), "open( $op $path )"); - if ( $op =~ m/>/ ) { + if ( $op eq '>' ) { + cmp_ok( -s "$to/$path", '==', 0, "truncate $to/$path" ); print $fh $content; - } else { + } elsif ( $op eq '>>' ) { + cmp_ok( -s "$to/$path", '==', $orig_size, "no truncate $to/$path" ); + print $fh $content; + + } elsif ( $op eq '<' ) { my $orig_content = $content; local $/; $content = <$fh>; if ( defined( $content ) ) { - cmp_ok( $content, 'eq', $orig_content, 'content' ); + cmp_ok( $content, 'eq', $orig_content, "content " . length($content) . " bytes" ); } else { - ok( $content, 'has content' ); + ok( $content, "has " . length($content) . " bytes" ); } + } else { + die "unsupported op: $op"; } + dump_debug 'before close'; ok( close($fh), 'close' ); + dump_debug 'after close'; - ok ( -e "$to/$path", 'exists' ); + ok ( -e "$to/$path", "exists $to/$path" ); my $pack = "$from/${path}.gz"; + my $size = length($content); if ( -e $pack ) { - ok( -s $pack, 'on disk packed' ); - ok( -e "$tmp/$path" , 'in tmp unpacked' ) if ( $op =~ m/ 0 ); + # check uncompressed size if read + ok( -e "$tmp/$path" , "in tmp $tmp/$path" ) if ( $op eq '<' ); + # check total size if not append + if ( $op ne '>>' ) { + cmp_ok( -s "$tmp/$path", '==', $size, "$tmp/$path = $size bytes" ); + } } else { - ok( -e "$from/$path", 'on disk unpacked' ); + ok( -e "$from/$path", "on disk $from/$path" ); + cmp_ok( -s "$from/$path", '==', $size, "$from/$path = $size bytes" ); } + dump_debug('at end'); + return $content; } @@ -61,7 +92,20 @@ file( '>', $file, '' ); file( '<', $file, '' ); - file( '>', $file, $content ); - file( '<', $file, $content ); +# file( '>', $file, $content ); +# file( '<', $file, $content ); } + +diag "multiple read-write"; + +ok( my $fh1 = IO::File->new("> $to/m"), 'open 1' ); +$fh1->autoflush; +ok( print($fh1 "1.1\n"), 'print 1.1' ); +ok( my $fh2 = IO::File->new(">> $to/m"), 'open 2' ); +$fh2->autoflush; +ok( print($fh2 "2.1\n"), 'print 2.1' ); +cmp_ok( read_file("$to/m"), 'eq', "1.1\n2.1\n", 'mixed' ); +ok( print($fh1 "1.2\n"), 'print 1.2' ); +cmp_ok( read_file("$to/m"), 'eq', "1.1\n1.2\n", 'just 1' ); +