This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
FreeBSD atoll() warning (Was Re: Code freeze
[perl5.git] / lib / File / Find / t / find.t
CommitLineData
3fa6e24b 1#!./perl
81793b90
GS
2
3
3fa6e24b
TW
4my %Expect_File = (); # what we expect for $_
5my %Expect_Name = (); # what we expect for $File::Find::name/fullname
6my %Expect_Dir = (); # what we expect for $File::Find::dir
81793b90 7my $symlink_exists = eval { symlink("",""); 1 };
7e47e6ff 8my $warn_msg;
3fa6e24b 9
1a3850a5
GA
10
11BEGIN {
12 chdir 't' if -d 't';
9f826d6a 13 unshift @INC => '../lib';
7e47e6ff 14
3fa6e24b 15 $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# $_[0]"; }
1a3850a5
GA
16}
17
aaaf2301 18if ( $symlink_exists ) { print "1..199\n"; }
bc125c03 19else { print "1..85\n"; }
1a3850a5 20
7dc9aaa5
MS
21# Uncomment this to see where File::Find is chdir'ing to. Helpful for
22# debugging its little jaunts around the filesystem.
23# BEGIN {
24# use Cwd;
25# *CORE::GLOBAL::chdir = sub ($) {
26# my($file, $line) = (caller)[1,2];
27#
28# printf "# cwd: %s\n", cwd();
29# print "# chdir: @_ from $file at $line\n";
30# my($return) = CORE::chdir($_[0]);
31# printf "# newcwd: %s\n", cwd();
32#
33# return $return;
34# };
35# }
36
37
ea05ec2d
CL
38BEGIN {
39 use File::Spec;
40 if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'VMS')
41 {
42 # This is a hack - at present File::Find does not produce native names on
43 # Win32 or VMS, so force File::Spec to use Unix names.
44 # must be set *before* importing File::Find
45 require File::Spec::Unix;
46 @File::Spec::ISA = 'File::Spec::Unix';
47 }
48 require File::Find;
49 import File::Find;
50}
6455dd3b 51
c80f55d1
JH
52cleanup();
53
912440be
NC
54$::count_commonsense = 0;
55find({wanted => sub { ++$::count_commonsense if $_ eq 'commonsense.t'; } },
bb7dc48b 56 File::Spec->curdir);
912440be
NC
57if ($::count_commonsense == 1) {
58 print "ok 1\n";
59} else {
60 print "not ok 1 # found $::count_commonsense files named 'commonsense.t'\n";
61}
3fa6e24b 62
912440be
NC
63$::count_commonsense = 0;
64finddepth({wanted => sub { ++$::count_commonsense if $_ eq 'commonsense.t'; } },
bb7dc48b 65 File::Spec->curdir);
912440be
NC
66if ($::count_commonsense == 1) {
67 print "ok 2\n";
68} else {
69 print "not ok 2 # found $::count_commonsense files named 'commonsense.t'\n";
70}
6455dd3b 71
81793b90 72my $case = 2;
5eb85357 73my $FastFileTests_OK = 0;
81793b90 74
c80f55d1 75sub cleanup {
3fa6e24b
TW
76 if (-d dir_path('for_find')) {
77 chdir(dir_path('for_find'));
6fecce66
RGS
78 }
79 if (-d dir_path('fa')) {
80 unlink file_path('fa', 'fa_ord'),
81 file_path('fa', 'fsl'),
82 file_path('fa', 'faa', 'faa_ord'),
83 file_path('fa', 'fab', 'fab_ord'),
84 file_path('fa', 'fab', 'faba', 'faba_ord'),
85 file_path('fb', 'fb_ord'),
86 file_path('fb', 'fba', 'fba_ord');
87 rmdir dir_path('fa', 'faa');
88 rmdir dir_path('fa', 'fab', 'faba');
89 rmdir dir_path('fa', 'fab');
90 rmdir dir_path('fa');
91 rmdir dir_path('fb', 'fba');
92 rmdir dir_path('fb');
93 }
94 chdir(File::Spec->updir);
95 if (-d dir_path('for_find')) {
96 rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
c80f55d1
JH
97 }
98}
99
81793b90 100END {
c80f55d1 101 cleanup();
81793b90
GS
102}
103
104sub Check($) {
3fa6e24b
TW
105 $case++;
106 if ($_[0]) { print "ok $case\n"; }
107 else { print "not ok $case\n"; }
81793b90
GS
108}
109
110sub CheckDie($) {
3fa6e24b
TW
111 $case++;
112 if ($_[0]) { print "ok $case\n"; }
113 else { print "not ok $case\n $!\n"; exit 0; }
81793b90
GS
114}
115
116sub touch {
3fa6e24b 117 CheckDie( open(my $T,'>',$_[0]) );
81793b90
GS
118}
119
120sub MkDir($$) {
3fa6e24b 121 CheckDie( mkdir($_[0],$_[1]) );
81793b90
GS
122}
123
3fa6e24b 124sub wanted_File_Dir {
aaaf2301 125 printf "# \$File::Find::dir => '$File::Find::dir'\t\$_ => '$_'\n";
3fa6e24b 126 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
a1ccf0c4 127 s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
3fa6e24b
TW
128 Check( $Expect_File{$_} );
129 if ( $FastFileTests_OK ) {
130 delete $Expect_File{ $_}
131 unless ( $Expect_Dir{$_} && ! -d _ );
132 } else {
133 delete $Expect_File{$_}
134 unless ( $Expect_Dir{$_} && ! -d $_ );
135 }
136}
7e47e6ff 137
3fa6e24b
TW
138sub wanted_File_Dir_prune {
139 &wanted_File_Dir;
140 $File::Find::prune=1 if $_ eq 'faba';
81793b90
GS
141}
142
3fa6e24b
TW
143sub wanted_Name {
144 my $n = $File::Find::name;
145 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.');
146 print "# \$File::Find::name => '$n'\n";
147 my $i = rindex($n,'/');
148 my $OK = exists($Expect_Name{$n});
149 unless ($^O eq 'MacOS') {
150 if ( $OK ) {
151 $OK= exists($Expect_Name{substr($n,0,$i)}) if $i >= 0;
152 }
7e47e6ff 153 }
3fa6e24b
TW
154 Check($OK);
155 delete $Expect_Name{$n};
57907763
GS
156}
157
3fa6e24b
TW
158sub wanted_File {
159 print "# \$_ => '$_'\n";
160 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
161 my $i = rindex($_,'/');
162 my $OK = exists($Expect_File{ $_});
163 unless ($^O eq 'MacOS') {
164 if ( $OK ) {
165 $OK= exists($Expect_File{ substr($_,0,$i)}) if $i >= 0;
166 }
7e47e6ff 167 }
3fa6e24b
TW
168 Check($OK);
169 delete $Expect_File{ $_};
57907763
GS
170}
171
7e47e6ff 172sub simple_wanted {
3fa6e24b
TW
173 print "# \$File::Find::dir => '$File::Find::dir'\n";
174 print "# \$_ => '$_'\n";
7e47e6ff
JH
175}
176
177sub noop_wanted {}
78eac027 178
7e47e6ff 179sub my_preprocess {
3fa6e24b
TW
180 @files = @_;
181 print "# --preprocess--\n";
182 print "# \$File::Find::dir => '$File::Find::dir' \n";
183 foreach $file (@files) {
30db15be 184 $file =~ s/\.(dir)?$// if $^O eq 'VMS';
3fa6e24b
TW
185 print "# $file \n";
186 delete $Expect_Dir{ $File::Find::dir }->{$file};
187 }
188 print "# --end preprocess--\n";
189 Check(scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0);
190 if (scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0) {
191 delete $Expect_Dir{ $File::Find::dir }
192 }
193 return @files;
7e47e6ff
JH
194}
195
196sub my_postprocess {
3fa6e24b
TW
197 print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
198 delete $Expect_Dir{ $File::Find::dir};
7e47e6ff
JH
199}
200
201
bb7dc48b
JH
202# Use dir_path() to specify a directory path that's expected for
203# $File::Find::dir (%Expect_Dir). Also use it in file operations like
204# chdir, rmdir etc.
3fa6e24b 205#
2586ba89 206# dir_path() concatenates directory names to form a *relative*
30db15be 207# directory path, independent from the platform it's run on, although
2586ba89 208# there are limitations. Don't try to create an absolute path,
bb7dc48b 209# because that may fail on operating systems that have the concept of
2586ba89
JH
210# volume names (e.g. Mac OS). As a special case, you can pass it a "."
211# as first argument, to create a directory path like "./fa/dir" on
bb7dc48b
JH
212# operating systems other than Mac OS (actually, Mac OS will ignore
213# the ".", if it's the first argument). If there's no second argument,
214# this function will return the empty string on Mac OS and the string
215# "./" otherwise.
3fa6e24b
TW
216
217sub dir_path {
2586ba89 218 my $first_arg = shift @_;
3fa6e24b 219
2586ba89 220 if ($first_arg eq '.') {
3fa6e24b
TW
221 if ($^O eq 'MacOS') {
222 return '' unless @_;
223 # ignore first argument; return a relative path
224 # with leading ":" and with trailing ":"
2586ba89 225 return File::Spec->catdir(@_);
3fa6e24b
TW
226 } else { # other OS
227 return './' unless @_;
228 my $path = File::Spec->catdir(@_);
229 # add leading "./"
230 $path = "./$path";
231 return $path;
232 }
233
2586ba89
JH
234 } else { # $first_arg ne '.'
235 return $first_arg unless @_; # return plain filename
236 return File::Spec->catdir($first_arg, @_); # relative path
3fa6e24b
TW
237 }
238}
7e47e6ff 239
7e47e6ff 240
bb7dc48b 241# Use topdir() to specify a directory path that you want to pass to
2586ba89
JH
242# find/finddepth. Basically, topdir() does the same as dir_path() (see
243# above), except that there's no trailing ":" on Mac OS.
7e47e6ff 244
3fa6e24b
TW
245sub topdir {
246 my $path = dir_path(@_);
247 $path =~ s/:$// if ($^O eq 'MacOS');
248 return $path;
249}
7e47e6ff 250
7e47e6ff 251
bb7dc48b 252# Use file_path() to specify a file path that's expected for $_
2586ba89 253# (%Expect_File). Also suitable for file operations like unlink etc.
3fa6e24b 254#
bb7dc48b 255# file_path() concatenates directory names (if any) and a filename to
2586ba89 256# form a *relative* file path (the last argument is assumed to be a
30db15be 257# file). It's independent from the platform it's run on, although
2586ba89
JH
258# there are limitations. As a special case, you can pass it a "." as
259# first argument, to create a file path like "./fa/file" on operating
260# systems other than Mac OS (actually, Mac OS will ignore the ".", if
261# it's the first argument). If there's no second argument, this
262# function will return the empty string on Mac OS and the string "./"
263# otherwise.
3fa6e24b
TW
264
265sub file_path {
2586ba89 266 my $first_arg = shift @_;
3fa6e24b 267
2586ba89 268 if ($first_arg eq '.') {
3fa6e24b
TW
269 if ($^O eq 'MacOS') {
270 return '' unless @_;
271 # ignore first argument; return a relative path
272 # with leading ":", but without trailing ":"
2586ba89 273 return File::Spec->catfile(@_);
3fa6e24b
TW
274 } else { # other OS
275 return './' unless @_;
276 my $path = File::Spec->catfile(@_);
277 # add leading "./"
278 $path = "./$path";
279 return $path;
280 }
281
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#
bb7dc48b
JH
295# Basically, file_path_name() does the same as file_path() (see
296# above), except that there's always a leading ":" on Mac OS, even for
297# plain file/directory names.
3fa6e24b
TW
298
299sub file_path_name {
300 my $path = file_path(@_);
301 $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/));
302 return $path;
303}
304
7e47e6ff 305
3fa6e24b
TW
306
307MkDir( dir_path('for_find'), 0770 );
308CheckDie(chdir( dir_path('for_find')));
309MkDir( dir_path('fa'), 0770 );
310MkDir( dir_path('fb'), 0770 );
311touch( file_path('fb', 'fb_ord') );
312MkDir( dir_path('fb', 'fba'), 0770 );
313touch( file_path('fb', 'fba', 'fba_ord') );
314if ($^O eq 'MacOS') {
315 CheckDie( symlink(':fb',':fa:fsl') ) if $symlink_exists;
7e47e6ff 316} else {
3fa6e24b
TW
317 CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
318}
319touch( file_path('fa', 'fa_ord') );
320
321MkDir( dir_path('fa', 'faa'), 0770 );
322touch( file_path('fa', 'faa', 'faa_ord') );
323MkDir( dir_path('fa', 'fab'), 0770 );
324touch( file_path('fa', 'fab', 'fab_ord') );
325MkDir( dir_path('fa', 'fab', 'faba'), 0770 );
326touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
327
328
bb7dc48b
JH
329%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
330 file_path('fa_ord') => 1, file_path('fab') => 1,
331 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 332 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 333
3fa6e24b
TW
334delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
335%Expect_Name = ();
bb7dc48b
JH
336
337%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
338 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 339 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 340
3fa6e24b
TW
341delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
342File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') );
343Check( scalar(keys %Expect_File) == 0 );
344
345
346print "# check re-entrancy\n";
bb7dc48b
JH
347
348%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
349 file_path('fa_ord') => 1, file_path('fab') => 1,
350 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 351 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 352
3fa6e24b
TW
353delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
354%Expect_Name = ();
bb7dc48b
JH
355
356%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
357 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 358 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 359
3fa6e24b 360delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
bb7dc48b
JH
361
362File::Find::find( {wanted => sub { wanted_File_Dir_prune();
363 File::Find::find( {wanted => sub
364 {} }, File::Spec->curdir ); } },
365 topdir('fa') );
366
3fa6e24b
TW
367Check( scalar(keys %Expect_File) == 0 );
368
369
370# no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
bb7dc48b
JH
371
372%Expect_File = (file_path_name('fa') => 1,
373 file_path_name('fa', 'fsl') => 1,
374 file_path_name('fa', 'fa_ord') => 1,
375 file_path_name('fa', 'fab') => 1,
376 file_path_name('fa', 'fab', 'fab_ord') => 1,
377 file_path_name('fa', 'fab', 'faba') => 1,
378 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
379 file_path_name('fa', 'faa') => 1,
380 file_path_name('fa', 'faa', 'faa_ord') => 1,);
381
3fa6e24b
TW
382delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
383%Expect_Name = ();
bb7dc48b
JH
384
385%Expect_Dir = (dir_path('fa') => 1,
386 dir_path('fa', 'faa') => 1,
387 dir_path('fa', 'fab') => 1,
388 dir_path('fa', 'fab', 'faba') => 1,
389 dir_path('fb') => 1,
390 dir_path('fb', 'fba') => 1);
391
392delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') }
393 unless $symlink_exists;
394
395File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1},
396 topdir('fa') ); Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
397
398
399%Expect_File = ();
bb7dc48b
JH
400
401%Expect_Name = (File::Spec->curdir => 1,
402 file_path_name('.', 'fa') => 1,
403 file_path_name('.', 'fa', 'fsl') => 1,
404 file_path_name('.', 'fa', 'fa_ord') => 1,
405 file_path_name('.', 'fa', 'fab') => 1,
406 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
407 file_path_name('.', 'fa', 'fab', 'faba') => 1,
408 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
409 file_path_name('.', 'fa', 'faa') => 1,
410 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
411 file_path_name('.', 'fb') => 1,
412 file_path_name('.', 'fb', 'fba') => 1,
413 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
414 file_path_name('.', 'fb', 'fb_ord') => 1);
415
3fa6e24b
TW
416delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
417%Expect_Dir = ();
418File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
419Check( scalar(keys %Expect_Name) == 0 );
420
421
bb7dc48b
JH
422# no_chdir is in effect, hence we use file_path_name to specify the
423# expected paths for %Expect_File
424
425%Expect_File = (File::Spec->curdir => 1,
426 file_path_name('.', 'fa') => 1,
427 file_path_name('.', 'fa', 'fsl') => 1,
428 file_path_name('.', 'fa', 'fa_ord') => 1,
429 file_path_name('.', 'fa', 'fab') => 1,
430 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
431 file_path_name('.', 'fa', 'fab', 'faba') => 1,
432 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
433 file_path_name('.', 'fa', 'faa') => 1,
434 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
435 file_path_name('.', 'fb') => 1,
436 file_path_name('.', 'fb', 'fba') => 1,
437 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
438 file_path_name('.', 'fb', 'fb_ord') => 1);
439
3fa6e24b
TW
440delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
441%Expect_Name = ();
442%Expect_Dir = ();
bb7dc48b
JH
443
444File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
445 File::Spec->curdir );
446
3fa6e24b
TW
447Check( scalar(keys %Expect_File) == 0 );
448
449
450print "# check preprocess\n";
451%Expect_File = ();
452%Expect_Name = ();
453%Expect_Dir = (
454 File::Spec->curdir => {fa => 1, fb => 1},
455 dir_path('.', 'fa') => {faa => 1, fab => 1, fa_ord => 1},
456 dir_path('.', 'fa', 'faa') => {faa_ord => 1},
457 dir_path('.', 'fa', 'fab') => {faba => 1, fab_ord => 1},
458 dir_path('.', 'fa', 'fab', 'faba') => {faba_ord => 1},
459 dir_path('.', 'fb') => {fba => 1, fb_ord => 1},
460 dir_path('.', 'fb', 'fba') => {fba_ord => 1}
461 );
bb7dc48b
JH
462
463File::Find::find( {wanted => \&noop_wanted,
464 preprocess => \&my_preprocess}, File::Spec->curdir );
465
3fa6e24b
TW
466Check( scalar(keys %Expect_Dir) == 0 );
467
468
469print "# check postprocess\n";
470%Expect_File = ();
471%Expect_Name = ();
472%Expect_Dir = (
473 File::Spec->curdir => 1,
474 dir_path('.', 'fa') => 1,
475 dir_path('.', 'fa', 'faa') => 1,
476 dir_path('.', 'fa', 'fab') => 1,
477 dir_path('.', 'fa', 'fab', 'faba') => 1,
478 dir_path('.', 'fb') => 1,
479 dir_path('.', 'fb', 'fba') => 1
480 );
bb7dc48b
JH
481
482File::Find::find( {wanted => \&noop_wanted,
483 postprocess => \&my_postprocess}, File::Spec->curdir );
484
3fa6e24b
TW
485Check( scalar(keys %Expect_Dir) == 0 );
486
17ab9c14
JB
487{
488 print "# checking argument localization\n";
489
490 ### this checks the fix of perlbug [19977] ###
491 my @foo = qw( a b c d e f );
492 my %pre = map { $_ => } @foo;
493
494 File::Find::find( sub { } , 'fa' ) for @foo;
495 delete $pre{$_} for @foo;
496
497 Check( scalar( keys %pre ) == 0 );
498}
3fa6e24b 499
bc125c03
NC
500# see thread starting
501# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-02/msg00351.html
502{
503 print "# checking that &_ and %_ are still accessible and that\n",
504 "# tie magic on \$_ is not triggered\n";
505
506 my $true_count;
507 my $sub = 0;
508 sub _ {
509 ++$sub;
510 }
511 my $tie_called = 0;
512
513 package Foo;
514 sub STORE {
515 ++$tie_called;
516 }
517 sub FETCH {return 'N'};
518 sub TIESCALAR {bless []};
519 package main;
520
521 Check( scalar( keys %_ ) == 0 );
522 my @foo = 'n';
523 tie $foo[0], "Foo";
524
525 File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo;
526 untie $_;
527
528 Check( $tie_called == 0);
529 Check( scalar( keys %_ ) == $true_count );
530 Check( $sub == $true_count );
531 Check( scalar( @foo ) == 1);
532 Check( $foo[0] eq 'N' );
533}
534
3fa6e24b 535if ( $symlink_exists ) {
bb7dc48b 536 print "# --- symbolic link tests --- \n";
3fa6e24b 537 $FastFileTests_OK= 1;
7e47e6ff 538
7e47e6ff 539
3fa6e24b
TW
540 # Verify that File::Find::find will call wanted even if the topdir of
541 # is a symlink to a directory, and it shouldn't follow the link
542 # unless follow is set, which it isn't in this case
543 %Expect_File = ( file_path('fsl') => 1 );
544 %Expect_Name = ();
545 %Expect_Dir = ();
546 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa', 'fsl') );
547 Check( scalar(keys %Expect_File) == 0 );
548
549
bb7dc48b
JH
550 %Expect_File = (File::Spec->curdir => 1, file_path('fa_ord') => 1,
551 file_path('fsl') => 1, file_path('fb_ord') => 1,
552 file_path('fba') => 1, file_path('fba_ord') => 1,
553 file_path('fab') => 1, file_path('fab_ord') => 1,
554 file_path('faba') => 1, file_path('faa') => 1,
555 file_path('faa_ord') => 1);
556
3fa6e24b 557 %Expect_Name = ();
bb7dc48b
JH
558
559 %Expect_Dir = (File::Spec->curdir => 1, dir_path('fa') => 1,
560 dir_path('faa') => 1, dir_path('fab') => 1,
561 dir_path('faba') => 1, dir_path('fb') => 1,
562 dir_path('fba') => 1);
563
564 File::Find::find( {wanted => \&wanted_File_Dir_prune,
565 follow_fast => 1}, topdir('fa') );
566
3fa6e24b
TW
567 Check( scalar(keys %Expect_File) == 0 );
568
569
bb7dc48b
JH
570 # no_chdir is in effect, hence we use file_path_name to specify
571 # the expected paths for %Expect_File
572
573 %Expect_File = (file_path_name('fa') => 1,
574 file_path_name('fa', 'fa_ord') => 1,
575 file_path_name('fa', 'fsl') => 1,
576 file_path_name('fa', 'fsl', 'fb_ord') => 1,
577 file_path_name('fa', 'fsl', 'fba') => 1,
578 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
579 file_path_name('fa', 'fab') => 1,
580 file_path_name('fa', 'fab', 'fab_ord') => 1,
581 file_path_name('fa', 'fab', 'faba') => 1,
582 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
583 file_path_name('fa', 'faa') => 1,
584 file_path_name('fa', 'faa', 'faa_ord') => 1);
585
3fa6e24b 586 %Expect_Name = ();
3fa6e24b 587
bb7dc48b
JH
588 %Expect_Dir = (dir_path('fa') => 1,
589 dir_path('fa', 'faa') => 1,
590 dir_path('fa', 'fab') => 1,
591 dir_path('fa', 'fab', 'faba') => 1,
592 dir_path('fb') => 1,
593 dir_path('fb', 'fba') => 1);
594
595 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
596 no_chdir => 1}, topdir('fa') );
597
598 Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
599
600 %Expect_File = ();
bb7dc48b
JH
601
602 %Expect_Name = (file_path_name('fa') => 1,
603 file_path_name('fa', 'fa_ord') => 1,
604 file_path_name('fa', 'fsl') => 1,
605 file_path_name('fa', 'fsl', 'fb_ord') => 1,
606 file_path_name('fa', 'fsl', 'fba') => 1,
607 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
608 file_path_name('fa', 'fab') => 1,
609 file_path_name('fa', 'fab', 'fab_ord') => 1,
610 file_path_name('fa', 'fab', 'faba') => 1,
611 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
612 file_path_name('fa', 'faa') => 1,
3fa6e24b 613 file_path_name('fa', 'faa', 'faa_ord') => 1);
bb7dc48b 614
3fa6e24b 615 %Expect_Dir = ();
3fa6e24b 616
bb7dc48b
JH
617 File::Find::finddepth( {wanted => \&wanted_Name,
618 follow_fast => 1}, topdir('fa') );
619
620 Check( scalar(keys %Expect_Name) == 0 );
3fa6e24b 621
bb7dc48b
JH
622 # no_chdir is in effect, hence we use file_path_name to specify
623 # the expected paths for %Expect_File
624
625 %Expect_File = (file_path_name('fa') => 1,
626 file_path_name('fa', 'fa_ord') => 1,
627 file_path_name('fa', 'fsl') => 1,
628 file_path_name('fa', 'fsl', 'fb_ord') => 1,
629 file_path_name('fa', 'fsl', 'fba') => 1,
630 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
631 file_path_name('fa', 'fab') => 1,
632 file_path_name('fa', 'fab', 'fab_ord') => 1,
633 file_path_name('fa', 'fab', 'faba') => 1,
634 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
635 file_path_name('fa', 'faa') => 1,
3fa6e24b 636 file_path_name('fa', 'faa', 'faa_ord') => 1);
bb7dc48b 637
3fa6e24b
TW
638 %Expect_Name = ();
639 %Expect_Dir = ();
bb7dc48b
JH
640
641 File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
642 no_chdir => 1}, topdir('fa') );
643
3fa6e24b
TW
644 Check( scalar(keys %Expect_File) == 0 );
645
646
647 print "# check dangling symbolic links\n";
648 MkDir( dir_path('dangling_dir'), 0770 );
bb7dc48b
JH
649 CheckDie( symlink( dir_path('dangling_dir'),
650 file_path('dangling_dir_sl') ) );
3fa6e24b
TW
651 rmdir dir_path('dangling_dir');
652 touch(file_path('dangling_file'));
653 if ($^O eq 'MacOS') {
654 CheckDie( symlink('dangling_file', ':fa:dangling_file_sl') );
655 } else {
656 CheckDie( symlink('../dangling_file','fa/dangling_file_sl') );
657 }
658 unlink file_path('dangling_file');
659
660 {
661 # these tests should also emit a warning
bb7dc48b
JH
662 use warnings;
663
664 %Expect_File = (File::Spec->curdir => 1,
615a2b9b 665 file_path('dangling_file_sl') => 1,
bb7dc48b
JH
666 file_path('fa_ord') => 1,
667 file_path('fsl') => 1,
668 file_path('fb_ord') => 1,
669 file_path('fba') => 1,
670 file_path('fba_ord') => 1,
671 file_path('fab') => 1,
672 file_path('fab_ord') => 1,
673 file_path('faba') => 1,
674 file_path('faba_ord') => 1,
675 file_path('faa') => 1,
676 file_path('faa_ord') => 1);
677
3fa6e24b
TW
678 %Expect_Name = ();
679 %Expect_Dir = ();
680 undef $warn_msg;
bb7dc48b
JH
681
682 File::Find::find( {wanted => \&wanted_File, follow => 1,
683 dangling_symlinks =>
684 sub { $warn_msg = "$_[0] is a dangling symbolic link" }
685 },
686 topdir('dangling_dir_sl'), topdir('fa') );
687
3fa6e24b 688 Check( scalar(keys %Expect_File) == 0 );
615a2b9b 689 Check( $warn_msg =~ m|dangling_file_sl is a dangling symbolic link| );
bb7dc48b
JH
690 unlink file_path('fa', 'dangling_file_sl'),
691 file_path('dangling_dir_sl');
692
3fa6e24b
TW
693 }
694
695
696 print "# check recursion\n";
697 if ($^O eq 'MacOS') {
698 CheckDie( symlink(':fa:faa',':fa:faa:faa_sl') );
699 } else {
700 CheckDie( symlink('../faa','fa/faa/faa_sl') );
701 }
7e47e6ff 702 undef $@;
bb7dc48b
JH
703 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
704 no_chdir => 1}, topdir('fa') ); };
a1ccf0c4 705 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link|i );
3fa6e24b 706 unlink file_path('fa', 'faa', 'faa_sl');
7e47e6ff 707
3fa6e24b
TW
708
709 print "# check follow_skip (file)\n";
710 if ($^O eq 'MacOS') {
711 CheckDie( symlink(':fa:fa_ord',':fa:fa_ord_sl') ); # symlink to a file
712 } else {
713 CheckDie( symlink('./fa_ord','fa/fa_ord_sl') ); # symlink to a file
714 }
7e47e6ff 715 undef $@;
bb7dc48b
JH
716
717 eval {File::Find::finddepth( {wanted => \&simple_wanted,
718 follow => 1,
719 follow_skip => 0, no_chdir => 1},
720 topdir('fa') );};
721
a1ccf0c4 722 Check( $@ =~ m|for_find[:/]fa[:/]fa_ord encountered a second time|i );
7e47e6ff 723
7e47e6ff 724
bb7dc48b
JH
725 # no_chdir is in effect, hence we use file_path_name to specify
726 # the expected paths for %Expect_File
727
728 %Expect_File = (file_path_name('fa') => 1,
aaaf2301
NC
729 file_path_name('fa', 'fa_ord') => 2,
730 # We may encounter the symlink first
731 file_path_name('fa', 'fa_ord_sl') => 2,
bb7dc48b
JH
732 file_path_name('fa', 'fsl') => 1,
733 file_path_name('fa', 'fsl', 'fb_ord') => 1,
734 file_path_name('fa', 'fsl', 'fba') => 1,
735 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
736 file_path_name('fa', 'fab') => 1,
737 file_path_name('fa', 'fab', 'fab_ord') => 1,
738 file_path_name('fa', 'fab', 'faba') => 1,
739 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
740 file_path_name('fa', 'faa') => 1,
741 file_path_name('fa', 'faa', 'faa_ord') => 1);
3fa6e24b
TW
742
743 %Expect_Name = ();
bb7dc48b
JH
744
745 %Expect_Dir = (dir_path('fa') => 1,
746 dir_path('fa', 'faa') => 1,
747 dir_path('fa', 'fab') => 1,
748 dir_path('fa', 'fab', 'faba') => 1,
749 dir_path('fb') => 1,
750 dir_path('fb','fba') => 1);
751
752 File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
753 follow_skip => 1, no_chdir => 1},
3fa6e24b 754 topdir('fa') );
615a2b9b 755 Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
756 unlink file_path('fa', 'fa_ord_sl');
757
758
759 print "# check follow_skip (directory)\n";
760 if ($^O eq 'MacOS') {
761 CheckDie( symlink(':fa:faa',':fa:faa_sl') ); # symlink to a directory
762 } else {
763 CheckDie( symlink('./faa','fa/faa_sl') ); # symlink to a directory
7e47e6ff 764 }
3fa6e24b 765 undef $@;
bb7dc48b
JH
766
767 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
768 follow_skip => 0, no_chdir => 1},
3fa6e24b 769 topdir('fa') );};
bb7dc48b 770
a1ccf0c4 771 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
3fa6e24b
TW
772
773
774 undef $@;
bb7dc48b
JH
775
776 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
777 follow_skip => 1, no_chdir => 1},
3fa6e24b 778 topdir('fa') );};
bb7dc48b 779
a1ccf0c4 780 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
3fa6e24b 781
bb7dc48b
JH
782 # no_chdir is in effect, hence we use file_path_name to specify
783 # the expected paths for %Expect_File
784
785 %Expect_File = (file_path_name('fa') => 1,
786 file_path_name('fa', 'fa_ord') => 1,
787 file_path_name('fa', 'fsl') => 1,
788 file_path_name('fa', 'fsl', 'fb_ord') => 1,
789 file_path_name('fa', 'fsl', 'fba') => 1,
790 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
791 file_path_name('fa', 'fab') => 1,
792 file_path_name('fa', 'fab', 'fab_ord') => 1,
793 file_path_name('fa', 'fab', 'faba') => 1,
794 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
795 file_path_name('fa', 'faa') => 1,
aaaf2301
NC
796 file_path_name('fa', 'faa', 'faa_ord') => 1,
797 # We may actually encounter the symlink first.
798 file_path_name('fa', 'faa_sl') => 1,
799 file_path_name('fa', 'faa_sl', 'faa_ord') => 1);
3fa6e24b
TW
800
801 %Expect_Name = ();
bb7dc48b
JH
802
803 %Expect_Dir = (dir_path('fa') => 1,
804 dir_path('fa', 'faa') => 1,
805 dir_path('fa', 'fab') => 1,
806 dir_path('fa', 'fab', 'faba') => 1,
807 dir_path('fb') => 1,
808 dir_path('fb', 'fba') => 1);
809
810 File::Find::find( {wanted => \&wanted_File_Dir, follow => 1,
811 follow_skip => 2, no_chdir => 1}, topdir('fa') );
812
aaaf2301
NC
813 # If we encountered the symlink first, then the entries corresponding to
814 # the real name remain, if the real name first then the symlink
815 my @names = sort keys %Expect_File;
615a2b9b 816 Check( @names == 1 );
aaaf2301
NC
817 # Normalise both to the original name
818 s/_sl// foreach @names;
615a2b9b 819 Check ($names[0] eq file_path_name('fa', 'faa', 'faa_ord'));
3fa6e24b
TW
820 unlink file_path('fa', 'faa_sl');
821
17ab9c14 822}