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