Update File-Path to 2.00
[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 => 72;
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 # first check for stupid permissions second for full, so we clean up
16 # behind ourselves
17 for my $perm (0111,0777) {
18     my $path = catdir(curdir(), "mhx", "bar");
19     mkpath($path);
20     chmod $perm, "mhx", $path;
21
22     my $oct = sprintf('0%o', $perm);
23     ok(-d "mhx", "mkdir parent dir $oct");
24     ok(-d $path, "mkdir child dir $oct");
25
26     rmtree("mhx");
27     ok(! -e "mhx", "mhx does not exist $oct");
28 }
29
30 # find a place to work
31 my ($error, $list, $file, $message);
32 my $tmp_base = catdir(
33     curdir(),
34     sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
35 );
36
37 # invent some names
38 my @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
46 my @created = mkpath(@dir);
47
48 is(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]);
52 is(scalar(@created), 0, "skipped making existing directory")
53     or diag("unexpectedly recreated @created");
54
55 @created = mkpath('');
56 is(scalar(@created), 0, "Can't create a directory named ''");
57
58 my $dir;
59 my $dir2;
60
61 SKIP: {
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;
68         
69     @created = mkpath($dir2, {mask => 0700});
70     is(scalar(@created), 1, "make directory with trailing parent segment");
71     is($created[0], $dir, "made parent");
72 };
73
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' );
81
82 @created = mkpath($tmp_base, 0);
83 is(scalar(@created), 0, "skipped making existing directories (old style 1)")
84     or diag("unexpectedly recreated @created");
85
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");
90
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");
94
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");
99
100 $count = rmtree($dir, 0);
101 is($count, 1, "removed directory (old style 1)");
102
103 $count = rmtree($dir2, 0, 1);
104 is($count, 1, "removed directory (old style 2)");
105
106 # mkdir foo ./E/../Y
107 # Y should exist
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" );
114
115 @created = mkpath(catdir(curdir(), $tmp_base));
116 is(scalar(@created), 0, "nothing created")
117     or diag(@created);
118
119 $dir  = catdir($tmp_base, 'a');
120 $dir2 = catdir($tmp_base, 'z');
121
122 rmtree( $dir, $dir2,
123     {
124         error     => \$error,
125         result    => \$list,
126         keep_root => 1,
127     }
128 );
129
130 is(scalar(@$error), 0, "no errors unlinking a and z");
131 is(scalar(@$list),  4, "list contains 4 elements")
132     or diag("@$list");
133
134 ok(-d $dir,  "dir a still exists");
135 ok(-d $dir2, "dir z still exists");
136
137 # borderline new-style heuristics
138 if (chdir $tmp_base) {
139     pass("chdir to temp dir");
140 }
141 else {
142     fail("chdir to temp dir: $!");
143 }
144
145 $dir   = catdir('a', 'd1');
146 $dir2  = catdir('a', 'd2');
147
148 @created = mkpath( $dir, 0, $dir2 );
149 is(scalar @created, 3, 'new-style 3 dirs created');
150
151 $count = rmtree( $dir, 0, $dir2, );
152 is($count, 3, 'new-style 3 dirs removed');
153
154 @created = mkpath( $dir, $dir2, 1 );
155 is(scalar @created, 3, 'new-style 3 dirs created (redux)');
156
157 $count = rmtree( $dir, $dir2, 1 );
158 is($count, 3, 'new-style 3 dirs removed (redux)');
159
160 @created = mkpath( $dir, $dir2 );
161 is(scalar @created, 2, 'new-style 2 dirs created');
162
163 $count = rmtree( $dir, $dir2 );
164 is($count, 2, 'new-style 2 dirs removed');
165
166 if (chdir updir()) {
167     pass("chdir parent");
168 }
169 else {
170     fail("chdir parent: $!");
171 }
172
173 # see what happens if a file exists where we want a directory
174 SKIP: {
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";
178     close OUT;
179     ok(-e $entry, "file exists in place of directory");
180
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");
185
186     eval {@created = mkpath($entry, 0, 0700)};
187     $error = $@;
188     chomp $error; # just to remove silly # in TAP output
189     cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
190         or diag(@created);
191 }
192
193 my $extra =  catdir(curdir(), qw(EXTRA 1 a));
194
195 SKIP: {
196     skip "extra scenarios not set up, see eg/setup-extra-tests", 8
197         unless -e $extra;
198
199     my ($list, $err);
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");
204
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} );
209
210     $dir = catdir('EXTRA', '3', 'S');
211     rmtree($dir, {error => \$error});
212     is( scalar(@$error), 2, 'two errors for an unreadable dir' );
213
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' );
220 }
221
222 SKIP: {
223     skip 'Test::Output not available', 10
224         unless $has_Test_Output;
225
226     SKIP: {
227         $dir = catdir('EXTRA', '3');
228         skip "extra scenarios not set up, see eg/setup-extra-tests", 2
229             unless -e $dir;
230
231         stderr_like( 
232             sub {rmtree($dir, {})},
233             qr{\ACan't remove directory \S+: .*? at \S+ line \d+\n},
234             'rmtree with file owned by root'
235         );
236
237         stderr_like( 
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+
244  at \S+ line \d+},
245             'rmtree with insufficient privileges'
246         );
247     }
248
249     my $base = catdir($tmp_base,'output');
250     $dir  = catdir($base,'A');
251     $dir2 = catdir($base,'B');
252
253     stderr_like(
254         \&rmtree,
255         qr/\ANo root path\(s\) specified\b/,
256         "rmtree of nothing carps sensibly"
257     );
258
259     stdout_is(
260         sub {@created = mkpath($dir, 1)},
261         "mkdir $base\nmkdir $dir\n",
262         'mkpath verbose (old style 1)'
263     );
264
265     stdout_is(
266         sub {@created = mkpath([$dir2], 1)},
267         "mkdir $dir2\n",
268         'mkpath verbose (old style 2)'
269     );
270
271     stdout_is(
272         sub {$count = rmtree([$dir, $dir2], 1, 1)},
273         "rmdir $dir\nrmdir $dir2\n",
274         'rmtree verbose (old style)'
275     );
276
277     stdout_is(
278         sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
279         "mkdir $dir\n",
280         'mkpath verbose (new style 1)'
281     );
282
283     stdout_is(
284         sub {@created = mkpath($dir2, 1, 0771)},
285         "mkdir $dir2\n",
286         'mkpath verbose (new style 2)'
287     );
288
289     SKIP: {
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";
293         close OUT;
294
295         ok(-e $file, "file created in directory");
296
297         stdout_is(
298             sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
299             "rmdir $dir\nunlink $file\nrmdir $dir2\n",
300             'rmtree safe verbose (new style)'
301         );
302     }
303 }
304
305 SKIP: {
306     skip "extra scenarios not set up, see eg/setup-extra-tests", 6
307         unless -d catdir(qw(EXTRA 1));
308
309     rmtree 'EXTRA', {safe => 0, error => \$error};
310     is( scalar(@$error), 7, 'seven deadly sins' );
311
312     rmtree 'EXTRA', {safe => 1, error => \$error};
313     is( scalar(@$error), 4, 'safe is better' );
314     for (@$error) {
315         ($file, $message) = each %$_;
316         if ($file =~  /[123]\z/) {
317             is(index($message, 'rmdir: '), 0, "failed to remove $file with rmdir")
318                 or diag($message);
319         }
320         else {
321             is(index($message, 'unlink: '), 0, "failed to remove $file with unlink")
322                 or diag($message);
323         }
324     }
325 }
326
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" );