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