4 |
|
|
5 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
6 |
|
|
7 |
use Test::More tests => 135; |
use Test::More tests => 250; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
|
use IO::File; |
10 |
|
|
11 |
my ( $from, $to, $tmp ) = ( '/tmp/comp', '/tmp/no-comp', '/dev/shm/comp' ); |
my ( $from, $to, $tmp ) = ( '/tmp/comp', '/tmp/no-comp', '/dev/shm/comp' ); |
12 |
|
|
14 |
ok( -e $to, 'to' ); |
ok( -e $to, 'to' ); |
15 |
ok( -e $tmp, 'tmp' ); |
ok( -e $tmp, 'tmp' ); |
16 |
|
|
17 |
|
ok( (system "touch $to/.debug") == 0, 'debug on' ); |
18 |
|
|
19 |
|
sub dump_debug { |
20 |
|
my $msg = shift; |
21 |
|
ok( open(my $d, '<', "$to/.debug"), 'open debug' ); |
22 |
|
local $/; |
23 |
|
my $dump = <$d>; |
24 |
|
diag "DEBUG: $msg\n$dump\n" if $debug; |
25 |
|
ok( close($d), 'close debug' ); |
26 |
|
} |
27 |
|
|
28 |
sub file { |
sub file { |
29 |
my ( $op, $path, $content ) = @_; |
my ( $op, $path, $content ) = @_; |
30 |
|
my $orig_size = -s "$to/$path"; |
31 |
ok( open( my $fh, $op, "$to/$path" ), "open( $op $path )"); |
ok( open( my $fh, $op, "$to/$path" ), "open( $op $path )"); |
32 |
if ( $op =~ m/>/ ) { |
if ( $op eq '>' ) { |
33 |
|
cmp_ok( -s "$to/$path", '==', 0, "truncate $to/$path" ); |
34 |
print $fh $content; |
print $fh $content; |
35 |
} else { |
} elsif ( $op eq '>>' ) { |
36 |
|
cmp_ok( -s "$to/$path", '==', $orig_size, "no truncate $to/$path" ); |
37 |
|
print $fh $content; |
38 |
|
|
39 |
|
} elsif ( $op eq '<' ) { |
40 |
|
my $orig_content = $content; |
41 |
local $/; |
local $/; |
42 |
$content = <$fh>; |
$content = <$fh>; |
43 |
ok( $content, 'has content' ); |
if ( defined( $content ) ) { |
44 |
|
cmp_ok( $content, 'eq', $orig_content, "content " . length($content) . " bytes" ); |
45 |
|
} else { |
46 |
|
ok( $content, "has " . length($content) . " bytes" ); |
47 |
|
} |
48 |
|
} else { |
49 |
|
die "unsupported op: $op"; |
50 |
} |
} |
51 |
|
dump_debug 'before close'; |
52 |
ok( close($fh), 'close' ); |
ok( close($fh), 'close' ); |
53 |
|
dump_debug 'after close'; |
54 |
|
|
55 |
|
ok ( -e "$to/$path", "exists $to/$path" ); |
56 |
|
|
57 |
ok ( -e "$to/$path", 'exists' ); |
my $pack = "$from/${path}.gz"; |
58 |
|
my $size = length($content); |
59 |
|
|
60 |
ok( -e "$from/$path" || -e "$from/${path}.gz", 'on disk' ); |
if ( -e $pack ) { |
61 |
|
ok( -s $pack, "on disk $pack" ) if ( $size > 0 ); |
62 |
|
# check uncompressed size if read |
63 |
|
ok( -e "$tmp/$path" , "in tmp $tmp/$path" ) if ( $op eq '<' ); |
64 |
|
# check total size if not append |
65 |
|
if ( $op ne '>>' ) { |
66 |
|
cmp_ok( -s "$tmp/$path", '==', $size, "$tmp/$path = $size bytes" ); |
67 |
|
} |
68 |
|
} else { |
69 |
|
ok( -e "$from/$path", "on disk $from/$path" ); |
70 |
|
cmp_ok( -s "$from/$path", '==', $size, "$from/$path = $size bytes" ); |
71 |
|
} |
72 |
|
|
73 |
ok( -e "$tmp/$path" , 'in tmp' ) if ( $op =~ m/</ ); |
dump_debug('at end'); |
74 |
|
|
75 |
return $content; |
return $content; |
76 |
} |
} |
84 |
my $file = "test.$i"; |
my $file = "test.$i"; |
85 |
|
|
86 |
file( '>', $file, $content ); |
file( '>', $file, $content ); |
87 |
cmp_ok( file( '<', $file ), 'eq', $content, "$file content" ); |
file( '<', $file, $content ); |
88 |
|
|
89 |
file( '>>', $file, '+append' ); |
file( '>>', $file, '+append' ); |
90 |
cmp_ok( file( '<', $file ), 'eq', $content . '+append', "$file append" ); |
file( '<', $file, $content . '+append' ); |
91 |
|
|
92 |
file( '>', $file, '' ); |
file( '>', $file, '' ); |
93 |
cmp_ok( file( '<', $file ), 'eq', '', "$file empty" ); |
file( '<', $file, '' ); |
94 |
|
|
95 |
file( '>', $file, $content ); |
# file( '>', $file, $content ); |
96 |
cmp_ok( file( '<', $file ), 'eq', $content, "$file content" ); |
# file( '<', $file, $content ); |
97 |
|
|
98 |
} |
} |
99 |
|
|
100 |
|
|
101 |
|
sub multiple_rw { |
102 |
|
|
103 |
|
diag "multiple read-write"; |
104 |
|
ok( my $fh1 = IO::File->new("> $to/m"), 'open 1' ); |
105 |
|
$fh1->autoflush; |
106 |
|
ok( print($fh1 "1.1\n"), 'print 1.1' ); |
107 |
|
ok( my $fh2 = IO::File->new(">> $to/m"), 'open 2' ); |
108 |
|
$fh2->autoflush; |
109 |
|
ok( print($fh2 "2.1\n"), 'print 2.1' ); |
110 |
|
cmp_ok( read_file("$to/m"), 'eq', "1.1\n2.1\n", 'mixed' ); |
111 |
|
ok( print($fh1 "1.2\n"), 'print 1.2' ); |
112 |
|
cmp_ok( read_file("$to/m"), 'eq', "1.1\n1.2\n", 'just 1' ); |
113 |
|
dump_debug 'own twice'; |
114 |
|
ok( print($fh1 "x" x 65535), 'print 1 64k' ); |
115 |
|
ok( close($fh1), 'close 1' ); |
116 |
|
dump_debug 'own once'; |
117 |
|
ok( close($fh2), 'close 2' ); |
118 |
|
dump_debug 'closed'; |
119 |
|
|
120 |
|
} |
121 |
|
|
122 |
|
multiple_rw; |
123 |
|
multiple_rw; |
124 |
|
|
125 |
|
|