--- t/fs.t 2007/07/08 21:03:26 10 +++ t/fs.t 2007/07/09 22:44:20 25 @@ -4,8 +4,9 @@ my $debug = shift @ARGV; -use Test::More tests => 135; +use Test::More tests => 744; use File::Slurp; +use IO::File; my ( $from, $to, $tmp ) = ( '/tmp/comp', '/tmp/no-comp', '/dev/shm/comp' ); @@ -13,27 +14,77 @@ 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" if $debug; + 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>; - ok( $content, 'has content' ); + if ( defined( $content ) ) { + cmp_ok( $content, 'eq', $orig_content, "content " . length($content) . " bytes" ); + } else { + 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" ); - ok( -e "$from/$path" || -e "$from/${path}.gz", 'on disk' ); + my $pack = "$from/${path}.gz"; + my $size = length($content); - ok( -e "$tmp/$path" , 'in tmp' ) 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 $pack, '==', $size, "$tmp/$path = $size bytes" ); + } + } else { + ok( -e "$from/$path", "on disk $from/$path" ); + diag "$op curr_size: $orig_size size: $size"; + $size += $orig_size if ( $op eq '>>' ); + cmp_ok( -s "$from/$path", '==', $size, "$from/$path = $size bytes" ); + } + + dump_debug('at end'); return $content; } +sub md5sum { + my $path = shift; + my $md5sum = `md5sum $path`; + $md5sum =~ s/\s+.*$//s; + warn "## md5sum($path) = $md5sum\n" if $debug; + return $md5sum; +} + my $buff = '<<--just a chunk of data-->>'; for my $i ( 1 .. 3 ) { @@ -43,15 +94,89 @@ my $file = "test.$i"; file( '>', $file, $content ); - cmp_ok( file( '<', $file ), 'eq', $content, "$file content" ); + file( '<', $file, $content ); file( '>>', $file, '+append' ); - cmp_ok( file( '<', $file ), 'eq', $content . '+append', "$file append" ); + file( '<', $file, $content . '+append' ); file( '>', $file, '' ); - cmp_ok( file( '<', $file ), 'eq', '', "$file empty" ); + file( '<', $file, '' ); file( '>', $file, $content ); - cmp_ok( file( '<', $file ), 'eq', $content, "$file content" ); + file( '<', $file, $content ); } + + +sub multiple_rw { + + 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' ); + dump_debug 'own twice'; + ok( print($fh1 "x" x 65535), 'print 1 64k' ); + ok( close($fh1), 'close 1' ); + dump_debug 'own once'; + ok( close($fh2), 'close 2' ); + dump_debug 'closed'; + + my @sizes; + my $size = 65536; + while ( $size > 1 ) { + push @sizes, $size; + $size /= 2; + } + + foreach my $size ( @sizes ) { + ok( my $fh1 = IO::File->new("> $to/m"), 'open 1' ); + ok( truncate( $fh1, $size ), 'truncate' ); + dump_debug 'truncate'; + ok( close($fh1), 'close 1' ); + cmp_ok( -s "$to/m", '==', $size, "truncated to $size" ); + } + + + foreach my $size ( sort @sizes ) { + my $orig_size = -s "$to/m"; + ok( my $fh1 = IO::File->new(">> $to/m"), 'open 1' ); + ok( print($fh1 "x" x $size), "print $size bytes" ); + dump_debug 'append'; + ok( close($fh1), 'close 1' ); + my $expected_size = $size + $orig_size; + cmp_ok( -s "$to/m", '==', $expected_size, "appended upto $expected_size" ); + } + + ok( $size = -s "$to/m", 'size' ); + ok( my $md5sum = md5sum("$to/m"), 'md5sum m' ); + ok( rename("$to/m", "$to/n"), 'rename' ); + ok( -e "$to/n", "$to/n exists" ); + ok( ! -e "$to/m", "$to/m gone" ); + cmp_ok( -s "$to/n", '==', $size, 'size' ); + cmp_ok( $md5sum, 'eq', md5sum("$to/n"), 'md5sums same' ); + + ok( unlink("$to/n"), "$to/n unlink" ); + ok( ! -e "$to/n", "$to/n gone" ); + + { + ok( my $fh1 = IO::File->new(">> $to/h"), "open $to/h" ); + ok( print($fh1 "foobar"), "print" ); + ok( unlink("$to/h"), 'create .fuse_hidden' ); + ok( ! -e "$to/h", "$to/h gone" ); + dump_debug 'hidden'; + ok( print($fh1 "foobar"), "print to hidden" ); + ok( close($fh1), "close $to/h" ); + diag $!; + } +} + +multiple_rw; +multiple_rw; + +