This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Silence build warnings in universal.c
[perl5.git] / lib / File / Find / t / find.t
CommitLineData
3fa6e24b 1#!./perl
dd87e51f
CB
2use strict;
3use Cwd;
81793b90 4
3fa6e24b
TW
5my %Expect_File = (); # what we expect for $_
6my %Expect_Name = (); # what we expect for $File::Find::name/fullname
7my %Expect_Dir = (); # what we expect for $File::Find::dir
81793b90 8my $symlink_exists = eval { symlink("",""); 1 };
dd87e51f 9my ($warn_msg, @files, $file);
3fa6e24b 10
1a3850a5
GA
11
12BEGIN {
234fd682 13 require File::Spec;
1a3850a5 14 chdir 't' if -d 't';
234fd682
JM
15 # May be doing dynamic loading while @INC is all relative
16 unshift @INC => File::Spec->rel2abs('../lib');
7e47e6ff 17
3fa6e24b 18 $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# $_[0]"; }
1a3850a5
GA
19}
20
aaaf2301 21if ( $symlink_exists ) { print "1..199\n"; }
bc125c03 22else { print "1..85\n"; }
1a3850a5 23
dd87e51f
CB
24my $orig_dir = cwd();
25
7dc9aaa5
MS
26# Uncomment this to see where File::Find is chdir'ing to. Helpful for
27# debugging its little jaunts around the filesystem.
28# BEGIN {
29# use Cwd;
30# *CORE::GLOBAL::chdir = sub ($) {
31# my($file, $line) = (caller)[1,2];
32#
33# printf "# cwd: %s\n", cwd();
34# print "# chdir: @_ from $file at $line\n";
35# my($return) = CORE::chdir($_[0]);
36# printf "# newcwd: %s\n", cwd();
37#
38# return $return;
39# };
40# }
41
42
ea05ec2d
CL
43BEGIN {
44 use File::Spec;
45 if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'VMS')
46 {
47 # This is a hack - at present File::Find does not produce native names on
48 # Win32 or VMS, so force File::Spec to use Unix names.
49 # must be set *before* importing File::Find
50 require File::Spec::Unix;
51 @File::Spec::ISA = 'File::Spec::Unix';
52 }
53 require File::Find;
54 import File::Find;
55}
6455dd3b 56
c80f55d1
JH
57cleanup();
58
912440be
NC
59$::count_commonsense = 0;
60find({wanted => sub { ++$::count_commonsense if $_ eq 'commonsense.t'; } },
bb7dc48b 61 File::Spec->curdir);
912440be
NC
62if ($::count_commonsense == 1) {
63 print "ok 1\n";
64} else {
65 print "not ok 1 # found $::count_commonsense files named 'commonsense.t'\n";
66}
3fa6e24b 67
912440be
NC
68$::count_commonsense = 0;
69finddepth({wanted => sub { ++$::count_commonsense if $_ eq 'commonsense.t'; } },
bb7dc48b 70 File::Spec->curdir);
912440be
NC
71if ($::count_commonsense == 1) {
72 print "ok 2\n";
73} else {
74 print "not ok 2 # found $::count_commonsense files named 'commonsense.t'\n";
75}
6455dd3b 76
81793b90 77my $case = 2;
5eb85357 78my $FastFileTests_OK = 0;
81793b90 79
c80f55d1 80sub cleanup {
dd87e51f
CB
81 chdir($orig_dir);
82 my $need_updir = 0;
3fa6e24b 83 if (-d dir_path('for_find')) {
dd87e51f 84 $need_updir = 1 if chdir(dir_path('for_find'));
6fecce66
RGS
85 }
86 if (-d dir_path('fa')) {
87 unlink file_path('fa', 'fa_ord'),
88 file_path('fa', 'fsl'),
89 file_path('fa', 'faa', 'faa_ord'),
90 file_path('fa', 'fab', 'fab_ord'),
91 file_path('fa', 'fab', 'faba', 'faba_ord'),
92 file_path('fb', 'fb_ord'),
93 file_path('fb', 'fba', 'fba_ord');
94 rmdir dir_path('fa', 'faa');
95 rmdir dir_path('fa', 'fab', 'faba');
96 rmdir dir_path('fa', 'fab');
97 rmdir dir_path('fa');
98 rmdir dir_path('fb', 'fba');
99 rmdir dir_path('fb');
100 }
dd87e51f
CB
101 if ($need_updir) {
102 my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
103 chdir($updir);
104 }
6fecce66
RGS
105 if (-d dir_path('for_find')) {
106 rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
c80f55d1
JH
107 }
108}
109
81793b90 110END {
c80f55d1 111 cleanup();
81793b90
GS
112}
113
114sub Check($) {
3fa6e24b
TW
115 $case++;
116 if ($_[0]) { print "ok $case\n"; }
117 else { print "not ok $case\n"; }
81793b90
GS
118}
119
120sub CheckDie($) {
3fa6e24b
TW
121 $case++;
122 if ($_[0]) { print "ok $case\n"; }
123 else { print "not ok $case\n $!\n"; exit 0; }
81793b90
GS
124}
125
126sub touch {
3fa6e24b 127 CheckDie( open(my $T,'>',$_[0]) );
81793b90
GS
128}
129
130sub MkDir($$) {
3fa6e24b 131 CheckDie( mkdir($_[0],$_[1]) );
81793b90
GS
132}
133
3fa6e24b 134sub wanted_File_Dir {
aaaf2301 135 printf "# \$File::Find::dir => '$File::Find::dir'\t\$_ => '$_'\n";
3fa6e24b 136 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
a1ccf0c4 137 s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
3fa6e24b
TW
138 Check( $Expect_File{$_} );
139 if ( $FastFileTests_OK ) {
140 delete $Expect_File{ $_}
141 unless ( $Expect_Dir{$_} && ! -d _ );
142 } else {
143 delete $Expect_File{$_}
144 unless ( $Expect_Dir{$_} && ! -d $_ );
145 }
146}
7e47e6ff 147
3fa6e24b
TW
148sub wanted_File_Dir_prune {
149 &wanted_File_Dir;
150 $File::Find::prune=1 if $_ eq 'faba';
81793b90
GS
151}
152
3fa6e24b
TW
153sub wanted_Name {
154 my $n = $File::Find::name;
155 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.');
156 print "# \$File::Find::name => '$n'\n";
157 my $i = rindex($n,'/');
158 my $OK = exists($Expect_Name{$n});
159 unless ($^O eq 'MacOS') {
160 if ( $OK ) {
161 $OK= exists($Expect_Name{substr($n,0,$i)}) if $i >= 0;
162 }
7e47e6ff 163 }
3fa6e24b
TW
164 Check($OK);
165 delete $Expect_Name{$n};
57907763
GS
166}
167
3fa6e24b
TW
168sub wanted_File {
169 print "# \$_ => '$_'\n";
170 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
171 my $i = rindex($_,'/');
172 my $OK = exists($Expect_File{ $_});
173 unless ($^O eq 'MacOS') {
174 if ( $OK ) {
175 $OK= exists($Expect_File{ substr($_,0,$i)}) if $i >= 0;
176 }
7e47e6ff 177 }
3fa6e24b
TW
178 Check($OK);
179 delete $Expect_File{ $_};
57907763
GS
180}
181
7e47e6ff 182sub simple_wanted {
3fa6e24b
TW
183 print "# \$File::Find::dir => '$File::Find::dir'\n";
184 print "# \$_ => '$_'\n";
7e47e6ff
JH
185}
186
187sub noop_wanted {}
78eac027 188
7e47e6ff 189sub my_preprocess {
3fa6e24b
TW
190 @files = @_;
191 print "# --preprocess--\n";
192 print "# \$File::Find::dir => '$File::Find::dir' \n";
193 foreach $file (@files) {
30db15be 194 $file =~ s/\.(dir)?$// if $^O eq 'VMS';
3fa6e24b
TW
195 print "# $file \n";
196 delete $Expect_Dir{ $File::Find::dir }->{$file};
197 }
198 print "# --end preprocess--\n";
199 Check(scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0);
200 if (scalar(keys %{$Expect_Dir{ $File::Find::dir }}) == 0) {
201 delete $Expect_Dir{ $File::Find::dir }
202 }
203 return @files;
7e47e6ff
JH
204}
205
206sub my_postprocess {
3fa6e24b
TW
207 print "# postprocess: \$File::Find::dir => '$File::Find::dir' \n";
208 delete $Expect_Dir{ $File::Find::dir};
7e47e6ff
JH
209}
210
211
bb7dc48b
JH
212# Use dir_path() to specify a directory path that's expected for
213# $File::Find::dir (%Expect_Dir). Also use it in file operations like
214# chdir, rmdir etc.
3fa6e24b 215#
2586ba89 216# dir_path() concatenates directory names to form a *relative*
30db15be 217# directory path, independent from the platform it's run on, although
2586ba89 218# there are limitations. Don't try to create an absolute path,
bb7dc48b 219# because that may fail on operating systems that have the concept of
2586ba89
JH
220# volume names (e.g. Mac OS). As a special case, you can pass it a "."
221# as first argument, to create a directory path like "./fa/dir" on
bb7dc48b
JH
222# operating systems other than Mac OS (actually, Mac OS will ignore
223# the ".", if it's the first argument). If there's no second argument,
224# this function will return the empty string on Mac OS and the string
225# "./" otherwise.
3fa6e24b
TW
226
227sub dir_path {
2586ba89 228 my $first_arg = shift @_;
3fa6e24b 229
2586ba89 230 if ($first_arg eq '.') {
3fa6e24b
TW
231 if ($^O eq 'MacOS') {
232 return '' unless @_;
233 # ignore first argument; return a relative path
234 # with leading ":" and with trailing ":"
2586ba89 235 return File::Spec->catdir(@_);
3fa6e24b
TW
236 } else { # other OS
237 return './' unless @_;
238 my $path = File::Spec->catdir(@_);
239 # add leading "./"
240 $path = "./$path";
241 return $path;
242 }
243
2586ba89
JH
244 } else { # $first_arg ne '.'
245 return $first_arg unless @_; # return plain filename
246 return File::Spec->catdir($first_arg, @_); # relative path
3fa6e24b
TW
247 }
248}
7e47e6ff 249
7e47e6ff 250
bb7dc48b 251# Use topdir() to specify a directory path that you want to pass to
2586ba89
JH
252# find/finddepth. Basically, topdir() does the same as dir_path() (see
253# above), except that there's no trailing ":" on Mac OS.
7e47e6ff 254
3fa6e24b
TW
255sub topdir {
256 my $path = dir_path(@_);
257 $path =~ s/:$// if ($^O eq 'MacOS');
258 return $path;
259}
7e47e6ff 260
7e47e6ff 261
bb7dc48b 262# Use file_path() to specify a file path that's expected for $_
2586ba89 263# (%Expect_File). Also suitable for file operations like unlink etc.
3fa6e24b 264#
bb7dc48b 265# file_path() concatenates directory names (if any) and a filename to
2586ba89 266# form a *relative* file path (the last argument is assumed to be a
30db15be 267# file). It's independent from the platform it's run on, although
2586ba89
JH
268# there are limitations. As a special case, you can pass it a "." as
269# first argument, to create a file path like "./fa/file" on operating
270# systems other than Mac OS (actually, Mac OS will ignore the ".", if
271# it's the first argument). If there's no second argument, this
272# function will return the empty string on Mac OS and the string "./"
273# otherwise.
3fa6e24b
TW
274
275sub file_path {
2586ba89 276 my $first_arg = shift @_;
3fa6e24b 277
2586ba89 278 if ($first_arg eq '.') {
3fa6e24b
TW
279 if ($^O eq 'MacOS') {
280 return '' unless @_;
281 # ignore first argument; return a relative path
282 # with leading ":", but without trailing ":"
2586ba89 283 return File::Spec->catfile(@_);
3fa6e24b
TW
284 } else { # other OS
285 return './' unless @_;
286 my $path = File::Spec->catfile(@_);
287 # add leading "./"
288 $path = "./$path";
289 return $path;
290 }
291
2586ba89
JH
292 } else { # $first_arg ne '.'
293 return $first_arg unless @_; # return plain filename
294 return File::Spec->catfile($first_arg, @_); # relative path
7e47e6ff 295 }
3fa6e24b
TW
296}
297
298
bb7dc48b
JH
299# Use file_path_name() to specify a file path that's expected for
300# $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
301# option is in effect, $_ is the same as $File::Find::Name. In that
302# case, also use this function to specify a file path that's expected
303# for $_.
3fa6e24b 304#
bb7dc48b
JH
305# Basically, file_path_name() does the same as file_path() (see
306# above), except that there's always a leading ":" on Mac OS, even for
307# plain file/directory names.
3fa6e24b
TW
308
309sub file_path_name {
310 my $path = file_path(@_);
311 $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/));
312 return $path;
313}
314
7e47e6ff 315
3fa6e24b
TW
316
317MkDir( dir_path('for_find'), 0770 );
318CheckDie(chdir( dir_path('for_find')));
319MkDir( dir_path('fa'), 0770 );
320MkDir( dir_path('fb'), 0770 );
321touch( file_path('fb', 'fb_ord') );
322MkDir( dir_path('fb', 'fba'), 0770 );
323touch( file_path('fb', 'fba', 'fba_ord') );
324if ($^O eq 'MacOS') {
325 CheckDie( symlink(':fb',':fa:fsl') ) if $symlink_exists;
7e47e6ff 326} else {
3fa6e24b
TW
327 CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
328}
329touch( file_path('fa', 'fa_ord') );
330
331MkDir( dir_path('fa', 'faa'), 0770 );
332touch( file_path('fa', 'faa', 'faa_ord') );
333MkDir( dir_path('fa', 'fab'), 0770 );
334touch( file_path('fa', 'fab', 'fab_ord') );
335MkDir( dir_path('fa', 'fab', 'faba'), 0770 );
336touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
337
338
bb7dc48b
JH
339%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
340 file_path('fa_ord') => 1, file_path('fab') => 1,
341 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 342 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 343
3fa6e24b
TW
344delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
345%Expect_Name = ();
bb7dc48b
JH
346
347%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
348 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 349 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 350
3fa6e24b
TW
351delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
352File::Find::find( {wanted => \&wanted_File_Dir_prune}, topdir('fa') );
353Check( scalar(keys %Expect_File) == 0 );
354
355
356print "# check re-entrancy\n";
bb7dc48b
JH
357
358%Expect_File = (File::Spec->curdir => 1, file_path('fsl') => 1,
359 file_path('fa_ord') => 1, file_path('fab') => 1,
360 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 361 file_path('faa') => 1, file_path('faa_ord') => 1);
bb7dc48b 362
3fa6e24b
TW
363delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
364%Expect_Name = ();
bb7dc48b
JH
365
366%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
367 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 368 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 369
3fa6e24b 370delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
bb7dc48b
JH
371
372File::Find::find( {wanted => sub { wanted_File_Dir_prune();
373 File::Find::find( {wanted => sub
374 {} }, File::Spec->curdir ); } },
375 topdir('fa') );
376
3fa6e24b
TW
377Check( scalar(keys %Expect_File) == 0 );
378
379
380# no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
bb7dc48b
JH
381
382%Expect_File = (file_path_name('fa') => 1,
383 file_path_name('fa', 'fsl') => 1,
384 file_path_name('fa', 'fa_ord') => 1,
385 file_path_name('fa', 'fab') => 1,
386 file_path_name('fa', 'fab', 'fab_ord') => 1,
387 file_path_name('fa', 'fab', 'faba') => 1,
388 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
389 file_path_name('fa', 'faa') => 1,
390 file_path_name('fa', 'faa', 'faa_ord') => 1,);
391
3fa6e24b
TW
392delete $Expect_File{ file_path_name('fa', 'fsl') } unless $symlink_exists;
393%Expect_Name = ();
bb7dc48b
JH
394
395%Expect_Dir = (dir_path('fa') => 1,
396 dir_path('fa', 'faa') => 1,
397 dir_path('fa', 'fab') => 1,
398 dir_path('fa', 'fab', 'faba') => 1,
399 dir_path('fb') => 1,
400 dir_path('fb', 'fba') => 1);
401
402delete @Expect_Dir{ dir_path('fb'), dir_path('fb', 'fba') }
403 unless $symlink_exists;
404
405File::Find::find( {wanted => \&wanted_File_Dir, no_chdir => 1},
406 topdir('fa') ); Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
407
408
409%Expect_File = ();
bb7dc48b
JH
410
411%Expect_Name = (File::Spec->curdir => 1,
412 file_path_name('.', 'fa') => 1,
413 file_path_name('.', 'fa', 'fsl') => 1,
414 file_path_name('.', 'fa', 'fa_ord') => 1,
415 file_path_name('.', 'fa', 'fab') => 1,
416 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
417 file_path_name('.', 'fa', 'fab', 'faba') => 1,
418 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
419 file_path_name('.', 'fa', 'faa') => 1,
420 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
421 file_path_name('.', 'fb') => 1,
422 file_path_name('.', 'fb', 'fba') => 1,
423 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
424 file_path_name('.', 'fb', 'fb_ord') => 1);
425
3fa6e24b
TW
426delete $Expect_Name{ file_path('.', 'fa', 'fsl') } unless $symlink_exists;
427%Expect_Dir = ();
428File::Find::finddepth( {wanted => \&wanted_Name}, File::Spec->curdir );
429Check( scalar(keys %Expect_Name) == 0 );
430
431
bb7dc48b
JH
432# no_chdir is in effect, hence we use file_path_name to specify the
433# expected paths for %Expect_File
434
435%Expect_File = (File::Spec->curdir => 1,
436 file_path_name('.', 'fa') => 1,
437 file_path_name('.', 'fa', 'fsl') => 1,
438 file_path_name('.', 'fa', 'fa_ord') => 1,
439 file_path_name('.', 'fa', 'fab') => 1,
440 file_path_name('.', 'fa', 'fab', 'fab_ord') => 1,
441 file_path_name('.', 'fa', 'fab', 'faba') => 1,
442 file_path_name('.', 'fa', 'fab', 'faba', 'faba_ord') => 1,
443 file_path_name('.', 'fa', 'faa') => 1,
444 file_path_name('.', 'fa', 'faa', 'faa_ord') => 1,
445 file_path_name('.', 'fb') => 1,
446 file_path_name('.', 'fb', 'fba') => 1,
447 file_path_name('.', 'fb', 'fba', 'fba_ord') => 1,
448 file_path_name('.', 'fb', 'fb_ord') => 1);
449
3fa6e24b
TW
450delete $Expect_File{ file_path_name('.', 'fa', 'fsl') } unless $symlink_exists;
451%Expect_Name = ();
452%Expect_Dir = ();
bb7dc48b
JH
453
454File::Find::finddepth( {wanted => \&wanted_File, no_chdir => 1},
455 File::Spec->curdir );
456
3fa6e24b
TW
457Check( scalar(keys %Expect_File) == 0 );
458
459
460print "# check preprocess\n";
461%Expect_File = ();
462%Expect_Name = ();
463%Expect_Dir = (
464 File::Spec->curdir => {fa => 1, fb => 1},
465 dir_path('.', 'fa') => {faa => 1, fab => 1, fa_ord => 1},
466 dir_path('.', 'fa', 'faa') => {faa_ord => 1},
467 dir_path('.', 'fa', 'fab') => {faba => 1, fab_ord => 1},
468 dir_path('.', 'fa', 'fab', 'faba') => {faba_ord => 1},
469 dir_path('.', 'fb') => {fba => 1, fb_ord => 1},
470 dir_path('.', 'fb', 'fba') => {fba_ord => 1}
471 );
bb7dc48b
JH
472
473File::Find::find( {wanted => \&noop_wanted,
474 preprocess => \&my_preprocess}, File::Spec->curdir );
475
3fa6e24b
TW
476Check( scalar(keys %Expect_Dir) == 0 );
477
478
479print "# check postprocess\n";
480%Expect_File = ();
481%Expect_Name = ();
482%Expect_Dir = (
483 File::Spec->curdir => 1,
484 dir_path('.', 'fa') => 1,
485 dir_path('.', 'fa', 'faa') => 1,
486 dir_path('.', 'fa', 'fab') => 1,
487 dir_path('.', 'fa', 'fab', 'faba') => 1,
488 dir_path('.', 'fb') => 1,
489 dir_path('.', 'fb', 'fba') => 1
490 );
bb7dc48b
JH
491
492File::Find::find( {wanted => \&noop_wanted,
493 postprocess => \&my_postprocess}, File::Spec->curdir );
494
3fa6e24b
TW
495Check( scalar(keys %Expect_Dir) == 0 );
496
17ab9c14
JB
497{
498 print "# checking argument localization\n";
499
500 ### this checks the fix of perlbug [19977] ###
501 my @foo = qw( a b c d e f );
502 my %pre = map { $_ => } @foo;
503
504 File::Find::find( sub { } , 'fa' ) for @foo;
505 delete $pre{$_} for @foo;
506
507 Check( scalar( keys %pre ) == 0 );
508}
3fa6e24b 509
bc125c03
NC
510# see thread starting
511# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-02/msg00351.html
512{
513 print "# checking that &_ and %_ are still accessible and that\n",
514 "# tie magic on \$_ is not triggered\n";
515
516 my $true_count;
517 my $sub = 0;
518 sub _ {
519 ++$sub;
520 }
521 my $tie_called = 0;
522
523 package Foo;
524 sub STORE {
525 ++$tie_called;
526 }
527 sub FETCH {return 'N'};
528 sub TIESCALAR {bless []};
529 package main;
530
531 Check( scalar( keys %_ ) == 0 );
532 my @foo = 'n';
533 tie $foo[0], "Foo";
534
535 File::Find::find( sub { $true_count++; $_{$_}++; &_; } , 'fa' ) for @foo;
536 untie $_;
537
538 Check( $tie_called == 0);
539 Check( scalar( keys %_ ) == $true_count );
540 Check( $sub == $true_count );
541 Check( scalar( @foo ) == 1);
542 Check( $foo[0] eq 'N' );
543}
544
3fa6e24b 545if ( $symlink_exists ) {
bb7dc48b 546 print "# --- symbolic link tests --- \n";
3fa6e24b 547 $FastFileTests_OK= 1;
7e47e6ff 548
7e47e6ff 549
3fa6e24b
TW
550 # Verify that File::Find::find will call wanted even if the topdir of
551 # is a symlink to a directory, and it shouldn't follow the link
552 # unless follow is set, which it isn't in this case
553 %Expect_File = ( file_path('fsl') => 1 );
554 %Expect_Name = ();
555 %Expect_Dir = ();
556 File::Find::find( {wanted => \&wanted_File_Dir}, topdir('fa', 'fsl') );
557 Check( scalar(keys %Expect_File) == 0 );
558
559
bb7dc48b
JH
560 %Expect_File = (File::Spec->curdir => 1, file_path('fa_ord') => 1,
561 file_path('fsl') => 1, file_path('fb_ord') => 1,
562 file_path('fba') => 1, file_path('fba_ord') => 1,
563 file_path('fab') => 1, file_path('fab_ord') => 1,
564 file_path('faba') => 1, file_path('faa') => 1,
565 file_path('faa_ord') => 1);
566
3fa6e24b 567 %Expect_Name = ();
bb7dc48b
JH
568
569 %Expect_Dir = (File::Spec->curdir => 1, dir_path('fa') => 1,
570 dir_path('faa') => 1, dir_path('fab') => 1,
571 dir_path('faba') => 1, dir_path('fb') => 1,
572 dir_path('fba') => 1);
573
574 File::Find::find( {wanted => \&wanted_File_Dir_prune,
575 follow_fast => 1}, topdir('fa') );
576
3fa6e24b
TW
577 Check( scalar(keys %Expect_File) == 0 );
578
579
bb7dc48b
JH
580 # no_chdir is in effect, hence we use file_path_name to specify
581 # the expected paths for %Expect_File
582
583 %Expect_File = (file_path_name('fa') => 1,
584 file_path_name('fa', 'fa_ord') => 1,
585 file_path_name('fa', 'fsl') => 1,
586 file_path_name('fa', 'fsl', 'fb_ord') => 1,
587 file_path_name('fa', 'fsl', 'fba') => 1,
588 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
589 file_path_name('fa', 'fab') => 1,
590 file_path_name('fa', 'fab', 'fab_ord') => 1,
591 file_path_name('fa', 'fab', 'faba') => 1,
592 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
593 file_path_name('fa', 'faa') => 1,
594 file_path_name('fa', 'faa', 'faa_ord') => 1);
595
3fa6e24b 596 %Expect_Name = ();
3fa6e24b 597
bb7dc48b
JH
598 %Expect_Dir = (dir_path('fa') => 1,
599 dir_path('fa', 'faa') => 1,
600 dir_path('fa', 'fab') => 1,
601 dir_path('fa', 'fab', 'faba') => 1,
602 dir_path('fb') => 1,
603 dir_path('fb', 'fba') => 1);
604
605 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
606 no_chdir => 1}, topdir('fa') );
607
608 Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
609
610 %Expect_File = ();
bb7dc48b
JH
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,
3fa6e24b 623 file_path_name('fa', 'faa', 'faa_ord') => 1);
bb7dc48b 624
3fa6e24b 625 %Expect_Dir = ();
3fa6e24b 626
bb7dc48b
JH
627 File::Find::finddepth( {wanted => \&wanted_Name,
628 follow_fast => 1}, topdir('fa') );
629
630 Check( scalar(keys %Expect_Name) == 0 );
3fa6e24b 631
bb7dc48b
JH
632 # no_chdir is in effect, hence we use file_path_name to specify
633 # the expected paths for %Expect_File
634
635 %Expect_File = (file_path_name('fa') => 1,
636 file_path_name('fa', 'fa_ord') => 1,
637 file_path_name('fa', 'fsl') => 1,
638 file_path_name('fa', 'fsl', 'fb_ord') => 1,
639 file_path_name('fa', 'fsl', 'fba') => 1,
640 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
641 file_path_name('fa', 'fab') => 1,
642 file_path_name('fa', 'fab', 'fab_ord') => 1,
643 file_path_name('fa', 'fab', 'faba') => 1,
644 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
645 file_path_name('fa', 'faa') => 1,
3fa6e24b 646 file_path_name('fa', 'faa', 'faa_ord') => 1);
bb7dc48b 647
3fa6e24b
TW
648 %Expect_Name = ();
649 %Expect_Dir = ();
bb7dc48b
JH
650
651 File::Find::finddepth( {wanted => \&wanted_File, follow_fast => 1,
652 no_chdir => 1}, topdir('fa') );
653
3fa6e24b
TW
654 Check( scalar(keys %Expect_File) == 0 );
655
656
657 print "# check dangling symbolic links\n";
658 MkDir( dir_path('dangling_dir'), 0770 );
bb7dc48b
JH
659 CheckDie( symlink( dir_path('dangling_dir'),
660 file_path('dangling_dir_sl') ) );
3fa6e24b
TW
661 rmdir dir_path('dangling_dir');
662 touch(file_path('dangling_file'));
663 if ($^O eq 'MacOS') {
664 CheckDie( symlink('dangling_file', ':fa:dangling_file_sl') );
665 } else {
666 CheckDie( symlink('../dangling_file','fa/dangling_file_sl') );
667 }
668 unlink file_path('dangling_file');
669
670 {
671 # these tests should also emit a warning
bb7dc48b
JH
672 use warnings;
673
674 %Expect_File = (File::Spec->curdir => 1,
615a2b9b 675 file_path('dangling_file_sl') => 1,
bb7dc48b
JH
676 file_path('fa_ord') => 1,
677 file_path('fsl') => 1,
678 file_path('fb_ord') => 1,
679 file_path('fba') => 1,
680 file_path('fba_ord') => 1,
681 file_path('fab') => 1,
682 file_path('fab_ord') => 1,
683 file_path('faba') => 1,
684 file_path('faba_ord') => 1,
685 file_path('faa') => 1,
686 file_path('faa_ord') => 1);
687
3fa6e24b
TW
688 %Expect_Name = ();
689 %Expect_Dir = ();
690 undef $warn_msg;
bb7dc48b
JH
691
692 File::Find::find( {wanted => \&wanted_File, follow => 1,
693 dangling_symlinks =>
694 sub { $warn_msg = "$_[0] is a dangling symbolic link" }
695 },
696 topdir('dangling_dir_sl'), topdir('fa') );
697
3fa6e24b 698 Check( scalar(keys %Expect_File) == 0 );
615a2b9b 699 Check( $warn_msg =~ m|dangling_file_sl is a dangling symbolic link| );
bb7dc48b
JH
700 unlink file_path('fa', 'dangling_file_sl'),
701 file_path('dangling_dir_sl');
702
3fa6e24b
TW
703 }
704
705
706 print "# check recursion\n";
707 if ($^O eq 'MacOS') {
708 CheckDie( symlink(':fa:faa',':fa:faa:faa_sl') );
709 } else {
710 CheckDie( symlink('../faa','fa/faa/faa_sl') );
711 }
7e47e6ff 712 undef $@;
bb7dc48b
JH
713 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
714 no_chdir => 1}, topdir('fa') ); };
a1ccf0c4 715 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]faa_sl is a recursive symbolic link|i );
3fa6e24b 716 unlink file_path('fa', 'faa', 'faa_sl');
7e47e6ff 717
3fa6e24b
TW
718
719 print "# check follow_skip (file)\n";
720 if ($^O eq 'MacOS') {
721 CheckDie( symlink(':fa:fa_ord',':fa:fa_ord_sl') ); # symlink to a file
722 } else {
723 CheckDie( symlink('./fa_ord','fa/fa_ord_sl') ); # symlink to a file
724 }
7e47e6ff 725 undef $@;
bb7dc48b
JH
726
727 eval {File::Find::finddepth( {wanted => \&simple_wanted,
728 follow => 1,
729 follow_skip => 0, no_chdir => 1},
730 topdir('fa') );};
731
a1ccf0c4 732 Check( $@ =~ m|for_find[:/]fa[:/]fa_ord encountered a second time|i );
7e47e6ff 733
7e47e6ff 734
bb7dc48b
JH
735 # no_chdir is in effect, hence we use file_path_name to specify
736 # the expected paths for %Expect_File
737
738 %Expect_File = (file_path_name('fa') => 1,
aaaf2301
NC
739 file_path_name('fa', 'fa_ord') => 2,
740 # We may encounter the symlink first
741 file_path_name('fa', 'fa_ord_sl') => 2,
bb7dc48b
JH
742 file_path_name('fa', 'fsl') => 1,
743 file_path_name('fa', 'fsl', 'fb_ord') => 1,
744 file_path_name('fa', 'fsl', 'fba') => 1,
745 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
746 file_path_name('fa', 'fab') => 1,
747 file_path_name('fa', 'fab', 'fab_ord') => 1,
748 file_path_name('fa', 'fab', 'faba') => 1,
749 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
750 file_path_name('fa', 'faa') => 1,
751 file_path_name('fa', 'faa', 'faa_ord') => 1);
3fa6e24b
TW
752
753 %Expect_Name = ();
bb7dc48b
JH
754
755 %Expect_Dir = (dir_path('fa') => 1,
756 dir_path('fa', 'faa') => 1,
757 dir_path('fa', 'fab') => 1,
758 dir_path('fa', 'fab', 'faba') => 1,
759 dir_path('fb') => 1,
760 dir_path('fb','fba') => 1);
761
762 File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
763 follow_skip => 1, no_chdir => 1},
3fa6e24b 764 topdir('fa') );
615a2b9b 765 Check( scalar(keys %Expect_File) == 0 );
3fa6e24b
TW
766 unlink file_path('fa', 'fa_ord_sl');
767
768
769 print "# check follow_skip (directory)\n";
770 if ($^O eq 'MacOS') {
771 CheckDie( symlink(':fa:faa',':fa:faa_sl') ); # symlink to a directory
772 } else {
773 CheckDie( symlink('./faa','fa/faa_sl') ); # symlink to a directory
7e47e6ff 774 }
3fa6e24b 775 undef $@;
bb7dc48b
JH
776
777 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
778 follow_skip => 0, no_chdir => 1},
3fa6e24b 779 topdir('fa') );};
bb7dc48b 780
a1ccf0c4 781 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
3fa6e24b
TW
782
783
784 undef $@;
bb7dc48b
JH
785
786 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
787 follow_skip => 1, no_chdir => 1},
3fa6e24b 788 topdir('fa') );};
bb7dc48b 789
a1ccf0c4 790 Check( $@ =~ m|for_find[:/]fa[:/]faa[:/]? encountered a second time|i );
3fa6e24b 791
bb7dc48b
JH
792 # no_chdir is in effect, hence we use file_path_name to specify
793 # the expected paths for %Expect_File
794
795 %Expect_File = (file_path_name('fa') => 1,
796 file_path_name('fa', 'fa_ord') => 1,
797 file_path_name('fa', 'fsl') => 1,
798 file_path_name('fa', 'fsl', 'fb_ord') => 1,
799 file_path_name('fa', 'fsl', 'fba') => 1,
800 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
801 file_path_name('fa', 'fab') => 1,
802 file_path_name('fa', 'fab', 'fab_ord') => 1,
803 file_path_name('fa', 'fab', 'faba') => 1,
804 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
805 file_path_name('fa', 'faa') => 1,
aaaf2301
NC
806 file_path_name('fa', 'faa', 'faa_ord') => 1,
807 # We may actually encounter the symlink first.
808 file_path_name('fa', 'faa_sl') => 1,
809 file_path_name('fa', 'faa_sl', 'faa_ord') => 1);
3fa6e24b
TW
810
811 %Expect_Name = ();
bb7dc48b
JH
812
813 %Expect_Dir = (dir_path('fa') => 1,
814 dir_path('fa', 'faa') => 1,
815 dir_path('fa', 'fab') => 1,
816 dir_path('fa', 'fab', 'faba') => 1,
817 dir_path('fb') => 1,
818 dir_path('fb', 'fba') => 1);
819
820 File::Find::find( {wanted => \&wanted_File_Dir, follow => 1,
821 follow_skip => 2, no_chdir => 1}, topdir('fa') );
822
aaaf2301
NC
823 # If we encountered the symlink first, then the entries corresponding to
824 # the real name remain, if the real name first then the symlink
825 my @names = sort keys %Expect_File;
615a2b9b 826 Check( @names == 1 );
aaaf2301
NC
827 # Normalise both to the original name
828 s/_sl// foreach @names;
615a2b9b 829 Check ($names[0] eq file_path_name('fa', 'faa', 'faa_ord'));
3fa6e24b
TW
830 unlink file_path('fa', 'faa_sl');
831
17ab9c14 832}