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