427ef250ce4eccea3585ff7b6f27c35919d5107f
[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 => 71;
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             or $^O eq 'cygwin';
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
75 my $count = rmtree({error => \$error});
76 is( $count, 0, 'rmtree of nothing, count of zero' );
77 is( scalar(@$error), 0, 'no diagnostic captured' );
78
79 @created = mkpath($tmp_base, 0);
80 is(scalar(@created), 0, "skipped making existing directories (old style 1)")
81     or diag("unexpectedly recreated @created");
82
83 $dir = catdir($tmp_base,'C');
84 @created = mkpath($tmp_base, $dir);
85 is(scalar(@created), 1, "created directory (new style 1)");
86 is($created[0], $dir, "created directory (new style 1) cross-check");
87
88 @created = mkpath($tmp_base, 0, 0700);
89 is(scalar(@created), 0, "skipped making existing directories (old style 2)")
90     or diag("unexpectedly recreated @created");
91
92 $dir2 = catdir($tmp_base,'D');
93 @created = mkpath($tmp_base, $dir, $dir2);
94 is(scalar(@created), 1, "created directory (new style 2)");
95 is($created[0], $dir2, "created directory (new style 2) cross-check");
96
97 $count = rmtree($dir, 0);
98 is($count, 1, "removed directory (old style 1)");
99
100 $count = rmtree($dir2, 0, 1);
101 is($count, 1, "removed directory (old style 2)");
102
103 # mkdir foo ./E/../Y
104 # Y should exist
105 # existence of E is neither here nor there
106 $dir = catdir($tmp_base, 'E', updir(), 'Y');
107 @created =mkpath($dir);
108 cmp_ok(scalar(@created), '>=', 1, "made one or more dirs because of ..");
109 cmp_ok(scalar(@created), '<=', 2, "made less than two dirs because of ..");
110 ok( -d catdir($tmp_base, 'Y'), "directory after parent" );
111
112 @created = mkpath(catdir(curdir(), $tmp_base));
113 is(scalar(@created), 0, "nothing created")
114     or diag(@created);
115
116 $dir  = catdir($tmp_base, 'a');
117 $dir2 = catdir($tmp_base, 'z');
118
119 rmtree( $dir, $dir2,
120     {
121         error     => \$error,
122         result    => \$list,
123         keep_root => 1,
124     }
125 );
126
127 is(scalar(@$error), 0, "no errors unlinking a and z");
128 is(scalar(@$list),  4, "list contains 4 elements")
129     or diag("@$list");
130
131 ok(-d $dir,  "dir a still exists");
132 ok(-d $dir2, "dir z still exists");
133
134 # borderline new-style heuristics
135 if (chdir $tmp_base) {
136     pass("chdir to temp dir");
137 }
138 else {
139     fail("chdir to temp dir: $!");
140 }
141
142 $dir   = catdir('a', 'd1');
143 $dir2  = catdir('a', 'd2');
144
145 @created = mkpath( $dir, 0, $dir2 );
146 is(scalar @created, 3, 'new-style 3 dirs created');
147
148 $count = rmtree( $dir, 0, $dir2, );
149 is($count, 3, 'new-style 3 dirs removed');
150
151 @created = mkpath( $dir, $dir2, 1 );
152 is(scalar @created, 3, 'new-style 3 dirs created (redux)');
153
154 $count = rmtree( $dir, $dir2, 1 );
155 is($count, 3, 'new-style 3 dirs removed (redux)');
156
157 @created = mkpath( $dir, $dir2 );
158 is(scalar @created, 2, 'new-style 2 dirs created');
159
160 $count = rmtree( $dir, $dir2 );
161 is($count, 2, 'new-style 2 dirs removed');
162
163 if (chdir updir()) {
164     pass("chdir parent");
165 }
166 else {
167     fail("chdir parent: $!");
168 }
169
170 # see what happens if a file exists where we want a directory
171 SKIP: {
172     my $entry = catdir($tmp_base, "file");
173     skip "Cannot create $entry", 4 unless open OUT, "> $entry";
174     print OUT "test file, safe to delete\n", scalar(localtime), "\n";
175     close OUT;
176     ok(-e $entry, "file exists in place of directory");
177
178     mkpath( $entry, {error => \$error} );
179     is( scalar(@$error), 1, "caught error condition" );
180     ($file, $message) = each %{$error->[0]};
181     is( $entry, $file, "and the message is: $message");
182
183     eval {@created = mkpath($entry, 0, 0700)};
184     $error = $@;
185     chomp $error; # just to remove silly # in TAP output
186     cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
187         or diag(@created);
188 }
189
190 my $extra =  catdir(curdir(), qw(EXTRA 1 a));
191
192 SKIP: {
193     skip "extra scenarios not set up, see eg/setup-extra-tests", 8
194         unless -e $extra;
195
196     my ($list, $err);
197     $dir = catdir( 'EXTRA', '1' );
198     rmtree( $dir, {result => \$list, error => \$err} );
199     is(scalar(@$list), 2, "extra dir $dir removed");
200     is(scalar(@$err), 1, "one error encountered");
201
202     $dir = catdir( 'EXTRA', '3', 'N' );
203     rmtree( $dir, {result => \$list, error => \$err} );
204     is( @$list, 1, q{remove a symlinked dir} );
205     is( @$err,  0, q{with no errors} );
206
207     $dir = catdir('EXTRA', '3', 'S');
208     rmtree($dir, {error => \$error});
209     is( scalar(@$error), 2, 'two errors for an unreadable dir' );
210
211     $dir = catdir( 'EXTRA', '4' );
212     rmtree($dir,  {result => \$list, error => \$err} );
213     is( @$list, 0, q{don't follow a symlinked dir} );
214     is( @$err,  1, q{one error when removing a symlink in r/o dir} );
215     eval { ($file, $message) = each %{$err->[0]} };
216     is( $file, $dir, 'symlink reported in error' );
217 }
218
219 {
220     $dir = catdir($tmp_base, 'Z');
221     @created = mkpath($dir);
222     is(scalar(@created), 1, "create a Z directory");
223
224     local @ARGV = ($dir);
225     rmtree( [grep -e $_, @ARGV], 0, 0 );
226     ok(!-e $dir, "blow it away via \@ARGV");
227 }
228
229 SKIP: {
230     skip 'Test::Output not available', 10
231         unless $has_Test_Output;
232
233
234     SKIP: {
235         $dir = catdir('EXTRA', '3');
236         skip "extra scenarios not set up, see eg/setup-extra-tests", 2
237             unless -e $dir;
238
239         stderr_like( 
240             sub {rmtree($dir, {})},
241             qr{\ACan't remove directory \S+: .*? at \S+ line \d+\n},
242             'rmtree with file owned by root'
243         );
244
245         stderr_like( 
246             sub {rmtree('EXTRA', {})},
247             qr{\ACan't make directory EXTRA read\+writeable: .*? at \S+ line \d+
248 (?:Can't remove directory EXTRA/\d: .*? at \S+ line \d+
249 )+Can't unlink file [^:]+: .*? at \S+ line \d+
250 Can't remove directory EXTRA: .*? at \S+ line \d+
251 and can't restore permissions to \d+
252  at \S+ line \d+},
253             'rmtree with insufficient privileges'
254         );
255     }
256
257     my $base = catdir($tmp_base,'output');
258     $dir  = catdir($base,'A');
259     $dir2 = catdir($base,'B');
260
261     stderr_like(
262         sub { rmtree( undef, 1 ) },
263         qr/\ANo root path\(s\) specified\b/,
264         "rmtree of nothing carps sensibly"
265     );
266
267     stdout_is(
268         sub {@created = mkpath($dir, 1)},
269         "mkdir $base\nmkdir $dir\n",
270         'mkpath verbose (old style 1)'
271     );
272
273     stdout_is(
274         sub {@created = mkpath([$dir2], 1)},
275         "mkdir $dir2\n",
276         'mkpath verbose (old style 2)'
277     );
278
279     stdout_is(
280         sub {$count = rmtree([$dir, $dir2], 1, 1)},
281         "rmdir $dir\nrmdir $dir2\n",
282         'rmtree verbose (old style)'
283     );
284
285     stdout_is(
286         sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
287         "mkdir $dir\n",
288         'mkpath verbose (new style 1)'
289     );
290
291     stdout_is(
292         sub {@created = mkpath($dir2, 1, 0771)},
293         "mkdir $dir2\n",
294         'mkpath verbose (new style 2)'
295     );
296
297     SKIP: {
298         $file = catdir($dir2, "file");
299         skip "Cannot create $file", 2 unless open OUT, "> $file";
300         print OUT "test file, safe to delete\n", scalar(localtime), "\n";
301         close OUT;
302
303         ok(-e $file, "file created in directory");
304
305         stdout_is(
306             sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
307             "rmdir $dir\nunlink $file\nrmdir $dir2\n",
308             'rmtree safe verbose (new style)'
309         );
310     }
311 }
312
313 SKIP: {
314     skip "extra scenarios not set up, see eg/setup-extra-tests", 6
315         unless -d catdir(qw(EXTRA 1));
316
317     rmtree 'EXTRA', {safe => 0, error => \$error};
318     is( scalar(@$error), 7, 'seven deadly sins' );
319
320     rmtree 'EXTRA', {safe => 1, error => \$error};
321     is( scalar(@$error), 4, 'safe is better' );
322     for (@$error) {
323         ($file, $message) = each %$_;
324         if ($file =~  /[123]\z/) {
325             is(index($message, 'rmdir: '), 0, "failed to remove $file with rmdir")
326                 or diag($message);
327         }
328         else {
329             is(index($message, 'unlink: '), 0, "failed to remove $file with unlink")
330                 or diag($message);
331         }
332     }
333 }
334
335 rmtree($tmp_base, {result => \$list} );
336 is(ref($list), 'ARRAY', "received a final list of results");
337 ok( !(-d $tmp_base), "test base directory gone" );