1 # Path.t -- tests for module File::Path
5 use Test::More tests => 120;
10 use_ok('File::Path', qw(rmtree mkpath make_path remove_tree));
11 use_ok('File::Spec::Functions');
14 eval "use Test::Output";
15 my $has_Test_Output = $@ ? 0 : 1;
17 my $Is_VMS = $^O eq 'VMS';
19 # first check for stupid permissions second for full, so we clean up
21 for my $perm (0111,0777) {
22 my $path = catdir(curdir(), "mhx", "bar");
24 chmod $perm, "mhx", $path;
26 my $oct = sprintf('0%o', $perm);
27 ok(-d "mhx", "mkdir parent dir $oct");
28 ok(-d $path, "mkdir child dir $oct");
31 ok(! -e "mhx", "mhx does not exist $oct");
34 # find a place to work
35 my ($error, $list, $file, $message);
36 my $tmp_base = catdir(
38 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
43 catdir($tmp_base, qw(a b)),
44 catdir($tmp_base, qw(a c)),
45 catdir($tmp_base, qw(z b)),
46 catdir($tmp_base, qw(z c)),
50 my @created = mkpath([@dir]);
52 is(scalar(@created), 7, "created list of directories");
54 # pray for no race conditions blowing them out from under us
55 @created = mkpath([$tmp_base]);
56 is(scalar(@created), 0, "skipped making existing directory")
57 or diag("unexpectedly recreated @created");
60 my $file_name = catfile( $tmp_base, 'a', 'delete.me' );
62 if (open OUT, "> $file_name") {
63 print OUT "this file may be deleted\n";
68 diag( "Failed to create file $file_name: $!" );
72 skip "cannot remove a file we failed to create", 1
73 unless $file_count == 1;
74 my $count = rmtree($file_name);
75 is($count, 1, "rmtree'ed a file");
78 @created = mkpath('');
79 is(scalar(@created), 0, "Can't create a directory named ''");
85 # background info: @_ = 1; !shift # gives '' not 0
86 # Message-Id: <3C820CE6-4400-4E91-AF43-A3D19B356E68@activestate.com>
87 # http://www.nntp.perl.org/group/perl.perl5.porters/2008/05/msg136625.html
88 mkpath(shift, !shift, 0755);
92 opendir D, shift or return -1;
93 my $count = () = readdir D;
94 closedir D or return -1;
101 open my $f, '>', 'foo.dat';
103 my $before = count(curdir());
104 cmp_ok($before, '>', 0, "baseline $before");
107 is(count(curdir()), $before + 1, "first after $before");
109 $before = count(curdir());
111 is(count(curdir()), $before + 1, "second after $before");
120 open my $f, '>', 'foo.dat';
122 my $before = count(curdir());
123 cmp_ok($before, '>', 0, "ARGV $before");
126 mkpath('3rd', !shift, 0755);
128 is(count(curdir()), $before + 1, "third after $before");
130 $before = count(curdir());
133 mkpath('4th', !shift, 0755);
135 is(count(curdir()), $before + 1, "fourth after $before");
142 # tests for rmtree() of ancestor directory
144 my $cwd = getcwd() or skip "failed to getcwd: $!", $nr_tests;
145 my $dir = catdir($cwd, 'remove');
146 my $dir2 = catdir($cwd, 'remove', 'this', 'dir');
148 skip "failed to mkpath '$dir2': $!", $nr_tests
149 unless mkpath($dir2, {verbose => 0});
150 skip "failed to chdir dir '$dir2': $!", $nr_tests
153 rmtree($dir, {error => \$error});
154 my $nr_err = @$error;
155 is($nr_err, 1, "ancestor error");
158 my ($file, $message) = each %{$error->[0]};
159 is($file, $dir, "ancestor named");
160 my $ortho_dir = $^O eq 'MSWin32' ? File::Path::_slash_lc($dir2) : $dir2;
161 $^O eq 'MSWin32' and $message
162 =~ s/\A(cannot remove path when cwd is )(.*)\Z/$1 . File::Path::_slash_lc($2)/e;
163 is($message, "cannot remove path when cwd is $ortho_dir", "ancestor reason");
164 ok(-d $dir2, "child not removed");
165 ok(-d $dir, "ancestor not removed");
175 ok(!(-d $dir), "ancestor now removed");
178 my $count = rmtree({error => \$error});
179 is( $count, 0, 'rmtree of nothing, count of zero' );
180 is( scalar(@$error), 0, 'no diagnostic captured' );
182 @created = mkpath($tmp_base, 0);
183 is(scalar(@created), 0, "skipped making existing directories (old style 1)")
184 or diag("unexpectedly recreated @created");
186 $dir = catdir($tmp_base,'C');
187 # mkpath returns unix syntax filespecs on VMS
188 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
189 @created = make_path($tmp_base, $dir);
190 is(scalar(@created), 1, "created directory (new style 1)");
191 is($created[0], $dir, "created directory (new style 1) cross-check");
193 @created = mkpath($tmp_base, 0, 0700);
194 is(scalar(@created), 0, "skipped making existing directories (old style 2)")
195 or diag("unexpectedly recreated @created");
197 $dir2 = catdir($tmp_base,'D');
198 # mkpath returns unix syntax filespecs on VMS
199 $dir2 = VMS::Filespec::unixify($dir2) if $Is_VMS;
200 @created = make_path($tmp_base, $dir, $dir2);
201 is(scalar(@created), 1, "created directory (new style 2)");
202 is($created[0], $dir2, "created directory (new style 2) cross-check");
204 $count = rmtree($dir, 0);
205 is($count, 1, "removed directory unsafe mode");
207 $count = rmtree($dir2, 0, 1);
208 is($count, 1, "removed directory safe mode");
212 # existence of E is neither here nor there
213 $dir = catdir($tmp_base, 'E', updir(), 'Y');
214 @created =mkpath($dir);
215 cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
216 cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
217 ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
219 @created = make_path(catdir(curdir(), $tmp_base));
220 is(scalar(@created), 0, "nothing created")
223 $dir = catdir($tmp_base, 'a');
224 $dir2 = catdir($tmp_base, 'z');
234 is(scalar(@$error), 0, "no errors unlinking a and z");
235 is(scalar(@$list), 4, "list contains 4 elements")
238 ok(-d $dir, "dir a still exists");
239 ok(-d $dir2, "dir z still exists");
241 $dir = catdir($tmp_base,'F');
242 # mkpath returns unix syntax filespecs on VMS
243 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
245 @created = mkpath($dir, undef, 0770);
246 is(scalar(@created), 1, "created directory (old style 2 verbose undef)");
247 is($created[0], $dir, "created directory (old style 2 verbose undef) cross-check");
248 is(rmtree($dir, undef, 0), 1, "removed directory 2 verbose undef");
250 @created = mkpath($dir, undef);
251 is(scalar(@created), 1, "created directory (old style 2a verbose undef)");
252 is($created[0], $dir, "created directory (old style 2a verbose undef) cross-check");
253 is(rmtree($dir, undef), 1, "removed directory 2a verbose undef");
255 @created = mkpath($dir, 0, undef);
256 is(scalar(@created), 1, "created directory (old style 3 mode undef)");
257 is($created[0], $dir, "created directory (old style 3 mode undef) cross-check");
258 is(rmtree($dir, 0, undef), 1, "removed directory 3 verbose undef");
260 $dir = catdir($tmp_base,'G');
261 $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
263 @created = mkpath($dir, undef, 0200);
264 is(scalar(@created), 1, "created write-only dir");
265 is($created[0], $dir, "created write-only directory cross-check");
266 is(rmtree($dir), 1, "removed write-only dir");
268 # borderline new-style heuristics
269 if (chdir $tmp_base) {
270 pass("chdir to temp dir");
273 fail("chdir to temp dir: $!");
276 $dir = catdir('a', 'd1');
277 $dir2 = catdir('a', 'd2');
279 @created = make_path( $dir, 0, $dir2 );
280 is(scalar @created, 3, 'new-style 3 dirs created');
282 $count = remove_tree( $dir, 0, $dir2, );
283 is($count, 3, 'new-style 3 dirs removed');
285 @created = make_path( $dir, $dir2, 1 );
286 is(scalar @created, 3, 'new-style 3 dirs created (redux)');
288 $count = remove_tree( $dir, $dir2, 1 );
289 is($count, 3, 'new-style 3 dirs removed (redux)');
291 @created = make_path( $dir, $dir2 );
292 is(scalar @created, 2, 'new-style 2 dirs created');
294 $count = remove_tree( $dir, $dir2 );
295 is($count, 2, 'new-style 2 dirs removed');
298 pass("chdir parent");
301 fail("chdir parent: $!");
305 # test bug http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=487319
306 skip "Don't need Force_Writeable semantics on $^O", 4
307 if grep {$^O eq $_} qw(amigaos dos epoc MSWin32 MacOS os2);
308 skip "Symlinks not available", 4 unless $Config{'d_symlink'};
310 $dir2 = 'bug487319-symlink';
311 @created = make_path($dir, {mask => 0700});
312 is(scalar @created, 1, 'bug 487319 setup');
313 symlink($dir, $dir2);
314 ok(-e $dir2, "debian bug 487319 setup symlink") or diag($dir2);
317 my $mask_initial = (stat $dir)[2];
320 my $mask = (stat $dir)[2];
321 is( $mask, $mask_initial, 'mask of symlink target dir unchanged (debian bug 487319)');
324 my $file = catfile($dir, 'file');
325 open my $out, '>', $file;
329 $mask_initial = (stat $file)[2];
331 my $file2 = catfile($dir, 'symlink');
332 symlink($file, $file2);
335 $mask = (stat $file)[2];
336 is( $mask, $mask_initial, 'mask of symlink target file unchanged (debian bug 487319)');
341 # see what happens if a file exists where we want a directory
343 my $entry = catdir($tmp_base, "file");
344 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
345 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
347 ok(-e $entry, "file exists in place of directory");
349 mkpath( $entry, {error => \$error} );
350 is( scalar(@$error), 1, "caught error condition" );
351 ($file, $message) = each %{$error->[0]};
352 is( $entry, $file, "and the message is: $message");
354 eval {@created = mkpath($entry, 0, 0700)};
356 chomp $error; # just to remove silly # in TAP output
357 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
361 my $extra = catdir(curdir(), qw(EXTRA 1 a));
364 skip "extra scenarios not set up, see eg/setup-extra-tests", 14
366 skip "Symlinks not available", 14 unless $Config{'d_symlink'};
369 $dir = catdir( 'EXTRA', '1' );
370 rmtree( $dir, {result => \$list, error => \$err} );
371 is(scalar(@$list), 2, "extra dir $dir removed");
372 is(scalar(@$err), 1, "one error encountered");
374 $dir = catdir( 'EXTRA', '3', 'N' );
375 rmtree( $dir, {result => \$list, error => \$err} );
376 is( @$list, 1, q{remove a symlinked dir} );
377 is( @$err, 0, q{with no errors} );
379 $dir = catdir('EXTRA', '3', 'S');
380 rmtree($dir, {error => \$error});
381 is( scalar(@$error), 1, 'one error for an unreadable dir' );
382 eval { ($file, $message) = each %{$error->[0]}};
383 is( $file, $dir, 'unreadable dir reported in error' )
386 $dir = catdir('EXTRA', '3', 'T');
387 rmtree($dir, {error => \$error});
388 is( scalar(@$error), 1, 'one error for an unreadable dir T' );
389 eval { ($file, $message) = each %{$error->[0]}};
390 is( $file, $dir, 'unreadable dir reported in error T' );
392 $dir = catdir( 'EXTRA', '4' );
393 rmtree($dir, {result => \$list, error => \$err} );
394 is( scalar(@$list), 0, q{don't follow a symlinked dir} );
395 is( scalar(@$err), 2, q{two errors when removing a symlink in r/o dir} );
396 eval { ($file, $message) = each %{$err->[0]} };
397 is( $file, $dir, 'symlink reported in error' );
399 $dir = catdir('EXTRA', '3', 'U');
400 $dir2 = catdir('EXTRA', '3', 'V');
401 rmtree($dir, $dir2, {verbose => 0, error => \$err, result => \$list});
402 is( scalar(@$list), 1, q{deleted 1 out of 2 directories} );
403 is( scalar(@$error), 1, q{left behind 1 out of 2 directories} );
404 eval { ($file, $message) = each %{$err->[0]} };
405 is( $file, $dir, 'first dir reported in error' );
409 $dir = catdir($tmp_base, 'ZZ');
410 @created = mkpath($dir);
411 is(scalar(@created), 1, "create a ZZ directory");
413 local @ARGV = ($dir);
414 rmtree( [grep -e $_, @ARGV], 0, 0 );
415 ok(!-e $dir, "blow it away via \@ARGV");
419 skip 'Test::Output not available', 14
420 unless $has_Test_Output;
423 $dir = catdir('EXTRA', '3');
424 skip "extra scenarios not set up, see eg/setup-extra-tests", 3
427 $dir = catdir('EXTRA', '3', 'U');
429 sub {rmtree($dir, {verbose => 0})},
430 qr{\Acannot make child directory read-write-exec for [^:]+: .* at \S+ line \d+},
431 q(rmtree can't chdir into root dir)
434 $dir = catdir('EXTRA', '3');
436 sub {rmtree($dir, {})},
437 qr{\Acannot make child directory read-write-exec for [^:]+: .* at (\S+) line (\d+)
438 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
439 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
440 cannot remove directory for [^:]+: .* at \1 line \2},
441 'rmtree with file owned by root'
445 sub {rmtree('EXTRA', {})},
446 qr{\Acannot remove directory for [^:]+: .* at (\S+) line (\d+)
447 cannot remove directory for [^:]+: .* at \1 line \2
448 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
449 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
450 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
451 cannot remove directory for [^:]+: .* at \1 line \2
452 cannot unlink file for [^:]+: .* at \1 line \2
453 cannot restore permissions to \d+ for [^:]+: .* at \1 line \2
454 cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
455 cannot remove directory for [^:]+: .* at \1 line \2
456 cannot restore permissions to \d+ for [^:]+: .* at \1 line \2},
457 'rmtree with insufficient privileges'
461 my $base = catdir($tmp_base,'output');
462 $dir = catdir($base,'A');
463 $dir2 = catdir($base,'B');
466 sub { rmtree( undef, 1 ) },
467 qr/\ANo root path\(s\) specified\b/,
468 "rmtree of nothing carps sensibly"
472 sub { rmtree( '', 1 ) },
473 qr/\ANo root path\(s\) specified\b/,
474 "rmtree of empty dir carps sensibly"
477 stderr_is( sub { make_path() }, '', "make_path no args does not carp" );
478 stderr_is( sub { remove_tree() }, '', "remove_tree no args does not carp" );
481 sub {@created = mkpath($dir, 1)},
482 "mkdir $base\nmkdir $dir\n",
483 'mkpath verbose (old style 1)'
487 sub {@created = mkpath([$dir2], 1)},
489 'mkpath verbose (old style 2)'
493 sub {$count = rmtree([$dir, $dir2], 1, 1)},
494 "rmdir $dir\nrmdir $dir2\n",
495 'rmtree verbose (old style)'
499 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
501 'mkpath verbose (new style 1)'
505 sub {@created = mkpath($dir2, 1, 0771)},
507 'mkpath verbose (new style 2)'
511 $file = catdir($dir2, "file");
512 skip "Cannot create $file", 2 unless open OUT, "> $file";
513 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
516 ok(-e $file, "file created in directory");
519 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
520 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
521 'rmtree safe verbose (new style)'
527 skip "extra scenarios not set up, see eg/setup-extra-tests", 11
528 unless -d catdir(qw(EXTRA 1));
530 rmtree 'EXTRA', {safe => 0, error => \$error};
531 is( scalar(@$error), 11, 'seven deadly sins' ); # well there used to be 7
533 rmtree 'EXTRA', {safe => 1, error => \$error};
534 is( scalar(@$error), 9, 'safe is better' );
536 ($file, $message) = each %$_;
537 if ($file =~ /[123]\z/) {
538 is(index($message, 'cannot remove directory: '), 0, "failed to remove $file with rmdir")
542 like($message, qr(\Acannot (?:restore permissions to \d+|chdir to child|unlink file): ), "failed to remove $file with unlink")
550 my $cwd = getcwd() or skip "failed to getcwd: $!", $nr_tests;
551 rmtree($tmp_base, {result => \$list} );
552 is(ref($list), 'ARRAY', "received a final list of results");
553 ok( !(-d $tmp_base), "test base directory gone" );
567 # create and delete directory
568 my $px = catdir($p, $x);
570 ok(rmtree($px), "rmtree"); # fails in File-Path-2.07