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 { s#\.$## if ($^O eq 'VMS' && $_ ne '.'); 285 ++$::count_tb if $tb{$_}; 286 }, 287 $bad_option => undef, 288 }, 289 File::Spec->curdir 290 ); 291 }; 292 like($warn_msg, qr/Invalid option/s, "Got warning for invalid option"); 293 like($warn_msg, qr/$bad_option/s, "Got warning for $bad_option"); 294 is($::count_tb, scalar(@testing_basenames), "count_tb incremented"); 295 undef $warn_msg; 296 297 $::count_tb = 0; 298 { 299 finddepth( 300 { 301 wanted => sub { s#\.$## if ($^O eq 'VMS' && $_ ne '.'); 302 ++$::count_tb if $tb{$_}; 303 }, 304 $bad_option => undef, 305 $second_bad_option => undef, 306 }, 307 File::Spec->curdir 308 ); 309 }; 310 like($warn_msg, qr/Invalid option/s, "Got warning for invalid option"); 311 like($warn_msg, qr/$bad_option/s, "Got warning for $bad_option"); 312 like($warn_msg, qr/$second_bad_option/s, "Got warning for $second_bad_option"); 313 is($::count_tb, scalar(@testing_basenames), "count_tb incremented"); 314 undef $warn_msg; 315} 316 317##### Basic tests for find() ##### 318# Set up list of files we expect to find. 319# Run find(), removing a file from the list once we have found it. 320# The list should be empty once we are done. 321 322%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1, 323 file_path('fa_ord') => 1, file_path('fab') => 1, 324 file_path('fab_ord') => 1, file_path('faba') => 1, 325 file_path('faa') => 1, file_path('faa_ord') => 1); 326 327delete $Expect_File{ file_path('fsl') } unless $symlink_exists; 328%Expect_Name = (); 329 330%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1, 331 dir_path('fab') => 1, dir_path('faba') => 1, 332 dir_path('fb') => 1, dir_path('fba') => 1); 333 334delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists; 335File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') ); 336is( scalar(keys %Expect_File), 0, "COMPLETE: Basic test of find()" ); 337 338##### Re-entrancy ##### 339 340print "# check re-entrancy\n"; 341 342%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1, 343 file_path('fa_ord') => 1, file_path('fab') => 1, 344 file_path('fab_ord') => 1, file_path('faba') => 1, 345 file_path('faa') => 1, file_path('faa_ord') => 1); 346 347delete $Expect_File{ file_path('fsl') } unless $symlink_exists; 348%Expect_Name = (); 349 350%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1, 351 dir_path('fab') => 1, dir_path('faba') => 1, 352 dir_path('fb') => 1, dir_path('fba') => 1); 353 354delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists; 355 356File::Find::find( {wanted => sub { wanted_File_Dir_prune(); 357 File::Find::find( {wanted => sub 358 {} }, File::Spec->curdir ); } }, 359 topdir('fa') ); 360 361is( scalar(keys %Expect_File), 0, "COMPLETE: Test of find() for re-entrancy" ); 362 363##### 'no_chdir' option ##### 364# no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File 365 366%Expect_File = (file_path_name('fa') => 1, 367 file_path_name('fa', 'fsl') => 1, 368 file_path_name('fa', 'fa_ord') => 1, 369 file_path_name('fa', 'fab') => 1, 370 file_path_name('fa', 'fab', 'fab_ord') => 1, 371 file_path_name('fa', 'fab', 'faba') => 1, 372 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 373 file_path_name('fa', 'faa') => 1, 374 file_path_name('fa', 'faa', 'faa_ord') => 1,); 375 376delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists; 377%Expect_Name = (); 378 379%Expect_Dir = (dir_path('fa') => 1, 380 dir_path('fa', 'faa') => 1, 381 dir_path('fa', 'fab') => 1, 382 dir_path('fa', 'fab', 'faba') => 1, 383 dir_path('fb') => 1, 384 dir_path('fb', 'fba') => 1); 385 386delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') } 387 unless $symlink_exists; 388 389File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1}, 390 topdir('fa') ); 391is( scalar(keys %Expect_File), 0, "COMPLETE: Test of 'no_chdir' option" ); 392 393##### Test for $File::Find::name ##### 394 395%Expect_File = (); 396 397%Expect_Name = (File::Spec->curdir => 1, 398 file_path_name('.', 'fa') => 1, 399 file_path_name('.', 'fa', 'fsl') => 1, 400 file_path_name('.', 'fa', 'fa_ord') => 1, 401 file_path_name('.', 'fa', 'fab') => 1, 402 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1, 403 file_path_name('.', 'fa', 'fab', 'faba') => 1, 404 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1, 405 file_path_name('.', 'fa', 'faa') => 1, 406 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1, 407 file_path_name('.', 'fb') => 1, 408 file_path_name('.', 'fb', 'fba') => 1, 409 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1, 410 file_path_name('.', 'fb', 'fb_ord') => 1); 411 412delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists; 413%Expect_Dir = (); 414File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir ); 415is( scalar(keys %Expect_Name), 0, "COMPLETE: Test for \$File::Find::name" ); 416 417 418##### ##### 419# no_chdir is in effect, hence we use file_path_name to specify the 420# expected paths for %Expect_File 421 422%Expect_File = (File::Spec->curdir => 1, 423 file_path_name('.', 'fa') => 1, 424 file_path_name('.', 'fa', 'fsl') => 1, 425 file_path_name('.', 'fa', 'fa_ord') => 1, 426 file_path_name('.', 'fa', 'fab') => 1, 427 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1, 428 file_path_name('.', 'fa', 'fab', 'faba') => 1, 429 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1, 430 file_path_name('.', 'fa', 'faa') => 1, 431 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1, 432 file_path_name('.', 'fb') => 1, 433 file_path_name('.', 'fb', 'fba') => 1, 434 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1, 435 file_path_name('.', 'fb', 'fb_ord') => 1); 436 437delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists; 438%Expect_Name = (); 439%Expect_Dir = (); 440 441File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1}, 442 File::Spec->curdir ); 443 444is( scalar(keys %Expect_File), 0, 445 "COMPLETE: Equivalency of \$_ and \$File::Find::Name with 'no_chdir'" ); 446 447##### ##### 448 449print "# check preprocess\n"; 450%Expect_File = (); 451%Expect_Name = (); 452%Expect_Dir = ( 453 File::Spec->curdir => {fa => 1, fb => 1}, 454 dir_path('.', 'fa') => {faa => 1, fab => 1, fa_ord => 1}, 455 dir_path('.', 'fa', 'faa') => {faa_ord => 1}, 456 dir_path('.', 'fa', 'fab') => {faba => 1, fab_ord => 1}, 457 dir_path('.', 'fa', 'fab', 'faba') => {faba_ord => 1}, 458 dir_path('.', 'fb') => {fba => 1, fb_ord => 1}, 459 dir_path('.', 'fb', 'fba') => {fba_ord => 1} 460 ); 461 462File::Find::find( {wanted => \&noop_wanted, 463 preprocess => \&my_preprocess}, File::Spec->curdir ); 464 465is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" ); 466 467##### ##### 468 469print "# check postprocess\n"; 470%Expect_File = (); 471%Expect_Name = (); 472%Expect_Dir = ( 473 File::Spec->curdir => 1, 474 dir_path('.', 'fa') => 1, 475 dir_path('.', 'fa', 'faa') => 1, 476 dir_path('.', 'fa', 'fab') => 1, 477 dir_path('.', 'fa', 'fab', 'faba') => 1, 478 dir_path('.', 'fb') => 1, 479 dir_path('.', 'fb', 'fba') => 1 480 ); 481 482File::Find::find( {wanted => \&noop_wanted, 483 postprocess => \&my_postprocess}, File::Spec->curdir ); 484 485is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" ); 486 487##### ##### 488{ 489 print "# checking argument localization\n"; 490 491 ### this checks the fix of perlbug [19977] ### 492 my @foo = qw( a b c d e f ); 493 my %pre = map { $_ => } @foo; 494 495 File::Find::find( sub { } , 'fa' ) for @foo; 496 delete $pre{$_} for @foo; 497 498 is( scalar(keys %pre), 0, "Got no files, as expected" ); 499} 500 501##### ##### 502# see thread starting 503# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-02/msg00351.html 504{ 505 print "# checking that &_ and %_ are still accessible and that\n", 506 "# tie magic on \$_ is not triggered\n"; 507 508 my $true_count; 509 my $sub = 0; 510 sub _ { 511 ++$sub; 512 } 513 my $tie_called = 0; 514 515 package Foo; 516 sub STORE { 517 ++$tie_called; 518 } 519 sub FETCH {return 'N'}; 520 sub TIESCALAR {bless []}; 521 package main; 522 523 is( scalar(keys %_), 0, "Got no files, as expected" ); 524 my @foo = 'n'; 525 tie $foo[0], "Foo"; 526 527 File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo; 528 untie $_; 529 530 is( $tie_called, 0, "Got no files tie_called, as expected" ); 531 is( scalar(keys %_), $true_count, "Got true count, as expected" ); 532 is( $sub, $true_count, "Got true count, as expected" ); 533 is( scalar( @foo), 1, "Got one file, as expected" ); 534 is( $foo[0], 'N', "Got 'N', as expected" ); 535} 536 537##### ##### 538if ( $symlink_exists ) { 539 print "# --- symbolic link tests --- \n"; 540 $FastFileTests_OK= 1; 541 542 # 'follow', 'follow_fast' and 'follow_skip' options only apply when a 543 # platform supports symlinks. 544 545 ##### ##### 546 547 # Verify that File::Find::find will call wanted even if the topdir 548 # is a symlink to a directory, and it should not follow the link 549 # unless follow is set, which it is not in this case 550 %Expect_File = ( file_path('fsl') => 1 ); 551 %Expect_Name = (); 552 %Expect_Dir = (); 553 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa', 'fsl') ); 554 is( scalar(keys %Expect_File), 0, 555 "COMPLETE: top dir can be symlink to dir; link not followed without 'follow' option" ); 556 557 ##### ##### 558 559 %Expect_File = (File::Spec->curdir => 1, file_path('fa_ord') => 1, 560 file_path('fsl') => 1, file_path('fb_ord') => 1, 561 file_path('fba') => 1, file_path('fba_ord') => 1, 562 file_path('fab') => 1, file_path('fab_ord') => 1, 563 file_path('faba') => 1, file_path('faa') => 1, 564 file_path('faa_ord') => 1); 565 566 %Expect_Name = (); 567 568 %Expect_Dir = (File::Spec->curdir => 1, dir_path('fa') => 1, 569 dir_path('faa') => 1, dir_path('fab') => 1, 570 dir_path('faba') => 1, dir_path('fb') => 1, 571 dir_path('fba') => 1); 572 573 File::Find::find( {wanted => \&wanted_File_Dir_prune, 574 follow_fast => 1}, topdir('fa') ); 575 576 is( scalar(keys %Expect_File), 0, 577 "COMPLETE: test of 'follow_fast' option: \$_ case" ); 578 579 ##### ##### 580 581 # no_chdir is in effect, hence we use file_path_name to specify 582 # the expected paths for %Expect_File 583 584 %Expect_File = (file_path_name('fa') => 1, 585 file_path_name('fa', 'fa_ord') => 1, 586 file_path_name('fa', 'fsl') => 1, 587 file_path_name('fa', 'fsl', 'fb_ord') => 1, 588 file_path_name('fa', 'fsl', 'fba') => 1, 589 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1, 590 file_path_name('fa', 'fab') => 1, 591 file_path_name('fa', 'fab', 'fab_ord') => 1, 592 file_path_name('fa', 'fab', 'faba') => 1, 593 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 594 file_path_name('fa', 'faa') => 1, 595 file_path_name('fa', 'faa', 'faa_ord') => 1); 596 597 %Expect_Name = (); 598 599 %Expect_Dir = (dir_path('fa') => 1, 600 dir_path('fa', 'faa') => 1, 601 dir_path('fa', 'fab') => 1, 602 dir_path('fa', 'fab', 'faba') => 1, 603 dir_path('fb') => 1, 604 dir_path('fb', 'fba') => 1); 605 606 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1, 607 no_chdir => 1}, topdir('fa') ); 608 609 is( scalar(keys %Expect_File), 0, 610 "COMPLETE: Test of 'follow_fast' and 'no_chdir' options together: \$_ case" ); 611 612 ##### ##### 613 614 %Expect_File = (); 615 616 %Expect_Name = (file_path_name('fa') => 1, 617 file_path_name('fa', 'fa_ord') => 1, 618 file_path_name('fa', 'fsl') => 1, 619 file_path_name('fa', 'fsl', 'fb_ord') => 1, 620 file_path_name('fa', 'fsl', 'fba') => 1, 621 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1, 622 file_path_name('fa', 'fab') => 1, 623 file_path_name('fa', 'fab', 'fab_ord') => 1, 624 file_path_name('fa', 'fab', 'faba') => 1, 625 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 626 file_path_name('fa', 'faa') => 1, 627 file_path_name('fa', 'faa', 'faa_ord') => 1); 628 629 %Expect_Dir = (); 630 631 File::Find::finddepth( {wanted => \&wanted_Name, 632 follow_fast => 1}, topdir('fa') ); 633 634 is( scalar(keys %Expect_Name), 0, 635 "COMPLETE: test of 'follow_fast' option: \$File::Find::name case" ); 636 637 ##### ##### 638 639 # no_chdir is in effect, hence we use file_path_name to specify 640 # the expected paths for %Expect_File 641 642 %Expect_File = (file_path_name('fa') => 1, 643 file_path_name('fa', 'fa_ord') => 1, 644 file_path_name('fa', 'fsl') => 1, 645 file_path_name('fa', 'fsl', 'fb_ord') => 1, 646 file_path_name('fa', 'fsl', 'fba') => 1, 647 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1, 648 file_path_name('fa', 'fab') => 1, 649 file_path_name('fa', 'fab', 'fab_ord') => 1, 650 file_path_name('fa', 'fab', 'faba') => 1, 651 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 652 file_path_name('fa', 'faa') => 1, 653 file_path_name('fa', 'faa', 'faa_ord') => 1); 654 655 %Expect_Name = (); 656 %Expect_Dir = (); 657 658 File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1, 659 no_chdir => 1}, topdir('fa') ); 660 661 is( scalar(keys %Expect_File), 0, 662 "COMPLETE: Test of 'follow_fast' and 'no_chdir' options together: \$File::Find::name case" ); 663 664 ##### ##### 665 666 print "# check dangling symbolic links\n"; 667 mkdir_ok( dir_path('dangling_dir'), 0770 ); 668 symlink_ok( dir_path('dangling_dir'), file_path('dangling_dir_sl'), 669 "Check dangling directory" ); 670 rmdir dir_path('dangling_dir'); 671 create_file_ok(file_path('dangling_file')); 672 symlink_ok('../dangling_file','fa/dangling_file_sl', 673 "Check dangling file" ); 674 unlink file_path('dangling_file'); 675 676 { 677 # these tests should also emit a warning 678 use warnings; 679 680 %Expect_File = (File::Spec->curdir => 1, 681 file_path('dangling_file_sl') => 1, 682 file_path('fa_ord') => 1, 683 file_path('fsl') => 1, 684 file_path('fb_ord') => 1, 685 file_path('fba') => 1, 686 file_path('fba_ord') => 1, 687 file_path('fab') => 1, 688 file_path('fab_ord') => 1, 689 file_path('faba') => 1, 690 file_path('faba_ord') => 1, 691 file_path('faa') => 1, 692 file_path('faa_ord') => 1); 693 694 %Expect_Name = (); 695 %Expect_Dir = (); 696 undef $warn_msg; 697 698 File::Find::find( {wanted => \&wanted_File, follow => 1, 699 dangling_symlinks => 700 sub { $warn_msg = "$_[0] is a dangling symbolic link" } 701 }, 702 topdir('dangling_dir_sl'), topdir('fa') ); 703 704 is( scalar(keys %Expect_File), 0, 705 "COMPLETE: test of 'follow' and 'dangling_symlinks' options" ); 706 like( $warn_msg, qr/dangling_file_sl is a dangling symbolic link/, 707 "Got expected warning message re dangling symbolic link" ); 708 unlink file_path('fa', 'dangling_file_sl'), 709 file_path('dangling_dir_sl'); 710 711 } 712 713 ##### ##### 714 715 print "# check recursion\n"; 716 symlink_ok('../faa','fa/faa/faa_sl'); 717 undef $@; 718 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, 719 no_chdir => 1}, topdir('fa') ); }; 720 like( 721 $@, 722 qr{for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link}i, 723 "Got expected error message for recursive symbolic link" 724 ); 725 unlink file_path('fa', 'faa', 'faa_sl'); 726 727 728 print "# check follow_skip (file)\n"; 729 symlink_ok('./fa_ord','fa/fa_ord_sl'); 730 undef $@; 731 732 eval {File::Find::finddepth( {wanted => \&simple_wanted, 733 follow => 1, 734 follow_skip => 0, no_chdir => 1}, 735 topdir('fa') );}; 736 737 like( 738 $@, 739 qr{for_find[:/]fa[:/]fa_ord encountered a second time}i, 740 "'follow_skip==0': got error message when file encountered a second time" 741 ); 742 743 ##### ##### 744 745 # no_chdir is in effect, hence we use file_path_name to specify 746 # the expected paths for %Expect_File 747 748 %Expect_File = (file_path_name('fa') => 1, 749 file_path_name('fa', 'fa_ord') => 2, 750 # We may encounter the symlink first 751 file_path_name('fa', 'fa_ord_sl') => 2, 752 file_path_name('fa', 'fsl') => 1, 753 file_path_name('fa', 'fsl', 'fb_ord') => 1, 754 file_path_name('fa', 'fsl', 'fba') => 1, 755 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1, 756 file_path_name('fa', 'fab') => 1, 757 file_path_name('fa', 'fab', 'fab_ord') => 1, 758 file_path_name('fa', 'fab', 'faba') => 1, 759 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 760 file_path_name('fa', 'faa') => 1, 761 file_path_name('fa', 'faa', 'faa_ord') => 1); 762 763 %Expect_Name = (); 764 765 %Expect_Dir = (dir_path('fa') => 1, 766 dir_path('fa', 'faa') => 1, 767 dir_path('fa', 'fab') => 1, 768 dir_path('fa', 'fab', 'faba') => 1, 769 dir_path('fb') => 1, 770 dir_path('fb','fba') => 1); 771 772 File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1, 773 follow_skip => 1, no_chdir => 1}, 774 topdir('fa') ); 775 is( scalar(keys %Expect_File), 0, 776 "COMPLETE: Test of 'follow', 'follow_skip==1' and 'no_chdir' options" ); 777 unlink file_path('fa', 'fa_ord_sl'); 778 779 ##### ##### 780 print "# check follow_skip (directory)\n"; 781 symlink_ok('./faa','fa/faa_sl'); 782 undef $@; 783 784 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, 785 follow_skip => 0, no_chdir => 1}, 786 topdir('fa') );}; 787 788 like( 789 $@, 790 qr{for_find[:/]fa[:/]faa[:/]? encountered a second time}i, 791 "'follow_skip==0': got error message when directory encountered a second time" 792 ); 793 794 795 undef $@; 796 797 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, 798 follow_skip => 1, no_chdir => 1}, 799 topdir('fa') );}; 800 801 like( 802 $@, 803 qr{for_find[:/]fa[:/]faa[:/]? encountered a second time}i, 804 "'follow_skip==1': got error message when directory encountered a second time" 805 ); 806 807 ##### ##### 808 809 # no_chdir is in effect, hence we use file_path_name to specify 810 # the expected paths for %Expect_File 811 812 %Expect_File = (file_path_name('fa') => 1, 813 file_path_name('fa', 'fa_ord') => 1, 814 file_path_name('fa', 'fsl') => 1, 815 file_path_name('fa', 'fsl', 'fb_ord') => 1, 816 file_path_name('fa', 'fsl', 'fba') => 1, 817 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1, 818 file_path_name('fa', 'fab') => 1, 819 file_path_name('fa', 'fab', 'fab_ord') => 1, 820 file_path_name('fa', 'fab', 'faba') => 1, 821 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 822 file_path_name('fa', 'faa') => 1, 823 file_path_name('fa', 'faa', 'faa_ord') => 1, 824 # We may actually encounter the symlink first. 825 file_path_name('fa', 'faa_sl') => 1, 826 file_path_name('fa', 'faa_sl', 'faa_ord') => 1); 827 828 %Expect_Name = (); 829 830 %Expect_Dir = (dir_path('fa') => 1, 831 dir_path('fa', 'faa') => 1, 832 dir_path('fa', 'fab') => 1, 833 dir_path('fa', 'fab', 'faba') => 1, 834 dir_path('fb') => 1, 835 dir_path('fb', 'fba') => 1); 836 837 File::Find::find( {wanted => \&wanted_File_Dir, follow => 1, 838 follow_skip => 2, no_chdir => 1}, topdir('fa') ); 839 840 ##### ##### 841 842 # If we encountered the symlink first, then the entries corresponding to 843 # the real name remain, if the real name first then the symlink 844 my @names = sort keys %Expect_File; 845 is( scalar(@names), 1, 846 "'follow_skip==2'" ); 847 # Normalise both to the original name 848 s/_sl// foreach @names; 849 is( 850 $names[0], 851 file_path_name('fa', 'faa', 'faa_ord'), 852 "Got file_path_name, as expected" 853 ); 854 unlink file_path('fa', 'faa_sl'); 855 856} 857 858##### Win32 checks - [perl #41555] ##### 859 860if ($^O eq 'MSWin32') { 861 require File::Spec::Win32; 862 my ($volume) = File::Spec::Win32->splitpath($orig_dir, 1); 863 print STDERR "VOLUME = $volume\n"; 864 865 ##### ##### 866 867 # with chdir 868 %Expect_File = (File::Spec->curdir => 1, 869 file_path('fsl') => 1, 870 file_path('fa_ord') => 1, 871 file_path('fab') => 1, 872 file_path('fab_ord') => 1, 873 file_path('faba') => 1, 874 file_path('faba_ord') => 1, 875 file_path('faa') => 1, 876 file_path('faa_ord') => 1); 877 878 delete $Expect_File{ file_path('fsl') } unless $symlink_exists; 879 %Expect_Name = (); 880 881 %Expect_Dir = (dir_path('fa') => 1, 882 dir_path('faa') => 1, 883 dir_path('fab') => 1, 884 dir_path('faba') => 1, 885 dir_path('fb') => 1, 886 dir_path('fba') => 1); 887 888 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa')); 889 is( scalar(keys %Expect_File), 0, "Got no files, as expected" ); 890 891 ##### ##### 892 893 # no_chdir 894 %Expect_File = ($volume . file_path_name('fa') => 1, 895 $volume . file_path_name('fa', 'fsl') => 1, 896 $volume . file_path_name('fa', 'fa_ord') => 1, 897 $volume . file_path_name('fa', 'fab') => 1, 898 $volume . file_path_name('fa', 'fab', 'fab_ord') => 1, 899 $volume . file_path_name('fa', 'fab', 'faba') => 1, 900 $volume . file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, 901 $volume . file_path_name('fa', 'faa') => 1, 902 $volume . file_path_name('fa', 'faa', 'faa_ord') => 1); 903 904 905 delete $Expect_File{ $volume . file_path_name('fa', 'fsl') } unless $symlink_exists; 906 %Expect_Name = (); 907 908 %Expect_Dir = ($volume . dir_path('fa') => 1, 909 $volume . dir_path('fa', 'faa') => 1, 910 $volume . dir_path('fa', 'fab') => 1, 911 $volume . dir_path('fa', 'fab', 'faba') => 1); 912 913 File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1}, $volume . topdir('fa')); 914 is( scalar(keys %Expect_File), 0, "Got no files, as expected" ); 915} 916 917 918##### Issue 68260 ##### 919 920if ($symlink_exists) { 921 print "# BUG 68260\n"; 922 mkdir_ok(dir_path ('fa', 'fac'), 0770); 923 mkdir_ok(dir_path ('fb', 'fbc'), 0770); 924 create_file_ok(file_path ('fa', 'fac', 'faca')); 925 symlink_ok('..////../fa/fac/faca', 'fb/fbc/fbca', 926 "RT 68260: able to symlink"); 927 928 use warnings; 929 my $dangling_symlink; 930 local $SIG {__WARN__} = sub { 931 local $" = " "; # " 932 $dangling_symlink ++ if "@_" =~ /dangling symbolic link/; 933 }; 934 935 File::Find::find ( 936 { 937 wanted => sub {1;}, 938 follow => 1, 939 follow_skip => 2, 940 dangling_symlinks => 1, 941 }, 942 File::Spec -> curdir 943 ); 944 945 ok(!$dangling_symlink, "Found no dangling symlink"); 946} 947 948if ($symlink_exists) { # perl #120388 949 print "# BUG 120388\n"; 950 mkdir_ok(dir_path ('fa', 'fax'), 0770); 951 create_file_ok(file_path ('fa', 'fax', 'faz')); 952 symlink_ok( file_path ('..', 'fa', 'fax', 'faz'), file_path ('fa', 'fay') ); 953 my @seen; 954 File::Find::find( {wanted => sub { 955 if (/^fa[yz]$/) { 956 push @seen, $_; 957 ok(-e $File::Find::fullname, 958 "file identified by 'fullname' exists"); 959 my $subdir = file_path qw/for_find fa fax faz/; 960 like( 961 $File::Find::fullname, 962 qr/\Q$subdir\E$/, 963 "fullname matches expected path" 964 ); 965 } 966 }, follow => 1}, topdir('fa')); 967 # make sure "fay"(symlink) found before "faz"(real file); 968 # otherwise test invalid 969 is(join(',', @seen), 'fay,faz', 970 "symlink found before real file, as expected"); 971} 972 973##### Issue 59750 ##### 974 975print "# RT 59750\n"; 976mkdir_ok( dir_path('fc'), 0770 ); 977mkdir_ok( dir_path('fc', 'fca'), 0770 ); 978mkdir_ok( dir_path('fc', 'fcb'), 0770 ); 979mkdir_ok( dir_path('fc', 'fcc'), 0770 ); 980create_file_ok( file_path('fc', 'fca', 'match_alpha') ); 981create_file_ok( file_path('fc', 'fca', 'match_beta') ); 982create_file_ok( file_path('fc', 'fcb', 'match_gamma') ); 983create_file_ok( file_path('fc', 'fcb', 'delta') ); 984create_file_ok( file_path('fc', 'fcc', 'match_epsilon') ); 985create_file_ok( file_path('fc', 'fcc', 'match_zeta') ); 986create_file_ok( file_path('fc', 'fcc', 'eta') ); 987 988my @files_from_mixed = (); 989sub wantmatch { 990 if ( $File::Find::name =~ m/match/ ) { 991 push @files_from_mixed, $_; 992 print "# \$_ => '$_'\n"; 993 } 994} 995find( \&wantmatch, ( 996 dir_path('fc', 'fca'), 997 dir_path('fc', 'fcb'), 998 dir_path('fc', 'fcc'), 999) ); 1000is( scalar(@files_from_mixed), 5, 1001 "Prepare test for RT #59750: got 5 'match' files as expected" ); 1002 1003@files_from_mixed = (); 1004find( \&wantmatch, ( 1005 dir_path('fc', 'fca'), 1006 dir_path('fc', 'fcb'), 1007 file_path('fc', 'fcc', 'match_epsilon'), 1008 file_path('fc', 'fcc', 'eta'), 1009) ); 1010is( scalar(@files_from_mixed), 4, 1011 "Can mix directories and (non-directory) files in list of directories searched by wanted()" ); 1012 1013##### More Win32 checks##### 1014 1015if ($^O eq 'MSWin32') { 1016 # Check F:F:f correctly handles a root directory path. 1017 # Rather than processing the entire drive (!), simply test that the 1018 # first file passed to the wanted routine is correct and then bail out. 1019 $orig_dir =~ /^(\w:)/ or die "expected a drive: $orig_dir"; 1020 my $drive = $1; 1021 1022 # Determine the file in the root directory which would be 1023 # first if processed in sorted order. Create one if necessary. 1024 my $expected_first_file; 1025 opendir(my $ROOT_DIR, "/") or die "cannot opendir /: $!\n"; 1026 foreach my $f (sort readdir $ROOT_DIR) { 1027 if (-f "/$f") { 1028 $expected_first_file = $f; 1029 last; 1030 } 1031 } 1032 closedir $ROOT_DIR; 1033 SKIP: 1034 { 1035 my $created_file; 1036 unless (defined $expected_first_file) { 1037 $expected_first_file = '__perl_File_Find_test.tmp'; 1038 open(F, ">", "/$expected_first_file") && close(F) 1039 or skip "cannot create file in root directory: $!", 8; 1040 $created_file = 1; 1041 } 1042 1043 # Run F:F:f with/without no_chdir for each possible style of root path. 1044 # NB. If HOME were "/", then an inadvertent chdir('') would fluke the 1045 # expected result, so ensure it is something else: 1046 local $ENV{HOME} = $orig_dir; 1047 foreach my $no_chdir (0, 1) { 1048 foreach my $root_dir ("/", "\\", "$drive/", "$drive\\") { 1049 eval { 1050 File::Find::find({ 1051 'no_chdir' => $no_chdir, 1052 'preprocess' => sub { return sort @_ }, 1053 'wanted' => sub { 1054 -f or return; # the first call is for $root_dir itself. 1055 my $got = $File::Find::name; 1056 my $exp = "$root_dir$expected_first_file"; 1057 print "# no_chdir=$no_chdir $root_dir '$got'\n"; 1058 is($got, $exp, 1059 "Win32: Run 'find' with 'no_chdir' set to $no_chdir" ); 1060 die "done"; # do not process the entire drive! 1061 }, 1062 }, $root_dir); 1063 }; 1064 # If F:F:f did not die "done" then it did not Check() either. 1065 unless ($@ and $@ =~ /done/) { 1066 print "# no_chdir=$no_chdir $root_dir ", 1067 ($@ ? "error: $@" : "no files found"), "\n"; 1068 ok(0, "Win32: 0"); 1069 } 1070 } 1071 } 1072 if ($created_file) { 1073 unlink("/$expected_first_file") 1074 or warn "can't unlink /$expected_first_file: $!\n"; 1075 } 1076 } 1077} 1078 1079{ 1080 local $@; 1081 eval { File::Find::find( 'foobar' ); }; 1082 like($@, qr/no &wanted subroutine given/, 1083 "find() correctly died for lack of &wanted via either coderef or hashref"); 1084} 1085 1086{ 1087 local $@; 1088 eval { File::Find::find( { follow => 1 } ); }; 1089 like($@, qr/no &wanted subroutine given/, 1090 "find() correctly died for lack of &wanted via hashref"); 1091} 1092 1093{ 1094 local $@; 1095 eval { File::Find::find( { wanted => 1 } ); }; 1096 like($@, qr/no &wanted subroutine given/, 1097 "find() correctly died: lack of coderef as value of 'wanted' element"); 1098} 1099 1100{ 1101 local $@; 1102 my $wanted = sub { print "hello world\n"; }; 1103 eval { File::Find::find( $wanted, ( undef ) ); }; 1104 like($@, qr/invalid top directory/, 1105 "find() correctly died due to undefined top directory"); 1106} 1107