This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
File::Find find.t: switch to done_testing()
[perl5.git] / ext / File-Find / t / find.t
1 #!./perl
2 use strict;
3 use Cwd;
4
5 my $warn_msg;
6
7 BEGIN {
8     require File::Spec;
9     if ($ENV{PERL_CORE}) {
10         # May be doing dynamic loading while @INC is all relative
11         @INC = map { $_ = File::Spec->rel2abs($_); /(.*)/; $1 } @INC;
12     }
13     $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# $_[0]"; };
14
15     if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'VMS') {
16         # This is a hack - at present File::Find does not produce native names
17         # on Win32 or VMS, so force File::Spec to use Unix names.
18         # must be set *before* importing File::Find
19         require File::Spec::Unix;
20         @File::Spec::ISA = 'File::Spec::Unix';
21     }
22     require File::Find;
23     import File::Find;
24 }
25
26 my $symlink_exists = eval { symlink("",""); 1 };
27
28 use Test::More;
29 use lib qw( ./t/lib );
30 use Testing qw(
31     create_file_ok
32     mkdir_ok
33     symlink_ok
34     dir_path
35     file_path
36 );
37
38 my %Expect_File = (); # what we expect for $_
39 my %Expect_Name = (); # what we expect for $File::Find::name/fullname
40 my %Expect_Dir  = (); # what we expect for $File::Find::dir
41 my (@files);
42
43 my $orig_dir = cwd();
44
45 # Uncomment this to see where File::Find is chdir-ing to.  Helpful for
46 # debugging its little jaunts around the filesystem.
47 # BEGIN {
48 #     use Cwd;
49 #     *CORE::GLOBAL::chdir = sub ($) {
50 #         my($file, $line) = (caller)[1,2];
51 #
52 #         printf "# cwd:      %s\n", cwd();
53 #         print "# chdir: @_ from $file at $line\n";
54 #         my($return) = CORE::chdir($_[0]);
55 #         printf "# newcwd:   %s\n", cwd();
56 #
57 #         return $return;
58 #     };
59 # }
60
61 cleanup();
62
63 ##### Sanity checks #####
64 # Do find() and finddepth() work correctly with an empty list of
65 # directories?
66 {
67     ok(eval { find(\&noop_wanted); 1 },
68        "'find' successfully returned for an empty list of directories");
69
70     ok(eval { finddepth(\&noop_wanted); 1 },
71        "'finddepth' successfully returned for an empty list of directories");
72 }
73
74 # Do find() and finddepth() work correctly in the directory
75 # from which we start?  (Test presumes the presence of 'taint.t' in same
76 # directory as this test file.)
77
78 $::count_taint = 0;
79 find({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
80    File::Spec->curdir);
81 is($::count_taint, 1, "'find' found exactly 1 file named 'taint.t'");
82
83 $::count_taint = 0;
84 finddepth({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
85     File::Spec->curdir);
86 is($::count_taint, 1, "'finddepth' found exactly 1 file named 'taint.t'");
87
88 my $FastFileTests_OK = 0;
89
90 sub cleanup {
91     chdir($orig_dir);
92     my $need_updir = 0;
93     if (-d dir_path('for_find')) {
94         $need_updir = 1 if chdir(dir_path('for_find'));
95     }
96     if (-d dir_path('fa')) {
97     unlink file_path('fa', 'fa_ord'),
98            file_path('fa', 'fsl'),
99            file_path('fa', 'faa', 'faa_ord'),
100            file_path('fa', 'fab', 'fab_ord'),
101            file_path('fa', 'fab', 'faba', 'faba_ord'),
102            file_path('fa', 'fac', 'faca'),
103            file_path('fb', 'fb_ord'),
104            file_path('fb', 'fba', 'fba_ord'),
105            file_path('fb', 'fbc', 'fbca'),
106            file_path('fa', 'fax', 'faz'),
107            file_path('fa', 'fay');
108     rmdir dir_path('fa', 'faa');
109     rmdir dir_path('fa', 'fab', 'faba');
110     rmdir dir_path('fa', 'fab');
111     rmdir dir_path('fa', 'fac');
112     rmdir dir_path('fa', 'fax');
113     rmdir dir_path('fa');
114     rmdir dir_path('fb', 'fba');
115     rmdir dir_path('fb', 'fbc');
116     rmdir dir_path('fb');
117     }
118     if (-d dir_path('fc')) {
119         unlink (
120             file_path('fc', 'fca', 'match_alpha'),
121             file_path('fc', 'fca', 'match_beta'),
122             file_path('fc', 'fcb', 'match_gamma'),
123             file_path('fc', 'fcb', 'delta'),
124             file_path('fc', 'fcc', 'match_epsilon'),
125             file_path('fc', 'fcc', 'match_zeta'),
126             file_path('fc', 'fcc', 'eta'),
127         );
128         rmdir dir_path('fc', 'fca');
129         rmdir dir_path('fc', 'fcb');
130         rmdir dir_path('fc', 'fcc');
131         rmdir dir_path('fc');
132     }
133     if ($need_updir) {
134         my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
135         chdir($updir);
136     }
137     if (-d dir_path('for_find')) {
138         rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
139     }
140 }
141
142 END {
143     cleanup();
144 }
145
146 sub wanted_File_Dir {
147     print "# \$File::Find::dir => '$File::Find::dir'\t\$_ => '$_'\n";
148     s#\.$## if ($^O eq 'VMS' && $_ ne '.'); #
149     s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
150     ok( $Expect_File{$_}, "found $_ for \$_, as expected" );
151     if ( $FastFileTests_OK ) {
152         delete $Expect_File{$_}
153           unless ( $Expect_Dir{$_} && ! -d _ );
154     }
155     else {
156         delete $Expect_File{$_}
157           unless ( $Expect_Dir{$_} && ! -d $_ );
158     }
159 }
160
161 sub wanted_File_Dir_prune {
162     &wanted_File_Dir;
163     $File::Find::prune = 1 if  $_ eq 'faba';
164 }
165
166 sub wanted_Name {
167     my $n = $File::Find::name;
168     $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.'); #
169     print "# \$File::Find::name => '$n'\n";
170     my $i = rindex($n,'/');
171     my $OK = exists($Expect_Name{$n});
172     if ( $OK ) {
173         $OK= exists($Expect_Name{substr($n,0,$i)})  if $i >= 0;
174     }
175     ok( $OK, "found $n for \$File::Find::name, as expected" );
176     delete $Expect_Name{$n};
177 }
178
179 sub wanted_File {
180     print "# \$_ => '$_'\n";
181     s#\.$## if ($^O eq 'VMS' && $_ ne '.'); #
182     my $i = rindex($_,'/');
183     my $OK = exists($Expect_File{ $_});
184     if ( $OK ) {
185         $OK= exists($Expect_File{ substr($_,0,$i)})  if $i >= 0;
186     }
187     ok( $OK, "found $_ for \$_, as expected" );
188     delete $Expect_File{ $_};
189 }
190
191 sub simple_wanted {
192     print "# \$File::Find::dir => '$File::Find::dir'\n";
193     print "# \$_ => '$_'\n";
194 }
195
196 sub noop_wanted {}
197
198 sub my_preprocess {
199     @files = @_;
200     print "# --preprocess--\n";
201     print "#   \$File::Find::dir => '$File::Find::dir' \n";
202     foreach my $file (@files) {
203         $file =~ s/\.(dir)?$//i if $^O eq 'VMS';
204         print "#   $file \n";
205         delete $Expect_Dir{ $File::Find::dir }->{$file};
206     }
207     print "# --end preprocess--\n";
208     is(scalar(keys %{$Expect_Dir{ $File::Find::dir }}), 0,
209         "my_preprocess: got 0, as expected");
210     if (scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0) {
211         delete $Expect_Dir{ $File::Find::dir }
212     }
213     return @files;
214 }
215
216 sub my_postprocess {
217     print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
218     delete $Expect_Dir{ $File::Find::dir};
219 }
220
221 # Use topdir() to specify a directory path that you want to pass to
222 # find/finddepth. Historically topdir() differed on Mac OS classic.
223
224 *topdir = \&dir_path;
225
226 # Use file_path_name() to specify a file path that is expected for
227 # $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
228 # option is in effect, $_ is the same as $File::Find::Name. In that
229 # case, also use this function to specify a file path that is expected
230 # for $_.
231 #
232 # Historically file_path_name differed on Mac OS classic.
233
234 *file_path_name = \&file_path;
235
236 ##### Create directories, files and symlinks used in testing #####
237
238 mkdir_ok( dir_path('for_find'), 0770 );
239 ok( chdir( dir_path('for_find')), "Able to chdir to 'for_find'")
240     or die("Unable to chdir to 'for_find'");
241
242 my @testing_basenames = ( qw| fb_ord fba_ord fa_ord faa_ord fab_ord faba_ord | );
243
244 mkdir_ok( dir_path('fa'), 0770 );
245 mkdir_ok( dir_path('fb'), 0770  );
246 create_file_ok( file_path('fb', $testing_basenames[0]) );
247 mkdir_ok( dir_path('fb', 'fba'), 0770  );
248 create_file_ok( file_path('fb', 'fba', $testing_basenames[1]) );
249 if ($symlink_exists) {
250     symlink_ok('../fb','fa/fsl');
251 }
252 create_file_ok( file_path('fa', $testing_basenames[2]) );
253
254 mkdir_ok( dir_path('fa', 'faa'), 0770  );
255 create_file_ok( file_path('fa', 'faa', $testing_basenames[3]) );
256 mkdir_ok( dir_path('fa', 'fab'), 0770  );
257 create_file_ok( file_path('fa', 'fab', $testing_basenames[4]) );
258 mkdir_ok( dir_path('fa', 'fab', 'faba'), 0770  );
259 create_file_ok( file_path('fa', 'fab', 'faba', $testing_basenames[5]) );
260
261 ##### RT #122547 #####
262 # Do find() and finddepth() correctly warn on invalid options?
263 ##### RT #133771 #####
264 # When running tests in parallel, avoid clash with tests in
265 # ext/File-Find/t/taint by moving into the temporary testing directory
266 # before testing for warnings on invalid options.
267
268 my %tb = map { $_ => 1 } @testing_basenames;
269
270 {
271     my $bad_option = 'foobar';
272     my $second_bad_option = 'really_foobar';
273
274     $::count_tb = 0;
275     local $SIG{__WARN__} = sub { $warn_msg = $_[0]; };
276     {
277         find(
278             {
279                 wanted => sub { s#\.$## if ($^O eq 'VMS' && $_ ne '.');
280                                 ++$::count_tb if $tb{$_};
281                               },
282                 $bad_option => undef,
283             },
284             File::Spec->curdir
285         );
286     };
287     like($warn_msg, qr/Invalid option/s, "Got warning for invalid option");
288     like($warn_msg, qr/$bad_option/s, "Got warning for $bad_option");
289     is($::count_tb, scalar(@testing_basenames), "count_tb incremented");
290     undef $warn_msg;
291
292     $::count_tb = 0;
293     {
294         finddepth(
295             {
296                 wanted => sub { s#\.$## if ($^O eq 'VMS' && $_ ne '.');
297                                 ++$::count_tb if $tb{$_};
298                               },
299                 $bad_option => undef,
300                 $second_bad_option => undef,
301             },
302             File::Spec->curdir
303         );
304     };
305     like($warn_msg, qr/Invalid option/s, "Got warning for invalid option");
306     like($warn_msg, qr/$bad_option/s, "Got warning for $bad_option");
307     like($warn_msg, qr/$second_bad_option/s, "Got warning for $second_bad_option");
308     is($::count_tb, scalar(@testing_basenames), "count_tb incremented");
309     undef $warn_msg;
310 }
311
312 ##### Basic tests for find() #####
313 # Set up list of files we expect to find.
314 # Run find(), removing a file from the list once we have found it.
315 # The list should be empty once we are done.
316
317 %Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
318                 file_path('fa_ord') => 1, file_path('fab') => 1,
319                 file_path('fab_ord') => 1, file_path('faba') => 1,
320                 file_path('faa') => 1, file_path('faa_ord') => 1);
321
322 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
323 %Expect_Name = ();
324
325 %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
326                 dir_path('fab') => 1, dir_path('faba') => 1,
327                 dir_path('fb') => 1, dir_path('fba') => 1);
328
329 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
330 File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') );
331 is( scalar(keys %Expect_File), 0, "COMPLETE: Basic test of find()" );
332
333 ##### Re-entrancy #####
334
335 print "# check re-entrancy\n";
336
337 %Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
338                 file_path('fa_ord') => 1, file_path('fab') => 1,
339                 file_path('fab_ord') => 1, file_path('faba') => 1,
340                 file_path('faa') => 1, file_path('faa_ord') => 1);
341
342 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
343 %Expect_Name = ();
344
345 %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
346                 dir_path('fab') => 1, dir_path('faba') => 1,
347                 dir_path('fb') => 1, dir_path('fba') => 1);
348
349 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
350
351 File::Find::find( {wanted => sub { wanted_File_Dir_prune();
352                                     File::Find::find( {wanted => sub
353                                     {} }, File::Spec->curdir ); } },
354                                     topdir('fa') );
355
356 is( scalar(keys %Expect_File), 0, "COMPLETE: Test of find() for re-entrancy" );
357
358 ##### 'no_chdir' option #####
359 # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
360
361 %Expect_File = (file_path_name('fa') => 1,
362         file_path_name('fa', 'fsl') => 1,
363         file_path_name('fa', 'fa_ord') => 1,
364         file_path_name('fa', 'fab') => 1,
365         file_path_name('fa', 'fab', 'fab_ord') => 1,
366         file_path_name('fa', 'fab', 'faba') => 1,
367         file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
368         file_path_name('fa', 'faa') => 1,
369         file_path_name('fa', 'faa', 'faa_ord') => 1,);
370
371 delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
372 %Expect_Name = ();
373
374 %Expect_Dir = (dir_path('fa') => 1,
375         dir_path('fa', 'faa') => 1,
376         dir_path('fa', 'fab') => 1,
377         dir_path('fa', 'fab', 'faba') => 1,
378         dir_path('fb') => 1,
379         dir_path('fb', 'fba') => 1);
380
381 delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') }
382     unless $symlink_exists;
383
384 File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1},
385           topdir('fa') );
386 is( scalar(keys %Expect_File), 0, "COMPLETE: Test of 'no_chdir' option" );
387
388 ##### Test for $File::Find::name #####
389
390 %Expect_File = ();
391
392 %Expect_Name = (File::Spec->curdir => 1,
393         file_path_name('.', 'fa') => 1,
394         file_path_name('.', 'fa', 'fsl') => 1,
395         file_path_name('.', 'fa', 'fa_ord') => 1,
396         file_path_name('.', 'fa', 'fab') => 1,
397         file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
398         file_path_name('.', 'fa', 'fab', 'faba') => 1,
399         file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
400         file_path_name('.', 'fa', 'faa') => 1,
401         file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
402         file_path_name('.', 'fb') => 1,
403         file_path_name('.', 'fb', 'fba') => 1,
404         file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
405         file_path_name('.', 'fb', 'fb_ord') => 1);
406
407 delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
408 %Expect_Dir = ();
409 File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
410 is( scalar(keys %Expect_Name), 0, "COMPLETE: Test for \$File::Find::name" );
411
412
413 ##### #####
414 # no_chdir is in effect, hence we use file_path_name to specify the
415 # expected paths for %Expect_File
416
417 %Expect_File = (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_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
433 %Expect_Name = ();
434 %Expect_Dir = ();
435
436 File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
437              File::Spec->curdir );
438
439 is( scalar(keys %Expect_File), 0,
440     "COMPLETE: Equivalency of \$_ and \$File::Find::Name with 'no_chdir'" );
441
442 ##### #####
443
444 print "# check preprocess\n";
445 %Expect_File = ();
446 %Expect_Name = ();
447 %Expect_Dir = (
448          File::Spec->curdir                 => {fa => 1, fb => 1},
449          dir_path('.', 'fa')                => {faa => 1, fab => 1, fa_ord => 1},
450          dir_path('.', 'fa', 'faa')         => {faa_ord => 1},
451          dir_path('.', 'fa', 'fab')         => {faba => 1, fab_ord => 1},
452          dir_path('.', 'fa', 'fab', 'faba') => {faba_ord => 1},
453          dir_path('.', 'fb')                => {fba => 1, fb_ord => 1},
454          dir_path('.', 'fb', 'fba')         => {fba_ord => 1}
455          );
456
457 File::Find::find( {wanted => \&noop_wanted,
458          preprocess => \&my_preprocess}, File::Spec->curdir );
459
460 is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" );
461
462 ##### #####
463
464 print "# check postprocess\n";
465 %Expect_File = ();
466 %Expect_Name = ();
467 %Expect_Dir = (
468          File::Spec->curdir                 => 1,
469          dir_path('.', 'fa')                => 1,
470          dir_path('.', 'fa', 'faa')         => 1,
471          dir_path('.', 'fa', 'fab')         => 1,
472          dir_path('.', 'fa', 'fab', 'faba') => 1,
473          dir_path('.', 'fb')                => 1,
474          dir_path('.', 'fb', 'fba')         => 1
475          );
476
477 File::Find::find( {wanted => \&noop_wanted,
478          postprocess => \&my_postprocess}, File::Spec->curdir );
479
480 is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" );
481
482 ##### #####
483 {
484     print "# checking argument localization\n";
485
486     ### this checks the fix of perlbug [19977] ###
487     my @foo = qw( a b c d e f );
488     my %pre = map { $_ => } @foo;
489
490     File::Find::find( sub {  } , 'fa' ) for @foo;
491     delete $pre{$_} for @foo;
492
493     is( scalar(keys %pre), 0, "Got no files, as expected" );
494 }
495
496 ##### #####
497 # see thread starting
498 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-02/msg00351.html
499 {
500     print "# checking that &_ and %_ are still accessible and that\n",
501     "# tie magic on \$_ is not triggered\n";
502
503     my $true_count;
504     my $sub = 0;
505     sub _ {
506         ++$sub;
507     }
508     my $tie_called = 0;
509
510     package Foo;
511     sub STORE {
512         ++$tie_called;
513     }
514     sub FETCH {return 'N'};
515     sub TIESCALAR {bless []};
516     package main;
517
518     is( scalar(keys %_), 0, "Got no files, as expected" );
519     my @foo = 'n';
520     tie $foo[0], "Foo";
521
522     File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo;
523     untie $_;
524
525     is( $tie_called, 0, "Got no files tie_called, as expected" );
526     is( scalar(keys %_), $true_count, "Got true count, as expected" );
527     is( $sub, $true_count, "Got true count, as expected" );
528     is( scalar( @foo), 1, "Got one file, as expected" );
529     is( $foo[0], 'N', "Got 'N', as expected" );
530 }
531
532 ##### #####
533 if ( $symlink_exists ) {
534     print "# --- symbolic link tests --- \n";
535     $FastFileTests_OK= 1;
536
537     # 'follow', 'follow_fast' and 'follow_skip' options only apply when a
538     # platform supports symlinks.
539
540     ##### #####
541
542     # Verify that File::Find::find will call wanted even if the topdir
543     # is a symlink to a directory, and it should not follow the link
544     # unless follow is set, which it is not in this case
545     %Expect_File = ( file_path('fsl') => 1 );
546     %Expect_Name = ();
547     %Expect_Dir = ();
548     File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa', 'fsl') );
549     is( scalar(keys %Expect_File), 0,
550         "COMPLETE: top dir can be symlink to dir; link not followed without 'follow' option" );
551
552     ##### #####
553
554     %Expect_File = (File::Spec->curdir => 1, file_path('fa_ord') => 1,
555                     file_path('fsl') => 1, file_path('fb_ord') => 1,
556                     file_path('fba') => 1, file_path('fba_ord') => 1,
557                     file_path('fab') => 1, file_path('fab_ord') => 1,
558                     file_path('faba') => 1, file_path('faa') => 1,
559                     file_path('faa_ord') => 1);
560
561     %Expect_Name = ();
562
563     %Expect_Dir = (File::Spec->curdir => 1, dir_path('fa') => 1,
564                    dir_path('faa') => 1, dir_path('fab') => 1,
565                    dir_path('faba') => 1, dir_path('fb') => 1,
566                    dir_path('fba') => 1);
567
568     File::Find::find( {wanted => \&wanted_File_Dir_prune,
569                follow_fast => 1}, topdir('fa') );
570
571     is( scalar(keys %Expect_File), 0,
572         "COMPLETE: test of 'follow_fast' option: \$_ case" );
573
574     ##### #####
575
576     # no_chdir is in effect, hence we use file_path_name to specify
577     # the expected paths for %Expect_File
578
579     %Expect_File = (file_path_name('fa') => 1,
580             file_path_name('fa', 'fa_ord') => 1,
581             file_path_name('fa', 'fsl') => 1,
582             file_path_name('fa', 'fsl', 'fb_ord') => 1,
583             file_path_name('fa', 'fsl', 'fba') => 1,
584             file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
585             file_path_name('fa', 'fab') => 1,
586             file_path_name('fa', 'fab', 'fab_ord') => 1,
587             file_path_name('fa', 'fab', 'faba') => 1,
588             file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
589             file_path_name('fa', 'faa') => 1,
590             file_path_name('fa', 'faa', 'faa_ord') => 1);
591
592     %Expect_Name = ();
593
594     %Expect_Dir = (dir_path('fa') => 1,
595             dir_path('fa', 'faa') => 1,
596             dir_path('fa', 'fab') => 1,
597             dir_path('fa', 'fab', 'faba') => 1,
598             dir_path('fb') => 1,
599             dir_path('fb', 'fba') => 1);
600
601     File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
602                no_chdir => 1}, topdir('fa') );
603
604     is( scalar(keys %Expect_File), 0,
605         "COMPLETE: Test of 'follow_fast' and 'no_chdir' options together: \$_ case" );
606
607     ##### #####
608
609     %Expect_File = ();
610
611     %Expect_Name = (file_path_name('fa') => 1,
612             file_path_name('fa', 'fa_ord') => 1,
613             file_path_name('fa', 'fsl') => 1,
614             file_path_name('fa', 'fsl', 'fb_ord') => 1,
615             file_path_name('fa', 'fsl', 'fba') => 1,
616             file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
617             file_path_name('fa', 'fab') => 1,
618             file_path_name('fa', 'fab', 'fab_ord') => 1,
619             file_path_name('fa', 'fab', 'faba') => 1,
620             file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
621             file_path_name('fa', 'faa') => 1,
622             file_path_name('fa', 'faa', 'faa_ord') => 1);
623
624     %Expect_Dir = ();
625
626     File::Find::finddepth( {wanted => \&wanted_Name,
627             follow_fast => 1}, topdir('fa') );
628
629     is( scalar(keys %Expect_Name), 0,
630         "COMPLETE: test of 'follow_fast' option: \$File::Find::name case" );
631
632     ##### #####
633
634     # no_chdir is in effect, hence we use file_path_name to specify
635     # the expected paths for %Expect_File
636
637     %Expect_File = (file_path_name('fa') => 1,
638             file_path_name('fa', 'fa_ord') => 1,
639             file_path_name('fa', 'fsl') => 1,
640             file_path_name('fa', 'fsl', 'fb_ord') => 1,
641             file_path_name('fa', 'fsl', 'fba') => 1,
642             file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
643             file_path_name('fa', 'fab') => 1,
644             file_path_name('fa', 'fab', 'fab_ord') => 1,
645             file_path_name('fa', 'fab', 'faba') => 1,
646             file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
647             file_path_name('fa', 'faa') => 1,
648             file_path_name('fa', 'faa', 'faa_ord') => 1);
649
650     %Expect_Name = ();
651     %Expect_Dir = ();
652
653     File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
654             no_chdir => 1}, topdir('fa') );
655
656     is( scalar(keys %Expect_File), 0,
657         "COMPLETE: Test of 'follow_fast' and 'no_chdir' options together: \$File::Find::name case" );
658
659     ##### #####
660
661     print "# check dangling symbolic links\n";
662     mkdir_ok( dir_path('dangling_dir'), 0770 );
663     symlink_ok( dir_path('dangling_dir'), file_path('dangling_dir_sl'),
664         "Check dangling directory" );
665     rmdir dir_path('dangling_dir');
666     create_file_ok(file_path('dangling_file'));
667     symlink_ok('../dangling_file','fa/dangling_file_sl',
668         "Check dangling file" );
669     unlink file_path('dangling_file');
670
671     {
672         # these tests should also emit a warning
673     use warnings;
674
675         %Expect_File = (File::Spec->curdir => 1,
676             file_path('dangling_file_sl') => 1,
677             file_path('fa_ord') => 1,
678             file_path('fsl') => 1,
679             file_path('fb_ord') => 1,
680             file_path('fba') => 1,
681             file_path('fba_ord') => 1,
682             file_path('fab') => 1,
683             file_path('fab_ord') => 1,
684             file_path('faba') => 1,
685             file_path('faba_ord') => 1,
686             file_path('faa') => 1,
687             file_path('faa_ord') => 1);
688
689         %Expect_Name = ();
690         %Expect_Dir = ();
691         undef $warn_msg;
692
693         File::Find::find( {wanted => \&wanted_File, follow => 1,
694                dangling_symlinks =>
695                    sub { $warn_msg = "$_[0] is a dangling symbolic link" }
696                            },
697                            topdir('dangling_dir_sl'), topdir('fa') );
698
699         is( scalar(keys %Expect_File), 0,
700             "COMPLETE: test of 'follow' and 'dangling_symlinks' options" );
701         like( $warn_msg, qr/dangling_file_sl is a dangling symbolic link/,
702             "Got expected warning message re dangling symbolic link" );
703         unlink file_path('fa', 'dangling_file_sl'),
704             file_path('dangling_dir_sl');
705
706     }
707
708     ##### #####
709
710     print "# check recursion\n";
711     symlink_ok('../faa','fa/faa/faa_sl');
712     undef $@;
713     eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
714                              no_chdir => 1}, topdir('fa') ); };
715     like(
716         $@,
717         qr{for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link}i,
718         "Got expected error message for recursive symbolic link"
719     );
720     unlink file_path('fa', 'faa', 'faa_sl');
721
722
723     print "# check follow_skip (file)\n";
724     symlink_ok('./fa_ord','fa/fa_ord_sl');
725     undef $@;
726
727     eval {File::Find::finddepth( {wanted => \&simple_wanted,
728                                   follow => 1,
729                                   follow_skip => 0, no_chdir => 1},
730                                   topdir('fa') );};
731
732     like(
733         $@,
734         qr{for_find[:/]fa[:/]fa_ord encountered a second time}i,
735         "'follow_skip==0': got error message when file encountered a second time"
736     );
737
738     ##### #####
739
740     # no_chdir is in effect, hence we use file_path_name to specify
741     # the expected paths for %Expect_File
742
743     %Expect_File = (file_path_name('fa') => 1,
744             file_path_name('fa', 'fa_ord') => 2,
745             # We may encounter the symlink first
746             file_path_name('fa', 'fa_ord_sl') => 2,
747             file_path_name('fa', 'fsl') => 1,
748             file_path_name('fa', 'fsl', 'fb_ord') => 1,
749             file_path_name('fa', 'fsl', 'fba') => 1,
750             file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
751             file_path_name('fa', 'fab') => 1,
752             file_path_name('fa', 'fab', 'fab_ord') => 1,
753             file_path_name('fa', 'fab', 'faba') => 1,
754             file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
755             file_path_name('fa', 'faa') => 1,
756             file_path_name('fa', 'faa', 'faa_ord') => 1);
757
758     %Expect_Name = ();
759
760     %Expect_Dir = (dir_path('fa') => 1,
761             dir_path('fa', 'faa') => 1,
762             dir_path('fa', 'fab') => 1,
763             dir_path('fa', 'fab', 'faba') => 1,
764             dir_path('fb') => 1,
765             dir_path('fb','fba') => 1);
766
767     File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
768                            follow_skip => 1, no_chdir => 1},
769                            topdir('fa') );
770     is( scalar(keys %Expect_File), 0,
771         "COMPLETE: Test of 'follow', 'follow_skip==1' and 'no_chdir' options" );
772     unlink file_path('fa', 'fa_ord_sl');
773
774     ##### #####
775     print "# check follow_skip (directory)\n";
776     symlink_ok('./faa','fa/faa_sl');
777     undef $@;
778
779     eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
780                             follow_skip => 0, no_chdir => 1},
781                             topdir('fa') );};
782
783     like(
784         $@,
785         qr{for_find[:/]fa[:/]faa[:/]? encountered a second time}i,
786         "'follow_skip==0': got error message when directory encountered a second time"
787     );
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     like(
797         $@,
798         qr{for_find[:/]fa[:/]faa[:/]? encountered a second time}i,
799         "'follow_skip==1': got error message when directory encountered a second time"
800      );
801
802     ##### #####
803
804     # no_chdir is in effect, hence we use file_path_name to specify
805     # the expected paths for %Expect_File
806
807     %Expect_File = (file_path_name('fa') => 1,
808             file_path_name('fa', 'fa_ord') => 1,
809             file_path_name('fa', 'fsl') => 1,
810             file_path_name('fa', 'fsl', 'fb_ord') => 1,
811             file_path_name('fa', 'fsl', 'fba') => 1,
812             file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
813             file_path_name('fa', 'fab') => 1,
814             file_path_name('fa', 'fab', 'fab_ord') => 1,
815             file_path_name('fa', 'fab', 'faba') => 1,
816             file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
817             file_path_name('fa', 'faa') => 1,
818             file_path_name('fa', 'faa', 'faa_ord') => 1,
819             # We may actually encounter the symlink first.
820             file_path_name('fa', 'faa_sl') => 1,
821             file_path_name('fa', 'faa_sl', 'faa_ord') => 1);
822
823     %Expect_Name = ();
824
825     %Expect_Dir = (dir_path('fa') => 1,
826             dir_path('fa', 'faa') => 1,
827             dir_path('fa', 'fab') => 1,
828             dir_path('fa', 'fab', 'faba') => 1,
829             dir_path('fb') => 1,
830             dir_path('fb', 'fba') => 1);
831
832     File::Find::find( {wanted => \&wanted_File_Dir, follow => 1,
833                follow_skip => 2, no_chdir => 1}, topdir('fa') );
834
835     ##### #####
836
837     # If we encountered the symlink first, then the entries corresponding to
838     # the real name remain, if the real name first then the symlink
839     my @names = sort keys %Expect_File;
840     is( scalar(@names), 1,
841         "'follow_skip==2'" );
842     # Normalise both to the original name
843     s/_sl// foreach @names;
844     is(
845         $names[0],
846         file_path_name('fa', 'faa', 'faa_ord'),
847         "Got file_path_name, as expected"
848     );
849     unlink file_path('fa', 'faa_sl');
850
851 }
852
853 ##### Win32 checks  - [perl #41555] #####
854
855 if ($^O eq 'MSWin32') {
856     require File::Spec::Win32;
857     my ($volume) = File::Spec::Win32->splitpath($orig_dir, 1);
858     print STDERR "VOLUME = $volume\n";
859
860     ##### #####
861
862     # with chdir
863     %Expect_File = (File::Spec->curdir => 1,
864                     file_path('fsl') => 1,
865                     file_path('fa_ord') => 1,
866                     file_path('fab') => 1,
867                     file_path('fab_ord') => 1,
868                     file_path('faba') => 1,
869                     file_path('faba_ord') => 1,
870                     file_path('faa') => 1,
871                     file_path('faa_ord') => 1);
872
873     delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
874     %Expect_Name = ();
875
876     %Expect_Dir = (dir_path('fa') => 1,
877                    dir_path('faa') => 1,
878                    dir_path('fab') => 1,
879                    dir_path('faba') => 1,
880                    dir_path('fb') => 1,
881                    dir_path('fba') => 1);
882
883     File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa'));
884     is( scalar(keys %Expect_File), 0, "Got no files, as expected" );
885
886     ##### #####
887
888     # no_chdir
889     %Expect_File = ($volume . file_path_name('fa') => 1,
890                     $volume . file_path_name('fa', 'fsl') => 1,
891                     $volume . file_path_name('fa', 'fa_ord') => 1,
892                     $volume . file_path_name('fa', 'fab') => 1,
893                     $volume . file_path_name('fa', 'fab', 'fab_ord') => 1,
894                     $volume . file_path_name('fa', 'fab', 'faba') => 1,
895                     $volume . file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
896                     $volume . file_path_name('fa', 'faa') => 1,
897                     $volume . file_path_name('fa', 'faa', 'faa_ord') => 1);
898
899
900     delete $Expect_File{ $volume . file_path_name('fa', 'fsl') } unless $symlink_exists;
901     %Expect_Name = ();
902
903     %Expect_Dir = ($volume . dir_path('fa') => 1,
904                    $volume . dir_path('fa', 'faa') => 1,
905                    $volume . dir_path('fa', 'fab') => 1,
906                    $volume . dir_path('fa', 'fab', 'faba') => 1);
907
908     File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1}, $volume . topdir('fa'));
909     is( scalar(keys %Expect_File), 0, "Got no files, as expected" );
910 }
911
912
913 ##### Issue 68260 #####
914
915 if ($symlink_exists) {
916     print "# BUG  68260\n";
917     mkdir_ok(dir_path ('fa', 'fac'), 0770);
918     mkdir_ok(dir_path ('fb', 'fbc'), 0770);
919     create_file_ok(file_path ('fa', 'fac', 'faca'));
920     symlink_ok('..////../fa/fac/faca', 'fb/fbc/fbca',
921         "RT 68260: able to symlink");
922
923     use warnings;
924     my $dangling_symlink;
925     local $SIG {__WARN__} = sub {
926         local $" = " ";         # "
927         $dangling_symlink ++ if "@_" =~ /dangling symbolic link/;
928     };
929
930     File::Find::find (
931         {
932             wanted            => sub {1;},
933             follow            => 1,
934             follow_skip       => 2,
935             dangling_symlinks => 1,
936         },
937         File::Spec -> curdir
938     );
939
940     ok(!$dangling_symlink, "Found no dangling symlink");
941 }
942
943 if ($symlink_exists) {  # perl #120388
944     print "# BUG  120388\n";
945     mkdir_ok(dir_path ('fa', 'fax'), 0770);
946     create_file_ok(file_path ('fa', 'fax', 'faz'));
947     symlink_ok( file_path ('..', 'fa', 'fax', 'faz'), file_path ('fa', 'fay') );
948     my @seen;
949     File::Find::find( {wanted => sub {
950         if (/^fa[yz]$/) {
951             push @seen, $_;
952             ok(-e $File::Find::fullname,
953                 "file identified by 'fullname' exists");
954             my $subdir = file_path qw/for_find fa fax faz/;
955             like(
956                 $File::Find::fullname,
957                 qr/\Q$subdir\E$/,
958                 "fullname matches expected path"
959             );
960         }
961     }, follow => 1}, topdir('fa'));
962     # make sure "fay"(symlink) found before "faz"(real file);
963     # otherwise test invalid
964     is(join(',', @seen), 'fay,faz',
965         "symlink found before real file, as expected");
966 }
967
968 ##### Issue 59750 #####
969
970 print "# RT 59750\n";
971 mkdir_ok( dir_path('fc'), 0770 );
972 mkdir_ok( dir_path('fc', 'fca'), 0770 );
973 mkdir_ok( dir_path('fc', 'fcb'), 0770 );
974 mkdir_ok( dir_path('fc', 'fcc'), 0770 );
975 create_file_ok( file_path('fc', 'fca', 'match_alpha') );
976 create_file_ok( file_path('fc', 'fca', 'match_beta') );
977 create_file_ok( file_path('fc', 'fcb', 'match_gamma') );
978 create_file_ok( file_path('fc', 'fcb', 'delta') );
979 create_file_ok( file_path('fc', 'fcc', 'match_epsilon') );
980 create_file_ok( file_path('fc', 'fcc', 'match_zeta') );
981 create_file_ok( file_path('fc', 'fcc', 'eta') );
982
983 my @files_from_mixed = ();
984 sub wantmatch {
985     if ( $File::Find::name =~ m/match/ ) {
986         push @files_from_mixed, $_;
987         print "# \$_ => '$_'\n";
988     }
989 }
990 find( \&wantmatch, (
991     dir_path('fc', 'fca'),
992     dir_path('fc', 'fcb'),
993     dir_path('fc', 'fcc'),
994 ) );
995 is( scalar(@files_from_mixed), 5,
996     "Prepare test for RT #59750: got 5 'match' files as expected" );
997
998 @files_from_mixed = ();
999 find( \&wantmatch, (
1000     dir_path('fc', 'fca'),
1001     dir_path('fc', 'fcb'),
1002     file_path('fc', 'fcc', 'match_epsilon'),
1003     file_path('fc', 'fcc', 'eta'),
1004 ) );
1005 is( scalar(@files_from_mixed), 4,
1006     "Can mix directories and (non-directory) files in list of directories searched by wanted()" );
1007
1008 ##### More Win32 checks#####
1009
1010 if ($^O eq 'MSWin32') {
1011     # Check F:F:f correctly handles a root directory path.
1012     # Rather than processing the entire drive (!), simply test that the
1013     # first file passed to the wanted routine is correct and then bail out.
1014     $orig_dir =~ /^(\w:)/ or die "expected a drive: $orig_dir";
1015     my $drive = $1;
1016
1017     # Determine the file in the root directory which would be
1018     # first if processed in sorted order. Create one if necessary.
1019     my $expected_first_file;
1020     opendir(my $ROOT_DIR, "/") or die "cannot opendir /: $!\n";
1021     foreach my $f (sort readdir $ROOT_DIR) {
1022         if (-f "/$f") {
1023             $expected_first_file = $f;
1024             last;
1025         }
1026     }
1027     closedir $ROOT_DIR;
1028   SKIP:
1029     {
1030         my $created_file;
1031         unless (defined $expected_first_file) {
1032             $expected_first_file = '__perl_File_Find_test.tmp';
1033             open(F, ">", "/$expected_first_file") && close(F)
1034                 or skip "cannot create file in root directory: $!", 8;
1035             $created_file = 1;
1036         }
1037
1038         # Run F:F:f with/without no_chdir for each possible style of root path.
1039         # NB. If HOME were "/", then an inadvertent chdir('') would fluke the
1040         # expected result, so ensure it is something else:
1041         local $ENV{HOME} = $orig_dir;
1042         foreach my $no_chdir (0, 1) {
1043             foreach my $root_dir ("/", "\\", "$drive/", "$drive\\") {
1044                 eval {
1045                     File::Find::find({
1046                         'no_chdir' => $no_chdir,
1047                             'preprocess' => sub { return sort @_ },
1048                             'wanted' => sub {
1049                                 -f or return; # the first call is for $root_dir itself.
1050                                 my $got = $File::Find::name;
1051                                 my $exp = "$root_dir$expected_first_file";
1052                                 print "# no_chdir=$no_chdir $root_dir '$got'\n";
1053                                 is($got, $exp,
1054                                    "Win32: Run 'find' with 'no_chdir' set to $no_chdir" );
1055                                 die "done"; # do not process the entire drive!
1056                         },
1057                     }, $root_dir);
1058                 };
1059                 # If F:F:f did not die "done" then it did not Check() either.
1060                 unless ($@ and $@ =~ /done/) {
1061                     print "# no_chdir=$no_chdir $root_dir ",
1062                         ($@ ? "error: $@" : "no files found"), "\n";
1063                     ok(0, "Win32: 0");
1064                 }
1065             }
1066         }
1067         if ($created_file) {
1068             unlink("/$expected_first_file")
1069                 or warn "can't unlink /$expected_first_file: $!\n";
1070         }
1071     }
1072 }
1073
1074 {
1075     local $@;
1076     eval { File::Find::find( 'foobar' ); };
1077     like($@, qr/no &wanted subroutine given/,
1078         "find() correctly died for lack of &wanted via either coderef or hashref");
1079 }
1080
1081 {
1082     local $@;
1083     eval { File::Find::find( { follow => 1 } ); };
1084     like($@, qr/no &wanted subroutine given/,
1085         "find() correctly died for lack of &wanted via hashref");
1086 }
1087
1088 {
1089     local $@;
1090     eval { File::Find::find( { wanted => 1 } ); };
1091     like($@, qr/no &wanted subroutine given/,
1092         "find() correctly died: lack of coderef as value of 'wanted' element");
1093 }
1094
1095 {
1096     local $@;
1097     my $wanted = sub { print "hello world\n"; };
1098     eval { File::Find::find( $wanted, ( undef ) ); };
1099     like($@, qr/invalid top directory/,
1100         "find() correctly died due to undefined top directory");
1101 }
1102
1103 done_testing();