1 # Path.t -- tests for module File::Path
5 use Test::More tests => 114;
9 use_ok('File::Path', qw(rmtree mkpath make_path remove_tree));
10 use_ok('File::Spec::Functions');
13 eval "use Test::Output";
14 my $has_Test_Output = $@ ? 0 : 1;
16 my $Is_VMS = $^O eq 'VMS';
18 # first check for stupid permissions second for full, so we clean up
20 for my $perm (0111,0777) {
21 my $path = catdir(curdir(), "mhx", "bar");
23 chmod $perm, "mhx", $path;
25 my $oct = sprintf('0%o', $perm);
26 ok(-d "mhx", "mkdir parent dir $oct");
27 ok(-d $path, "mkdir child dir $oct");
30 ok(! -e "mhx", "mhx does not exist $oct");
33 # find a place to work
34 my ($error, $list, $file, $message);
35 my $tmp_base = catdir(
37 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
42 catdir($tmp_base, qw(a b)),
43 catdir($tmp_base, qw(a c)),
44 catdir($tmp_base, qw(z b)),
45 catdir($tmp_base, qw(z c)),
49 my @created = mkpath([@dir]);
51 is(scalar(@created), 7, "created list of directories");
53 # pray for no race conditions blowing them out from under us
54 @created = mkpath([$tmp_base]);
55 is(scalar(@created), 0, "skipped making existing directory")
56 or diag("unexpectedly recreated @created");
59 my $file_name = catfile( $tmp_base, 'a', 'delete.me' );
61 if (open OUT, "> $file_name") {
62 print OUT "this file may be deleted\n";
67 diag( "Failed to create file $file_name: $!" );
71 skip "cannot remove a file we failed to create", 1
72 unless $file_count == 1;
73 my $count = rmtree($file_name);
74 is($count, 1, "rmtree'ed a file");
77 @created = mkpath('');
78 is(scalar(@created), 0, "Can't create a directory named ''");
84 # background info: @_ = 1; !shift # gives '' not 0
85 # Message-Id: <3C820CE6-4400-4E91-AF43-A3D19B356E68@activestate.com>
86 # http://www.nntp.perl.org/group/perl.perl5.porters/2008/05/msg136625.html
87 mkpath(shift, !shift, 0755);
91 opendir D, shift or return -1;
92 my $count = () = readdir D;
93 closedir D or return -1;
100 my $before = count(curdir());
101 cmp_ok($before, '>', 0, "baseline $before");
104 is(count(curdir()), $before + 1, "first after $before");
106 $before = count(curdir());
108 is(count(curdir()), $before + 1, "second after $before");
117 my $before = count(curdir());
118 cmp_ok($before, '>', 0, "ARGV $before");
121 mkpath('3rd', !shift, 0755);
123 is(count(curdir()), $before + 1, "third after $before");
125 $before = count(curdir());
128 mkpath('4th', !shift, 0755);
130 is(count(curdir()), $before + 1, "fourth after $before");
137 # tests for rmtree() of ancestor directory
139 my $cwd = getcwd() or skip "failed to getcwd: $!", $nr_tests;
140 my $dir = catdir($cwd, 'remove');
141 my $dir2 = catdir($cwd, 'remove', 'this', 'dir');
143 skip "failed to mkpath '$dir2': $!", $nr_tests
144 unless mkpath($dir2, {verbose => 0});
145 skip "failed to chdir dir '$dir2': $!", $nr_tests
148 rmtree($dir, {error => \$error});
149 my $nr_err = @$error;
150 is($nr_err, 1, "ancestor error");
153 my ($file, $message) = each %{$error->[0]};
154 is($file, $dir, "ancestor named");
155 my $ortho_dir = $^O eq 'MSWin32' ? File::Path::_slash_lc($dir2) : $dir2;
156 $^O eq 'MSWin32' and $message
157 =~ s/\A(cannot remove path when cwd is )(.*)\Z/$1 . File::Path::_slash_lc($2)/e;
158 is($message, "cannot remove path when cwd is $ortho_dir", "ancestor reason");
159 ok(-d $dir2, "child not removed");
160 ok(-d $dir, "ancestor not removed");
170 ok(!(-d $dir), "ancestor now removed");
173 my $count = rmtree({error => \$error});
174 is( $count, 0, 'rmtree of nothing, count of zero' );
175 is( scalar(@$error), 0, 'no diagnostic captured' );
177 @created = mkpath($tmp_base, 0);
178 is(scalar(@created), 0, "skipped making existing directories (old style 1)")
179 or diag("unexpectedly recreated @created");
181 $dir = catdir($tmp_base,'C');
182 # mkpath returns unix syntax filespecs on VMS
183 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
184 @created = make_path($tmp_base, $dir);
185 is(scalar(@created), 1, "created directory (new style 1)");
186 is($created[0], $dir, "created directory (new style 1) cross-check");
188 @created = mkpath($tmp_base, 0, 0700);
189 is(scalar(@created), 0, "skipped making existing directories (old style 2)")
190 or diag("unexpectedly recreated @created");
192 $dir2 = catdir($tmp_base,'D');
193 # mkpath returns unix syntax filespecs on VMS
194 $dir2 = VMS::Filespec::unixify($dir2) if $Is_VMS;
195 @created = make_path($tmp_base, $dir, $dir2);
196 is(scalar(@created), 1, "created directory (new style 2)");
197 is($created[0], $dir2, "created directory (new style 2) cross-check");
199 $count = rmtree($dir, 0);
200 is($count, 1, "removed directory unsafe mode");
202 $count = rmtree($dir2, 0, 1);
203 my $removed = $Is_VMS ? 0 : 1;
204 is($count, $removed, "removed directory safe mode");
208 # existence of E is neither here nor there
209 $dir = catdir($tmp_base, 'E', updir(), 'Y');
210 @created =mkpath($dir);
211 cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
212 cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
213 ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
215 @created = make_path(catdir(curdir(), $tmp_base));
216 is(scalar(@created), 0, "nothing created")
219 $dir = catdir($tmp_base, 'a');
220 $dir2 = catdir($tmp_base, 'z');
230 is(scalar(@$error), 0, "no errors unlinking a and z");
231 is(scalar(@$list), 4, "list contains 4 elements")
234 ok(-d $dir, "dir a still exists");
235 ok(-d $dir2, "dir z still exists");
237 $dir = catdir($tmp_base,'F');
238 # mkpath returns unix syntax filespecs on VMS
239 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
241 @created = mkpath($dir, undef, 0770);
242 is(scalar(@created), 1, "created directory (old style 2 verbose undef)");
243 is($created[0], $dir, "created directory (old style 2 verbose undef) cross-check");
244 is(rmtree($dir, undef, 0), 1, "removed directory 2 verbose undef");
246 @created = mkpath($dir, undef);
247 is(scalar(@created), 1, "created directory (old style 2a verbose undef)");
248 is($created[0], $dir, "created directory (old style 2a verbose undef) cross-check");
249 is(rmtree($dir, undef), 1, "removed directory 2a verbose undef");
251 @created = mkpath($dir, 0, undef);
252 is(scalar(@created), 1, "created directory (old style 3 mode undef)");
253 is($created[0], $dir, "created directory (old style 3 mode undef) cross-check");
254 is(rmtree($dir, 0, undef), 1, "removed directory 3 verbose undef");
256 $dir = catdir($tmp_base,'G');
257 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
259 @created = mkpath($dir, undef, 0200);
260 is(scalar(@created), 1, "created write-only dir");
261 is($created[0], $dir, "created write-only directory cross-check");
262 is(rmtree($dir), 1, "removed write-only dir");
264 # borderline new-style heuristics
265 if (chdir $tmp_base) {
266 pass("chdir to temp dir");
269 fail("chdir to temp dir: $!");
272 $dir = catdir('a', 'd1');
273 $dir2 = catdir('a', 'd2');
275 @created = make_path( $dir, 0, $dir2 );
276 is(scalar @created, 3, 'new-style 3 dirs created');
278 $count = remove_tree( $dir, 0, $dir2, );
279 is($count, 3, 'new-style 3 dirs removed');
281 @created = make_path( $dir, $dir2, 1 );
282 is(scalar @created, 3, 'new-style 3 dirs created (redux)');
284 $count = remove_tree( $dir, $dir2, 1 );
285 is($count, 3, 'new-style 3 dirs removed (redux)');
287 @created = make_path( $dir, $dir2 );
288 is(scalar @created, 2, 'new-style 2 dirs created');
290 $count = remove_tree( $dir, $dir2 );
291 is($count, 2, 'new-style 2 dirs removed');
294 pass("chdir parent");
297 fail("chdir parent: $!");
301 # test bug http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=487319
302 skip "Don't need Force_Writeable semantics on $^O", 4
303 if grep {$^O eq $_} qw(amigaos dos epoc MSWin32 MacOS os2);
305 $dir2 = 'bug487319-symlink';
306 @created = make_path($dir, {mask => 0700});
307 is(scalar @created, 1, 'bug 487319 setup');
308 symlink($dir, $dir2);
309 ok(-e $dir2, "debian bug 487319 setup symlink") or diag($dir2);
312 my $mask_initial = (stat $dir)[2];
315 my $mask = (stat $dir)[2];
316 is( $mask, $mask_initial, 'mask of symlink target dir unchanged (debian bug 487319)');
319 my $file = catfile($dir, 'file');
320 open my $out, '>', $file;
324 $mask_initial = (stat $file)[2];
326 my $file2 = catfile($dir, 'symlink');
327 symlink($file, $file2);
330 $mask = (stat $file)[2];
331 is( $mask, $mask_initial, 'mask of symlink target file unchanged (debian bug 487319)');
336 # see what happens if a file exists where we want a directory
338 my $entry = catdir($tmp_base, "file");
339 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
340 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
342 ok(-e $entry, "file exists in place of directory");
344 mkpath( $entry, {error => \$error} );
345 is( scalar(@$error), 1, "caught error condition" );
346 ($file, $message) = each %{$error->[0]};
347 is( $entry, $file, "and the message is: $message");
349 eval {@created = mkpath($entry, 0, 0700)};
351 chomp $error; # just to remove silly # in TAP output
352 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
356 my $extra = catdir(curdir(), qw(EXTRA 1 a));
359 skip "extra scenarios not set up, see eg/setup-extra-tests", 14
363 $dir = catdir( 'EXTRA', '1' );
364 rmtree( $dir, {result => \$list, error => \$err} );
365 is(scalar(@$list), 2, "extra dir $dir removed");
366 is(scalar(@$err), 1, "one error encountered");
368 $dir = catdir( 'EXTRA', '3', 'N' );
369 rmtree( $dir, {result => \$list, error => \$err} );
370 is( @$list, 1, q{remove a symlinked dir} );
371 is( @$err, 0, q{with no errors} );
373 $dir = catdir('EXTRA', '3', 'S');
374 rmtree($dir, {error => \$error});
375 is( scalar(@$error), 1, 'one error for an unreadable dir' );
376 eval { ($file, $message) = each %{$error->[0]}};
377 is( $file, $dir, 'unreadable dir reported in error' )
380 $dir = catdir('EXTRA', '3', 'T');
381 rmtree($dir, {error => \$error});
382 is( scalar(@$error), 1, 'one error for an unreadable dir T' );
383 eval { ($file, $message) = each %{$error->[0]}};
384 is( $file, $dir, 'unreadable dir reported in error T' );
386 $dir = catdir( 'EXTRA', '4' );
387 rmtree($dir, {result => \$list, error => \$err} );
388 is( scalar(@$list), 0, q{don't follow a symlinked dir} );
389 is( scalar(@$err), 2, q{two errors when removing a symlink in r/o dir} );
390 eval { ($file, $message) = each %{$err->[0]} };
391 is( $file, $dir, 'symlink reported in error' );
393 $dir = catdir('EXTRA', '3', 'U');
394 $dir2 = catdir('EXTRA', '3', 'V');
395 rmtree($dir, $dir2, {verbose => 0, error => \$err, result => \$list});
396 is( scalar(@$list), 1, q{deleted 1 out of 2 directories} );
397 is( scalar(@$error), 1, q{left behind 1 out of 2 directories} );
398 eval { ($file, $message) = each %{$err->[0]} };
399 is( $file, $dir, 'first dir reported in error' );
403 $dir = catdir($tmp_base, 'ZZ');
404 @created = mkpath($dir);
405 is(scalar(@created), 1, "create a ZZ directory");
407 local @ARGV = ($dir);
408 rmtree( [grep -e $_, @ARGV], 0, 0 );
409 ok(!-e $dir, "blow it away via \@ARGV");
413 skip 'Test::Output not available', 14
414 unless $has_Test_Output;
417 $dir = catdir('EXTRA', '3');
418 skip "extra scenarios not set up, see eg/setup-extra-tests", 3
421 $dir = catdir('EXTRA', '3', 'U');
423 sub {rmtree($dir, {verbose => 0})},
424 qr{\Acannot make child directory read-write-exec for [^:]+: .* at \S+ line \d+},
425 q(rmtree can't chdir into root dir)
428 $dir = catdir('EXTRA', '3');
430 sub {rmtree($dir, {})},
431 qr{\Acannot make child directory read-write-exec for [^:]+: .* at (\S+) line (\d+)
432 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
433 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
434 cannot remove directory for [^:]+: .* at \1 line \2},
435 'rmtree with file owned by root'
439 sub {rmtree('EXTRA', {})},
440 qr{\Acannot remove directory for [^:]+: .* at (\S+) line (\d+)
441 cannot remove directory for [^:]+: .* at \1 line \2
442 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
443 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
444 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
445 cannot remove directory for [^:]+: .* at \1 line \2
446 cannot unlink file for [^:]+: .* at \1 line \2
447 cannot restore permissions to \d+ for [^:]+: .* at \1 line \2
448 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
449 cannot remove directory for [^:]+: .* at \1 line \2
450 cannot restore permissions to \d+ for [^:]+: .* at \1 line \2},
451 'rmtree with insufficient privileges'
455 my $base = catdir($tmp_base,'output');
456 $dir = catdir($base,'A');
457 $dir2 = catdir($base,'B');
460 sub { rmtree( undef, 1 ) },
461 qr/\ANo root path\(s\) specified\b/,
462 "rmtree of nothing carps sensibly"
466 sub { rmtree( '', 1 ) },
467 qr/\ANo root path\(s\) specified\b/,
468 "rmtree of empty dir carps sensibly"
471 stderr_is( sub { make_path() }, '', "make_path no args does not carp" );
472 stderr_is( sub { remove_tree() }, '', "remove_tree no args does not carp" );
475 sub {@created = mkpath($dir, 1)},
476 "mkdir $base\nmkdir $dir\n",
477 'mkpath verbose (old style 1)'
481 sub {@created = mkpath([$dir2], 1)},
483 'mkpath verbose (old style 2)'
487 sub {$count = rmtree([$dir, $dir2], 1, 1)},
488 "rmdir $dir\nrmdir $dir2\n",
489 'rmtree verbose (old style)'
493 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
495 'mkpath verbose (new style 1)'
499 sub {@created = mkpath($dir2, 1, 0771)},
501 'mkpath verbose (new style 2)'
505 $file = catdir($dir2, "file");
506 skip "Cannot create $file", 2 unless open OUT, "> $file";
507 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
510 ok(-e $file, "file created in directory");
513 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
514 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
515 'rmtree safe verbose (new style)'
521 skip "extra scenarios not set up, see eg/setup-extra-tests", 11
522 unless -d catdir(qw(EXTRA 1));
524 rmtree 'EXTRA', {safe => 0, error => \$error};
525 is( scalar(@$error), 11, 'seven deadly sins' ); # well there used to be 7
527 rmtree 'EXTRA', {safe => 1, error => \$error};
528 is( scalar(@$error), 9, 'safe is better' );
530 ($file, $message) = each %$_;
531 if ($file =~ /[123]\z/) {
532 is(index($message, 'cannot remove directory: '), 0, "failed to remove $file with rmdir")
536 like($message, qr(\Acannot (?:restore permissions to \d+|chdir to child|unlink file): ), "failed to remove $file with unlink")
542 rmtree($tmp_base, {result => \$list} );
543 is(ref($list), 'ARRAY', "received a final list of results");
544 ok( !(-d $tmp_base), "test base directory gone" );