This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gcc -Wall nits picked out by a non-UNIX system
[perl5.git] / t / lib / filefind.t
CommitLineData
81793b90
GS
1####!./perl
2
3
4my %Expect;
5my $symlink_exists = eval { symlink("",""); 1 };
1a3850a5
GA
6
7BEGIN {
8 chdir 't' if -d 't';
20822f61 9 @INC = '../lib';
1a3850a5
GA
10}
11
78eac027 12if ( $symlink_exists ) { print "1..119\n"; }
57907763 13else { print "1..61\n"; }
1a3850a5
GA
14
15use File::Find;
16
c80f55d1
JH
17cleanup();
18
1a3850a5 19find(sub { print "ok 1\n" if $_ eq 'filefind.t'; }, ".");
55d729e4 20finddepth(sub { print "ok 2\n" if $_ eq 'filefind.t'; }, ".");
81793b90 21
81793b90 22my $case = 2;
5eb85357 23my $FastFileTests_OK = 0;
81793b90 24
c80f55d1
JH
25sub cleanup {
26 if (-d 'for_find') {
27 chdir('for_find');
28 }
29 if (-d 'fa') {
30 unlink 'fa/fa_ord', 'fa/fsl', 'fa/faa/faa_ord',
31 'fa/fab/fab_ord', 'fa/fab/faba/faba_ord',
32 'fb/fb_ord', 'fb/fba/fba_ord';
33 rmdir 'fa/faa';
34 rmdir 'fa/fab/faba';
35 rmdir 'fa/fab';
36 rmdir 'fa';
37 rmdir 'fb/fba';
38 rmdir 'fb';
39 chdir '..';
40 rmdir 'for_find';
41 }
42}
43
81793b90 44END {
c80f55d1 45 cleanup();
81793b90
GS
46}
47
48sub Check($) {
49 $case++;
50 if ($_[0]) { print "ok $case\n"; }
51 else { print "not ok $case\n"; }
52}
53
54sub CheckDie($) {
55 $case++;
56 if ($_[0]) { print "ok $case\n"; }
57 else { print "not ok $case\n $!\n"; exit 0; }
58}
59
60sub touch {
61 CheckDie( open(my $T,'>',$_[0]) );
62}
63
64sub MkDir($$) {
65 CheckDie( mkdir($_[0],$_[1]) );
66}
67
68sub wanted {
fd886cf2 69 print "# '$_' => 1\n";
11611c0f 70 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
81793b90 71 Check( $Expect{$_} );
5eb85357
GS
72 if ( $FastFileTests_OK ) {
73 delete $Expect{$_}
74 unless ( $Expect_Dir{$_} && ! -d _ );
75 } else {
76 delete $Expect{$_}
77 unless ( $Expect_Dir{$_} && ! -d $_ );
78 }
10fbe481 79 $File::Find::prune=1 if $_ eq 'faba';
704ea872 80
81793b90
GS
81}
82
57907763
GS
83sub dn_wanted {
84 my $n = $File::Find::name;
11611c0f 85 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.');
57907763
GS
86 print "# '$n' => 1\n";
87 my $i = rindex($n,'/');
88 my $OK = exists($Expect{$n});
89 if ( $OK ) {
90 $OK= exists($Expect{substr($n,0,$i)}) if $i >= 0;
91 }
92 Check($OK);
93 delete $Expect{$n};
94}
95
96sub d_wanted {
97 print "# '$_' => 1\n";
11611c0f 98 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
57907763
GS
99 my $i = rindex($_,'/');
100 my $OK = exists($Expect{$_});
101 if ( $OK ) {
102 $OK= exists($Expect{substr($_,0,$i)}) if $i >= 0;
103 }
104 Check($OK);
105 delete $Expect{$_};
106}
107
108MkDir( 'for_find',0770 );
109CheckDie(chdir(for_find));
10fbe481
GS
110MkDir( 'fa',0770 );
111MkDir( 'fb',0770 );
112touch('fb/fb_ord');
113MkDir( 'fb/fba',0770 );
114touch('fb/fba/fba_ord');
115CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
116touch('fa/fa_ord');
117
118MkDir( 'fa/faa',0770 );
119touch('fa/faa/faa_ord');
120MkDir( 'fa/fab',0770 );
121touch('fa/fab/fab_ord');
122MkDir( 'fa/fab/faba',0770 );
123touch('fa/fab/faba/faba_ord');
124
125%Expect = ('.' => 1, 'fsl' => 1, 'fa_ord' => 1, 'fab' => 1, 'fab_ord' => 1,
126 'faba' => 1, 'faa' => 1, 'faa_ord' => 1);
127delete $Expect{'fsl'} unless $symlink_exists;
704ea872
GS
128%Expect_Dir = ('fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
129 'fb' => 1, 'fba' => 1);
130delete @Expect_Dir{'fb','fba'} unless $symlink_exists;
10fbe481 131File::Find::find( {wanted => \&wanted, },'fa' );
81793b90
GS
132Check( scalar(keys %Expect) == 0 );
133
10fbe481
GS
134%Expect=('fa' => 1, 'fa/fsl' => 1, 'fa/fa_ord' => 1, 'fa/fab' => 1,
135 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1,
136 'fa/fab/faba/faba_ord' => 1, 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
137delete $Expect{'fa/fsl'} unless $symlink_exists;
704ea872
GS
138%Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
139 'fb' => 1, 'fb/fba' => 1);
140delete @Expect_Dir{'fb','fb/fba'} unless $symlink_exists;
10fbe481 141File::Find::find( {wanted => \&wanted, no_chdir => 1},'fa' );
81793b90
GS
142
143Check( scalar(keys %Expect) == 0 );
144
57907763
GS
145%Expect=('.' => 1, './fa' => 1, './fa/fsl' => 1, './fa/fa_ord' => 1, './fa/fab' => 1,
146 './fa/fab/fab_ord' => 1, './fa/fab/faba' => 1,
147 './fa/fab/faba/faba_ord' => 1, './fa/faa' => 1, './fa/faa/faa_ord' => 1,
148 './fb' => 1, './fb/fba' => 1, './fb/fba/fba_ord' => 1, './fb/fb_ord' => 1);
149delete $Expect{'./fa/fsl'} unless $symlink_exists;
704ea872
GS
150%Expect_Dir = ('./fa' => 1, './fa/faa' => 1, '/fa/fab' => 1, './fa/fab/faba' => 1,
151 './fb' => 1, './fb/fba' => 1);
152delete @Expect_Dir{'./fb','./fb/fba'} unless $symlink_exists;
57907763
GS
153File::Find::finddepth( {wanted => \&dn_wanted },'.' );
154Check( scalar(keys %Expect) == 0 );
155
156%Expect=('.' => 1, './fa' => 1, './fa/fsl' => 1, './fa/fa_ord' => 1, './fa/fab' => 1,
157 './fa/fab/fab_ord' => 1, './fa/fab/faba' => 1,
158 './fa/fab/faba/faba_ord' => 1, './fa/faa' => 1, './fa/faa/faa_ord' => 1,
159 './fb' => 1, './fb/fba' => 1, './fb/fba/fba_ord' => 1, './fb/fb_ord' => 1);
160delete $Expect{'./fa/fsl'} unless $symlink_exists;
704ea872
GS
161%Expect_Dir = ('./fa' => 1, './fa/faa' => 1, '/fa/fab' => 1, './fa/fab/faba' => 1,
162 './fb' => 1, './fb/fba' => 1);
163delete @Expect_Dir{'./fb','./fb/fba'} unless $symlink_exists;
57907763
GS
164File::Find::finddepth( {wanted => \&d_wanted, no_chdir => 1 },'.' );
165Check( scalar(keys %Expect) == 0 );
166
81793b90 167if ( $symlink_exists ) {
5eb85357 168 $FastFileTests_OK= 1;
10fbe481
GS
169 %Expect=('.' => 1, 'fa_ord' => 1, 'fsl' => 1, 'fb_ord' => 1, 'fba' => 1,
170 'fba_ord' => 1, 'fab' => 1, 'fab_ord' => 1, 'faba' => 1, 'faa' => 1,
171 'faa_ord' => 1);
704ea872
GS
172 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
173 'fb' => 1, 'fb/fba' => 1);
81793b90 174
10fbe481 175 File::Find::find( {wanted => \&wanted, follow_fast => 1},'fa' );
81793b90 176 Check( scalar(keys %Expect) == 0 );
57907763 177
10fbe481
GS
178 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
179 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
180 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
181 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
704ea872
GS
182 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
183 'fb' => 1, 'fb/fba' => 1);
10fbe481 184 File::Find::find( {wanted => \&wanted, follow_fast => 1, no_chdir => 1},'fa' );
81793b90 185 Check( scalar(keys %Expect) == 0 );
57907763
GS
186
187 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
188 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
189 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
190 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
704ea872
GS
191 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
192 'fb' => 1, 'fb/fba' => 1);
57907763
GS
193
194 File::Find::finddepth( {wanted => \&dn_wanted, follow_fast => 1},'fa' );
195 Check( scalar(keys %Expect) == 0 );
196
197 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
198 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
199 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
200 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
704ea872
GS
201 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
202 'fb' => 1, 'fb/fba' => 1);
57907763
GS
203
204 File::Find::finddepth( {wanted => \&d_wanted, follow_fast => 1, no_chdir => 1},'fa' );
205 Check( scalar(keys %Expect) == 0 );
78eac027
DD
206
207 # Verify that File::Find::find will call wanted even if the topdir of
208 # is a symlink to a directory, and it shouldn't follow the link
209 # unless follow is set, which it isn't in this case
210 %Expect = ('fsl' => 1);
211 %Expect_Dir = ();
212 File::Find::find( {wanted => \&wanted, },'fa/fsl' );
213 Check( scalar(keys %Expect) == 0 );
214
81793b90
GS
215}
216
217print "# of cases: $case\n";