Free memory in case of error/failure to compile.
[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
3376a30f 5use Test::More tests => 71;
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
138# borderline new-style heuristics
139if (chdir $tmp_base) {
140 pass("chdir to temp dir");
141}
142else {
143 fail("chdir to temp dir: $!");
037c8c09 144}
12c2e016 145
146$dir = catdir('a', 'd1');
147$dir2 = catdir('a', 'd2');
148
149@created = mkpath( $dir, 0, $dir2 );
150is(scalar @created, 3, 'new-style 3 dirs created');
151
152$count = rmtree( $dir, 0, $dir2, );
153is($count, 3, 'new-style 3 dirs removed');
154
155@created = mkpath( $dir, $dir2, 1 );
156is(scalar @created, 3, 'new-style 3 dirs created (redux)');
157
158$count = rmtree( $dir, $dir2, 1 );
159is($count, 3, 'new-style 3 dirs removed (redux)');
160
161@created = mkpath( $dir, $dir2 );
162is(scalar @created, 2, 'new-style 2 dirs created');
163
164$count = rmtree( $dir, $dir2 );
165is($count, 2, 'new-style 2 dirs removed');
166
167if (chdir updir()) {
168 pass("chdir parent");
169}
170else {
171 fail("chdir parent: $!");
172}
173
174# see what happens if a file exists where we want a directory
175SKIP: {
176 my $entry = catdir($tmp_base, "file");
177 skip "Cannot create $entry", 4 unless open OUT, "> $entry";
178 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
179 close OUT;
180 ok(-e $entry, "file exists in place of directory");
181
182 mkpath( $entry, {error => \$error} );
183 is( scalar(@$error), 1, "caught error condition" );
184 ($file, $message) = each %{$error->[0]};
185 is( $entry, $file, "and the message is: $message");
186
187 eval {@created = mkpath($entry, 0, 0700)};
188 $error = $@;
189 chomp $error; # just to remove silly # in TAP output
190 cmp_ok( $error, 'ne', "", "no directory created (old-style) err=$error" )
191 or diag(@created);
192}
193
194my $extra = catdir(curdir(), qw(EXTRA 1 a));
195
196SKIP: {
197 skip "extra scenarios not set up, see eg/setup-extra-tests", 8
198 unless -e $extra;
199
200 my ($list, $err);
201 $dir = catdir( 'EXTRA', '1' );
202 rmtree( $dir, {result => \$list, error => \$err} );
203 is(scalar(@$list), 2, "extra dir $dir removed");
204 is(scalar(@$err), 1, "one error encountered");
205
206 $dir = catdir( 'EXTRA', '3', 'N' );
207 rmtree( $dir, {result => \$list, error => \$err} );
208 is( @$list, 1, q{remove a symlinked dir} );
209 is( @$err, 0, q{with no errors} );
210
211 $dir = catdir('EXTRA', '3', 'S');
212 rmtree($dir, {error => \$error});
213 is( scalar(@$error), 2, 'two errors for an unreadable dir' );
214
215 $dir = catdir( 'EXTRA', '4' );
216 rmtree($dir, {result => \$list, error => \$err} );
217 is( @$list, 0, q{don't follow a symlinked dir} );
218 is( @$err, 1, q{one error when removing a symlink in r/o dir} );
219 eval { ($file, $message) = each %{$err->[0]} };
220 is( $file, $dir, 'symlink reported in error' );
221}
222
3376a30f 223{
d2f50e7f 224 $dir = catdir($tmp_base, 'ZZ');
3376a30f 225 @created = mkpath($dir);
d2f50e7f 226 is(scalar(@created), 1, "create a ZZ directory");
3376a30f 227
228 local @ARGV = ($dir);
229 rmtree( [grep -e $_, @ARGV], 0, 0 );
230 ok(!-e $dir, "blow it away via \@ARGV");
231}
232
12c2e016 233SKIP: {
234 skip 'Test::Output not available', 10
235 unless $has_Test_Output;
236
3376a30f 237
12c2e016 238 SKIP: {
239 $dir = catdir('EXTRA', '3');
240 skip "extra scenarios not set up, see eg/setup-extra-tests", 2
241 unless -e $dir;
242
243 stderr_like(
244 sub {rmtree($dir, {})},
245 qr{\ACan't remove directory \S+: .*? at \S+ line \d+\n},
246 'rmtree with file owned by root'
247 );
248
249 stderr_like(
250 sub {rmtree('EXTRA', {})},
251 qr{\ACan't make directory EXTRA read\+writeable: .*? at \S+ line \d+
252(?:Can't remove directory EXTRA/\d: .*? at \S+ line \d+
253)+Can't unlink file [^:]+: .*? at \S+ line \d+
254Can't remove directory EXTRA: .*? at \S+ line \d+
255and can't restore permissions to \d+
256 at \S+ line \d+},
257 'rmtree with insufficient privileges'
258 );
259 }
260
261 my $base = catdir($tmp_base,'output');
262 $dir = catdir($base,'A');
263 $dir2 = catdir($base,'B');
264
265 stderr_like(
3376a30f 266 sub { rmtree( undef, 1 ) },
12c2e016 267 qr/\ANo root path\(s\) specified\b/,
268 "rmtree of nothing carps sensibly"
269 );
270
271 stdout_is(
272 sub {@created = mkpath($dir, 1)},
273 "mkdir $base\nmkdir $dir\n",
274 'mkpath verbose (old style 1)'
275 );
276
277 stdout_is(
278 sub {@created = mkpath([$dir2], 1)},
279 "mkdir $dir2\n",
280 'mkpath verbose (old style 2)'
281 );
282
283 stdout_is(
284 sub {$count = rmtree([$dir, $dir2], 1, 1)},
285 "rmdir $dir\nrmdir $dir2\n",
286 'rmtree verbose (old style)'
287 );
288
289 stdout_is(
290 sub {@created = mkpath($dir, {verbose => 1, mask => 0750})},
291 "mkdir $dir\n",
292 'mkpath verbose (new style 1)'
293 );
294
295 stdout_is(
296 sub {@created = mkpath($dir2, 1, 0771)},
297 "mkdir $dir2\n",
298 'mkpath verbose (new style 2)'
299 );
300
301 SKIP: {
302 $file = catdir($dir2, "file");
303 skip "Cannot create $file", 2 unless open OUT, "> $file";
304 print OUT "test file, safe to delete\n", scalar(localtime), "\n";
305 close OUT;
306
307 ok(-e $file, "file created in directory");
308
309 stdout_is(
310 sub {$count = rmtree($dir, $dir2, {verbose => 1, safe => 1})},
311 "rmdir $dir\nunlink $file\nrmdir $dir2\n",
312 'rmtree safe verbose (new style)'
313 );
314 }
315}
316
317SKIP: {
318 skip "extra scenarios not set up, see eg/setup-extra-tests", 6
319 unless -d catdir(qw(EXTRA 1));
320
321 rmtree 'EXTRA', {safe => 0, error => \$error};
322 is( scalar(@$error), 7, 'seven deadly sins' );
323
324 rmtree 'EXTRA', {safe => 1, error => \$error};
325 is( scalar(@$error), 4, 'safe is better' );
326 for (@$error) {
327 ($file, $message) = each %$_;
328 if ($file =~ /[123]\z/) {
329 is(index($message, 'rmdir: '), 0, "failed to remove $file with rmdir")
330 or diag($message);
331 }
332 else {
333 is(index($message, 'unlink: '), 0, "failed to remove $file with unlink")
334 or diag($message);
335 }
336 }
337}
338
339rmtree($tmp_base, {result => \$list} );
340is(ref($list), 'ARRAY', "received a final list of results");
341ok( !(-d $tmp_base), "test base directory gone" );