This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
-l was missing an SPAGAIN.
[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;
d0c91b6a 13plan(tests => 28 + 27*14);
42e55ab1 14
fbb0b3b3
RGS
15ok( -d 'op' );
16ok( -f 'TEST' );
17ok( !-f 'op' );
18ok( !-d 'TEST' );
19ok( -r 'TEST' );
42e55ab1 20
22c35a8c 21# make sure TEST is r-x
ff2be7ed
NC
22eval { chmod 0555, 'TEST' or die "chmod 0555, 'TEST' failed: $!" };
23chomp ($bad_chmod = $@);
846f25a3
GW
24
25$oldeuid = $>; # root can read and write anything
26eval '$> = 1'; # so switch uid (may not be implemented)
27
28print "# oldeuid = $oldeuid, euid = $>\n";
29
fbb0b3b3
RGS
30SKIP: {
31 if (!$Config{d_seteuid}) {
32 skip('no seteuid');
33 }
34 elsif ($Config{config_args} =~/Dmksymlinks/) {
35 skip('we cannot chmod symlinks');
36 }
37 elsif ($bad_chmod) {
ff2be7ed 38 skip( $bad_chmod );
fbb0b3b3
RGS
39 }
40 else {
41 ok( !-w 'TEST' );
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 );
90unlink $tempfile;
e7d3eb55 91
7294df96
RGS
92# test that _ is a bareword after filetest operators
93
94-f 'TEST';
95ok( -f _ );
96sub _ { "this is not a file name" }
97ok( -f _ );
d89f1457
BM
98
99my $over;
100{
101 package OverFtest;
102
4d57d24f
BM
103 use overload
104 -X => sub {
105 $over = [overload::StrVal($_[0]), $_[1]];
106 "-$_[1]";
107 };
d89f1457 108}
f6aa8023
BM
109{
110 package OverString;
111
112 # No fallback. -X should fall back to string overload even without
113 # it.
114 use overload q/""/ => sub { $over = 1; "TEST" };
115}
116{
117 package OverBoth;
118
119 use overload
120 q/""/ => sub { "TEST" },
121 -X => sub { "-$_[1]" };
122}
123{
124 package OverNeither;
125
4d57d24f 126 # Need fallback. Previous versions of perl required 'fallback' to do
f6aa8023
BM
127 # -X operations on an object with no "" overload.
128 use overload
129 '+' => sub { 1 },
130 fallback => 1;
131}
132
133my $ft = bless [], "OverFtest";
134my $ftstr = overload::StrVal($ft);
135my $str = bless [], "OverString";
136my $both = bless [], "OverBoth";
137my $neither = bless [], "OverNeither";
138my $nstr = overload::StrVal($neither);
d89f1457 139
d0c91b6a
BM
140open my $gv, "<", "TEST";
141bless $gv, "OverString";
142open my $io, "<", "TEST";
143$io = *{$io}{IO};
144bless $io, "OverString";
145
d89f1457 146for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
d3ebc3eb 147 $over = [];
f6aa8023
BM
148 ok( my $rv = eval "-$op \$ft", "overloaded -$op succeeds" )
149 or diag( $@ );
150 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
d89f1457
BM
151 is( $over->[1], $op, "correct op for overloaded -$op" );
152 is( $rv, "-$op", "correct return value for overloaded -$op");
f6aa8023
BM
153
154 $over = 0;
155 $rv = eval "-$op \$str";
156 ok( !$@, "-$op succeeds with string overloading" )
157 or diag( $@ );
158 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
159 is( $over, 1, "string overload called for -$op" );
160
d0c91b6a
BM
161 my ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
162
163 $over = 0;
164 eval "-$op \$gv";
165 is( $over, $exp, "string overload $is called for -$op on GLOB" );
166
52f7f5ab
BM
167 # IO refs always get string overload called. This might be a bug.
168 $op eq "t" || $op eq "T" || $op eq "B"
169 and ($exp, $is) = (1, "is");
170
d0c91b6a
BM
171 $over = 0;
172 eval "-$op \$io";
173 is( $over, $exp, "string overload $is called for -$op on IO");
174
f6aa8023
BM
175 $rv = eval "-$op \$both";
176 is( $rv, "-$op", "correct -$op on string/-X overload" );
177
178 $rv = eval "-$op \$neither";
179 ok( !$@, "-$op succeeds with random overloading" )
180 or diag( $@ );
181 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
f6aa8023 182
4d57d24f
BM
183 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
184 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
185}