This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
writing to the root directory requires elevated privileges
[perl5.git] / ext / File-Find / t / find.t
CommitLineData
3fa6e24b 1#!./perl
dd87e51f
CB
2use strict;
3use Cwd;
81793b90 4
2a0c00d3 5my $warn_msg;
1a3850a5
GA
6
7BEGIN {
234fd682 8 require File::Spec;
05bc6e4e
NC
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;
1a3850a5
GA
24}
25
2a0c00d3 26my $symlink_exists = eval { symlink("",""); 1 };
867fa9ee 27my $test_count = 111;
1da2e9eb 28$test_count += 127 if $symlink_exists;
a0b245d5 29$test_count += 26 if $^O eq 'MSWin32';
68c65ec0
B
30$test_count += 2 if $^O eq 'MSWin32' and $symlink_exists;
31
2a0c00d3
JK
32use Test::More;
33plan tests => $test_count;
deed50f2
JK
34use lib qw( ./t/lib );
35use Testing qw(
36 create_file_ok
37 mkdir_ok
38 symlink_ok
61097a0a
JK
39 dir_path
40 file_path
deed50f2 41);
2a0c00d3
JK
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);
1a3850a5 47
dd87e51f
CB
48my $orig_dir = cwd();
49
2a0c00d3 50# Uncomment this to see where File::Find is chdir-ing to. Helpful for
7dc9aaa5
MS
51# debugging its little jaunts around the filesystem.
52# BEGIN {
53# use Cwd;
2a0c00d3 54# *CORE::GLOBAL::chdir = sub ($) {
7dc9aaa5
MS
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
c80f55d1
JH
66cleanup();
67
2a0c00d3 68##### Sanity checks #####
867fa9ee
MEA
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
2a0c00d3
JK
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
e12eeff8
NC
83$::count_taint = 0;
84find({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
bb7dc48b 85 File::Spec->curdir);
2a0c00d3 86is($::count_taint, 1, "'find' found exactly 1 file named 'taint.t'");
3fa6e24b 87
e12eeff8
NC
88$::count_taint = 0;
89finddepth({wanted => sub { ++$::count_taint if $_ eq 'taint.t'; } },
2a0c00d3
JK
90 File::Spec->curdir);
91is($::count_taint, 1, "'finddepth' found exactly 1 file named 'taint.t'");
6455dd3b 92
5eb85357 93my $FastFileTests_OK = 0;
81793b90 94
c80f55d1 95sub cleanup {
dd87e51f
CB
96 chdir($orig_dir);
97 my $need_updir = 0;
3fa6e24b 98 if (-d dir_path('for_find')) {
dd87e51f 99 $need_updir = 1 if chdir(dir_path('for_find'));
6fecce66
RGS
100 }
101 if (-d dir_path('fa')) {
2a0c00d3
JK
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'),
1da2e9eb
VE
110 file_path('fb', 'fbc', 'fbca'),
111 file_path('fa', 'fax', 'faz'),
112 file_path('fa', 'fay');
2a0c00d3
JK
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');
1da2e9eb 117 rmdir dir_path('fa', 'fax');
2a0c00d3
JK
118 rmdir dir_path('fa');
119 rmdir dir_path('fb', 'fba');
120 rmdir dir_path('fb', 'fbc');
121 rmdir dir_path('fb');
6fecce66 122 }
488985bf
JK
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 }
dd87e51f
CB
138 if ($need_updir) {
139 my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
140 chdir($updir);
141 }
6fecce66 142 if (-d dir_path('for_find')) {
2a0c00d3 143 rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
c80f55d1
JH
144 }
145}
146
81793b90 147END {
c80f55d1 148 cleanup();
81793b90
GS
149}
150
3fa6e24b 151sub wanted_File_Dir {
0fe503d1 152 print "# \$File::Find::dir => '$File::Find::dir'\t\$_ => '$_'\n";
2a0c00d3 153 s#\.$## if ($^O eq 'VMS' && $_ ne '.'); #
a1ccf0c4 154 s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
2a0c00d3 155 ok( $Expect_File{$_}, "found $_ for \$_, as expected" );
3fa6e24b 156 if ( $FastFileTests_OK ) {
2a0c00d3 157 delete $Expect_File{$_}
3fa6e24b 158 unless ( $Expect_Dir{$_} && ! -d _ );
2a0c00d3
JK
159 }
160 else {
161 delete $Expect_File{$_}
3fa6e24b
TW
162 unless ( $Expect_Dir{$_} && ! -d $_ );
163 }
164}
7e47e6ff 165
3fa6e24b
TW
166sub wanted_File_Dir_prune {
167 &wanted_File_Dir;
2a0c00d3 168 $File::Find::prune = 1 if $_ eq 'faba';
81793b90
GS
169}
170
3fa6e24b
TW
171sub wanted_Name {
172 my $n = $File::Find::name;
2a0c00d3 173 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.'); #
3fa6e24b
TW
174 print "# \$File::Find::name => '$n'\n";
175 my $i = rindex($n,'/');
176 my $OK = exists($Expect_Name{$n});
786dada4 177 if ( $OK ) {
2a0c00d3 178 $OK= exists($Expect_Name{substr($n,0,$i)}) if $i >= 0;
7e47e6ff 179 }
2a0c00d3 180 ok( $OK, "found $n for \$File::Find::name, as expected" );
3fa6e24b 181 delete $Expect_Name{$n};
57907763
GS
182}
183
3fa6e24b
TW
184sub wanted_File {
185 print "# \$_ => '$_'\n";
2a0c00d3 186 s#\.$## if ($^O eq 'VMS' && $_ ne '.'); #
3fa6e24b
TW
187 my $i = rindex($_,'/');
188 my $OK = exists($Expect_File{ $_});
786dada4 189 if ( $OK ) {
2a0c00d3 190 $OK= exists($Expect_File{ substr($_,0,$i)}) if $i >= 0;
7e47e6ff 191 }
2a0c00d3 192 ok( $OK, "found $_ for \$_, as expected" );
3fa6e24b 193 delete $Expect_File{ $_};
57907763
GS
194}
195
7e47e6ff 196sub simple_wanted {
3fa6e24b
TW
197 print "# \$File::Find::dir => '$File::Find::dir'\n";
198 print "# \$_ => '$_'\n";
7e47e6ff
JH
199}
200
201sub noop_wanted {}
78eac027 202
7e47e6ff 203sub my_preprocess {
3fa6e24b
TW
204 @files = @_;
205 print "# --preprocess--\n";
206 print "# \$File::Find::dir => '$File::Find::dir' \n";
2a0c00d3 207 foreach my $file (@files) {
30e67801 208 $file =~ s/\.(dir)?$//i if $^O eq 'VMS';
3fa6e24b
TW
209 print "# $file \n";
210 delete $Expect_Dir{ $File::Find::dir }->{$file};
211 }
212 print "# --end preprocess--\n";
2a0c00d3
JK
213 is(scalar(keys %{$Expect_Dir{ $File::Find::dir }}), 0,
214 "my_preprocess: got 0, as expected");
3fa6e24b
TW
215 if (scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0) {
216 delete $Expect_Dir{ $File::Find::dir }
217 }
218 return @files;
7e47e6ff
JH
219}
220
221sub my_postprocess {
3fa6e24b
TW
222 print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
223 delete $Expect_Dir{ $File::Find::dir};
7e47e6ff
JH
224}
225
bb7dc48b 226# Use topdir() to specify a directory path that you want to pass to
786dada4 227# find/finddepth. Historically topdir() differed on Mac OS classic.
7e47e6ff 228
786dada4 229*topdir = \&dir_path;
7e47e6ff 230
2a0c00d3 231# Use file_path_name() to specify a file path that is expected for
bb7dc48b
JH
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
2a0c00d3 234# case, also use this function to specify a file path that is expected
bb7dc48b 235# for $_.
3fa6e24b 236#
786dada4
NC
237# Historically file_path_name differed on Mac OS classic.
238
239*file_path_name = \&file_path;
3fa6e24b 240
2a0c00d3
JK
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'");
0abd1628
JK
246
247my @testing_basenames = ( qw| fb_ord fba_ord fa_ord faa_ord fab_ord faba_ord | );
248
2a0c00d3
JK
249mkdir_ok( dir_path('fa'), 0770 );
250mkdir_ok( dir_path('fb'), 0770 );
0abd1628 251create_file_ok( file_path('fb', $testing_basenames[0]) );
2a0c00d3 252mkdir_ok( dir_path('fb', 'fba'), 0770 );
0abd1628 253create_file_ok( file_path('fb', 'fba', $testing_basenames[1]) );
2a0c00d3
JK
254if ($symlink_exists) {
255 symlink_ok('../fb','fa/fsl');
256}
0abd1628 257create_file_ok( file_path('fa', $testing_basenames[2]) );
7e47e6ff 258
2a0c00d3 259mkdir_ok( dir_path('fa', 'faa'), 0770 );
0abd1628 260create_file_ok( file_path('fa', 'faa', $testing_basenames[3]) );
2a0c00d3 261mkdir_ok( dir_path('fa', 'fab'), 0770 );
0abd1628 262create_file_ok( file_path('fa', 'fab', $testing_basenames[4]) );
2a0c00d3 263mkdir_ok( dir_path('fa', 'fab', 'faba'), 0770 );
0abd1628
JK
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}
3fa6e24b 312
2a0c00d3
JK
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.
3fa6e24b 317
bb7dc48b
JH
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,
3fa6e24b 321 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 322
3fa6e24b
TW
323delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
324%Expect_Name = ();
bb7dc48b
JH
325
326%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
327 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 328 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 329
3fa6e24b 330delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
2a0c00d3
JK
331File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') );
332is( scalar(keys %Expect_File), 0, "COMPLETE: Basic test of find()" );
3fa6e24b 333
2a0c00d3 334##### Re-entrancy #####
3fa6e24b
TW
335
336print "# check re-entrancy\n";
bb7dc48b
JH
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,
3fa6e24b 341 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 342
3fa6e24b
TW
343delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
344%Expect_Name = ();
bb7dc48b
JH
345
346%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
347 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 348 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 349
3fa6e24b 350delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
bb7dc48b
JH
351
352File::Find::find( {wanted => sub { wanted_File_Dir_prune();
353 File::Find::find( {wanted => sub
354 {} }, File::Spec->curdir ); } },
355 topdir('fa') );
356
2a0c00d3 357is( scalar(keys %Expect_File), 0, "COMPLETE: Test of find() for re-entrancy" );
3fa6e24b 358
2a0c00d3 359##### 'no_chdir' option #####
3fa6e24b 360# no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
bb7dc48b
JH
361
362%Expect_File = (file_path_name('fa') => 1,
2a0c00d3
JK
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,);
bb7dc48b 371
3fa6e24b
TW
372delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
373%Expect_Name = ();
bb7dc48b
JH
374
375%Expect_Dir = (dir_path('fa') => 1,
2a0c00d3
JK
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);
bb7dc48b
JH
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},
2a0c00d3
JK
386 topdir('fa') );
387is( scalar(keys %Expect_File), 0, "COMPLETE: Test of 'no_chdir' option" );
3fa6e24b 388
2a0c00d3 389##### Test for $File::Find::name #####
3fa6e24b
TW
390
391%Expect_File = ();
bb7dc48b
JH
392
393%Expect_Name = (File::Spec->curdir => 1,
2a0c00d3
JK
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);
bb7dc48b 407
3fa6e24b 408delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
2a0c00d3 409%Expect_Dir = ();
3fa6e24b 410File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
2a0c00d3 411is( scalar(keys %Expect_Name), 0, "COMPLETE: Test for \$File::Find::name" );
3fa6e24b
TW
412
413
2a0c00d3 414##### #####
bb7dc48b
JH
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,
2a0c00d3
JK
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);
bb7dc48b 432
3fa6e24b
TW
433delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
434%Expect_Name = ();
2a0c00d3 435%Expect_Dir = ();
bb7dc48b
JH
436
437File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
2a0c00d3 438 File::Spec->curdir );
bb7dc48b 439
2a0c00d3
JK
440is( scalar(keys %Expect_File), 0,
441 "COMPLETE: Equivalency of \$_ and \$File::Find::Name with 'no_chdir'" );
3fa6e24b 442
2a0c00d3 443##### #####
3fa6e24b
TW
444
445print "# check preprocess\n";
446%Expect_File = ();
447%Expect_Name = ();
448%Expect_Dir = (
2a0c00d3
JK
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 );
bb7dc48b
JH
457
458File::Find::find( {wanted => \&noop_wanted,
2a0c00d3 459 preprocess => \&my_preprocess}, File::Spec->curdir );
bb7dc48b 460
2a0c00d3 461is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" );
3fa6e24b 462
2a0c00d3 463##### #####
3fa6e24b
TW
464
465print "# check postprocess\n";
466%Expect_File = ();
467%Expect_Name = ();
468%Expect_Dir = (
2a0c00d3
JK
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 );
bb7dc48b
JH
477
478File::Find::find( {wanted => \&noop_wanted,
2a0c00d3 479 postprocess => \&my_postprocess}, File::Spec->curdir );
bb7dc48b 480
2a0c00d3 481is( scalar(keys %Expect_Dir), 0, "Got no files, as expected" );
3fa6e24b 482
2a0c00d3 483##### #####
17ab9c14
JB
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
2a0c00d3 494 is( scalar(keys %pre), 0, "Got no files, as expected" );
17ab9c14 495}
3fa6e24b 496
2a0c00d3 497##### #####
bc125c03
NC
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",
2a0c00d3
JK
502 "# tie magic on \$_ is not triggered\n";
503
bc125c03
NC
504 my $true_count;
505 my $sub = 0;
506 sub _ {
2a0c00d3 507 ++$sub;
bc125c03
NC
508 }
509 my $tie_called = 0;
510
511 package Foo;
512 sub STORE {
2a0c00d3 513 ++$tie_called;
bc125c03
NC
514 }
515 sub FETCH {return 'N'};
516 sub TIESCALAR {bless []};
517 package main;
518
2a0c00d3 519 is( scalar(keys %_), 0, "Got no files, as expected" );
bc125c03
NC
520 my @foo = 'n';
521 tie $foo[0], "Foo";
522
523 File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo;
524 untie $_;
525
2a0c00d3
JK
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" );
bc125c03
NC
531}
532
2a0c00d3 533##### #####
3fa6e24b 534if ( $symlink_exists ) {
bb7dc48b 535 print "# --- symbolic link tests --- \n";
3fa6e24b 536 $FastFileTests_OK= 1;
7e47e6ff 537
2a0c00d3
JK
538 # 'follow', 'follow_fast' and 'follow_skip' options only apply when a
539 # platform supports symlinks.
7e47e6ff 540
2a0c00d3
JK
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
3fa6e24b
TW
546 %Expect_File = ( file_path('fsl') => 1 );
547 %Expect_Name = ();
548 %Expect_Dir = ();
549 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa', 'fsl') );
2a0c00d3
JK
550 is( scalar(keys %Expect_File), 0,
551 "COMPLETE: top dir can be symlink to dir; link not followed without 'follow' option" );
552
553 ##### #####
3fa6e24b 554
bb7dc48b
JH
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
3fa6e24b 562 %Expect_Name = ();
bb7dc48b
JH
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,
2a0c00d3 570 follow_fast => 1}, topdir('fa') );
bb7dc48b 571
2a0c00d3
JK
572 is( scalar(keys %Expect_File), 0,
573 "COMPLETE: test of 'follow_fast' option: \$_ case" );
3fa6e24b 574
2a0c00d3 575 ##### #####
3fa6e24b 576
bb7dc48b
JH
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,
2a0c00d3
JK
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);
bb7dc48b 592
3fa6e24b 593 %Expect_Name = ();
3fa6e24b 594
bb7dc48b 595 %Expect_Dir = (dir_path('fa') => 1,
2a0c00d3
JK
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);
bb7dc48b
JH
601
602 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
2a0c00d3
JK
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" );
bb7dc48b 607
2a0c00d3 608 ##### #####
3fa6e24b
TW
609
610 %Expect_File = ();
bb7dc48b
JH
611
612 %Expect_Name = (file_path_name('fa') => 1,
2a0c00d3
JK
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);
bb7dc48b 624
3fa6e24b 625 %Expect_Dir = ();
3fa6e24b 626
bb7dc48b 627 File::Find::finddepth( {wanted => \&wanted_Name,
2a0c00d3 628 follow_fast => 1}, topdir('fa') );
bb7dc48b 629
2a0c00d3
JK
630 is( scalar(keys %Expect_Name), 0,
631 "COMPLETE: test of 'follow_fast' option: \$File::Find::name case" );
632
633 ##### #####
3fa6e24b 634
bb7dc48b
JH
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,
2a0c00d3
JK
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);
bb7dc48b 650
3fa6e24b
TW
651 %Expect_Name = ();
652 %Expect_Dir = ();
bb7dc48b
JH
653
654 File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
2a0c00d3
JK
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" );
bb7dc48b 659
2a0c00d3 660 ##### #####
3fa6e24b 661
3fa6e24b 662 print "# check dangling symbolic links\n";
2a0c00d3
JK
663 mkdir_ok( dir_path('dangling_dir'), 0770 );
664 symlink_ok( dir_path('dangling_dir'), file_path('dangling_dir_sl'),
665 "Check dangling directory" );
3fa6e24b 666 rmdir dir_path('dangling_dir');
2a0c00d3
JK
667 create_file_ok(file_path('dangling_file'));
668 symlink_ok('../dangling_file','fa/dangling_file_sl',
669 "Check dangling file" );
3fa6e24b
TW
670 unlink file_path('dangling_file');
671
2a0c00d3 672 {
3fa6e24b 673 # these tests should also emit a warning
2a0c00d3 674 use warnings;
bb7dc48b
JH
675
676 %Expect_File = (File::Spec->curdir => 1,
2a0c00d3
JK
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);
bb7dc48b 689
3fa6e24b
TW
690 %Expect_Name = ();
691 %Expect_Dir = ();
692 undef $warn_msg;
bb7dc48b
JH
693
694 File::Find::find( {wanted => \&wanted_File, follow => 1,
2a0c00d3
JK
695 dangling_symlinks =>
696 sub { $warn_msg = "$_[0] is a dangling symbolic link" }
bb7dc48b
JH
697 },
698 topdir('dangling_dir_sl'), topdir('fa') );
699
2a0c00d3
JK
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" );
bb7dc48b 704 unlink file_path('fa', 'dangling_file_sl'),
2a0c00d3 705 file_path('dangling_dir_sl');
bb7dc48b 706
3fa6e24b
TW
707 }
708
2a0c00d3 709 ##### #####
3fa6e24b
TW
710
711 print "# check recursion\n";
2a0c00d3 712 symlink_ok('../faa','fa/faa/faa_sl');
7e47e6ff 713 undef $@;
bb7dc48b
JH
714 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
715 no_chdir => 1}, topdir('fa') ); };
2a0c00d3
JK
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');
7e47e6ff 722
3fa6e24b
TW
723
724 print "# check follow_skip (file)\n";
2a0c00d3 725 symlink_ok('./fa_ord','fa/fa_ord_sl');
7e47e6ff 726 undef $@;
bb7dc48b
JH
727
728 eval {File::Find::finddepth( {wanted => \&simple_wanted,
729 follow => 1,
730 follow_skip => 0, no_chdir => 1},
731 topdir('fa') );};
732
2a0c00d3
JK
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 );
7e47e6ff 738
2a0c00d3 739 ##### #####
7e47e6ff 740
bb7dc48b
JH
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,
2a0c00d3
JK
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);
3fa6e24b
TW
758
759 %Expect_Name = ();
bb7dc48b
JH
760
761 %Expect_Dir = (dir_path('fa') => 1,
2a0c00d3
JK
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);
bb7dc48b
JH
767
768 File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
769 follow_skip => 1, no_chdir => 1},
3fa6e24b 770 topdir('fa') );
2a0c00d3
JK
771 is( scalar(keys %Expect_File), 0,
772 "COMPLETE: Test of 'follow', 'follow_skip==1' and 'no_chdir' options" );
3fa6e24b
TW
773 unlink file_path('fa', 'fa_ord_sl');
774
2a0c00d3 775 ##### #####
3fa6e24b 776 print "# check follow_skip (directory)\n";
2a0c00d3 777 symlink_ok('./faa','fa/faa_sl');
3fa6e24b 778 undef $@;
bb7dc48b
JH
779
780 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
781 follow_skip => 0, no_chdir => 1},
3fa6e24b 782 topdir('fa') );};
bb7dc48b 783
2a0c00d3
JK
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
3fa6e24b 790
3fa6e24b 791 undef $@;
bb7dc48b
JH
792
793 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
794 follow_skip => 1, no_chdir => 1},
3fa6e24b 795 topdir('fa') );};
bb7dc48b 796
2a0c00d3
JK
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 ##### #####
3fa6e24b 804
bb7dc48b
JH
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,
2a0c00d3
JK
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);
3fa6e24b
TW
823
824 %Expect_Name = ();
bb7dc48b
JH
825
826 %Expect_Dir = (dir_path('fa') => 1,
2a0c00d3
JK
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);
bb7dc48b
JH
832
833 File::Find::find( {wanted => \&wanted_File_Dir, follow => 1,
2a0c00d3
JK
834 follow_skip => 2, no_chdir => 1}, topdir('fa') );
835
836 ##### #####
bb7dc48b 837
aaaf2301
NC
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;
2a0c00d3
JK
841 is( scalar(@names), 1,
842 "'follow_skip==2'" );
aaaf2301
NC
843 # Normalise both to the original name
844 s/_sl// foreach @names;
2a0c00d3
JK
845 is(
846 $names[0],
847 file_path_name('fa', 'faa', 'faa_ord'),
848 "Got file_path_name, as expected"
849 );
3fa6e24b
TW
850 unlink file_path('fa', 'faa_sl');
851
17ab9c14 852}
68c65ec0 853
2a0c00d3 854##### Win32 checks - [perl #41555] #####
68c65ec0 855
68c65ec0
B
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";
2a0c00d3
JK
860
861 ##### #####
862
68c65ec0
B
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);
2a0c00d3 883
68c65ec0 884 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa'));
2a0c00d3
JK
885 is( scalar(keys %Expect_File), 0, "Got no files, as expected" );
886
887 ##### #####
888
68c65ec0
B
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);
2a0c00d3 899
68c65ec0
B
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);
2a0c00d3 908
68c65ec0 909 File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1}, $volume . topdir('fa'));
2a0c00d3 910 is( scalar(keys %Expect_File), 0, "Got no files, as expected" );
68c65ec0 911}
51393fc0
A
912
913
2a0c00d3
JK
914##### Issue 68260 #####
915
916if ($symlink_exists) {
51393fc0 917 print "# BUG 68260\n";
2a0c00d3
JK
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");
51393fc0
A
923
924 use warnings;
925 my $dangling_symlink;
926 local $SIG {__WARN__} = sub {
2a0c00d3 927 local $" = " "; # "
51393fc0
A
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
2a0c00d3 941 ok(!$dangling_symlink, "Found no dangling symlink");
51393fc0 942}
a0b245d5 943
1da2e9eb
VE
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
2a0c00d3
JK
969##### Issue 59750 #####
970
488985bf 971print "# RT 59750\n";
2a0c00d3
JK
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') );
488985bf
JK
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) );
2a0c00d3
JK
996is( scalar(@files_from_mixed), 5,
997 "Prepare test for RT #59750: got 5 'match' files as expected" );
488985bf
JK
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) );
2a0c00d3
JK
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#####
a0b245d5
AD
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;
2a0c00d3
JK
1021 opendir(my $ROOT_DIR, "/") or die "cannot opendir /: $!\n";
1022 foreach my $f (sort readdir $ROOT_DIR) {
a0b245d5
AD
1023 if (-f "/$f") {
1024 $expected_first_file = $f;
1025 last;
1026 }
1027 }
2a0c00d3 1028 closedir $ROOT_DIR;
33617017
TC
1029 SKIP:
1030 {
a0b245d5
AD
1031 my $created_file;
1032 unless (defined $expected_first_file) {
1033 $expected_first_file = '__perl_File_Find_test.tmp';
1034 open(F, ">", "/$expected_first_file") && close(F)
33617017 1035 or skip "cannot create file in root directory: $!", 8;
a0b245d5
AD
1036 $created_file = 1;
1037 }
1038
1039 # Run F:F:f with/without no_chdir for each possible style of root path.
1040 # NB. If HOME were "/", then an inadvertent chdir('') would fluke the
1041 # expected result, so ensure it is something else:
1042 local $ENV{HOME} = $orig_dir;
1043 foreach my $no_chdir (0, 1) {
1044 foreach my $root_dir ("/", "\\", "$drive/", "$drive\\") {
1045 eval {
1046 File::Find::find({
1047 'no_chdir' => $no_chdir,
1048 'preprocess' => sub { return sort @_ },
1049 'wanted' => sub {
1050 -f or return; # the first call is for $root_dir itself.
1051 my $got = $File::Find::name;
1052 my $exp = "$root_dir$expected_first_file";
1053 print "# no_chdir=$no_chdir $root_dir '$got'\n";
2a0c00d3
JK
1054 is($got, $exp,
1055 "Win32: Run 'find' with 'no_chdir' set to $no_chdir" );
1056 die "done"; # do not process the entire drive!
a0b245d5
AD
1057 },
1058 }, $root_dir);
1059 };
1060 # If F:F:f did not die "done" then it did not Check() either.
1061 unless ($@ and $@ =~ /done/) {
1062 print "# no_chdir=$no_chdir $root_dir ",
1063 ($@ ? "error: $@" : "no files found"), "\n";
2a0c00d3 1064 ok(0, "Win32: 0");
a0b245d5
AD
1065 }
1066 }
1067 }
1068 if ($created_file) {
1069 unlink("/$expected_first_file")
1070 or warn "can't unlink /$expected_first_file: $!\n";
1071 }
33617017 1072 }
a0b245d5 1073}
1c6ce212
JK
1074
1075{
1076 local $@;
1077 eval { File::Find::find( 'foobar' ); };
1078 like($@, qr/no &wanted subroutine given/,
1079 "find() correctly died for lack of &wanted via either coderef or hashref");
1080}
1081
1082{
1083 local $@;
1084 eval { File::Find::find( { follow => 1 } ); };
1085 like($@, qr/no &wanted subroutine given/,
1086 "find() correctly died for lack of &wanted via hashref");
1087}
1088
1089{
1090 local $@;
1091 eval { File::Find::find( { wanted => 1 } ); };
1092 like($@, qr/no &wanted subroutine given/,
1093 "find() correctly died: lack of coderef as value of 'wanted' element");
1094}
1095
1096{
1097 local $@;
1098 my $wanted = sub { print "hello world\n"; };
1099 eval { File::Find::find( $wanted, ( undef ) ); };
1100 like($@, qr/invalid top directory/,
1101 "find() correctly died due to undefined top directory");
1102}