This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Noted recent changes to Module::CoreList and added http://perlpunks.de/corelist
[perl5.git] / t / op / filetest.t
CommitLineData
42e55ab1
JH
1#!./perl
2
3# There are few filetest operators that are portable enough to test.
4# See pod/perlport.pod for details.
5
6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
fbb0b3b3 9 require './test.pl';
42e55ab1
JH
10}
11
eb57363f 12use Config;
433644ee 13plan(tests => 34 + 27*14);
42e55ab1 14
fbb0b3b3
RGS
15ok( -d 'op' );
16ok( -f 'TEST' );
17ok( !-f 'op' );
18ok( !-d 'TEST' );
19ok( -r 'TEST' );
42e55ab1 20
d4188802
NC
21# Make a read only file
22my $ro_file = tempfile();
23
24{
25 open my $fh, '>', $ro_file or die "open $fh: $!";
26 close $fh or die "close $fh: $!";
27}
28
29chmod 0555, $ro_file or die "chmod 0555, '$ro_file' failed: $!";
846f25a3
GW
30
31$oldeuid = $>; # root can read and write anything
32eval '$> = 1'; # so switch uid (may not be implemented)
33
34print "# oldeuid = $oldeuid, euid = $>\n";
35
fbb0b3b3
RGS
36SKIP: {
37 if (!$Config{d_seteuid}) {
38 skip('no seteuid');
39 }
fbb0b3b3 40 else {
d4188802 41 ok( !-w $ro_file );
fbb0b3b3 42 }
22c35a8c 43}
42e55ab1 44
846f25a3 45# Scripts are not -x everywhere so cannot test that.
42e55ab1 46
fd1e013e
F
47eval '$> = $oldeuid'; # switch uid back (may not be implemented)
48
49# this would fail for the euid 1
50# (unless we have unpacked the source code as uid 1...)
fbb0b3b3 51ok( -r 'op' );
42e55ab1 52
846f25a3
GW
53# this would fail for the euid 1
54# (unless we have unpacked the source code as uid 1...)
fbb0b3b3
RGS
55SKIP: {
56 if ($Config{d_seteuid}) {
57 ok( -w 'op' );
58 } else {
59 skip('no seteuid');
60 }
3eeba6fb 61}
42e55ab1 62
fbb0b3b3
RGS
63ok( -x 'op' ); # Hohum. Are directories -x everywhere?
64
65is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
66
67# Test stackability of filetest operators
42e55ab1 68
fbb0b3b3
RGS
69ok( defined( -f -d 'TEST' ) && ! -f -d _ );
70ok( !defined( -e 'zoo' ) );
71ok( !defined( -e -d 'zoo' ) );
72ok( !defined( -f -e 'zoo' ) );
73ok( -f -e 'TEST' );
74ok( -e -f 'TEST' );
75ok( defined(-d -e 'TEST') );
76ok( defined(-e -d 'TEST') );
77ok( ! -f -d 'op' );
78ok( -x -d -x 'op' );
79ok( (-s -f 'TEST' > 1), "-s returns real size" );
80ok( -f -s 'TEST' == 1 );
7294df96 81
e7d3eb55 82# now with an empty file
1ab9acc5
B
83my $tempfile = tempfile();
84open my $fh, ">", $tempfile;
e7d3eb55 85close $fh;
1ab9acc5
B
86ok( -f $tempfile );
87is( -s $tempfile, 0 );
88is( -f -s $tempfile, 0 );
89is( -s -f $tempfile, 0 );
15f2ab62 90unlink_all $tempfile;
e7d3eb55 91
1f26655e
FC
92# stacked -l
93eval { -l -e "TEST" };
94like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
95 'stacked -l non-lstat error with warnings off';
96{
97 local $^W = 1;
98 eval { -l -e "TEST" };
99 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
100 'stacked -l non-lstat error with warnings on';
101}
102# Make sure -l is using the previous stat buffer, and not using the previ-
103# ous op’s return value as a file name.
104SKIP: {
105 use Perl::OSType 'os_type';
106 if (os_type ne 'Unix') { skip "Not Unix", 1 }
107 chomp(my $ln = `which ln`);
108 if ( ! -e $ln ) { skip "No ln" , 1 }
109 lstat "TEST";
110 `ln -s TEST 1`;
111 ok ! -l -e _, 'stacked -l uses previous stat, not previous retval';
112 unlink 1;
433644ee
FC
113
114 # Since we already have our skip block set up, we might as well put this
115 # test here, too:
116 # -l always treats a non-bareword argument as a file name
117 system qw "ln -s TEST", \*foo;
118 local $^W = 1;
119 ok -l \*foo, '-l \*foo is a file name';
120 unlink \*foo;
1f26655e
FC
121}
122
7294df96
RGS
123# test that _ is a bareword after filetest operators
124
125-f 'TEST';
126ok( -f _ );
127sub _ { "this is not a file name" }
128ok( -f _ );
d89f1457
BM
129
130my $over;
131{
132 package OverFtest;
133
4d57d24f 134 use overload
c1d4704a 135 fallback => 1,
4d57d24f 136 -X => sub {
c1d4704a 137 $over = [qq($_[0]), $_[1]];
4d57d24f
BM
138 "-$_[1]";
139 };
d89f1457 140}
f6aa8023
BM
141{
142 package OverString;
143
144 # No fallback. -X should fall back to string overload even without
145 # it.
146 use overload q/""/ => sub { $over = 1; "TEST" };
147}
148{
149 package OverBoth;
150
151 use overload
152 q/""/ => sub { "TEST" },
153 -X => sub { "-$_[1]" };
154}
155{
156 package OverNeither;
157
4d57d24f 158 # Need fallback. Previous versions of perl required 'fallback' to do
f6aa8023
BM
159 # -X operations on an object with no "" overload.
160 use overload
161 '+' => sub { 1 },
162 fallback => 1;
163}
164
165my $ft = bless [], "OverFtest";
c1d4704a 166my $ftstr = qq($ft);
f6aa8023
BM
167my $str = bless [], "OverString";
168my $both = bless [], "OverBoth";
169my $neither = bless [], "OverNeither";
c1d4704a 170my $nstr = qq($neither);
d89f1457 171
d0c91b6a
BM
172open my $gv, "<", "TEST";
173bless $gv, "OverString";
174open my $io, "<", "TEST";
175$io = *{$io}{IO};
176bless $io, "OverString";
177
822146ea
RGS
178my $fcntl_not_available;
179eval { require Fcntl } or $fcntl_not_available = 1;
fa7f7498 180
d89f1457 181for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
d3ebc3eb 182 $over = [];
f6aa8023
BM
183 ok( my $rv = eval "-$op \$ft", "overloaded -$op succeeds" )
184 or diag( $@ );
185 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
d89f1457
BM
186 is( $over->[1], $op, "correct op for overloaded -$op" );
187 is( $rv, "-$op", "correct return value for overloaded -$op");
f6aa8023 188
fa7f7498 189 my ($exp, $is) = (1, "is");
190 if (
822146ea 191 !$fcntl_not_available and (
fa7f7498 192 $op eq "u" and not eval { Fcntl::S_ISUID() } or
193 $op eq "g" and not eval { Fcntl::S_ISGID() } or
194 $op eq "k" and not eval { Fcntl::S_ISVTX() }
822146ea 195 )
fa7f7498 196 ) {
197 ($exp, $is) = (0, "not");
198 }
199
f6aa8023
BM
200 $over = 0;
201 $rv = eval "-$op \$str";
202 ok( !$@, "-$op succeeds with string overloading" )
203 or diag( $@ );
204 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
fa7f7498 205 is( $over, $exp, "string overload $is called for -$op" );
f6aa8023 206
fa7f7498 207 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
d0c91b6a
BM
208
209 $over = 0;
210 eval "-$op \$gv";
211 is( $over, $exp, "string overload $is called for -$op on GLOB" );
212
52f7f5ab
BM
213 # IO refs always get string overload called. This might be a bug.
214 $op eq "t" || $op eq "T" || $op eq "B"
215 and ($exp, $is) = (1, "is");
216
d0c91b6a
BM
217 $over = 0;
218 eval "-$op \$io";
219 is( $over, $exp, "string overload $is called for -$op on IO");
220
f6aa8023
BM
221 $rv = eval "-$op \$both";
222 is( $rv, "-$op", "correct -$op on string/-X overload" );
223
224 $rv = eval "-$op \$neither";
225 ok( !$@, "-$op succeeds with random overloading" )
226 or diag( $@ );
227 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
f6aa8023 228
4d57d24f
BM
229 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
230 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
231}
ca867162
FC
232
233# -l stack corruption: this bug occurred from 5.8 to 5.14
234{
235 push my @foo, "bar", -l baz;
236 is $foo[0], "bar", '-l bareword does not corrupt the stack';
237}
49498caf
FC
238
239# File test ops should not call get-magic on the topmost SV on the stack if
240# it belongs to another op.
241{
242 my $w;
243 sub oon::TIESCALAR{bless[],'oon'}
244 sub oon::FETCH{$w++}
245 tie my $t, 'oon';
246 push my @a, $t, -t;
247 is $w, 1, 'file test does not call FETCH on stack item not its own';
248}