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