1 # Path.t -- tests for module File::Path
5 use Test::More tests => 72;
9 use_ok('File::Spec::Functions');
12 eval "use Test::Output";
13 my $has_Test_Output = $@ ? 0 : 1;
15 # first check for stupid permissions second for full, so we clean up
17 for my $perm (0111,0777) {
18 my $path = catdir(curdir(), "mhx", "bar");
20 chmod $perm, "mhx", $path;
22 my $oct = sprintf('0%o', $perm);
23 ok(-d "mhx", "mkdir parent dir $oct");
24 ok(-d $path, "mkdir child dir $oct");
27 ok(! -e "mhx", "mhx does not exist $oct");
30 # find a place to work
31 my ($error, $list, $file, $message);
32 my $tmp_base = catdir(
34 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
39 catdir($tmp_base, qw(a b)),
40 catdir($tmp_base, qw(a c)),
41 catdir($tmp_base, qw(z b)),
42 catdir($tmp_base, qw(z c)),
46 my @created = mkpath(@dir);
48 is(scalar(@created), 7, "created list of directories");
50 # pray for no race conditions blowing them out from under us
51 @created = mkpath([$tmp_base]);
52 is(scalar(@created), 0, "skipped making existing directory")
53 or diag("unexpectedly recreated @created");
55 @created = mkpath('');
56 is(scalar(@created), 0, "Can't create a directory named ''");
62 $dir = catdir($tmp_base, 'B');
63 $dir2 = catdir($dir, updir());
64 # IOW: File::Spec->catdir( qw(foo bar), File::Spec->updir ) eq 'foo'
65 # rather than foo/bar/..
66 skip "updir() canonicalises path on this platform", 2
67 if $dir2 eq $tmp_base;
69 @created = mkpath($dir2, {mask => 0700});
70 is(scalar(@created), 1, "make directory with trailing parent segment");
71 is($created[0], $dir, "made parent");
74 my $count = rmtree({error => \$error});
75 is( $count, 0, 'rmtree of nothing, count of zero' );
76 is( scalar(@$error), 1, 'one diagnostic captureed' );
77 eval { ($file, $message) = each %{$error->[0]} }; # too early to die, just in case
78 is( $@, '', 'decoded diagnostic' );
79 is( $file, '', 'general diagnostic' );
80 is( $message, 'No root path(s) specified', 'expected diagnostic received' );
82 @created = mkpath($tmp_base, 0);
83 is(scalar(@created), 0, "skipped making existing directories (old style 1)")
84 or diag("unexpectedly recreated @created");
86 $dir = catdir($tmp_base,'C');
87 @created = mkpath($tmp_base, $dir);
88 is(scalar(@created), 1, "created directory (new style 1)");
89 is($created[0], $dir, "created directory (new style 1) cross-check");
91 @created = mkpath($tmp_base, 0, 0700);
92 is(scalar(@created), 0, "skipped making existing directories (old style 2)")
93 or diag("unexpectedly recreated @created");
95 $dir2 = catdir($tmp_base,'D');
96 @created = mkpath($tmp_base, $dir, $dir2);
97 is(scalar(@created), 1, "created directory (new style 2)");
98 is($created[0], $dir2, "created directory (new style 2) cross-check");
100 $count = rmtree($dir, 0);
101 is($count, 1, "removed directory (old style 1)");
103 $count = rmtree($dir2, 0, 1);
104 is($count, 1, "removed directory (old style 2)");
108 # existence of E is neither here nor there
109 $dir = catdir($tmp_base, 'E', updir(), 'Y');
110 @created =mkpath($dir);
111 cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
112 cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
113 ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
115 @created = mkpath(catdir(curdir(), $tmp_base));
116 is(scalar(@created), 0, "nothing created")
119 $dir = catdir($tmp_base, 'a');
120 $dir2 = catdir($tmp_base, 'z');
130 is(scalar(@$error), 0, "no errors unlinking a and z");
131 is(scalar(@$list), 4, "list contains 4 elements")
134 ok(-d $dir, "dir a still exists");
135 ok(-d $dir2, "dir z still exists");
137 # borderline new-style heuristics
138 if (chdir $tmp_base) {
139 pass("chdir to temp dir");
142 fail("chdir to temp dir: $!");
145 $dir = catdir('a', 'd1');
146 $dir2 = catdir('a', 'd2');
148 @created = mkpath( $dir, 0, $dir2 );
149 is(scalar @created, 3, 'new-style 3 dirs created');
151 $count = rmtree( $dir, 0, $dir2, );
152 is($count, 3, 'new-style 3 dirs removed');
154 @created = mkpath( $dir, $dir2, 1 );
155 is(scalar @created, 3, 'new-style 3 dirs created (redux)');
157 $count = rmtree( $dir, $dir2, 1 );
158 is($count, 3, 'new-style 3 dirs removed (redux)');
160 @created = mkpath( $dir, $dir2 );
161 is(scalar @created, 2, 'new-style 2 dirs created');
163 $count = rmtree( $dir, $dir2 );
164 is($count, 2, 'new-style 2 dirs removed');
167 pass("chdir parent");
170 fail("chdir parent: $!");
173 # see what happens if a file exists where we want a directory
175 my $entry = catdir($tmp_base, "file");
176 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
177 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
179 ok(-e $entry, "file exists in place of directory");
181 mkpath( $entry, {error => \$error} );
182 is( scalar(@$error), 1, "caught error condition" );
183 ($file, $message) = each %{$error->[0]};
184 is( $entry, $file, "and the message is: $message");
186 eval {@created = mkpath($entry, 0, 0700)};
188 chomp $error; # just to remove silly # in TAP output
189 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
193 my $extra = catdir(curdir(), qw(EXTRA 1 a));
196 skip "extra scenarios not set up, see eg/setup-extra-tests", 8
200 $dir = catdir( 'EXTRA', '1' );
201 rmtree( $dir, {result => \$list, error => \$err} );
202 is(scalar(@$list), 2, "extra dir $dir removed");
203 is(scalar(@$err), 1, "one error encountered");
205 $dir = catdir( 'EXTRA', '3', 'N' );
206 rmtree( $dir, {result => \$list, error => \$err} );
207 is( @$list, 1, q{remove a symlinked dir} );
208 is( @$err, 0, q{with no errors} );
210 $dir = catdir('EXTRA', '3', 'S');
211 rmtree($dir, {error => \$error});
212 is( scalar(@$error), 2, 'two errors for an unreadable dir' );
214 $dir = catdir( 'EXTRA', '4' );
215 rmtree($dir, {result => \$list, error => \$err} );
216 is( @$list, 0, q{don't follow a symlinked dir} );
217 is( @$err, 1, q{one error when removing a symlink in r/o dir} );
218 eval { ($file, $message) = each %{$err->[0]} };
219 is( $file, $dir, 'symlink reported in error' );
223 skip 'Test::Output not available', 10
224 unless $has_Test_Output;
227 $dir = catdir('EXTRA', '3');
228 skip "extra scenarios not set up, see eg/setup-extra-tests", 2
232 sub {rmtree($dir, {})},
233 qr{\ACan't remove directory \S+: .*? at \S+ line \d+\n},
234 'rmtree with file owned by root'
238 sub {rmtree('EXTRA', {})},
239 qr{\ACan't make directory EXTRA read\+writeable: .*? at \S+ line \d+
240 (?:Can't remove directory EXTRA/\d: .*? at \S+ line \d+
241 )+Can't unlink file [^:]+: .*? at \S+ line \d+
242 Can't remove directory EXTRA: .*? at \S+ line \d+
243 and can't restore permissions to \d+
245 'rmtree with insufficient privileges'
249 my $base = catdir($tmp_base,'output');
250 $dir = catdir($base,'A');
251 $dir2 = catdir($base,'B');
255 qr/\ANo root path\(s\) specified\b/,
256 "rmtree of nothing carps sensibly"
260 sub {@created = mkpath($dir, 1)},
261 "mkdir $base\nmkdir $dir\n",
262 'mkpath verbose (old style 1)'
266 sub {@created = mkpath([$dir2], 1)},
268 'mkpath verbose (old style 2)'
272 sub {$count = rmtree([$dir, $dir2], 1, 1)},
273 "rmdir $dir\nrmdir $dir2\n",
274 'rmtree verbose (old style)'
278 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
280 'mkpath verbose (new style 1)'
284 sub {@created = mkpath($dir2, 1, 0771)},
286 'mkpath verbose (new style 2)'
290 $file = catdir($dir2, "file");
291 skip "Cannot create $file", 2 unless open OUT, "> $file";
292 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
295 ok(-e $file, "file created in directory");
298 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
299 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
300 'rmtree safe verbose (new style)'
306 skip "extra scenarios not set up, see eg/setup-extra-tests", 6
307 unless -d catdir(qw(EXTRA 1));
309 rmtree 'EXTRA', {safe => 0, error => \$error};
310 is( scalar(@$error), 7, 'seven deadly sins' );
312 rmtree 'EXTRA', {safe => 1, error => \$error};
313 is( scalar(@$error), 4, 'safe is better' );
315 ($file, $message) = each %$_;
316 if ($file =~ /[123]\z/) {
317 is(index($message, 'rmdir: '), 0, "failed to remove $file with rmdir")
321 is(index($message, 'unlink: '), 0, "failed to remove $file with unlink")
327 rmtree($tmp_base, {result => \$list} );
328 is(ref($list), 'ARRAY', "received a final list of results");
329 ok( !(-d $tmp_base), "test base directory gone" );