This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
BSD/OS (bsdi) hints update by Timur I. Bakeyev and Todd C. Miller,
[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';
93430cb4 9 unshift @INC, '../lib';
1a3850a5
GA
10}
11
57907763
GS
12if ( $symlink_exists ) { print "1..117\n"; }
13else { print "1..61\n"; }
1a3850a5
GA
14
15use File::Find;
16
1a3850a5 17find(sub { print "ok 1\n" if $_ eq 'filefind.t'; }, ".");
55d729e4 18finddepth(sub { print "ok 2\n" if $_ eq 'filefind.t'; }, ".");
81793b90
GS
19
20
21my $case = 2;
22
23END {
10fbe481
GS
24 unlink 'fa/fa_ord','fa/fsl','fa/faa/faa_ord',
25 'fa/fab/fab_ord','fa/fab/faba/faba_ord','fb/fb_ord','fb/fba/fba_ord';
26 rmdir 'fa/faa';
27 rmdir 'fa/fab/faba';
28 rmdir 'fa/fab';
29 rmdir 'fa';
30 rmdir 'fb/fba';
31 rmdir 'fb';
57907763
GS
32 chdir '..';
33 rmdir 'for_find';
81793b90
GS
34}
35
36sub Check($) {
37 $case++;
38 if ($_[0]) { print "ok $case\n"; }
39 else { print "not ok $case\n"; }
40}
41
42sub CheckDie($) {
43 $case++;
44 if ($_[0]) { print "ok $case\n"; }
45 else { print "not ok $case\n $!\n"; exit 0; }
46}
47
48sub touch {
49 CheckDie( open(my $T,'>',$_[0]) );
50}
51
52sub MkDir($$) {
53 CheckDie( mkdir($_[0],$_[1]) );
54}
55
56sub wanted {
fd886cf2 57 print "# '$_' => 1\n";
11611c0f 58 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
81793b90
GS
59 Check( $Expect{$_} );
60 delete $Expect{$_};
10fbe481 61 $File::Find::prune=1 if $_ eq 'faba';
81793b90
GS
62}
63
57907763
GS
64sub dn_wanted {
65 my $n = $File::Find::name;
11611c0f 66 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.');
57907763
GS
67 print "# '$n' => 1\n";
68 my $i = rindex($n,'/');
69 my $OK = exists($Expect{$n});
70 if ( $OK ) {
71 $OK= exists($Expect{substr($n,0,$i)}) if $i >= 0;
72 }
73 Check($OK);
74 delete $Expect{$n};
75}
76
77sub d_wanted {
78 print "# '$_' => 1\n";
11611c0f 79 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
57907763
GS
80 my $i = rindex($_,'/');
81 my $OK = exists($Expect{$_});
82 if ( $OK ) {
83 $OK= exists($Expect{substr($_,0,$i)}) if $i >= 0;
84 }
85 Check($OK);
86 delete $Expect{$_};
87}
88
89MkDir( 'for_find',0770 );
90CheckDie(chdir(for_find));
10fbe481
GS
91MkDir( 'fa',0770 );
92MkDir( 'fb',0770 );
93touch('fb/fb_ord');
94MkDir( 'fb/fba',0770 );
95touch('fb/fba/fba_ord');
96CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
97touch('fa/fa_ord');
98
99MkDir( 'fa/faa',0770 );
100touch('fa/faa/faa_ord');
101MkDir( 'fa/fab',0770 );
102touch('fa/fab/fab_ord');
103MkDir( 'fa/fab/faba',0770 );
104touch('fa/fab/faba/faba_ord');
105
106%Expect = ('.' => 1, 'fsl' => 1, 'fa_ord' => 1, 'fab' => 1, 'fab_ord' => 1,
107 'faba' => 1, 'faa' => 1, 'faa_ord' => 1);
108delete $Expect{'fsl'} unless $symlink_exists;
109File::Find::find( {wanted => \&wanted, },'fa' );
81793b90
GS
110Check( scalar(keys %Expect) == 0 );
111
10fbe481
GS
112%Expect=('fa' => 1, 'fa/fsl' => 1, 'fa/fa_ord' => 1, 'fa/fab' => 1,
113 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1,
114 'fa/fab/faba/faba_ord' => 1, 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
115delete $Expect{'fa/fsl'} unless $symlink_exists;
116File::Find::find( {wanted => \&wanted, no_chdir => 1},'fa' );
81793b90
GS
117
118Check( scalar(keys %Expect) == 0 );
119
57907763
GS
120%Expect=('.' => 1, './fa' => 1, './fa/fsl' => 1, './fa/fa_ord' => 1, './fa/fab' => 1,
121 './fa/fab/fab_ord' => 1, './fa/fab/faba' => 1,
122 './fa/fab/faba/faba_ord' => 1, './fa/faa' => 1, './fa/faa/faa_ord' => 1,
123 './fb' => 1, './fb/fba' => 1, './fb/fba/fba_ord' => 1, './fb/fb_ord' => 1);
124delete $Expect{'./fa/fsl'} unless $symlink_exists;
125File::Find::finddepth( {wanted => \&dn_wanted },'.' );
126Check( scalar(keys %Expect) == 0 );
127
128%Expect=('.' => 1, './fa' => 1, './fa/fsl' => 1, './fa/fa_ord' => 1, './fa/fab' => 1,
129 './fa/fab/fab_ord' => 1, './fa/fab/faba' => 1,
130 './fa/fab/faba/faba_ord' => 1, './fa/faa' => 1, './fa/faa/faa_ord' => 1,
131 './fb' => 1, './fb/fba' => 1, './fb/fba/fba_ord' => 1, './fb/fb_ord' => 1);
132delete $Expect{'./fa/fsl'} unless $symlink_exists;
133File::Find::finddepth( {wanted => \&d_wanted, no_chdir => 1 },'.' );
134Check( scalar(keys %Expect) == 0 );
135
81793b90 136if ( $symlink_exists ) {
10fbe481
GS
137 %Expect=('.' => 1, 'fa_ord' => 1, 'fsl' => 1, 'fb_ord' => 1, 'fba' => 1,
138 'fba_ord' => 1, 'fab' => 1, 'fab_ord' => 1, 'faba' => 1, 'faa' => 1,
139 'faa_ord' => 1);
81793b90 140
10fbe481 141 File::Find::find( {wanted => \&wanted, follow_fast => 1},'fa' );
81793b90 142 Check( scalar(keys %Expect) == 0 );
57907763 143
10fbe481
GS
144 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
145 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
146 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
147 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
148 File::Find::find( {wanted => \&wanted, follow_fast => 1, no_chdir => 1},'fa' );
81793b90 149 Check( scalar(keys %Expect) == 0 );
57907763
GS
150
151 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
152 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
153 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
154 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
155
156 File::Find::finddepth( {wanted => \&dn_wanted, follow_fast => 1},'fa' );
157 Check( scalar(keys %Expect) == 0 );
158
159 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
160 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
161 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
162 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
163
164 File::Find::finddepth( {wanted => \&d_wanted, follow_fast => 1, no_chdir => 1},'fa' );
165 Check( scalar(keys %Expect) == 0 );
81793b90
GS
166}
167
168print "# of cases: $case\n";