Re: [perl #41555] Bug in File::Find on Windows when target
[p5sagit/p5-mst-13.2.git] / lib / File / Find / t / find.t
1 #!./perl
2 use strict;
3 use Cwd;
4
5 my %Expect_File = (); # what we expect for $_ 
6 my %Expect_Name = (); # what we expect for $File::Find::name/fullname
7 my %Expect_Dir  = (); # what we expect for $File::Find::dir
8 my $symlink_exists = eval { symlink("",""); 1 };
9 my ($warn_msg, @files, $file);
10
11
12 BEGIN {
13     require File::Spec;
14     chdir 't' if -d 't';
15     # May be doing dynamic loading while @INC is all relative
16     unshift @INC => File::Spec->rel2abs('../lib');
17
18     $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# $_[0]"; }
19 }
20
21 my $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
26 print "1..$test_count\n";
27 #if ( $symlink_exists ) { print "1..199\n"; }
28 #else                   { print "1..85\n";  }
29
30 my $orig_dir = cwd();
31
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
49 BEGIN {
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 }
62
63 cleanup();
64
65 $::count_commonsense = 0;
66 find({wanted => sub { ++$::count_commonsense if $_ eq 'commonsense.t'; } },
67    File::Spec->curdir);
68 if ($::count_commonsense == 1) {
69   print "ok 1\n";
70 } else {
71   print "not ok 1 # found $::count_commonsense files named 'commonsense.t'\n";
72 }
73
74 $::count_commonsense = 0;
75 finddepth({wanted => sub { ++$::count_commonsense if $_ eq 'commonsense.t'; } },
76         File::Spec->curdir);
77 if ($::count_commonsense == 1) {
78   print "ok 2\n";
79 } else {
80   print "not ok 2 # found $::count_commonsense files named 'commonsense.t'\n";
81 }
82
83 my $case = 2;
84 my $FastFileTests_OK = 0;
85
86 sub cleanup {
87     chdir($orig_dir);
88     my $need_updir = 0;
89     if (-d dir_path('for_find')) {
90         $need_updir = 1 if chdir(dir_path('for_find'));
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     }
107     if ($need_updir) {
108         my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
109         chdir($updir);
110     }
111     if (-d dir_path('for_find')) {
112         rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
113     }
114 }
115
116 END {
117     cleanup();
118 }
119
120 sub Check($) {
121     $case++;
122     if ($_[0]) { print "ok $case\n"; }
123     else       { print "not ok $case\n"; }
124 }
125
126 sub CheckDie($) {
127     $case++;
128     if ($_[0]) { print "ok $case\n"; }
129     else { print "not ok $case\n $!\n"; exit 0; }
130 }
131
132 sub touch {
133     CheckDie( open(my $T,'>',$_[0]) );
134 }
135
136 sub MkDir($$) {
137     CheckDie( mkdir($_[0],$_[1]) );
138 }
139
140 sub wanted_File_Dir {
141     printf "# \$File::Find::dir => '$File::Find::dir'\t\$_ => '$_'\n";
142     s#\.$## if ($^O eq 'VMS' && $_ ne '.');
143     s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
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 }
153
154 sub wanted_File_Dir_prune {
155     &wanted_File_Dir;
156     $File::Find::prune=1 if  $_ eq 'faba';
157 }
158
159 sub 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         }
169     }
170     Check($OK);
171     delete $Expect_Name{$n};
172 }
173
174 sub 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         }
183     }
184     Check($OK);
185     delete $Expect_File{ $_};
186 }
187
188 sub simple_wanted {
189     print "# \$File::Find::dir => '$File::Find::dir'\n";
190     print "# \$_ => '$_'\n";
191 }
192
193 sub noop_wanted {}
194
195 sub my_preprocess {
196     @files = @_;
197     print "# --preprocess--\n";
198     print "#   \$File::Find::dir => '$File::Find::dir' \n";
199     foreach $file (@files) {
200         $file =~ s/\.(dir)?$// if $^O eq 'VMS';
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;
210 }
211
212 sub my_postprocess {
213     print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
214     delete $Expect_Dir{ $File::Find::dir};
215 }
216
217
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.
221 #
222 # dir_path() concatenates directory names to form a *relative*
223 # directory path, independent from the platform it's run on, although
224 # there are limitations. Don't try to create an absolute path,
225 # because that may fail on operating systems that have the concept of
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
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.
232
233 sub dir_path {
234     my $first_arg = shift @_;
235
236     if ($first_arg eq '.') {
237         if ($^O eq 'MacOS') {
238             return '' unless @_;
239             # ignore first argument; return a relative path
240             # with leading ":" and with trailing ":"
241             return File::Spec->catdir(@_); 
242         } else { # other OS
243             return './' unless @_;
244             my $path = File::Spec->catdir(@_);
245             # add leading "./"
246             $path = "./$path";
247             return $path;
248         }
249
250     } else { # $first_arg ne '.'
251         return $first_arg unless @_; # return plain filename
252         return File::Spec->catdir($first_arg, @_); # relative path
253     }
254 }
255
256
257 # Use topdir() to specify a directory path that you want to pass to
258 # find/finddepth. Basically, topdir() does the same as dir_path() (see
259 # above), except that there's no trailing ":" on Mac OS.
260
261 sub topdir {
262     my $path = dir_path(@_);
263     $path =~ s/:$// if ($^O eq 'MacOS');
264     return $path;
265 }
266
267
268 # Use file_path() to specify a file path that's expected for $_
269 # (%Expect_File). Also suitable for file operations like unlink etc.
270 #
271 # file_path() concatenates directory names (if any) and a filename to
272 # form a *relative* file path (the last argument is assumed to be a
273 # file). It's independent from the platform it's run on, although
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.
280
281 sub file_path {
282     my $first_arg = shift @_;
283
284     if ($first_arg eq '.') {
285         if ($^O eq 'MacOS') {
286             return '' unless @_;
287             # ignore first argument; return a relative path  
288             # with leading ":", but without trailing ":"
289             return File::Spec->catfile(@_); 
290         } else { # other OS
291             return './' unless @_;
292             my $path = File::Spec->catfile(@_);
293             # add leading "./" 
294             $path = "./$path"; 
295             return $path;
296         }
297
298     } else { # $first_arg ne '.'
299         return $first_arg unless @_; # return plain filename
300         return File::Spec->catfile($first_arg, @_); # relative path
301     }
302 }
303
304
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 $_.
310 #
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.
314
315 sub file_path_name {
316     my $path = file_path(@_);
317     $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/));
318     return $path;
319 }
320
321
322
323 MkDir( dir_path('for_find'), 0770 );
324 CheckDie(chdir( dir_path('for_find')));
325 MkDir( dir_path('fa'), 0770 );
326 MkDir( dir_path('fb'), 0770  );
327 touch( file_path('fb', 'fb_ord') );
328 MkDir( dir_path('fb', 'fba'), 0770  );
329 touch( file_path('fb', 'fba', 'fba_ord') );
330 if ($^O eq 'MacOS') {
331       CheckDie( symlink(':fb',':fa:fsl') ) if $symlink_exists;
332 } else {
333       CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
334 }
335 touch( file_path('fa', 'fa_ord') );
336
337 MkDir( dir_path('fa', 'faa'), 0770  );
338 touch( file_path('fa', 'faa', 'faa_ord') );
339 MkDir( dir_path('fa', 'fab'), 0770  );
340 touch( file_path('fa', 'fab', 'fab_ord') );
341 MkDir( dir_path('fa', 'fab', 'faba'), 0770  );
342 touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
343
344
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,
348                 file_path('faa') => 1, file_path('faa_ord') => 1);
349
350 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
351 %Expect_Name = ();
352
353 %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
354                 dir_path('fab') => 1, dir_path('faba') => 1,
355                 dir_path('fb') => 1, dir_path('fba') => 1);
356
357 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
358 File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') ); 
359 Check( scalar(keys %Expect_File) == 0 );
360
361
362 print "# check re-entrancy\n";
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,
367                 file_path('faa') => 1, file_path('faa_ord') => 1);
368
369 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
370 %Expect_Name = ();
371
372 %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
373                 dir_path('fab') => 1, dir_path('faba') => 1,
374                 dir_path('fb') => 1, dir_path('fba') => 1);
375
376 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
377
378 File::Find::find( {wanted => sub { wanted_File_Dir_prune();
379                                     File::Find::find( {wanted => sub
380                                     {} }, File::Spec->curdir ); } },
381                                     topdir('fa') );
382
383 Check( 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
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
398 delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
399 %Expect_Name = ();
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
408 delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') }
409     unless $symlink_exists;
410
411 File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1},
412                   topdir('fa') ); Check( scalar(keys %Expect_File) == 0 );
413
414
415 %Expect_File = ();
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
432 delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
433 %Expect_Dir = (); 
434 File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
435 Check( scalar(keys %Expect_Name) == 0 );
436
437
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
456 delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
457 %Expect_Name = ();
458 %Expect_Dir = (); 
459
460 File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
461                      File::Spec->curdir );
462
463 Check( scalar(keys %Expect_File) == 0 );
464
465
466 print "# 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           );
478
479 File::Find::find( {wanted => \&noop_wanted,
480                    preprocess => \&my_preprocess}, File::Spec->curdir );
481
482 Check( scalar(keys %Expect_Dir) == 0 );
483
484
485 print "# 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           );
497
498 File::Find::find( {wanted => \&noop_wanted,
499                    postprocess => \&my_postprocess}, File::Spec->curdir );
500
501 Check( scalar(keys %Expect_Dir) == 0 );
502
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 }
515
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
551 if ( $symlink_exists ) {
552     print "# --- symbolic link tests --- \n";
553     $FastFileTests_OK= 1;
554
555
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  
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
573     %Expect_Name = ();
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
583     Check( scalar(keys %Expect_File) == 0 );  
584
585
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
602     %Expect_Name = ();
603
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 );
615
616     %Expect_File = ();
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,
629                     file_path_name('fa', 'faa', 'faa_ord') => 1);
630
631     %Expect_Dir = ();
632
633     File::Find::finddepth( {wanted => \&wanted_Name,
634                             follow_fast => 1}, topdir('fa') );
635
636     Check( scalar(keys %Expect_Name) == 0 );
637
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,
652                     file_path_name('fa', 'faa', 'faa_ord') => 1);
653
654     %Expect_Name = ();
655     %Expect_Dir = ();
656
657     File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
658                             no_chdir => 1}, topdir('fa') );
659
660     Check( scalar(keys %Expect_File) == 0 );     
661
662  
663     print "# check dangling symbolic links\n";
664     MkDir( dir_path('dangling_dir'), 0770 );
665     CheckDie( symlink( dir_path('dangling_dir'),
666                        file_path('dangling_dir_sl') ) );
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
678         use warnings;
679
680         %Expect_File = (File::Spec->curdir => 1,
681                         file_path('dangling_file_sl') => 1,
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
694         %Expect_Name = ();
695         %Expect_Dir = ();
696         undef $warn_msg;
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
704         Check( scalar(keys %Expect_File) == 0 );
705         Check( $warn_msg =~ m|dangling_file_sl is a dangling symbolic link| );  
706         unlink file_path('fa', 'dangling_file_sl'),
707                          file_path('dangling_dir_sl');
708
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     }
718     undef $@;
719     eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
720                              no_chdir => 1}, topdir('fa') ); };
721     Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link|i );  
722     unlink file_path('fa', 'faa', 'faa_sl'); 
723
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     }
731     undef $@;
732
733     eval {File::Find::finddepth( {wanted => \&simple_wanted,
734                                   follow => 1,
735                                   follow_skip => 0, no_chdir => 1},
736                                   topdir('fa') );};
737
738     Check( $@ =~ m|for_find[:/]fa[:/]fa_ord encountered a second time|i );
739
740
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,
745                     file_path_name('fa', 'fa_ord') => 2,
746                     # We may encounter the symlink first
747                     file_path_name('fa', 'fa_ord_sl') => 2,
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);
758
759     %Expect_Name = ();
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},
770                            topdir('fa') );
771     Check( scalar(keys %Expect_File) == 0 );
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
780     }
781     undef $@;
782
783     eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
784                             follow_skip => 0, no_chdir => 1},
785                             topdir('fa') );};
786
787     Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
788
789   
790     undef $@;
791
792     eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
793                             follow_skip => 1, no_chdir => 1},
794                             topdir('fa') );};
795
796     Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );  
797
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,
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);
816
817     %Expect_Name = ();
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
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;
832     Check( @names == 1 );
833     # Normalise both to the original name
834     s/_sl// foreach @names;
835     Check ($names[0] eq file_path_name('fa', 'faa', 'faa_ord'));
836     unlink file_path('fa', 'faa_sl');
837
838 }
839
840
841 # Win32 checks  - [perl #41555]
842 if ($^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 }