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