This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
podcheck.t: skip make-rmg-checklist
[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';
61602f40 106 if (os_type ne 'Unix') { skip "Not Unix", 2 }
42ef694e 107 if (-l "TEST") { skip "TEST is a symlink", 2 }
1f26655e 108 chomp(my $ln = `which ln`);
61602f40 109 if ( ! -e $ln ) { skip "No ln" , 2 }
1f26655e
FC
110 lstat "TEST";
111 `ln -s TEST 1`;
112 ok ! -l -e _, 'stacked -l uses previous stat, not previous retval';
113 unlink 1;
433644ee
FC
114
115 # Since we already have our skip block set up, we might as well put this
116 # test here, too:
117 # -l always treats a non-bareword argument as a file name
118 system qw "ln -s TEST", \*foo;
119 local $^W = 1;
120 ok -l \*foo, '-l \*foo is a file name';
121 unlink \*foo;
1f26655e
FC
122}
123
7294df96
RGS
124# test that _ is a bareword after filetest operators
125
126-f 'TEST';
127ok( -f _ );
128sub _ { "this is not a file name" }
129ok( -f _ );
d89f1457
BM
130
131my $over;
132{
133 package OverFtest;
134
4d57d24f 135 use overload
c1d4704a 136 fallback => 1,
4d57d24f 137 -X => sub {
c1d4704a 138 $over = [qq($_[0]), $_[1]];
4d57d24f
BM
139 "-$_[1]";
140 };
d89f1457 141}
f6aa8023
BM
142{
143 package OverString;
144
145 # No fallback. -X should fall back to string overload even without
146 # it.
147 use overload q/""/ => sub { $over = 1; "TEST" };
148}
149{
150 package OverBoth;
151
152 use overload
153 q/""/ => sub { "TEST" },
154 -X => sub { "-$_[1]" };
155}
156{
157 package OverNeither;
158
4d57d24f 159 # Need fallback. Previous versions of perl required 'fallback' to do
f6aa8023
BM
160 # -X operations on an object with no "" overload.
161 use overload
162 '+' => sub { 1 },
163 fallback => 1;
164}
165
166my $ft = bless [], "OverFtest";
c1d4704a 167my $ftstr = qq($ft);
f6aa8023
BM
168my $str = bless [], "OverString";
169my $both = bless [], "OverBoth";
170my $neither = bless [], "OverNeither";
c1d4704a 171my $nstr = qq($neither);
d89f1457 172
d0c91b6a
BM
173open my $gv, "<", "TEST";
174bless $gv, "OverString";
175open my $io, "<", "TEST";
176$io = *{$io}{IO};
177bless $io, "OverString";
178
822146ea
RGS
179my $fcntl_not_available;
180eval { require Fcntl } or $fcntl_not_available = 1;
fa7f7498 181
d89f1457 182for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
d3ebc3eb 183 $over = [];
f6aa8023
BM
184 ok( my $rv = eval "-$op \$ft", "overloaded -$op succeeds" )
185 or diag( $@ );
186 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
d89f1457
BM
187 is( $over->[1], $op, "correct op for overloaded -$op" );
188 is( $rv, "-$op", "correct return value for overloaded -$op");
f6aa8023 189
fa7f7498
BM
190 my ($exp, $is) = (1, "is");
191 if (
822146ea 192 !$fcntl_not_available and (
fa7f7498
BM
193 $op eq "u" and not eval { Fcntl::S_ISUID() } or
194 $op eq "g" and not eval { Fcntl::S_ISGID() } or
195 $op eq "k" and not eval { Fcntl::S_ISVTX() }
822146ea 196 )
fa7f7498
BM
197 ) {
198 ($exp, $is) = (0, "not");
199 }
200
f6aa8023
BM
201 $over = 0;
202 $rv = eval "-$op \$str";
203 ok( !$@, "-$op succeeds with string overloading" )
204 or diag( $@ );
205 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
fa7f7498 206 is( $over, $exp, "string overload $is called for -$op" );
f6aa8023 207
fa7f7498 208 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
d0c91b6a
BM
209
210 $over = 0;
211 eval "-$op \$gv";
212 is( $over, $exp, "string overload $is called for -$op on GLOB" );
213
52f7f5ab
BM
214 # IO refs always get string overload called. This might be a bug.
215 $op eq "t" || $op eq "T" || $op eq "B"
216 and ($exp, $is) = (1, "is");
217
d0c91b6a
BM
218 $over = 0;
219 eval "-$op \$io";
220 is( $over, $exp, "string overload $is called for -$op on IO");
221
f6aa8023
BM
222 $rv = eval "-$op \$both";
223 is( $rv, "-$op", "correct -$op on string/-X overload" );
224
225 $rv = eval "-$op \$neither";
226 ok( !$@, "-$op succeeds with random overloading" )
227 or diag( $@ );
228 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
f6aa8023 229
4d57d24f
BM
230 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
231 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
232}
ca867162
FC
233
234# -l stack corruption: this bug occurred from 5.8 to 5.14
235{
236 push my @foo, "bar", -l baz;
237 is $foo[0], "bar", '-l bareword does not corrupt the stack';
238}
49498caf
FC
239
240# File test ops should not call get-magic on the topmost SV on the stack if
241# it belongs to another op.
242{
243 my $w;
244 sub oon::TIESCALAR{bless[],'oon'}
245 sub oon::FETCH{$w++}
246 tie my $t, 'oon';
247 push my @a, $t, -t;
248 is $w, 1, 'file test does not call FETCH on stack item not its own';
249}