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