Update to CPANPLUS 0.87_01
[p5sagit/p5-mst-13.2.git] / lib / File / Path.t
CommitLineData
12c2e016 1# Path.t -- tests for module File::Path
1a3850a5 2
037c8c09 3use strict;
4
0e5b5e32 5use Test::More tests => 120;
30cf951a 6use Config;
1a3850a5 7
12c2e016 8BEGIN {
3f083399 9 use_ok('Cwd');
10 use_ok('File::Path', qw(rmtree mkpath make_path remove_tree));
12c2e016 11 use_ok('File::Spec::Functions');
12}
13
14eval "use Test::Output";
15my $has_Test_Output = $@ ? 0 : 1;
1a3850a5 16
30cf951a 17my $Is_VMS = $^O eq 'VMS';
5808899a 18
037c8c09 19# first check for stupid permissions second for full, so we clean up
20# behind ourselves
21for my $perm (0111,0777) {
e7780b56 22 my $path = catdir(curdir(), "mhx", "bar");
d5201bd2 23 mkpath($path);
e7780b56 24 chmod $perm, "mhx", $path;
1a3850a5 25
12c2e016 26 my $oct = sprintf('0%o', $perm);
27 ok(-d "mhx", "mkdir parent dir $oct");
28 ok(-d $path, "mkdir child dir $oct");
1a3850a5 29
e7780b56 30 rmtree("mhx");
12c2e016 31 ok(! -e "mhx", "mhx does not exist $oct");
32}
33
34# find a place to work
35my ($error, $list, $file, $message);
36my $tmp_base = catdir(
37 curdir(),
38 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
39);
40
41# invent some names
42my @dir = (
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)),
47);
48
49# create them
3f083399 50my @created = mkpath([@dir]);
12c2e016 51
52is(scalar(@created), 7, "created list of directories");
53
54# pray for no race conditions blowing them out from under us
55@created = mkpath([$tmp_base]);
56is(scalar(@created), 0, "skipped making existing directory")
57 or diag("unexpectedly recreated @created");
58
351a5cfe 59# create a file
60my $file_name = catfile( $tmp_base, 'a', 'delete.me' );
61my $file_count = 0;
62if (open OUT, "> $file_name") {
63 print OUT "this file may be deleted\n";
64 close OUT;
65 ++$file_count;
66}
67else {
68 diag( "Failed to create file $file_name: $!" );
69}
70
71SKIP: {
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");
76}
77
12c2e016 78@created = mkpath('');
79is(scalar(@created), 0, "Can't create a directory named ''");
80
81my $dir;
82my $dir2;
83
3f083399 84sub gisle {
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);
89}
90
91sub count {
92 opendir D, shift or return -1;
93 my $count = () = readdir D;
94 closedir D or return -1;
95 return $count;
96}
97
98{
99 mkdir 'solo', 0755;
100 chdir 'solo';
30cf951a 101 open my $f, '>', 'foo.dat';
102 close $f;
3f083399 103 my $before = count(curdir());
104 cmp_ok($before, '>', 0, "baseline $before");
105
106 gisle('1st', 1);
107 is(count(curdir()), $before + 1, "first after $before");
108
109 $before = count(curdir());
110 gisle('2nd', 1);
111 is(count(curdir()), $before + 1, "second after $before");
112
113 chdir updir();
114 rmtree 'solo';
115}
116
117{
118 mkdir 'solo', 0755;
119 chdir 'solo';
30cf951a 120 open my $f, '>', 'foo.dat';
121 close $f;
3f083399 122 my $before = count(curdir());
123 cmp_ok($before, '>', 0, "ARGV $before");
124 {
125 local @ARGV = (1);
126 mkpath('3rd', !shift, 0755);
127 }
128 is(count(curdir()), $before + 1, "third after $before");
129
130 $before = count(curdir());
131 {
132 local @ARGV = (1);
133 mkpath('4th', !shift, 0755);
134 }
135 is(count(curdir()), $before + 1, "fourth after $before");
136
137 chdir updir();
138 rmtree 'solo';
139}
140
c42ebacb 141SKIP: {
142 # tests for rmtree() of ancestor directory
143 my $nr_tests = 6;
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');
147
148 skip "failed to mkpath '$dir2': $!", $nr_tests
149 unless mkpath($dir2, {verbose => 0});
150 skip "failed to chdir dir '$dir2': $!", $nr_tests
151 unless chdir($dir2);
152
153 rmtree($dir, {error => \$error});
154 my $nr_err = @$error;
155 is($nr_err, 1, "ancestor error");
156
157 if ($nr_err) {
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");
166 }
167 else {
168 fail( "ancestor 1");
169 fail( "ancestor 2");
170 fail( "ancestor 3");
171 fail( "ancestor 4");
172 }
173 chdir $cwd;
174 rmtree($dir);
175 ok(!(-d $dir), "ancestor now removed");
176};
177
12c2e016 178my $count = rmtree({error => \$error});
179is( $count, 0, 'rmtree of nothing, count of zero' );
3376a30f 180is( scalar(@$error), 0, 'no diagnostic captured' );
12c2e016 181
182@created = mkpath($tmp_base, 0);
183is(scalar(@created), 0, "skipped making existing directories (old style 1)")
184 or diag("unexpectedly recreated @created");
185
186$dir = catdir($tmp_base,'C');
fa06c9c1 187# mkpath returns unix syntax filespecs on VMS
5808899a 188$dir = VMS::Filespec::unixify($dir) if $Is_VMS;
3f083399 189@created = make_path($tmp_base, $dir);
12c2e016 190is(scalar(@created), 1, "created directory (new style 1)");
191is($created[0], $dir, "created directory (new style 1) cross-check");
192
193@created = mkpath($tmp_base, 0, 0700);
194is(scalar(@created), 0, "skipped making existing directories (old style 2)")
195 or diag("unexpectedly recreated @created");
196
197$dir2 = catdir($tmp_base,'D');
fa06c9c1 198# mkpath returns unix syntax filespecs on VMS
5808899a 199$dir2 = VMS::Filespec::unixify($dir2) if $Is_VMS;
3f083399 200@created = make_path($tmp_base, $dir, $dir2);
12c2e016 201is(scalar(@created), 1, "created directory (new style 2)");
202is($created[0], $dir2, "created directory (new style 2) cross-check");
203
204$count = rmtree($dir, 0);
5808899a 205is($count, 1, "removed directory unsafe mode");
12c2e016 206
207$count = rmtree($dir2, 0, 1);
33839f2f 208my $removed = $Is_VMS ? 0 : 1;
209is($count, $removed, "removed directory safe mode");
12c2e016 210
211# mkdir foo ./E/../Y
212# Y should exist
213# existence of E is neither here nor there
214$dir = catdir($tmp_base, 'E', updir(), 'Y');
215@created =mkpath($dir);
216cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
217cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
218ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
219
3f083399 220@created = make_path(catdir(curdir(), $tmp_base));
12c2e016 221is(scalar(@created), 0, "nothing created")
222 or diag(@created);
223
224$dir = catdir($tmp_base, 'a');
225$dir2 = catdir($tmp_base, 'z');
226
227rmtree( $dir, $dir2,
228 {
229 error => \$error,
230 result => \$list,
231 keep_root => 1,
232 }
233);
234
235is(scalar(@$error), 0, "no errors unlinking a and z");
236is(scalar(@$list), 4, "list contains 4 elements")
237 or diag("@$list");
238
239ok(-d $dir, "dir a still exists");
240ok(-d $dir2, "dir z still exists");
241
cd117d8b 242$dir = catdir($tmp_base,'F');
181b7e95 243# mkpath returns unix syntax filespecs on VMS
5808899a 244$dir = VMS::Filespec::unixify($dir) if $Is_VMS;
cd117d8b 245
246@created = mkpath($dir, undef, 0770);
247is(scalar(@created), 1, "created directory (old style 2 verbose undef)");
248is($created[0], $dir, "created directory (old style 2 verbose undef) cross-check");
249is(rmtree($dir, undef, 0), 1, "removed directory 2 verbose undef");
250
251@created = mkpath($dir, undef);
252is(scalar(@created), 1, "created directory (old style 2a verbose undef)");
253is($created[0], $dir, "created directory (old style 2a verbose undef) cross-check");
254is(rmtree($dir, undef), 1, "removed directory 2a verbose undef");
255
256@created = mkpath($dir, 0, undef);
257is(scalar(@created), 1, "created directory (old style 3 mode undef)");
258is($created[0], $dir, "created directory (old style 3 mode undef) cross-check");
259is(rmtree($dir, 0, undef), 1, "removed directory 3 verbose undef");
260
0b3d36bd 261$dir = catdir($tmp_base,'G');
5808899a 262$dir = VMS::Filespec::unixify($dir) if $Is_VMS;
0b3d36bd 263
264@created = mkpath($dir, undef, 0200);
265is(scalar(@created), 1, "created write-only dir");
266is($created[0], $dir, "created write-only directory cross-check");
267is(rmtree($dir), 1, "removed write-only dir");
268
12c2e016 269# borderline new-style heuristics
270if (chdir $tmp_base) {
271 pass("chdir to temp dir");
272}
273else {
274 fail("chdir to temp dir: $!");
037c8c09 275}
12c2e016 276
277$dir = catdir('a', 'd1');
278$dir2 = catdir('a', 'd2');
279
3f083399 280@created = make_path( $dir, 0, $dir2 );
12c2e016 281is(scalar @created, 3, 'new-style 3 dirs created');
282
3f083399 283$count = remove_tree( $dir, 0, $dir2, );
12c2e016 284is($count, 3, 'new-style 3 dirs removed');
285
3f083399 286@created = make_path( $dir, $dir2, 1 );
12c2e016 287is(scalar @created, 3, 'new-style 3 dirs created (redux)');
288
3f083399 289$count = remove_tree( $dir, $dir2, 1 );
12c2e016 290is($count, 3, 'new-style 3 dirs removed (redux)');
291
3f083399 292@created = make_path( $dir, $dir2 );
12c2e016 293is(scalar @created, 2, 'new-style 2 dirs created');
294
3f083399 295$count = remove_tree( $dir, $dir2 );
12c2e016 296is($count, 2, 'new-style 2 dirs removed');
297
298if (chdir updir()) {
299 pass("chdir parent");
300}
301else {
302 fail("chdir parent: $!");
303}
304
3f083399 305SKIP: {
306 # test bug http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=487319
307 skip "Don't need Force_Writeable semantics on $^O", 4
308 if grep {$^O eq $_} qw(amigaos dos epoc MSWin32 MacOS os2);
30cf951a 309 skip "Symlinks not available", 4 unless $Config{'d_symlink'};
3f083399 310 $dir = 'bug487319';
311 $dir2 = 'bug487319-symlink';
312 @created = make_path($dir, {mask => 0700});
313 is(scalar @created, 1, 'bug 487319 setup');
314 symlink($dir, $dir2);
315 ok(-e $dir2, "debian bug 487319 setup symlink") or diag($dir2);
316
317 chmod 0500, $dir;
318 my $mask_initial = (stat $dir)[2];
319 remove_tree($dir2);
320
321 my $mask = (stat $dir)[2];
322 is( $mask, $mask_initial, 'mask of symlink target dir unchanged (debian bug 487319)');
323
324 # now try a file
325 my $file = catfile($dir, 'file');
326 open my $out, '>', $file;
327 close $out;
328
329 chmod 0500, $file;
330 $mask_initial = (stat $file)[2];
331
332 my $file2 = catfile($dir, 'symlink');
333 symlink($file, $file2);
334 remove_tree($file2);
335
336 $mask = (stat $file)[2];
337 is( $mask, $mask_initial, 'mask of symlink target file unchanged (debian bug 487319)');
338
339 remove_tree($dir);
340}
341
12c2e016 342# see what happens if a file exists where we want a directory
343SKIP: {
344 my $entry = catdir($tmp_base, "file");
345 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
346 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
347 close OUT;
348 ok(-e $entry, "file exists in place of directory");
349
350 mkpath( $entry, {error => \$error} );
351 is( scalar(@$error), 1, "caught error condition" );
352 ($file, $message) = each %{$error->[0]};
353 is( $entry, $file, "and the message is: $message");
354
355 eval {@created = mkpath($entry, 0, 0700)};
356 $error = $@;
357 chomp $error; # just to remove silly # in TAP output
358 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
359 or diag(@created);
360}
361
362my $extra = catdir(curdir(), qw(EXTRA 1 a));
363
364SKIP: {
37b1cd44 365 skip "extra scenarios not set up, see eg/setup-extra-tests", 14
12c2e016 366 unless -e $extra;
30cf951a 367 skip "Symlinks not available", 14 unless $Config{'d_symlink'};
12c2e016 368
369 my ($list, $err);
370 $dir = catdir( 'EXTRA', '1' );
371 rmtree( $dir, {result => \$list, error => \$err} );
372 is(scalar(@$list), 2, "extra dir $dir removed");
373 is(scalar(@$err), 1, "one error encountered");
374
375 $dir = catdir( 'EXTRA', '3', 'N' );
376 rmtree( $dir, {result => \$list, error => \$err} );
377 is( @$list, 1, q{remove a symlinked dir} );
378 is( @$err, 0, q{with no errors} );
379
380 $dir = catdir('EXTRA', '3', 'S');
381 rmtree($dir, {error => \$error});
0b3d36bd 382 is( scalar(@$error), 1, 'one error for an unreadable dir' );
37b1cd44 383 eval { ($file, $message) = each %{$error->[0]}};
384 is( $file, $dir, 'unreadable dir reported in error' )
385 or diag($message);
12c2e016 386
cd117d8b 387 $dir = catdir('EXTRA', '3', 'T');
388 rmtree($dir, {error => \$error});
37b1cd44 389 is( scalar(@$error), 1, 'one error for an unreadable dir T' );
390 eval { ($file, $message) = each %{$error->[0]}};
391 is( $file, $dir, 'unreadable dir reported in error T' );
cd117d8b 392
12c2e016 393 $dir = catdir( 'EXTRA', '4' );
394 rmtree($dir, {result => \$list, error => \$err} );
37b1cd44 395 is( scalar(@$list), 0, q{don't follow a symlinked dir} );
396 is( scalar(@$err), 2, q{two errors when removing a symlink in r/o dir} );
12c2e016 397 eval { ($file, $message) = each %{$err->[0]} };
398 is( $file, $dir, 'symlink reported in error' );
37b1cd44 399
400 $dir = catdir('EXTRA', '3', 'U');
401 $dir2 = catdir('EXTRA', '3', 'V');
402 rmtree($dir, $dir2, {verbose => 0, error => \$err, result => \$list});
403 is( scalar(@$list), 1, q{deleted 1 out of 2 directories} );
404 is( scalar(@$error), 1, q{left behind 1 out of 2 directories} );
405 eval { ($file, $message) = each %{$err->[0]} };
406 is( $file, $dir, 'first dir reported in error' );
12c2e016 407}
408
3376a30f 409{
d2f50e7f 410 $dir = catdir($tmp_base, 'ZZ');
3376a30f 411 @created = mkpath($dir);
d2f50e7f 412 is(scalar(@created), 1, "create a ZZ directory");
3376a30f 413
414 local @ARGV = ($dir);
415 rmtree( [grep -e $_, @ARGV], 0, 0 );
416 ok(!-e $dir, "blow it away via \@ARGV");
417}
418
12c2e016 419SKIP: {
cd117d8b 420 skip 'Test::Output not available', 14
12c2e016 421 unless $has_Test_Output;
422
423 SKIP: {
424 $dir = catdir('EXTRA', '3');
538f81fb 425 skip "extra scenarios not set up, see eg/setup-extra-tests", 3
12c2e016 426 unless -e $dir;
427
cd117d8b 428 $dir = catdir('EXTRA', '3', 'U');
429 stderr_like(
430 sub {rmtree($dir, {verbose => 0})},
0b3d36bd 431 qr{\Acannot make child directory read-write-exec for [^:]+: .* at \S+ line \d+},
432 q(rmtree can't chdir into root dir)
cd117d8b 433 );
434
435 $dir = catdir('EXTRA', '3');
12c2e016 436 stderr_like(
437 sub {rmtree($dir, {})},
0b3d36bd 438 qr{\Acannot make child directory read-write-exec for [^:]+: .* at (\S+) line (\d+)
439cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
440cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
441cannot remove directory for [^:]+: .* at \1 line \2},
12c2e016 442 'rmtree with file owned by root'
443 );
444
445 stderr_like(
446 sub {rmtree('EXTRA', {})},
0b3d36bd 447 qr{\Acannot remove directory for [^:]+: .* at (\S+) line (\d+)
448cannot remove directory for [^:]+: .* at \1 line \2
449cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
450cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
451cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
452cannot remove directory for [^:]+: .* at \1 line \2
453cannot unlink file for [^:]+: .* at \1 line \2
454cannot restore permissions to \d+ for [^:]+: .* at \1 line \2
455cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
456cannot remove directory for [^:]+: .* at \1 line \2
457cannot restore permissions to \d+ for [^:]+: .* at \1 line \2},
12c2e016 458 'rmtree with insufficient privileges'
459 );
460 }
461
462 my $base = catdir($tmp_base,'output');
463 $dir = catdir($base,'A');
464 $dir2 = catdir($base,'B');
465
466 stderr_like(
3376a30f 467 sub { rmtree( undef, 1 ) },
12c2e016 468 qr/\ANo root path\(s\) specified\b/,
469 "rmtree of nothing carps sensibly"
470 );
471
cd117d8b 472 stderr_like(
473 sub { rmtree( '', 1 ) },
474 qr/\ANo root path\(s\) specified\b/,
475 "rmtree of empty dir carps sensibly"
476 );
477
3f083399 478 stderr_is( sub { make_path() }, '', "make_path no args does not carp" );
479 stderr_is( sub { remove_tree() }, '', "remove_tree no args does not carp" );
cd117d8b 480
12c2e016 481 stdout_is(
482 sub {@created = mkpath($dir, 1)},
483 "mkdir $base\nmkdir $dir\n",
484 'mkpath verbose (old style 1)'
485 );
486
487 stdout_is(
488 sub {@created = mkpath([$dir2], 1)},
489 "mkdir $dir2\n",
490 'mkpath verbose (old style 2)'
491 );
492
493 stdout_is(
494 sub {$count = rmtree([$dir, $dir2], 1, 1)},
495 "rmdir $dir\nrmdir $dir2\n",
496 'rmtree verbose (old style)'
497 );
498
499 stdout_is(
500 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
501 "mkdir $dir\n",
502 'mkpath verbose (new style 1)'
503 );
504
505 stdout_is(
506 sub {@created = mkpath($dir2, 1, 0771)},
507 "mkdir $dir2\n",
508 'mkpath verbose (new style 2)'
509 );
510
511 SKIP: {
512 $file = catdir($dir2, "file");
513 skip "Cannot create $file", 2 unless open OUT, "> $file";
514 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
515 close OUT;
516
517 ok(-e $file, "file created in directory");
518
519 stdout_is(
520 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
521 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
522 'rmtree safe verbose (new style)'
523 );
524 }
525}
526
527SKIP: {
0b3d36bd 528 skip "extra scenarios not set up, see eg/setup-extra-tests", 11
12c2e016 529 unless -d catdir(qw(EXTRA 1));
530
531 rmtree 'EXTRA', {safe => 0, error => \$error};
0b3d36bd 532 is( scalar(@$error), 11, 'seven deadly sins' ); # well there used to be 7
12c2e016 533
534 rmtree 'EXTRA', {safe => 1, error => \$error};
0b3d36bd 535 is( scalar(@$error), 9, 'safe is better' );
12c2e016 536 for (@$error) {
537 ($file, $message) = each %$_;
538 if ($file =~ /[123]\z/) {
0b3d36bd 539 is(index($message, 'cannot remove directory: '), 0, "failed to remove $file with rmdir")
12c2e016 540 or diag($message);
541 }
542 else {
0b3d36bd 543 like($message, qr(\Acannot (?:restore permissions to \d+|chdir to child|unlink file): ), "failed to remove $file with unlink")
544 or diag($message)
12c2e016 545 }
546 }
547}
548
0e5b5e32 549SKIP: {
550 my $nr_tests = 6;
551 my $cwd = getcwd() or skip "failed to getcwd: $!", $nr_tests;
552 rmtree($tmp_base, {result => \$list} );
553 is(ref($list), 'ARRAY', "received a final list of results");
554 ok( !(-d $tmp_base), "test base directory gone" );
555
556 my $p = getcwd();
557 my $x = "x$$";
558 my $xx = $x . "x";
559
560 # setup
561 ok(mkpath($xx));
562 ok(chdir($xx));
563 END {
564 ok(chdir($p));
565 ok(rmtree($xx));
566 }
567
568 # create and delete directory
569 my $px = catdir($p, $x);
570 ok(mkpath($px));
571 ok(rmtree($px), "rmtree"); # fails in File-Path-2.07
572}