This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for the previous commit
[perl5.git] / ext / File-Find / t / find.t
CommitLineData
3fa6e24b 1#!./perl
dd87e51f
CB
2use strict;
3use Cwd;
81793b90 4
3fa6e24b
TW
5my %Expect_File = (); # what we expect for $_
6my %Expect_Name = (); # what we expect for $File::Find::name/fullname
7my %Expect_Dir = (); # what we expect for $File::Find::dir
81793b90 8my $symlink_exists = eval { symlink("",""); 1 };
dd87e51f 9my ($warn_msg, @files, $file);
3fa6e24b 10
1a3850a5
GA
11
12BEGIN {
234fd682 13 require File::Spec;
05bc6e4e
NC
14 if ($ENV{PERL_CORE}) {
15 # May be doing dynamic loading while @INC is all relative
16 @INC = map { $_ = File::Spec->rel2abs($_); /(.*)/; $1 } @INC;
17 }
18 $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# $_[0]"; };
19
20 if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'VMS') {
21 # This is a hack - at present File::Find does not produce native names
22 # on Win32 or VMS, so force File::Spec to use Unix names.
23 # must be set *before* importing File::Find
24 require File::Spec::Unix;
25 @File::Spec::ISA = 'File::Spec::Unix';
26 }
27 require File::Find;
28 import File::Find;
1a3850a5
GA
29}
30
488985bf 31my $test_count = 98;
1c8cb608 32$test_count += 119 if $symlink_exists;
a0b245d5 33$test_count += 26 if $^O eq 'MSWin32';
68c65ec0
B
34$test_count += 2 if $^O eq 'MSWin32' and $symlink_exists;
35
36print "1..$test_count\n";
37#if ( $symlink_exists ) { print "1..199\n"; }
38#else { print "1..85\n"; }
1a3850a5 39
dd87e51f
CB
40my $orig_dir = cwd();
41
7dc9aaa5
MS
42# Uncomment this to see where File::Find is chdir'ing to. Helpful for
43# debugging its little jaunts around the filesystem.
44# BEGIN {
45# use Cwd;
46# *CORE::GLOBAL::chdir = sub ($) {
47# my($file, $line) = (caller)[1,2];
48#
49# printf "# cwd: %s\n", cwd();
50# print "# chdir: @_ from $file at $line\n";
51# my($return) = CORE::chdir($_[0]);
52# printf "# newcwd: %s\n", cwd();
53#
54# return $return;
55# };
56# }
57
c80f55d1
JH
58cleanup();
59
e12eeff8
NC
60$::count_taint = 0;
61find({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
bb7dc48b 62 File::Spec->curdir);
e12eeff8 63if ($::count_taint == 1) {
912440be
NC
64 print "ok 1\n";
65} else {
e12eeff8 66 print "not ok 1 # found $::count_taint files named 'taint.t'\n";
912440be 67}
3fa6e24b 68
e12eeff8
NC
69$::count_taint = 0;
70finddepth({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
bb7dc48b 71 File::Spec->curdir);
e12eeff8 72if ($::count_taint == 1) {
912440be
NC
73 print "ok 2\n";
74} else {
e12eeff8 75 print "not ok 2 # found $::count_taint files named 'taint.t'\n";
912440be 76}
6455dd3b 77
81793b90 78my $case = 2;
5eb85357 79my $FastFileTests_OK = 0;
81793b90 80
c80f55d1 81sub cleanup {
dd87e51f
CB
82 chdir($orig_dir);
83 my $need_updir = 0;
3fa6e24b 84 if (-d dir_path('for_find')) {
dd87e51f 85 $need_updir = 1 if chdir(dir_path('for_find'));
6fecce66
RGS
86 }
87 if (-d dir_path('fa')) {
88 unlink file_path('fa', 'fa_ord'),
89 file_path('fa', 'fsl'),
90 file_path('fa', 'faa', 'faa_ord'),
91 file_path('fa', 'fab', 'fab_ord'),
92 file_path('fa', 'fab', 'faba', 'faba_ord'),
51393fc0 93 file_path('fa', 'fac', 'faca'),
6fecce66 94 file_path('fb', 'fb_ord'),
51393fc0
A
95 file_path('fb', 'fba', 'fba_ord'),
96 file_path('fb', 'fbc', 'fbca');
6fecce66
RGS
97 rmdir dir_path('fa', 'faa');
98 rmdir dir_path('fa', 'fab', 'faba');
99 rmdir dir_path('fa', 'fab');
51393fc0 100 rmdir dir_path('fa', 'fac');
6fecce66
RGS
101 rmdir dir_path('fa');
102 rmdir dir_path('fb', 'fba');
51393fc0 103 rmdir dir_path('fb', 'fbc');
6fecce66
RGS
104 rmdir dir_path('fb');
105 }
488985bf
JK
106 if (-d dir_path('fc')) {
107 unlink (
108 file_path('fc', 'fca', 'match_alpha'),
109 file_path('fc', 'fca', 'match_beta'),
110 file_path('fc', 'fcb', 'match_gamma'),
111 file_path('fc', 'fcb', 'delta'),
112 file_path('fc', 'fcc', 'match_epsilon'),
113 file_path('fc', 'fcc', 'match_zeta'),
114 file_path('fc', 'fcc', 'eta'),
115 );
116 rmdir dir_path('fc', 'fca');
117 rmdir dir_path('fc', 'fcb');
118 rmdir dir_path('fc', 'fcc');
119 rmdir dir_path('fc');
120 }
dd87e51f
CB
121 if ($need_updir) {
122 my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
123 chdir($updir);
124 }
6fecce66
RGS
125 if (-d dir_path('for_find')) {
126 rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
c80f55d1
JH
127 }
128}
129
81793b90 130END {
c80f55d1 131 cleanup();
81793b90
GS
132}
133
134sub Check($) {
3fa6e24b
TW
135 $case++;
136 if ($_[0]) { print "ok $case\n"; }
137 else { print "not ok $case\n"; }
81793b90
GS
138}
139
140sub CheckDie($) {
3fa6e24b
TW
141 $case++;
142 if ($_[0]) { print "ok $case\n"; }
143 else { print "not ok $case\n $!\n"; exit 0; }
81793b90
GS
144}
145
146sub touch {
3fa6e24b 147 CheckDie( open(my $T,'>',$_[0]) );
81793b90
GS
148}
149
150sub MkDir($$) {
3fa6e24b 151 CheckDie( mkdir($_[0],$_[1]) );
81793b90
GS
152}
153
3fa6e24b 154sub wanted_File_Dir {
aaaf2301 155 printf "# \$File::Find::dir => '$File::Find::dir'\t\$_ => '$_'\n";
3fa6e24b 156 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
a1ccf0c4 157 s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
3fa6e24b
TW
158 Check( $Expect_File{$_} );
159 if ( $FastFileTests_OK ) {
160 delete $Expect_File{ $_}
161 unless ( $Expect_Dir{$_} && ! -d _ );
162 } else {
163 delete $Expect_File{$_}
164 unless ( $Expect_Dir{$_} && ! -d $_ );
165 }
166}
7e47e6ff 167
3fa6e24b
TW
168sub wanted_File_Dir_prune {
169 &wanted_File_Dir;
170 $File::Find::prune=1 if $_ eq 'faba';
81793b90
GS
171}
172
3fa6e24b
TW
173sub wanted_Name {
174 my $n = $File::Find::name;
175 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.');
176 print "# \$File::Find::name => '$n'\n";
177 my $i = rindex($n,'/');
178 my $OK = exists($Expect_Name{$n});
786dada4
NC
179 if ( $OK ) {
180 $OK= exists($Expect_Name{substr($n,0,$i)}) if $i >= 0;
7e47e6ff 181 }
3fa6e24b
TW
182 Check($OK);
183 delete $Expect_Name{$n};
57907763
GS
184}
185
3fa6e24b
TW
186sub wanted_File {
187 print "# \$_ => '$_'\n";
188 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
189 my $i = rindex($_,'/');
190 my $OK = exists($Expect_File{ $_});
786dada4
NC
191 if ( $OK ) {
192 $OK= exists($Expect_File{ substr($_,0,$i)}) if $i >= 0;
7e47e6ff 193 }
3fa6e24b
TW
194 Check($OK);
195 delete $Expect_File{ $_};
57907763
GS
196}
197
7e47e6ff 198sub simple_wanted {
3fa6e24b
TW
199 print "# \$File::Find::dir => '$File::Find::dir'\n";
200 print "# \$_ => '$_'\n";
7e47e6ff
JH
201}
202
203sub noop_wanted {}
78eac027 204
7e47e6ff 205sub my_preprocess {
3fa6e24b
TW
206 @files = @_;
207 print "# --preprocess--\n";
208 print "# \$File::Find::dir => '$File::Find::dir' \n";
209 foreach $file (@files) {
30e67801 210 $file =~ s/\.(dir)?$//i if $^O eq 'VMS';
3fa6e24b
TW
211 print "# $file \n";
212 delete $Expect_Dir{ $File::Find::dir }->{$file};
213 }
214 print "# --end preprocess--\n";
215 Check(scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0);
216 if (scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0) {
217 delete $Expect_Dir{ $File::Find::dir }
218 }
219 return @files;
7e47e6ff
JH
220}
221
222sub my_postprocess {
3fa6e24b
TW
223 print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
224 delete $Expect_Dir{ $File::Find::dir};
7e47e6ff
JH
225}
226
227
bb7dc48b
JH
228# Use dir_path() to specify a directory path that's expected for
229# $File::Find::dir (%Expect_Dir). Also use it in file operations like
230# chdir, rmdir etc.
3fa6e24b 231#
2586ba89 232# dir_path() concatenates directory names to form a *relative*
30db15be 233# directory path, independent from the platform it's run on, although
2586ba89 234# there are limitations. Don't try to create an absolute path,
bb7dc48b 235# because that may fail on operating systems that have the concept of
2586ba89 236# volume names (e.g. Mac OS). As a special case, you can pass it a "."
786dada4
NC
237# as first argument, to create a directory path like "./fa/dir". If there's
238# no second argument, this function will return "./"
3fa6e24b
TW
239
240sub dir_path {
2586ba89 241 my $first_arg = shift @_;
3fa6e24b 242
2586ba89 243 if ($first_arg eq '.') {
786dada4
NC
244 return './' unless @_;
245 my $path = File::Spec->catdir(@_);
246 # add leading "./"
247 $path = "./$path";
248 return $path;
2586ba89
JH
249 } else { # $first_arg ne '.'
250 return $first_arg unless @_; # return plain filename
251 return File::Spec->catdir($first_arg, @_); # relative path
3fa6e24b
TW
252 }
253}
7e47e6ff 254
7e47e6ff 255
bb7dc48b 256# Use topdir() to specify a directory path that you want to pass to
786dada4 257# find/finddepth. Historically topdir() differed on Mac OS classic.
7e47e6ff 258
786dada4 259*topdir = \&dir_path;
7e47e6ff 260
7e47e6ff 261
bb7dc48b 262# Use file_path() to specify a file path that's expected for $_
2586ba89 263# (%Expect_File). Also suitable for file operations like unlink etc.
3fa6e24b 264#
bb7dc48b 265# file_path() concatenates directory names (if any) and a filename to
2586ba89 266# form a *relative* file path (the last argument is assumed to be a
30db15be 267# file). It's independent from the platform it's run on, although
2586ba89
JH
268# there are limitations. As a special case, you can pass it a "." as
269# first argument, to create a file path like "./fa/file" on operating
786dada4
NC
270# systems. If there's no second argument, this function will return the
271# string "./"
3fa6e24b
TW
272
273sub file_path {
2586ba89 274 my $first_arg = shift @_;
3fa6e24b 275
2586ba89 276 if ($first_arg eq '.') {
786dada4
NC
277 return './' unless @_;
278 my $path = File::Spec->catfile(@_);
279 # add leading "./"
280 $path = "./$path";
281 return $path;
2586ba89
JH
282 } else { # $first_arg ne '.'
283 return $first_arg unless @_; # return plain filename
284 return File::Spec->catfile($first_arg, @_); # relative path
7e47e6ff 285 }
3fa6e24b
TW
286}
287
288
bb7dc48b
JH
289# Use file_path_name() to specify a file path that's expected for
290# $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
291# option is in effect, $_ is the same as $File::Find::Name. In that
292# case, also use this function to specify a file path that's expected
293# for $_.
3fa6e24b 294#
786dada4
NC
295# Historically file_path_name differed on Mac OS classic.
296
297*file_path_name = \&file_path;
3fa6e24b 298
7e47e6ff 299
3fa6e24b
TW
300
301MkDir( dir_path('for_find'), 0770 );
302CheckDie(chdir( dir_path('for_find')));
303MkDir( dir_path('fa'), 0770 );
304MkDir( dir_path('fb'), 0770 );
305touch( file_path('fb', 'fb_ord') );
306MkDir( dir_path('fb', 'fba'), 0770 );
307touch( file_path('fb', 'fba', 'fba_ord') );
786dada4 308CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
3fa6e24b
TW
309touch( file_path('fa', 'fa_ord') );
310
311MkDir( dir_path('fa', 'faa'), 0770 );
312touch( file_path('fa', 'faa', 'faa_ord') );
313MkDir( dir_path('fa', 'fab'), 0770 );
314touch( file_path('fa', 'fab', 'fab_ord') );
315MkDir( dir_path('fa', 'fab', 'faba'), 0770 );
316touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
317
318
bb7dc48b
JH
319%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
320 file_path('fa_ord') => 1, file_path('fab') => 1,
321 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 322 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 323
3fa6e24b
TW
324delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
325%Expect_Name = ();
bb7dc48b
JH
326
327%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
328 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 329 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 330
3fa6e24b
TW
331delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
332File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') );
333Check( scalar(keys %Expect_File) == 0 );
334
335
336print "# check re-entrancy\n";
bb7dc48b
JH
337
338%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
339 file_path('fa_ord') => 1, file_path('fab') => 1,
340 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 341 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 342
3fa6e24b
TW
343delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
344%Expect_Name = ();
bb7dc48b
JH
345
346%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
347 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 348 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 349
3fa6e24b 350delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
bb7dc48b
JH
351
352File::Find::find( {wanted => sub { wanted_File_Dir_prune();
353 File::Find::find( {wanted => sub
354 {} }, File::Spec->curdir ); } },
355 topdir('fa') );
356
3fa6e24b
TW
357Check( scalar(keys %Expect_File) == 0 );
358
359
360# no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
bb7dc48b
JH
361
362%Expect_File = (file_path_name('fa') => 1,
363 file_path_name('fa', 'fsl') => 1,
364 file_path_name('fa', 'fa_ord') => 1,
365 file_path_name('fa', 'fab') => 1,
366 file_path_name('fa', 'fab', 'fab_ord') => 1,
367 file_path_name('fa', 'fab', 'faba') => 1,
368 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
369 file_path_name('fa', 'faa') => 1,
370 file_path_name('fa', 'faa', 'faa_ord') => 1,);
371
3fa6e24b
TW
372delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
373%Expect_Name = ();
bb7dc48b
JH
374
375%Expect_Dir = (dir_path('fa') => 1,
376 dir_path('fa', 'faa') => 1,
377 dir_path('fa', 'fab') => 1,
378 dir_path('fa', 'fab', 'faba') => 1,
379 dir_path('fb') => 1,
380 dir_path('fb', 'fba') => 1);
381
382delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') }
383 unless $symlink_exists;
384
385File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1},
386 topdir('fa') ); Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
387
388
389%Expect_File = ();
bb7dc48b
JH
390
391%Expect_Name = (File::Spec->curdir => 1,
392 file_path_name('.', 'fa') => 1,
393 file_path_name('.', 'fa', 'fsl') => 1,
394 file_path_name('.', 'fa', 'fa_ord') => 1,
395 file_path_name('.', 'fa', 'fab') => 1,
396 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
397 file_path_name('.', 'fa', 'fab', 'faba') => 1,
398 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
399 file_path_name('.', 'fa', 'faa') => 1,
400 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
401 file_path_name('.', 'fb') => 1,
402 file_path_name('.', 'fb', 'fba') => 1,
403 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
404 file_path_name('.', 'fb', 'fb_ord') => 1);
405
3fa6e24b
TW
406delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
407%Expect_Dir = ();
408File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
409Check( scalar(keys %Expect_Name) == 0 );
410
411
bb7dc48b
JH
412# no_chdir is in effect, hence we use file_path_name to specify the
413# expected paths for %Expect_File
414
415%Expect_File = (File::Spec->curdir => 1,
416 file_path_name('.', 'fa') => 1,
417 file_path_name('.', 'fa', 'fsl') => 1,
418 file_path_name('.', 'fa', 'fa_ord') => 1,
419 file_path_name('.', 'fa', 'fab') => 1,
420 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
421 file_path_name('.', 'fa', 'fab', 'faba') => 1,
422 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
423 file_path_name('.', 'fa', 'faa') => 1,
424 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
425 file_path_name('.', 'fb') => 1,
426 file_path_name('.', 'fb', 'fba') => 1,
427 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
428 file_path_name('.', 'fb', 'fb_ord') => 1);
429
3fa6e24b
TW
430delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
431%Expect_Name = ();
432%Expect_Dir = ();
bb7dc48b
JH
433
434File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
435 File::Spec->curdir );
436
3fa6e24b
TW
437Check( scalar(keys %Expect_File) == 0 );
438
439
440print "# check preprocess\n";
441%Expect_File = ();
442%Expect_Name = ();
443%Expect_Dir = (
444 File::Spec->curdir => {fa => 1, fb => 1},
445 dir_path('.', 'fa') => {faa => 1, fab => 1, fa_ord => 1},
446 dir_path('.', 'fa', 'faa') => {faa_ord => 1},
447 dir_path('.', 'fa', 'fab') => {faba => 1, fab_ord => 1},
448 dir_path('.', 'fa', 'fab', 'faba') => {faba_ord => 1},
449 dir_path('.', 'fb') => {fba => 1, fb_ord => 1},
450 dir_path('.', 'fb', 'fba') => {fba_ord => 1}
451 );
bb7dc48b
JH
452
453File::Find::find( {wanted => \&noop_wanted,
454 preprocess => \&my_preprocess}, File::Spec->curdir );
455
3fa6e24b
TW
456Check( scalar(keys %Expect_Dir) == 0 );
457
458
459print "# check postprocess\n";
460%Expect_File = ();
461%Expect_Name = ();
462%Expect_Dir = (
463 File::Spec->curdir => 1,
464 dir_path('.', 'fa') => 1,
465 dir_path('.', 'fa', 'faa') => 1,
466 dir_path('.', 'fa', 'fab') => 1,
467 dir_path('.', 'fa', 'fab', 'faba') => 1,
468 dir_path('.', 'fb') => 1,
469 dir_path('.', 'fb', 'fba') => 1
470 );
bb7dc48b
JH
471
472File::Find::find( {wanted => \&noop_wanted,
473 postprocess => \&my_postprocess}, File::Spec->curdir );
474
3fa6e24b
TW
475Check( scalar(keys %Expect_Dir) == 0 );
476
17ab9c14
JB
477{
478 print "# checking argument localization\n";
479
480 ### this checks the fix of perlbug [19977] ###
481 my @foo = qw( a b c d e f );
482 my %pre = map { $_ => } @foo;
483
484 File::Find::find( sub { } , 'fa' ) for @foo;
485 delete $pre{$_} for @foo;
486
487 Check( scalar( keys %pre ) == 0 );
488}
3fa6e24b 489
bc125c03
NC
490# see thread starting
491# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-02/msg00351.html
492{
493 print "# checking that &_ and %_ are still accessible and that\n",
494 "# tie magic on \$_ is not triggered\n";
495
496 my $true_count;
497 my $sub = 0;
498 sub _ {
499 ++$sub;
500 }
501 my $tie_called = 0;
502
503 package Foo;
504 sub STORE {
505 ++$tie_called;
506 }
507 sub FETCH {return 'N'};
508 sub TIESCALAR {bless []};
509 package main;
510
511 Check( scalar( keys %_ ) == 0 );
512 my @foo = 'n';
513 tie $foo[0], "Foo";
514
515 File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo;
516 untie $_;
517
518 Check( $tie_called == 0);
519 Check( scalar( keys %_ ) == $true_count );
520 Check( $sub == $true_count );
521 Check( scalar( @foo ) == 1);
522 Check( $foo[0] eq 'N' );
523}
524
3fa6e24b 525if ( $symlink_exists ) {
bb7dc48b 526 print "# --- symbolic link tests --- \n";
3fa6e24b 527 $FastFileTests_OK= 1;
7e47e6ff 528
7e47e6ff 529
3fa6e24b
TW
530 # Verify that File::Find::find will call wanted even if the topdir of
531 # is a symlink to a directory, and it shouldn't follow the link
532 # unless follow is set, which it isn't in this case
533 %Expect_File = ( file_path('fsl') => 1 );
534 %Expect_Name = ();
535 %Expect_Dir = ();
536 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa', 'fsl') );
537 Check( scalar(keys %Expect_File) == 0 );
538
539
bb7dc48b
JH
540 %Expect_File = (File::Spec->curdir => 1, file_path('fa_ord') => 1,
541 file_path('fsl') => 1, file_path('fb_ord') => 1,
542 file_path('fba') => 1, file_path('fba_ord') => 1,
543 file_path('fab') => 1, file_path('fab_ord') => 1,
544 file_path('faba') => 1, file_path('faa') => 1,
545 file_path('faa_ord') => 1);
546
3fa6e24b 547 %Expect_Name = ();
bb7dc48b
JH
548
549 %Expect_Dir = (File::Spec->curdir => 1, dir_path('fa') => 1,
550 dir_path('faa') => 1, dir_path('fab') => 1,
551 dir_path('faba') => 1, dir_path('fb') => 1,
552 dir_path('fba') => 1);
553
554 File::Find::find( {wanted => \&wanted_File_Dir_prune,
555 follow_fast => 1}, topdir('fa') );
556
3fa6e24b
TW
557 Check( scalar(keys %Expect_File) == 0 );
558
559
bb7dc48b
JH
560 # no_chdir is in effect, hence we use file_path_name to specify
561 # the expected paths for %Expect_File
562
563 %Expect_File = (file_path_name('fa') => 1,
564 file_path_name('fa', 'fa_ord') => 1,
565 file_path_name('fa', 'fsl') => 1,
566 file_path_name('fa', 'fsl', 'fb_ord') => 1,
567 file_path_name('fa', 'fsl', 'fba') => 1,
568 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
569 file_path_name('fa', 'fab') => 1,
570 file_path_name('fa', 'fab', 'fab_ord') => 1,
571 file_path_name('fa', 'fab', 'faba') => 1,
572 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
573 file_path_name('fa', 'faa') => 1,
574 file_path_name('fa', 'faa', 'faa_ord') => 1);
575
3fa6e24b 576 %Expect_Name = ();
3fa6e24b 577
bb7dc48b
JH
578 %Expect_Dir = (dir_path('fa') => 1,
579 dir_path('fa', 'faa') => 1,
580 dir_path('fa', 'fab') => 1,
581 dir_path('fa', 'fab', 'faba') => 1,
582 dir_path('fb') => 1,
583 dir_path('fb', 'fba') => 1);
584
585 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
586 no_chdir => 1}, topdir('fa') );
587
588 Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
589
590 %Expect_File = ();
bb7dc48b
JH
591
592 %Expect_Name = (file_path_name('fa') => 1,
593 file_path_name('fa', 'fa_ord') => 1,
594 file_path_name('fa', 'fsl') => 1,
595 file_path_name('fa', 'fsl', 'fb_ord') => 1,
596 file_path_name('fa', 'fsl', 'fba') => 1,
597 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
598 file_path_name('fa', 'fab') => 1,
599 file_path_name('fa', 'fab', 'fab_ord') => 1,
600 file_path_name('fa', 'fab', 'faba') => 1,
601 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
602 file_path_name('fa', 'faa') => 1,
3fa6e24b 603 file_path_name('fa', 'faa', 'faa_ord') => 1);
bb7dc48b 604
3fa6e24b 605 %Expect_Dir = ();
3fa6e24b 606
bb7dc48b
JH
607 File::Find::finddepth( {wanted => \&wanted_Name,
608 follow_fast => 1}, topdir('fa') );
609
610 Check( scalar(keys %Expect_Name) == 0 );
3fa6e24b 611
bb7dc48b
JH
612 # no_chdir is in effect, hence we use file_path_name to specify
613 # the expected paths for %Expect_File
614
615 %Expect_File = (file_path_name('fa') => 1,
616 file_path_name('fa', 'fa_ord') => 1,
617 file_path_name('fa', 'fsl') => 1,
618 file_path_name('fa', 'fsl', 'fb_ord') => 1,
619 file_path_name('fa', 'fsl', 'fba') => 1,
620 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
621 file_path_name('fa', 'fab') => 1,
622 file_path_name('fa', 'fab', 'fab_ord') => 1,
623 file_path_name('fa', 'fab', 'faba') => 1,
624 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
625 file_path_name('fa', 'faa') => 1,
3fa6e24b 626 file_path_name('fa', 'faa', 'faa_ord') => 1);
bb7dc48b 627
3fa6e24b
TW
628 %Expect_Name = ();
629 %Expect_Dir = ();
bb7dc48b
JH
630
631 File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
632 no_chdir => 1}, topdir('fa') );
633
3fa6e24b
TW
634 Check( scalar(keys %Expect_File) == 0 );
635
636
637 print "# check dangling symbolic links\n";
638 MkDir( dir_path('dangling_dir'), 0770 );
bb7dc48b
JH
639 CheckDie( symlink( dir_path('dangling_dir'),
640 file_path('dangling_dir_sl') ) );
3fa6e24b
TW
641 rmdir dir_path('dangling_dir');
642 touch(file_path('dangling_file'));
786dada4 643 CheckDie( symlink('../dangling_file','fa/dangling_file_sl') );
3fa6e24b
TW
644 unlink file_path('dangling_file');
645
646 {
647 # these tests should also emit a warning
bb7dc48b
JH
648 use warnings;
649
650 %Expect_File = (File::Spec->curdir => 1,
615a2b9b 651 file_path('dangling_file_sl') => 1,
bb7dc48b
JH
652 file_path('fa_ord') => 1,
653 file_path('fsl') => 1,
654 file_path('fb_ord') => 1,
655 file_path('fba') => 1,
656 file_path('fba_ord') => 1,
657 file_path('fab') => 1,
658 file_path('fab_ord') => 1,
659 file_path('faba') => 1,
660 file_path('faba_ord') => 1,
661 file_path('faa') => 1,
662 file_path('faa_ord') => 1);
663
3fa6e24b
TW
664 %Expect_Name = ();
665 %Expect_Dir = ();
666 undef $warn_msg;
bb7dc48b
JH
667
668 File::Find::find( {wanted => \&wanted_File, follow => 1,
669 dangling_symlinks =>
670 sub { $warn_msg = "$_[0] is a dangling symbolic link" }
671 },
672 topdir('dangling_dir_sl'), topdir('fa') );
673
3fa6e24b 674 Check( scalar(keys %Expect_File) == 0 );
615a2b9b 675 Check( $warn_msg =~ m|dangling_file_sl is a dangling symbolic link| );
bb7dc48b
JH
676 unlink file_path('fa', 'dangling_file_sl'),
677 file_path('dangling_dir_sl');
678
3fa6e24b
TW
679 }
680
681
682 print "# check recursion\n";
786dada4 683 CheckDie( symlink('../faa','fa/faa/faa_sl') );
7e47e6ff 684 undef $@;
bb7dc48b
JH
685 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
686 no_chdir => 1}, topdir('fa') ); };
a1ccf0c4 687 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link|i );
3fa6e24b 688 unlink file_path('fa', 'faa', 'faa_sl');
7e47e6ff 689
3fa6e24b
TW
690
691 print "# check follow_skip (file)\n";
786dada4 692 CheckDie( symlink('./fa_ord','fa/fa_ord_sl') ); # symlink to a file
7e47e6ff 693 undef $@;
bb7dc48b
JH
694
695 eval {File::Find::finddepth( {wanted => \&simple_wanted,
696 follow => 1,
697 follow_skip => 0, no_chdir => 1},
698 topdir('fa') );};
699
a1ccf0c4 700 Check( $@ =~ m|for_find[:/]fa[:/]fa_ord encountered a second time|i );
7e47e6ff 701
7e47e6ff 702
bb7dc48b
JH
703 # no_chdir is in effect, hence we use file_path_name to specify
704 # the expected paths for %Expect_File
705
706 %Expect_File = (file_path_name('fa') => 1,
aaaf2301
NC
707 file_path_name('fa', 'fa_ord') => 2,
708 # We may encounter the symlink first
709 file_path_name('fa', 'fa_ord_sl') => 2,
bb7dc48b
JH
710 file_path_name('fa', 'fsl') => 1,
711 file_path_name('fa', 'fsl', 'fb_ord') => 1,
712 file_path_name('fa', 'fsl', 'fba') => 1,
713 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
714 file_path_name('fa', 'fab') => 1,
715 file_path_name('fa', 'fab', 'fab_ord') => 1,
716 file_path_name('fa', 'fab', 'faba') => 1,
717 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
718 file_path_name('fa', 'faa') => 1,
719 file_path_name('fa', 'faa', 'faa_ord') => 1);
3fa6e24b
TW
720
721 %Expect_Name = ();
bb7dc48b
JH
722
723 %Expect_Dir = (dir_path('fa') => 1,
724 dir_path('fa', 'faa') => 1,
725 dir_path('fa', 'fab') => 1,
726 dir_path('fa', 'fab', 'faba') => 1,
727 dir_path('fb') => 1,
728 dir_path('fb','fba') => 1);
729
730 File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
731 follow_skip => 1, no_chdir => 1},
3fa6e24b 732 topdir('fa') );
615a2b9b 733 Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
734 unlink file_path('fa', 'fa_ord_sl');
735
736
737 print "# check follow_skip (directory)\n";
786dada4 738 CheckDie( symlink('./faa','fa/faa_sl') ); # symlink to a directory
3fa6e24b 739 undef $@;
bb7dc48b
JH
740
741 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
742 follow_skip => 0, no_chdir => 1},
3fa6e24b 743 topdir('fa') );};
bb7dc48b 744
a1ccf0c4 745 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
3fa6e24b
TW
746
747
748 undef $@;
bb7dc48b
JH
749
750 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
751 follow_skip => 1, no_chdir => 1},
3fa6e24b 752 topdir('fa') );};
bb7dc48b 753
a1ccf0c4 754 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
3fa6e24b 755
bb7dc48b
JH
756 # no_chdir is in effect, hence we use file_path_name to specify
757 # the expected paths for %Expect_File
758
759 %Expect_File = (file_path_name('fa') => 1,
760 file_path_name('fa', 'fa_ord') => 1,
761 file_path_name('fa', 'fsl') => 1,
762 file_path_name('fa', 'fsl', 'fb_ord') => 1,
763 file_path_name('fa', 'fsl', 'fba') => 1,
764 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
765 file_path_name('fa', 'fab') => 1,
766 file_path_name('fa', 'fab', 'fab_ord') => 1,
767 file_path_name('fa', 'fab', 'faba') => 1,
768 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
769 file_path_name('fa', 'faa') => 1,
aaaf2301
NC
770 file_path_name('fa', 'faa', 'faa_ord') => 1,
771 # We may actually encounter the symlink first.
772 file_path_name('fa', 'faa_sl') => 1,
773 file_path_name('fa', 'faa_sl', 'faa_ord') => 1);
3fa6e24b
TW
774
775 %Expect_Name = ();
bb7dc48b
JH
776
777 %Expect_Dir = (dir_path('fa') => 1,
778 dir_path('fa', 'faa') => 1,
779 dir_path('fa', 'fab') => 1,
780 dir_path('fa', 'fab', 'faba') => 1,
781 dir_path('fb') => 1,
782 dir_path('fb', 'fba') => 1);
783
784 File::Find::find( {wanted => \&wanted_File_Dir, follow => 1,
785 follow_skip => 2, no_chdir => 1}, topdir('fa') );
786
aaaf2301
NC
787 # If we encountered the symlink first, then the entries corresponding to
788 # the real name remain, if the real name first then the symlink
789 my @names = sort keys %Expect_File;
615a2b9b 790 Check( @names == 1 );
aaaf2301
NC
791 # Normalise both to the original name
792 s/_sl// foreach @names;
615a2b9b 793 Check ($names[0] eq file_path_name('fa', 'faa', 'faa_ord'));
3fa6e24b
TW
794 unlink file_path('fa', 'faa_sl');
795
17ab9c14 796}
68c65ec0
B
797
798
799# Win32 checks - [perl #41555]
800if ($^O eq 'MSWin32') {
801 require File::Spec::Win32;
802 my ($volume) = File::Spec::Win32->splitpath($orig_dir, 1);
803 print STDERR "VOLUME = $volume\n";
804
805 # with chdir
806 %Expect_File = (File::Spec->curdir => 1,
807 file_path('fsl') => 1,
808 file_path('fa_ord') => 1,
809 file_path('fab') => 1,
810 file_path('fab_ord') => 1,
811 file_path('faba') => 1,
812 file_path('faba_ord') => 1,
813 file_path('faa') => 1,
814 file_path('faa_ord') => 1);
815
816 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
817 %Expect_Name = ();
818
819 %Expect_Dir = (dir_path('fa') => 1,
820 dir_path('faa') => 1,
821 dir_path('fab') => 1,
822 dir_path('faba') => 1,
823 dir_path('fb') => 1,
824 dir_path('fba') => 1);
825
826
827
828 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa'));
829 Check( scalar(keys %Expect_File) == 0 );
830
831 # no_chdir
832 %Expect_File = ($volume . file_path_name('fa') => 1,
833 $volume . file_path_name('fa', 'fsl') => 1,
834 $volume . file_path_name('fa', 'fa_ord') => 1,
835 $volume . file_path_name('fa', 'fab') => 1,
836 $volume . file_path_name('fa', 'fab', 'fab_ord') => 1,
837 $volume . file_path_name('fa', 'fab', 'faba') => 1,
838 $volume . file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
839 $volume . file_path_name('fa', 'faa') => 1,
840 $volume . file_path_name('fa', 'faa', 'faa_ord') => 1);
841
842
843 delete $Expect_File{ $volume . file_path_name('fa', 'fsl') } unless $symlink_exists;
844 %Expect_Name = ();
845
846 %Expect_Dir = ($volume . dir_path('fa') => 1,
847 $volume . dir_path('fa', 'faa') => 1,
848 $volume . dir_path('fa', 'fab') => 1,
849 $volume . dir_path('fa', 'fab', 'faba') => 1);
850
851 File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1}, $volume . topdir('fa'));
852 Check( scalar(keys %Expect_File) == 0 );
853}
51393fc0
A
854
855
856if ($symlink_exists) { # Issue 68260
857 print "# BUG 68260\n";
858 MkDir (dir_path ('fa', 'fac'), 0770);
859 MkDir (dir_path ('fb', 'fbc'), 0770);
860 touch (file_path ('fa', 'fac', 'faca'));
786dada4 861 CheckDie (symlink ('..////../fa/fac/faca', 'fb/fbc/fbca'));
51393fc0
A
862
863 use warnings;
864 my $dangling_symlink;
865 local $SIG {__WARN__} = sub {
866 local $" = " ";
867 $dangling_symlink ++ if "@_" =~ /dangling symbolic link/;
868 };
869
870 File::Find::find (
871 {
872 wanted => sub {1;},
873 follow => 1,
874 follow_skip => 2,
875 dangling_symlinks => 1,
876 },
877 File::Spec -> curdir
878 );
879
880 Check (!$dangling_symlink);
881}
a0b245d5 882
488985bf
JK
883print "# RT 59750\n";
884MkDir( dir_path('fc'), 0770 );
885MkDir( dir_path('fc', 'fca'), 0770 );
886MkDir( dir_path('fc', 'fcb'), 0770 );
887MkDir( dir_path('fc', 'fcc'), 0770 );
888touch( file_path('fc', 'fca', 'match_alpha') );
889touch( file_path('fc', 'fca', 'match_beta') );
890touch( file_path('fc', 'fcb', 'match_gamma') );
891touch( file_path('fc', 'fcb', 'delta') );
892touch( file_path('fc', 'fcc', 'match_epsilon') );
893touch( file_path('fc', 'fcc', 'match_zeta') );
894touch( file_path('fc', 'fcc', 'eta') );
895
896my @files_from_mixed = ();
897sub wantmatch {
898 if ( $File::Find::name =~ m/match/ ) {
899 push @files_from_mixed, $_;
900 print "# \$_ => '$_'\n";
901 }
902}
903find( \&wantmatch, (
904 dir_path('fc', 'fca'),
905 dir_path('fc', 'fcb'),
906 dir_path('fc', 'fcc'),
907) );
908Check( scalar(@files_from_mixed) == 5 );
909
910@files_from_mixed = ();
911find( \&wantmatch, (
912 dir_path('fc', 'fca'),
913 dir_path('fc', 'fcb'),
914 file_path('fc', 'fcc', 'match_epsilon'),
915 file_path('fc', 'fcc', 'eta'),
916) );
917Check( scalar(@files_from_mixed) == 4 );
a0b245d5
AD
918
919if ($^O eq 'MSWin32') {
920 # Check F:F:f correctly handles a root directory path.
921 # Rather than processing the entire drive (!), simply test that the
922 # first file passed to the wanted routine is correct and then bail out.
923 $orig_dir =~ /^(\w:)/ or die "expected a drive: $orig_dir";
924 my $drive = $1;
925
926 # Determine the file in the root directory which would be
927 # first if processed in sorted order. Create one if necessary.
928 my $expected_first_file;
929 opendir(ROOT_DIR, "/") or die "cannot opendir /: $!\n";
930 foreach my $f (sort readdir ROOT_DIR) {
931 if (-f "/$f") {
932 $expected_first_file = $f;
933 last;
934 }
935 }
936 closedir ROOT_DIR;
937 my $created_file;
938 unless (defined $expected_first_file) {
939 $expected_first_file = '__perl_File_Find_test.tmp';
940 open(F, ">", "/$expected_first_file") && close(F)
941 or die "cannot create file in root directory: $!\n";
942 $created_file = 1;
943 }
944
945 # Run F:F:f with/without no_chdir for each possible style of root path.
946 # NB. If HOME were "/", then an inadvertent chdir('') would fluke the
947 # expected result, so ensure it is something else:
948 local $ENV{HOME} = $orig_dir;
949 foreach my $no_chdir (0, 1) {
950 foreach my $root_dir ("/", "\\", "$drive/", "$drive\\") {
951 eval {
952 File::Find::find({
953 'no_chdir' => $no_chdir,
954 'preprocess' => sub { return sort @_ },
955 'wanted' => sub {
956 -f or return; # the first call is for $root_dir itself.
957 my $got = $File::Find::name;
958 my $exp = "$root_dir$expected_first_file";
959 print "# no_chdir=$no_chdir $root_dir '$got'\n";
960 Check($got eq $exp);
961 die "done"; # don't process the entire drive!
962 },
963 }, $root_dir);
964 };
965 # If F:F:f did not die "done" then it did not Check() either.
966 unless ($@ and $@ =~ /done/) {
967 print "# no_chdir=$no_chdir $root_dir ",
968 ($@ ? "error: $@" : "no files found"), "\n";
969 Check(0);
970 }
971 }
972 }
973 if ($created_file) {
974 unlink("/$expected_first_file")
975 or warn "can't unlink /$expected_first_file: $!\n";
976 }
977}