1 # Path.t -- tests for module File::Path
5 use Test::More tests => 98;
9 use_ok('File::Spec::Functions');
12 eval "use Test::Output";
13 my $has_Test_Output = $@ ? 0 : 1;
15 my $Is_VMS = $^O eq 'VMS';
17 # first check for stupid permissions second for full, so we clean up
19 for my $perm (0111,0777) {
20 my $path = catdir(curdir(), "mhx", "bar");
22 chmod $perm, "mhx", $path;
24 my $oct = sprintf('0%o', $perm);
25 ok(-d "mhx", "mkdir parent dir $oct");
26 ok(-d $path, "mkdir child dir $oct");
29 ok(! -e "mhx", "mhx does not exist $oct");
32 # find a place to work
33 my ($error, $list, $file, $message);
34 my $tmp_base = catdir(
36 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
41 catdir($tmp_base, qw(a b)),
42 catdir($tmp_base, qw(a c)),
43 catdir($tmp_base, qw(z b)),
44 catdir($tmp_base, qw(z c)),
48 my @created = mkpath(@dir);
50 is(scalar(@created), 7, "created list of directories");
52 # pray for no race conditions blowing them out from under us
53 @created = mkpath([$tmp_base]);
54 is(scalar(@created), 0, "skipped making existing directory")
55 or diag("unexpectedly recreated @created");
57 @created = mkpath('');
58 is(scalar(@created), 0, "Can't create a directory named ''");
64 $dir = catdir($tmp_base, 'B');
65 $dir2 = catdir($dir, updir());
66 # IOW: File::Spec->catdir( qw(foo bar), File::Spec->updir ) eq 'foo'
67 # rather than foo/bar/..
68 skip "updir() canonicalises path on this platform", 2
72 @created = mkpath($dir2, {mask => 0700});
73 is(scalar(@created), 1, "make directory with trailing parent segment");
74 is($created[0], $dir, "made parent");
77 my $count = rmtree({error => \$error});
78 is( $count, 0, 'rmtree of nothing, count of zero' );
79 is( scalar(@$error), 0, 'no diagnostic captured' );
81 @created = mkpath($tmp_base, 0);
82 is(scalar(@created), 0, "skipped making existing directories (old style 1)")
83 or diag("unexpectedly recreated @created");
85 $dir = catdir($tmp_base,'C');
86 # mkpath returns unix syntax filespecs on VMS
87 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
88 @created = mkpath($tmp_base, $dir);
89 is(scalar(@created), 1, "created directory (new style 1)");
90 is($created[0], $dir, "created directory (new style 1) cross-check");
92 @created = mkpath($tmp_base, 0, 0700);
93 is(scalar(@created), 0, "skipped making existing directories (old style 2)")
94 or diag("unexpectedly recreated @created");
96 $dir2 = catdir($tmp_base,'D');
97 # mkpath returns unix syntax filespecs on VMS
98 $dir2 = VMS::Filespec::unixify($dir2) if $Is_VMS;
99 @created = mkpath($tmp_base, $dir, $dir2);
100 is(scalar(@created), 1, "created directory (new style 2)");
101 is($created[0], $dir2, "created directory (new style 2) cross-check");
103 $count = rmtree($dir, 0);
104 is($count, 1, "removed directory unsafe mode");
106 $count = rmtree($dir2, 0, 1);
107 my $removed = $Is_VMS ? 0 : 1;
108 is($count, $removed, "removed directory safe mode");
112 # existence of E is neither here nor there
113 $dir = catdir($tmp_base, 'E', updir(), 'Y');
114 @created =mkpath($dir);
115 cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
116 cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
117 ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
119 @created = mkpath(catdir(curdir(), $tmp_base));
120 is(scalar(@created), 0, "nothing created")
123 $dir = catdir($tmp_base, 'a');
124 $dir2 = catdir($tmp_base, 'z');
134 is(scalar(@$error), 0, "no errors unlinking a and z");
135 is(scalar(@$list), 4, "list contains 4 elements")
138 ok(-d $dir, "dir a still exists");
139 ok(-d $dir2, "dir z still exists");
141 $dir = catdir($tmp_base,'F');
142 # mkpath returns unix syntax filespecs on VMS
143 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
145 @created = mkpath($dir, undef, 0770);
146 is(scalar(@created), 1, "created directory (old style 2 verbose undef)");
147 is($created[0], $dir, "created directory (old style 2 verbose undef) cross-check");
148 is(rmtree($dir, undef, 0), 1, "removed directory 2 verbose undef");
150 @created = mkpath($dir, undef);
151 is(scalar(@created), 1, "created directory (old style 2a verbose undef)");
152 is($created[0], $dir, "created directory (old style 2a verbose undef) cross-check");
153 is(rmtree($dir, undef), 1, "removed directory 2a verbose undef");
155 @created = mkpath($dir, 0, undef);
156 is(scalar(@created), 1, "created directory (old style 3 mode undef)");
157 is($created[0], $dir, "created directory (old style 3 mode undef) cross-check");
158 is(rmtree($dir, 0, undef), 1, "removed directory 3 verbose undef");
160 $dir = catdir($tmp_base,'G');
161 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
163 @created = mkpath($dir, undef, 0200);
164 is(scalar(@created), 1, "created write-only dir");
165 is($created[0], $dir, "created write-only directory cross-check");
166 is(rmtree($dir), 1, "removed write-only dir");
168 # borderline new-style heuristics
169 if (chdir $tmp_base) {
170 pass("chdir to temp dir");
173 fail("chdir to temp dir: $!");
176 $dir = catdir('a', 'd1');
177 $dir2 = catdir('a', 'd2');
179 @created = mkpath( $dir, 0, $dir2 );
180 is(scalar @created, 3, 'new-style 3 dirs created');
182 $count = rmtree( $dir, 0, $dir2, );
183 is($count, 3, 'new-style 3 dirs removed');
185 @created = mkpath( $dir, $dir2, 1 );
186 is(scalar @created, 3, 'new-style 3 dirs created (redux)');
188 $count = rmtree( $dir, $dir2, 1 );
189 is($count, 3, 'new-style 3 dirs removed (redux)');
191 @created = mkpath( $dir, $dir2 );
192 is(scalar @created, 2, 'new-style 2 dirs created');
194 $count = rmtree( $dir, $dir2 );
195 is($count, 2, 'new-style 2 dirs removed');
198 pass("chdir parent");
201 fail("chdir parent: $!");
204 # see what happens if a file exists where we want a directory
206 my $entry = catdir($tmp_base, "file");
207 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
208 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
210 ok(-e $entry, "file exists in place of directory");
212 mkpath( $entry, {error => \$error} );
213 is( scalar(@$error), 1, "caught error condition" );
214 ($file, $message) = each %{$error->[0]};
215 is( $entry, $file, "and the message is: $message");
217 eval {@created = mkpath($entry, 0, 0700)};
219 chomp $error; # just to remove silly # in TAP output
220 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
224 my $extra = catdir(curdir(), qw(EXTRA 1 a));
227 skip "extra scenarios not set up, see eg/setup-extra-tests", 14
231 $dir = catdir( 'EXTRA', '1' );
232 rmtree( $dir, {result => \$list, error => \$err} );
233 is(scalar(@$list), 2, "extra dir $dir removed");
234 is(scalar(@$err), 1, "one error encountered");
236 $dir = catdir( 'EXTRA', '3', 'N' );
237 rmtree( $dir, {result => \$list, error => \$err} );
238 is( @$list, 1, q{remove a symlinked dir} );
239 is( @$err, 0, q{with no errors} );
241 $dir = catdir('EXTRA', '3', 'S');
242 rmtree($dir, {error => \$error});
243 is( scalar(@$error), 1, 'one error for an unreadable dir' );
244 eval { ($file, $message) = each %{$error->[0]}};
245 is( $file, $dir, 'unreadable dir reported in error' )
248 $dir = catdir('EXTRA', '3', 'T');
249 rmtree($dir, {error => \$error});
250 is( scalar(@$error), 1, 'one error for an unreadable dir T' );
251 eval { ($file, $message) = each %{$error->[0]}};
252 is( $file, $dir, 'unreadable dir reported in error T' );
254 $dir = catdir( 'EXTRA', '4' );
255 rmtree($dir, {result => \$list, error => \$err} );
256 is( scalar(@$list), 0, q{don't follow a symlinked dir} );
257 is( scalar(@$err), 2, q{two errors when removing a symlink in r/o dir} );
258 eval { ($file, $message) = each %{$err->[0]} };
259 is( $file, $dir, 'symlink reported in error' );
261 $dir = catdir('EXTRA', '3', 'U');
262 $dir2 = catdir('EXTRA', '3', 'V');
263 rmtree($dir, $dir2, {verbose => 0, error => \$err, result => \$list});
264 is( scalar(@$list), 1, q{deleted 1 out of 2 directories} );
265 is( scalar(@$error), 1, q{left behind 1 out of 2 directories} );
266 eval { ($file, $message) = each %{$err->[0]} };
267 is( $file, $dir, 'first dir reported in error' );
271 $dir = catdir($tmp_base, 'ZZ');
272 @created = mkpath($dir);
273 is(scalar(@created), 1, "create a ZZ directory");
275 local @ARGV = ($dir);
276 rmtree( [grep -e $_, @ARGV], 0, 0 );
277 ok(!-e $dir, "blow it away via \@ARGV");
281 skip 'Test::Output not available', 14
282 unless $has_Test_Output;
285 $dir = catdir('EXTRA', '3');
286 skip "extra scenarios not set up, see eg/setup-extra-tests", 3
289 $dir = catdir('EXTRA', '3', 'U');
291 sub {rmtree($dir, {verbose => 0})},
292 qr{\Acannot make child directory read-write-exec for [^:]+: .* at \S+ line \d+},
293 q(rmtree can't chdir into root dir)
296 $dir = catdir('EXTRA', '3');
298 sub {rmtree($dir, {})},
299 qr{\Acannot make child directory read-write-exec for [^:]+: .* at (\S+) line (\d+)
300 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
301 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
302 cannot remove directory for [^:]+: .* at \1 line \2},
303 'rmtree with file owned by root'
307 sub {rmtree('EXTRA', {})},
308 qr{\Acannot remove directory for [^:]+: .* at (\S+) line (\d+)
309 cannot remove directory for [^:]+: .* at \1 line \2
310 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
311 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
312 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
313 cannot remove directory for [^:]+: .* at \1 line \2
314 cannot unlink file for [^:]+: .* at \1 line \2
315 cannot restore permissions to \d+ for [^:]+: .* at \1 line \2
316 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
317 cannot remove directory for [^:]+: .* at \1 line \2
318 cannot restore permissions to \d+ for [^:]+: .* at \1 line \2},
319 'rmtree with insufficient privileges'
323 my $base = catdir($tmp_base,'output');
324 $dir = catdir($base,'A');
325 $dir2 = catdir($base,'B');
328 sub { rmtree( undef, 1 ) },
329 qr/\ANo root path\(s\) specified\b/,
330 "rmtree of nothing carps sensibly"
334 sub { rmtree( '', 1 ) },
335 qr/\ANo root path\(s\) specified\b/,
336 "rmtree of empty dir carps sensibly"
339 stderr_is( sub { mkpath() }, '', "mkpath no args does not carp" );
340 stderr_is( sub { rmtree() }, '', "rmtree no args does not carp" );
343 sub {@created = mkpath($dir, 1)},
344 "mkdir $base\nmkdir $dir\n",
345 'mkpath verbose (old style 1)'
349 sub {@created = mkpath([$dir2], 1)},
351 'mkpath verbose (old style 2)'
355 sub {$count = rmtree([$dir, $dir2], 1, 1)},
356 "rmdir $dir\nrmdir $dir2\n",
357 'rmtree verbose (old style)'
361 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
363 'mkpath verbose (new style 1)'
367 sub {@created = mkpath($dir2, 1, 0771)},
369 'mkpath verbose (new style 2)'
373 $file = catdir($dir2, "file");
374 skip "Cannot create $file", 2 unless open OUT, "> $file";
375 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
378 ok(-e $file, "file created in directory");
381 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
382 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
383 'rmtree safe verbose (new style)'
389 skip "extra scenarios not set up, see eg/setup-extra-tests", 11
390 unless -d catdir(qw(EXTRA 1));
392 rmtree 'EXTRA', {safe => 0, error => \$error};
393 is( scalar(@$error), 11, 'seven deadly sins' ); # well there used to be 7
395 rmtree 'EXTRA', {safe => 1, error => \$error};
396 is( scalar(@$error), 9, 'safe is better' );
398 ($file, $message) = each %$_;
399 if ($file =~ /[123]\z/) {
400 is(index($message, 'cannot remove directory: '), 0, "failed to remove $file with rmdir")
404 like($message, qr(\Acannot (?:restore permissions to \d+|chdir to child|unlink file): ), "failed to remove $file with unlink")
410 rmtree($tmp_base, {result => \$list} );
411 is(ref($list), 'ARRAY', "received a final list of results");
412 ok( !(-d $tmp_base), "test base directory gone" );