More tests for when fieldhash magic (doesn't) trigger.
[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
0b3d36bd 5use Test::More tests => 93;
1a3850a5 6
12c2e016 7BEGIN {
8 use_ok('File::Path');
9 use_ok('File::Spec::Functions');
10}
11
12eval "use Test::Output";
13my $has_Test_Output = $@ ? 0 : 1;
1a3850a5 14
037c8c09 15# first check for stupid permissions second for full, so we clean up
16# behind ourselves
17for my $perm (0111,0777) {
e7780b56 18 my $path = catdir(curdir(), "mhx", "bar");
d5201bd2 19 mkpath($path);
e7780b56 20 chmod $perm, "mhx", $path;
1a3850a5 21
12c2e016 22 my $oct = sprintf('0%o', $perm);
23 ok(-d "mhx", "mkdir parent dir $oct");
24 ok(-d $path, "mkdir child dir $oct");
1a3850a5 25
e7780b56 26 rmtree("mhx");
12c2e016 27 ok(! -e "mhx", "mhx does not exist $oct");
28}
29
30# find a place to work
31my ($error, $list, $file, $message);
32my $tmp_base = catdir(
33 curdir(),
34 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
35);
36
37# invent some names
38my @dir = (
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)),
43);
44
45# create them
46my @created = mkpath(@dir);
47
48is(scalar(@created), 7, "created list of directories");
49
50# pray for no race conditions blowing them out from under us
51@created = mkpath([$tmp_base]);
52is(scalar(@created), 0, "skipped making existing directory")
53 or diag("unexpectedly recreated @created");
54
55@created = mkpath('');
56is(scalar(@created), 0, "Can't create a directory named ''");
57
58my $dir;
59my $dir2;
60
61SKIP: {
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
91c4f65e 67 if $dir2 eq $tmp_base
68 or $^O eq 'cygwin';
12c2e016 69
70 @created = mkpath($dir2, {mask => 0700});
71 is(scalar(@created), 1, "make directory with trailing parent segment");
72 is($created[0], $dir, "made parent");
73};
74
75my $count = rmtree({error => \$error});
76is( $count, 0, 'rmtree of nothing, count of zero' );
3376a30f 77is( scalar(@$error), 0, 'no diagnostic captured' );
12c2e016 78
79@created = mkpath($tmp_base, 0);
80is(scalar(@created), 0, "skipped making existing directories (old style 1)")
81 or diag("unexpectedly recreated @created");
82
83$dir = catdir($tmp_base,'C');
fa06c9c1 84# mkpath returns unix syntax filespecs on VMS
85$dir = VMS::Filespec::unixify($dir) if $^O eq 'VMS';
12c2e016 86@created = mkpath($tmp_base, $dir);
87is(scalar(@created), 1, "created directory (new style 1)");
88is($created[0], $dir, "created directory (new style 1) cross-check");
89
90@created = mkpath($tmp_base, 0, 0700);
91is(scalar(@created), 0, "skipped making existing directories (old style 2)")
92 or diag("unexpectedly recreated @created");
93
94$dir2 = catdir($tmp_base,'D');
fa06c9c1 95# mkpath returns unix syntax filespecs on VMS
96$dir2 = VMS::Filespec::unixify($dir2) if $^O eq 'VMS';
12c2e016 97@created = mkpath($tmp_base, $dir, $dir2);
98is(scalar(@created), 1, "created directory (new style 2)");
99is($created[0], $dir2, "created directory (new style 2) cross-check");
100
101$count = rmtree($dir, 0);
102is($count, 1, "removed directory (old style 1)");
103
104$count = rmtree($dir2, 0, 1);
105is($count, 1, "removed directory (old style 2)");
106
107# mkdir foo ./E/../Y
108# Y should exist
109# existence of E is neither here nor there
110$dir = catdir($tmp_base, 'E', updir(), 'Y');
111@created =mkpath($dir);
112cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
113cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
114ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
115
116@created = mkpath(catdir(curdir(), $tmp_base));
117is(scalar(@created), 0, "nothing created")
118 or diag(@created);
119
120$dir = catdir($tmp_base, 'a');
121$dir2 = catdir($tmp_base, 'z');
122
123rmtree( $dir, $dir2,
124 {
125 error => \$error,
126 result => \$list,
127 keep_root => 1,
128 }
129);
130
131is(scalar(@$error), 0, "no errors unlinking a and z");
132is(scalar(@$list), 4, "list contains 4 elements")
133 or diag("@$list");
134
135ok(-d $dir, "dir a still exists");
136ok(-d $dir2, "dir z still exists");
137
cd117d8b 138$dir = catdir($tmp_base,'F');
181b7e95 139# mkpath returns unix syntax filespecs on VMS
140$dir = VMS::Filespec::unixify($dir) if $^O eq 'VMS';
cd117d8b 141
142@created = mkpath($dir, undef, 0770);
143is(scalar(@created), 1, "created directory (old style 2 verbose undef)");
144is($created[0], $dir, "created directory (old style 2 verbose undef) cross-check");
145is(rmtree($dir, undef, 0), 1, "removed directory 2 verbose undef");
146
147@created = mkpath($dir, undef);
148is(scalar(@created), 1, "created directory (old style 2a verbose undef)");
149is($created[0], $dir, "created directory (old style 2a verbose undef) cross-check");
150is(rmtree($dir, undef), 1, "removed directory 2a verbose undef");
151
152@created = mkpath($dir, 0, undef);
153is(scalar(@created), 1, "created directory (old style 3 mode undef)");
154is($created[0], $dir, "created directory (old style 3 mode undef) cross-check");
155is(rmtree($dir, 0, undef), 1, "removed directory 3 verbose undef");
156
0b3d36bd 157$dir = catdir($tmp_base,'G');
158$dir = VMS::Filespec::unixify($dir) if $^O eq 'VMS';
159
160@created = mkpath($dir, undef, 0200);
161is(scalar(@created), 1, "created write-only dir");
162is($created[0], $dir, "created write-only directory cross-check");
163is(rmtree($dir), 1, "removed write-only dir");
164
12c2e016 165# borderline new-style heuristics
166if (chdir $tmp_base) {
167 pass("chdir to temp dir");
168}
169else {
170 fail("chdir to temp dir: $!");
037c8c09 171}
12c2e016 172
173$dir = catdir('a', 'd1');
174$dir2 = catdir('a', 'd2');
175
176@created = mkpath( $dir, 0, $dir2 );
177is(scalar @created, 3, 'new-style 3 dirs created');
178
179$count = rmtree( $dir, 0, $dir2, );
180is($count, 3, 'new-style 3 dirs removed');
181
182@created = mkpath( $dir, $dir2, 1 );
183is(scalar @created, 3, 'new-style 3 dirs created (redux)');
184
185$count = rmtree( $dir, $dir2, 1 );
186is($count, 3, 'new-style 3 dirs removed (redux)');
187
188@created = mkpath( $dir, $dir2 );
189is(scalar @created, 2, 'new-style 2 dirs created');
190
191$count = rmtree( $dir, $dir2 );
192is($count, 2, 'new-style 2 dirs removed');
193
194if (chdir updir()) {
195 pass("chdir parent");
196}
197else {
198 fail("chdir parent: $!");
199}
200
201# see what happens if a file exists where we want a directory
202SKIP: {
203 my $entry = catdir($tmp_base, "file");
204 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
205 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
206 close OUT;
207 ok(-e $entry, "file exists in place of directory");
208
209 mkpath( $entry, {error => \$error} );
210 is( scalar(@$error), 1, "caught error condition" );
211 ($file, $message) = each %{$error->[0]};
212 is( $entry, $file, "and the message is: $message");
213
214 eval {@created = mkpath($entry, 0, 0700)};
215 $error = $@;
216 chomp $error; # just to remove silly # in TAP output
217 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
218 or diag(@created);
219}
220
221my $extra = catdir(curdir(), qw(EXTRA 1 a));
222
223SKIP: {
0b3d36bd 224 skip "extra scenarios not set up, see eg/setup-extra-tests", 9
12c2e016 225 unless -e $extra;
226
227 my ($list, $err);
228 $dir = catdir( 'EXTRA', '1' );
229 rmtree( $dir, {result => \$list, error => \$err} );
230 is(scalar(@$list), 2, "extra dir $dir removed");
231 is(scalar(@$err), 1, "one error encountered");
232
233 $dir = catdir( 'EXTRA', '3', 'N' );
234 rmtree( $dir, {result => \$list, error => \$err} );
235 is( @$list, 1, q{remove a symlinked dir} );
236 is( @$err, 0, q{with no errors} );
237
238 $dir = catdir('EXTRA', '3', 'S');
239 rmtree($dir, {error => \$error});
0b3d36bd 240 is( scalar(@$error), 1, 'one error for an unreadable dir' );
12c2e016 241
cd117d8b 242 $dir = catdir('EXTRA', '3', 'T');
243 rmtree($dir, {error => \$error});
0b3d36bd 244 is( scalar(@$error), 1, 'one error for an unreadable dir' );
cd117d8b 245
12c2e016 246 $dir = catdir( 'EXTRA', '4' );
247 rmtree($dir, {result => \$list, error => \$err} );
248 is( @$list, 0, q{don't follow a symlinked dir} );
0b3d36bd 249 is( @$err, 2, q{two errors when removing a symlink in r/o dir} );
12c2e016 250 eval { ($file, $message) = each %{$err->[0]} };
251 is( $file, $dir, 'symlink reported in error' );
252}
253
3376a30f 254{
d2f50e7f 255 $dir = catdir($tmp_base, 'ZZ');
3376a30f 256 @created = mkpath($dir);
d2f50e7f 257 is(scalar(@created), 1, "create a ZZ directory");
3376a30f 258
259 local @ARGV = ($dir);
260 rmtree( [grep -e $_, @ARGV], 0, 0 );
261 ok(!-e $dir, "blow it away via \@ARGV");
262}
263
12c2e016 264SKIP: {
cd117d8b 265 skip 'Test::Output not available', 14
12c2e016 266 unless $has_Test_Output;
267
268 SKIP: {
269 $dir = catdir('EXTRA', '3');
538f81fb 270 skip "extra scenarios not set up, see eg/setup-extra-tests", 3
12c2e016 271 unless -e $dir;
272
cd117d8b 273 $dir = catdir('EXTRA', '3', 'U');
274 stderr_like(
275 sub {rmtree($dir, {verbose => 0})},
0b3d36bd 276 qr{\Acannot make child directory read-write-exec for [^:]+: .* at \S+ line \d+},
277 q(rmtree can't chdir into root dir)
cd117d8b 278 );
279
280 $dir = catdir('EXTRA', '3');
12c2e016 281 stderr_like(
282 sub {rmtree($dir, {})},
0b3d36bd 283 qr{\Acannot make child directory read-write-exec for [^:]+: .* at (\S+) line (\d+)
284cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
285cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
286cannot remove directory for [^:]+: .* at \1 line \2},
12c2e016 287 'rmtree with file owned by root'
288 );
289
290 stderr_like(
291 sub {rmtree('EXTRA', {})},
0b3d36bd 292 qr{\Acannot remove directory for [^:]+: .* at (\S+) line (\d+)
293cannot remove directory for [^:]+: .* at \1 line \2
294cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
295cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
296cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
297cannot remove directory for [^:]+: .* at \1 line \2
298cannot unlink file for [^:]+: .* at \1 line \2
299cannot restore permissions to \d+ for [^:]+: .* at \1 line \2
300cannot make child directory read-write-exec for [^:]+: .* at \1 line \2
301cannot remove directory for [^:]+: .* at \1 line \2
302cannot restore permissions to \d+ for [^:]+: .* at \1 line \2},
12c2e016 303 'rmtree with insufficient privileges'
304 );
305 }
306
307 my $base = catdir($tmp_base,'output');
308 $dir = catdir($base,'A');
309 $dir2 = catdir($base,'B');
310
311 stderr_like(
3376a30f 312 sub { rmtree( undef, 1 ) },
12c2e016 313 qr/\ANo root path\(s\) specified\b/,
314 "rmtree of nothing carps sensibly"
315 );
316
cd117d8b 317 stderr_like(
318 sub { rmtree( '', 1 ) },
319 qr/\ANo root path\(s\) specified\b/,
320 "rmtree of empty dir carps sensibly"
321 );
322
323 stderr_is( sub { mkpath() }, '', "mkpath no args does not carp" );
324 stderr_is( sub { rmtree() }, '', "rmtree no args does not carp" );
325
12c2e016 326 stdout_is(
327 sub {@created = mkpath($dir, 1)},
328 "mkdir $base\nmkdir $dir\n",
329 'mkpath verbose (old style 1)'
330 );
331
332 stdout_is(
333 sub {@created = mkpath([$dir2], 1)},
334 "mkdir $dir2\n",
335 'mkpath verbose (old style 2)'
336 );
337
338 stdout_is(
339 sub {$count = rmtree([$dir, $dir2], 1, 1)},
340 "rmdir $dir\nrmdir $dir2\n",
341 'rmtree verbose (old style)'
342 );
343
344 stdout_is(
345 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
346 "mkdir $dir\n",
347 'mkpath verbose (new style 1)'
348 );
349
350 stdout_is(
351 sub {@created = mkpath($dir2, 1, 0771)},
352 "mkdir $dir2\n",
353 'mkpath verbose (new style 2)'
354 );
355
356 SKIP: {
357 $file = catdir($dir2, "file");
358 skip "Cannot create $file", 2 unless open OUT, "> $file";
359 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
360 close OUT;
361
362 ok(-e $file, "file created in directory");
363
364 stdout_is(
365 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
366 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
367 'rmtree safe verbose (new style)'
368 );
369 }
370}
371
372SKIP: {
0b3d36bd 373 skip "extra scenarios not set up, see eg/setup-extra-tests", 11
12c2e016 374 unless -d catdir(qw(EXTRA 1));
375
376 rmtree 'EXTRA', {safe => 0, error => \$error};
0b3d36bd 377 is( scalar(@$error), 11, 'seven deadly sins' ); # well there used to be 7
12c2e016 378
379 rmtree 'EXTRA', {safe => 1, error => \$error};
0b3d36bd 380 is( scalar(@$error), 9, 'safe is better' );
12c2e016 381 for (@$error) {
382 ($file, $message) = each %$_;
383 if ($file =~ /[123]\z/) {
0b3d36bd 384 is(index($message, 'cannot remove directory: '), 0, "failed to remove $file with rmdir")
12c2e016 385 or diag($message);
386 }
387 else {
0b3d36bd 388 like($message, qr(\Acannot (?:restore permissions to \d+|chdir to child|unlink file): ), "failed to remove $file with unlink")
389 or diag($message)
12c2e016 390 }
391 }
392}
393
394rmtree($tmp_base, {result => \$list} );
395is(ref($list), 'ARRAY', "received a final list of results");
396ok( !(-d $tmp_base), "test base directory gone" );