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