This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/filehandle.t: Provide descriptions for all tests.
[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
bce3b594 12plan(tests => 49 + 27*14);
42e55ab1 13
bce3b594
NC
14is(-d 'op', 1);
15is(-f 'TEST', 1);
16isnt(-f 'op', 1);
17isnt(-d 'TEST', 1);
18is(-r 'TEST', 1);
42e55ab1 19
e687eccd
NC
20# Make a read only file. This happens to be empty, so we also use it later.
21my $ro_empty_file = tempfile();
d4188802
NC
22
23{
e687eccd 24 open my $fh, '>', $ro_empty_file or die "open $fh: $!";
d4188802
NC
25 close $fh or die "close $fh: $!";
26}
27
e687eccd 28chmod 0555, $ro_empty_file or die "chmod 0555, '$ro_empty_file' failed: $!";
846f25a3 29
fbb0b3b3 30SKIP: {
dcefe67b
NC
31 my $restore_root;
32 if ($> == 0) {
33 # root can read and write anything, so switch uid (may not be
34 # implemented)
35 eval '$> = 1';
36
37 skip("Can't drop root privs to test read-only files") if $> == 0;
38 note("Dropped root privs to test read-only files. \$> == $>");
39 ++$restore_root;
fbb0b3b3 40 }
42e55ab1 41
bce3b594 42 isnt(-w $ro_empty_file, 1);
42e55ab1 43
dcefe67b
NC
44 if ($restore_root) {
45 # If the previous assignment to $> worked, so should this:
46 $> = 0;
47 note("Restored root privs after testing read-only files. \$> == $>");
48 }
49}
fd1e013e 50
64f0b68d 51# these would fail for the euid 1
fd1e013e 52# (unless we have unpacked the source code as uid 1...)
bce3b594
NC
53is(-r 'op', 1);
54is(-w 'op', 1);
55is(-x 'op', 1); # Hohum. Are directories -x everywhere?
fbb0b3b3
RGS
56
57is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
58
59# Test stackability of filetest operators
42e55ab1 60
bce3b594
NC
61is(defined( -f -d 'TEST' ), 1);
62isnt(-f -d _, 1);
63isnt(defined( -e 'zoo' ), 1);
64isnt(defined( -e -d 'zoo' ), 1);
65isnt(defined( -f -e 'zoo' ), 1);
66is(-f -e 'TEST', 1);
67is(-e -f 'TEST', 1);
68is(defined(-d -e 'TEST'), 1);
69is(defined(-e -d 'TEST'), 1);
70isnt( -f -d 'op', 1);
71is(-x -d -x 'op', 1);
fea55e48
NC
72my ($size) = (stat 'TEST')[7];
73cmp_ok($size, '>', 1, 'TEST is longer than 1 byte');
74is( (-s -f 'TEST'), $size, "-s returns real size" );
bce3b594 75is(-f -s 'TEST', 1);
7294df96 76
e7d3eb55 77# now with an empty file
bce3b594 78is(-f $ro_empty_file, 1);
e687eccd
NC
79is( -s $ro_empty_file, 0 );
80is( -f -s $ro_empty_file, 0 );
81is( -s -f $ro_empty_file, 0 );
e7d3eb55 82
1f26655e
FC
83# stacked -l
84eval { -l -e "TEST" };
85like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
86 'stacked -l non-lstat error with warnings off';
87{
88 local $^W = 1;
89 eval { -l -e "TEST" };
90 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
91 'stacked -l non-lstat error with warnings on';
92}
93# Make sure -l is using the previous stat buffer, and not using the previ-
94# ous op’s return value as a file name.
8f67f7cf 95# t/TEST can be a symlink under -Dmksymlinks, so use our temporary file.
1f26655e
FC
96SKIP: {
97 use Perl::OSType 'os_type';
61602f40 98 if (os_type ne 'Unix') { skip "Not Unix", 2 }
1f26655e 99 chomp(my $ln = `which ln`);
61602f40 100 if ( ! -e $ln ) { skip "No ln" , 2 }
8f67f7cf
NC
101 lstat $ro_empty_file;
102 `ln -s $ro_empty_file 1`;
bce3b594 103 isnt(-l -e _, 1, 'stacked -l uses previous stat, not previous retval');
1f26655e 104 unlink 1;
433644ee
FC
105
106 # Since we already have our skip block set up, we might as well put this
107 # test here, too:
108 # -l always treats a non-bareword argument as a file name
8f67f7cf 109 system 'ln', '-s', $ro_empty_file, \*foo;
433644ee 110 local $^W = 1;
bce3b594 111 is(-l \*foo, 1, '-l \*foo is a file name');
433644ee 112 unlink \*foo;
1f26655e
FC
113}
114
7294df96
RGS
115# test that _ is a bareword after filetest operators
116
117-f 'TEST';
bce3b594 118is(-f _, 1);
7294df96 119sub _ { "this is not a file name" }
bce3b594 120is(-f _, 1);
d89f1457
BM
121
122my $over;
123{
124 package OverFtest;
125
4d57d24f 126 use overload
c1d4704a 127 fallback => 1,
4d57d24f 128 -X => sub {
c1d4704a 129 $over = [qq($_[0]), $_[1]];
4d57d24f
BM
130 "-$_[1]";
131 };
d89f1457 132}
f6aa8023
BM
133{
134 package OverString;
135
136 # No fallback. -X should fall back to string overload even without
137 # it.
138 use overload q/""/ => sub { $over = 1; "TEST" };
139}
140{
141 package OverBoth;
142
143 use overload
144 q/""/ => sub { "TEST" },
145 -X => sub { "-$_[1]" };
146}
147{
148 package OverNeither;
149
4d57d24f 150 # Need fallback. Previous versions of perl required 'fallback' to do
f6aa8023
BM
151 # -X operations on an object with no "" overload.
152 use overload
153 '+' => sub { 1 },
154 fallback => 1;
155}
156
157my $ft = bless [], "OverFtest";
c1d4704a 158my $ftstr = qq($ft);
f6aa8023
BM
159my $str = bless [], "OverString";
160my $both = bless [], "OverBoth";
161my $neither = bless [], "OverNeither";
c1d4704a 162my $nstr = qq($neither);
d89f1457 163
d0c91b6a
BM
164open my $gv, "<", "TEST";
165bless $gv, "OverString";
166open my $io, "<", "TEST";
167$io = *{$io}{IO};
168bless $io, "OverString";
169
822146ea
RGS
170my $fcntl_not_available;
171eval { require Fcntl } or $fcntl_not_available = 1;
fa7f7498 172
d89f1457 173for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
d3ebc3eb 174 $over = [];
bce3b594
NC
175 my $rv = eval "-$op \$ft";
176 isnt( $rv, undef, "overloaded -$op succeeds" )
f6aa8023
BM
177 or diag( $@ );
178 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
d89f1457
BM
179 is( $over->[1], $op, "correct op for overloaded -$op" );
180 is( $rv, "-$op", "correct return value for overloaded -$op");
f6aa8023 181
fa7f7498 182 my ($exp, $is) = (1, "is");
183 if (
822146ea 184 !$fcntl_not_available and (
fa7f7498 185 $op eq "u" and not eval { Fcntl::S_ISUID() } or
186 $op eq "g" and not eval { Fcntl::S_ISGID() } or
187 $op eq "k" and not eval { Fcntl::S_ISVTX() }
822146ea 188 )
fa7f7498 189 ) {
190 ($exp, $is) = (0, "not");
191 }
192
f6aa8023
BM
193 $over = 0;
194 $rv = eval "-$op \$str";
bce3b594 195 is($@, "", "-$op succeeds with string overloading");
f6aa8023 196 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
fa7f7498 197 is( $over, $exp, "string overload $is called for -$op" );
f6aa8023 198
fa7f7498 199 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
d0c91b6a
BM
200
201 $over = 0;
202 eval "-$op \$gv";
203 is( $over, $exp, "string overload $is called for -$op on GLOB" );
204
52f7f5ab
BM
205 # IO refs always get string overload called. This might be a bug.
206 $op eq "t" || $op eq "T" || $op eq "B"
207 and ($exp, $is) = (1, "is");
208
d0c91b6a
BM
209 $over = 0;
210 eval "-$op \$io";
211 is( $over, $exp, "string overload $is called for -$op on IO");
212
f6aa8023
BM
213 $rv = eval "-$op \$both";
214 is( $rv, "-$op", "correct -$op on string/-X overload" );
215
216 $rv = eval "-$op \$neither";
bce3b594 217 is($@, "", "-$op succeeds with random overloading");
f6aa8023 218 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
f6aa8023 219
4d57d24f
BM
220 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
221 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
222}
ca867162
FC
223
224# -l stack corruption: this bug occurred from 5.8 to 5.14
225{
226 push my @foo, "bar", -l baz;
227 is $foo[0], "bar", '-l bareword does not corrupt the stack';
228}
49498caf 229
31b139ba
FC
230# -l and fatal warnings
231stat "test.pl";
232eval { use warnings FATAL => io; -l cradd };
bce3b594
NC
233isnt(stat _, 1,
234 'fatal warnings do not prevent -l HANDLE from setting stat status');
31b139ba 235
49498caf
FC
236# File test ops should not call get-magic on the topmost SV on the stack if
237# it belongs to another op.
238{
239 my $w;
240 sub oon::TIESCALAR{bless[],'oon'}
241 sub oon::FETCH{$w++}
242 tie my $t, 'oon';
243 push my @a, $t, -t;
244 is $w, 1, 'file test does not call FETCH on stack item not its own';
245}
5731662b 246
97c226b8
FC
247# -T and -B
248
bd5f6c01
FC
249my $Perl = which_perl();
250
5731662b 251SKIP: {
2ad48547 252 skip "no -T on filehandles", 8 unless eval { -T STDERR; 1 };
97c226b8
FC
253
254 # Test that -T HANDLE sets the last stat type
5731662b
FC
255 -l "perl.c"; # last stat type is now lstat
256 -T STDERR; # should set it to stat, since -T does a stat
257 eval { -l _ }; # should die, because the last stat type is not lstat
258 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
259 '-T HANDLE sets the stat type';
97c226b8
FC
260
261 # statgv should be cleared when freed
262 fresh_perl_is
263 'open my $fh, "test.pl"; -r $fh; undef $fh; open my $fh2, '
bd5f6c01 264 . "q\0$Perl\0; print -B _",
97c226b8
FC
265 '',
266 { switches => ['-l'] },
267 'PL_statgv should not point to freed-and-reused SV';
268
269 # or coerced into a non-glob
270 fresh_perl_is
271 'open Fh, "test.pl"; -r($h{i} = *Fh); $h{i} = 3; undef %h;'
272 . 'open my $fh2, ' . "q\0" . which_perl() . "\0; print -B _",
273 '',
274 { switches => ['-l'] },
275 'PL_statgv should not point to coerced-freed-and-reused GV';
bd5f6c01
FC
276
277 # -T _ should work after stat $ioref
278 open my $fh, 'test.pl';
279 stat $Perl; # a binary file
280 stat *$fh{IO};
bce3b594 281 is(-T _, 1, '-T _ works after stat $ioref');
bd5f6c01
FC
282
283 # and after -r $ioref
284 -r *$fh{IO};
bce3b594 285 is(-T _, 1, '-T _ works after -r $ioref');
eb4c377a
FC
286
287 # -T _ on closed filehandle should still reset stat info
288 stat $fh;
289 close $fh;
290 -T _;
bce3b594 291 isnt(stat _, 1, '-T _ on closed filehandle resets stat info');
21a64c3e
FC
292
293 lstat "test.pl";
294 -T $fh; # closed
295 eval { lstat _ };
296 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
297 '-T on closed handle resets last stat type';
2ad48547
FC
298
299 # Fatal warnings should not affect the setting of errno.
300 $! = 7;
301 -T cradd;
302 my $errno = $!;
303 $! = 7;
304 eval { use warnings FATAL => unopened; -T cradd };
305 my $errno2 = $!;
306 is $errno2, $errno,
307 'fatal warnings do not affect errno after -T BADHADNLE';
5731662b 308}
de61bf2a
FC
309
310is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "",
311 'no uninit warnings from -T with no preceding stat';
7b7309af 312
ad2d99e3 313SKIP: {
bee528ec 314 my $rand_file_name = 'filetest-' . rand =~ y/.//dr;
ad2d99e3
FC
315 if (-e $rand_file_name) { skip "File $rand_file_name exists", 1 }
316 stat 'test.pl';
317 -T $rand_file_name;
bce3b594 318 isnt(stat _, 1, '-T "nonexistent" resets stat success status');
ad2d99e3
FC
319}
320
7b7309af
FC
321# Unsuccessful filetests on filehandles should leave stat buffers in the
322# same state whether fatal warnings are on or off.
323{
324 stat "test.pl";
325 # This GV has no IO
326 -r *phlon;
327 my $failed_stat1 = stat _;
328
329 stat "test.pl";
330 eval { use warnings FATAL => unopened; -r *phlon };
331 my $failed_stat2 = stat _;
332
333 is $failed_stat2, $failed_stat1,
334 'failed -r($gv_without_io) with and w/out fatal warnings';
335
336 stat "test.pl";
337 -r cength; # at compile time autovivifies IO, but with no fp
338 $failed_stat1 = stat _;
339
340 stat "test.pl";
341 eval { use warnings FATAL => unopened; -r cength };
342 $failed_stat2 = stat _;
343
344 is $failed_stat2, $failed_stat1,
345 'failed -r($gv_with_io_but_no_fp) with and w/out fatal warnings';
346}