10 # May be doing dynamic loading while @INC is all relative
11 @INC = map { $_ = File::Spec->rel2abs($_); /(.*)/; $1 } @INC;
13 $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# $_[0]"; };
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';
26 my $symlink_exists = eval { symlink("",""); 1 };
29 use lib qw( ./t/lib );
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
45 # Uncomment this to see where File::Find is chdir-ing to. Helpful for
46 # debugging its little jaunts around the filesystem.
49 # *CORE::GLOBAL::chdir = sub ($) {
50 # my($file, $line) = (caller)[1,2];
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();
63 ##### Sanity checks #####
64 # Do find() and finddepth() work correctly with an empty list of
67 ok(eval { find(\&noop_wanted); 1 },
68 "'find' successfully returned for an empty list of directories");
70 ok(eval { finddepth(\&noop_wanted); 1 },
71 "'finddepth' successfully returned for an empty list of directories");
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.)
79 find({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
81 is($::count_taint, 1, "'find' found exactly 1 file named 'taint.t'");
84 finddepth({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
86 is($::count_taint, 1, "'finddepth' found exactly 1 file named 'taint.t'");
88 my $FastFileTests_OK = 0;
93 if (-d dir_path('for_find')) {
94 $need_updir = 1 if chdir(dir_path('for_find'));
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');
118 if (-d dir_path('fc')) {
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'),
128 rmdir dir_path('fc', 'fca');
129 rmdir dir_path('fc', 'fcb');
130 rmdir dir_path('fc', 'fcc');
131 rmdir dir_path('fc');
134 my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
137 if (-d dir_path('for_find')) {
138 rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
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 _ );
156 delete $Expect_File{$_}
157 unless ( $Expect_Dir{$_} && ! -d $_ );
161 sub wanted_File_Dir_prune {
163 $File::Find::prune = 1 if $_ eq 'faba';
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});
173 $OK= exists($Expect_Name{substr($n,0,$i)}) if $i >= 0;
175 ok( $OK, "found $n for \$File::Find::name, as expected" );
176 delete $Expect_Name{$n};
180 print "# \$_ => '$_'\n";
181 s#\.$## if ($^O eq 'VMS' && $_ ne '.'); #
182 my $i = rindex($_,'/');
183 my $OK = exists($Expect_File{ $_});
185 $OK= exists($Expect_File{ substr($_,0,$i)}) if $i >= 0;
187 ok( $OK, "found $_ for \$_, as expected" );
188 delete $Expect_File{ $_};
192 print "# \$File::Find::dir => '$File::Find::dir'\n";
193 print "# \$_ => '$_'\n";
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';
205 delete $Expect_Dir{ $File::Find::dir }->{$file};
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 }
217 print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
218 delete $Expect_Dir{ $File::Find::dir};
221 # Use topdir() to specify a directory path that you want to pass to
222 # find/finddepth. Historically topdir() differed on Mac OS classic.
224 *topdir = \&dir_path;
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
232 # Historically file_path_name differed on Mac OS classic.
234 *file_path_name = \&file_path;
236 ##### Create directories, files and symlinks used in testing #####
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'");
242 my @testing_basenames = ( qw| fb_ord fba_ord fa_ord faa_ord fab_ord faba_ord | );
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');
252 create_file_ok( file_path('fa', $testing_basenames[2]) );
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]) );
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.
268 my %tb = map { $_ => 1 } @testing_basenames;
271 my $bad_option = 'foobar';
272 my $second_bad_option = 'really_foobar';
275 local $SIG{__WARN__} = sub { $warn_msg = $_[0]; };
279 wanted => sub { s#\.$## if ($^O eq 'VMS' && $_ ne '.');
280 ++$::count_tb if $tb{$_};
282 $bad_option => undef,
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");
296 wanted => sub { s#\.$## if ($^O eq 'VMS' && $_ ne '.');
297 ++$::count_tb if $tb{$_};
299 $bad_option => undef,
300 $second_bad_option => undef,
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");
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.
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);
322 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
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);
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()" );
333 ##### Re-entrancy #####
335 print "# check re-entrancy\n";
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);
342 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
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);
349 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
351 File::Find::find( {wanted => sub { wanted_File_Dir_prune();
352 File::Find::find( {wanted => sub
353 {} }, File::Spec->curdir ); } },
356 is( scalar(keys %Expect_File), 0, "COMPLETE: Test of find() for re-entrancy" );
358 ##### 'no_chdir' option #####
359 # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
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,);
371 delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
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,
379 dir_path('fb', 'fba') => 1);
381 delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') }
382 unless $symlink_exists;
384 File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1},
386 is( scalar(keys %Expect_File), 0, "COMPLETE: Test of 'no_chdir' option" );
388 ##### Test for $File::Find::name #####
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);
407 delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
409 File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
410 is( scalar(keys %Expect_Name), 0, "COMPLETE: Test for \$File::Find::name" );
414 # no_chdir is in effect, hence we use file_path_name to specify the
415 # expected paths for %Expect_File
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);
432 delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
436 File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
437 File::Spec->curdir );
439 is( scalar(keys %Expect_File), 0,
440 "COMPLETE: Equivalency of \$_ and \$File::Find::Name with 'no_chdir'" );
444 print "# check preprocess\n";
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}
457 File::Find::find( {wanted => \&noop_wanted,
458 preprocess => \&my_preprocess}, File::Spec->curdir );
460 is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" );
464 print "# check postprocess\n";
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
477 File::Find::find( {wanted => \&noop_wanted,
478 postprocess => \&my_postprocess}, File::Spec->curdir );
480 is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" );
484 print "# checking argument localization\n";
486 ### this checks the fix of perlbug [19977] ###
487 my @foo = qw( a b c d e f );
488 my %pre = map { $_ => } @foo;
490 File::Find::find( sub { } , 'fa' ) for @foo;
491 delete $pre{$_} for @foo;
493 is( scalar(keys %pre), 0, "Got no files, as expected" );
497 # see thread starting
498 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-02/msg00351.html
500 print "# checking that &_ and %_ are still accessible and that\n",
501 "# tie magic on \$_ is not triggered\n";
514 sub FETCH {return 'N'};
515 sub TIESCALAR {bless []};
518 is( scalar(keys %_), 0, "Got no files, as expected" );
522 File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo;
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" );
533 if ( $symlink_exists ) {
534 print "# --- symbolic link tests --- \n";
535 $FastFileTests_OK= 1;
537 # 'follow', 'follow_fast' and 'follow_skip' options only apply when a
538 # platform supports symlinks.
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 );
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" );
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);
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);
568 File::Find::find( {wanted => \&wanted_File_Dir_prune,
569 follow_fast => 1}, topdir('fa') );
571 is( scalar(keys %Expect_File), 0,
572 "COMPLETE: test of 'follow_fast' option: \$_ case" );
576 # no_chdir is in effect, hence we use file_path_name to specify
577 # the expected paths for %Expect_File
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);
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,
599 dir_path('fb', 'fba') => 1);
601 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
602 no_chdir => 1}, topdir('fa') );
604 is( scalar(keys %Expect_File), 0,
605 "COMPLETE: Test of 'follow_fast' and 'no_chdir' options together: \$_ case" );
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);
626 File::Find::finddepth( {wanted => \&wanted_Name,
627 follow_fast => 1}, topdir('fa') );
629 is( scalar(keys %Expect_Name), 0,
630 "COMPLETE: test of 'follow_fast' option: \$File::Find::name case" );
634 # no_chdir is in effect, hence we use file_path_name to specify
635 # the expected paths for %Expect_File
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);
653 File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
654 no_chdir => 1}, topdir('fa') );
656 is( scalar(keys %Expect_File), 0,
657 "COMPLETE: Test of 'follow_fast' and 'no_chdir' options together: \$File::Find::name case" );
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');
672 # these tests should also emit a warning
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);
693 File::Find::find( {wanted => \&wanted_File, follow => 1,
695 sub { $warn_msg = "$_[0] is a dangling symbolic link" }
697 topdir('dangling_dir_sl'), topdir('fa') );
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');
710 print "# check recursion\n";
711 symlink_ok('../faa','fa/faa/faa_sl');
713 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
714 no_chdir => 1}, topdir('fa') ); };
717 qr{for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link}i,
718 "Got expected error message for recursive symbolic link"
720 unlink file_path('fa', 'faa', 'faa_sl');
723 print "# check follow_skip (file)\n";
724 symlink_ok('./fa_ord','fa/fa_ord_sl');
727 eval {File::Find::finddepth( {wanted => \&simple_wanted,
729 follow_skip => 0, no_chdir => 1},
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"
740 # no_chdir is in effect, hence we use file_path_name to specify
741 # the expected paths for %Expect_File
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);
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,
765 dir_path('fb','fba') => 1);
767 File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
768 follow_skip => 1, no_chdir => 1},
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');
775 print "# check follow_skip (directory)\n";
776 symlink_ok('./faa','fa/faa_sl');
779 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
780 follow_skip => 0, no_chdir => 1},
785 qr{for_find[:/]fa[:/]faa[:/]? encountered a second time}i,
786 "'follow_skip==0': got error message when directory encountered a second time"
792 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
793 follow_skip => 1, no_chdir => 1},
798 qr{for_find[:/]fa[:/]faa[:/]? encountered a second time}i,
799 "'follow_skip==1': got error message when directory encountered a second time"
804 # no_chdir is in effect, hence we use file_path_name to specify
805 # the expected paths for %Expect_File
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);
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,
830 dir_path('fb', 'fba') => 1);
832 File::Find::find( {wanted => \&wanted_File_Dir, follow => 1,
833 follow_skip => 2, no_chdir => 1}, topdir('fa') );
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;
846 file_path_name('fa', 'faa', 'faa_ord'),
847 "Got file_path_name, as expected"
849 unlink file_path('fa', 'faa_sl');
853 ##### Win32 checks - [perl #41555] #####
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";
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);
873 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
876 %Expect_Dir = (dir_path('fa') => 1,
877 dir_path('faa') => 1,
878 dir_path('fab') => 1,
879 dir_path('faba') => 1,
881 dir_path('fba') => 1);
883 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa'));
884 is( scalar(keys %Expect_File), 0, "Got no files, as expected" );
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);
900 delete $Expect_File{ $volume . file_path_name('fa', 'fsl') } unless $symlink_exists;
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);
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" );
913 ##### Issue 68260 #####
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");
924 my $dangling_symlink;
925 local $SIG {__WARN__} = sub {
927 $dangling_symlink ++ if "@_" =~ /dangling symbolic link/;
935 dangling_symlinks => 1,
940 ok(!$dangling_symlink, "Found no dangling symlink");
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') );
949 File::Find::find( {wanted => sub {
952 ok(-e $File::Find::fullname,
953 "file identified by 'fullname' exists");
954 my $subdir = file_path qw/for_find fa fax faz/;
956 $File::Find::fullname,
958 "fullname matches expected path"
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");
968 ##### Issue 59750 #####
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') );
983 my @files_from_mixed = ();
985 if ( $File::Find::name =~ m/match/ ) {
986 push @files_from_mixed, $_;
987 print "# \$_ => '$_'\n";
991 dir_path('fc', 'fca'),
992 dir_path('fc', 'fcb'),
993 dir_path('fc', 'fcc'),
995 is( scalar(@files_from_mixed), 5,
996 "Prepare test for RT #59750: got 5 'match' files as expected" );
998 @files_from_mixed = ();
1000 dir_path('fc', 'fca'),
1001 dir_path('fc', 'fcb'),
1002 file_path('fc', 'fcc', 'match_epsilon'),
1003 file_path('fc', 'fcc', 'eta'),
1005 is( scalar(@files_from_mixed), 4,
1006 "Can mix directories and (non-directory) files in list of directories searched by wanted()" );
1008 ##### More Win32 checks#####
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";
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) {
1023 $expected_first_file = $f;
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;
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\\") {
1046 'no_chdir' => $no_chdir,
1047 'preprocess' => sub { return sort @_ },
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";
1054 "Win32: Run 'find' with 'no_chdir' set to $no_chdir" );
1055 die "done"; # do not process the entire drive!
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";
1067 if ($created_file) {
1068 unlink("/$expected_first_file")
1069 or warn "can't unlink /$expected_first_file: $!\n";
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");
1083 eval { File::Find::find( { follow => 1 } ); };
1084 like($@, qr/no &wanted subroutine given/,
1085 "find() correctly died for lack of &wanted via hashref");
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");
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");