3 # There are few filetest operators that are portable enough to test.
4 # See pod/perlport.pod for details.
13 plan(tests => 28 + 27*14);
21 # Make a read only file
22 my $ro_file = tempfile();
25 open my $fh, '>', $ro_file or die "open $fh: $!";
26 close $fh or die "close $fh: $!";
29 chmod 0555, $ro_file or die "chmod 0555, '$ro_file' failed: $!";
31 $oldeuid = $>; # root can read and write anything
32 eval '$> = 1'; # so switch uid (may not be implemented)
34 print "# oldeuid = $oldeuid, euid = $>\n";
37 if (!$Config{d_seteuid}) {
45 # Scripts are not -x everywhere so cannot test that.
47 eval '$> = $oldeuid'; # switch uid back (may not be implemented)
49 # this would fail for the euid 1
50 # (unless we have unpacked the source code as uid 1...)
53 # this would fail for the euid 1
54 # (unless we have unpacked the source code as uid 1...)
56 if ($Config{d_seteuid}) {
63 ok( -x 'op' ); # Hohum. Are directories -x everywhere?
65 is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
67 # Test stackability of filetest operators
69 ok( defined( -f -d 'TEST' ) && ! -f -d _ );
70 ok( !defined( -e 'zoo' ) );
71 ok( !defined( -e -d 'zoo' ) );
72 ok( !defined( -f -e 'zoo' ) );
75 ok( defined(-d -e 'TEST') );
76 ok( defined(-e -d 'TEST') );
79 ok( (-s -f 'TEST' > 1), "-s returns real size" );
80 ok( -f -s 'TEST' == 1 );
82 # now with an empty file
83 my $tempfile = tempfile();
84 open my $fh, ">", $tempfile;
87 is( -s $tempfile, 0 );
88 is( -f -s $tempfile, 0 );
89 is( -s -f $tempfile, 0 );
92 # test that _ is a bareword after filetest operators
96 sub _ { "this is not a file name" }
106 $over = [qq($_[0]), $_[1]];
113 # No fallback. -X should fall back to string overload even without
115 use overload q/""/ => sub { $over = 1; "TEST" };
121 q/""/ => sub { "TEST" },
122 -X => sub { "-$_[1]" };
127 # Need fallback. Previous versions of perl required 'fallback' to do
128 # -X operations on an object with no "" overload.
134 my $ft = bless [], "OverFtest";
136 my $str = bless [], "OverString";
137 my $both = bless [], "OverBoth";
138 my $neither = bless [], "OverNeither";
139 my $nstr = qq($neither);
141 open my $gv, "<", "TEST";
142 bless $gv, "OverString";
143 open my $io, "<", "TEST";
145 bless $io, "OverString";
147 my $fcntl_not_available;
148 eval { require Fcntl } or $fcntl_not_available = 1;
150 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
152 ok( my $rv = eval "-$op \$ft", "overloaded -$op succeeds" )
154 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
155 is( $over->[1], $op, "correct op for overloaded -$op" );
156 is( $rv, "-$op", "correct return value for overloaded -$op");
158 my ($exp, $is) = (1, "is");
160 !$fcntl_not_available and (
161 $op eq "u" and not eval { Fcntl::S_ISUID() } or
162 $op eq "g" and not eval { Fcntl::S_ISGID() } or
163 $op eq "k" and not eval { Fcntl::S_ISVTX() }
166 ($exp, $is) = (0, "not");
170 $rv = eval "-$op \$str";
171 ok( !$@, "-$op succeeds with string overloading" )
173 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
174 is( $over, $exp, "string overload $is called for -$op" );
176 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
180 is( $over, $exp, "string overload $is called for -$op on GLOB" );
182 # IO refs always get string overload called. This might be a bug.
183 $op eq "t" || $op eq "T" || $op eq "B"
184 and ($exp, $is) = (1, "is");
188 is( $over, $exp, "string overload $is called for -$op on IO");
190 $rv = eval "-$op \$both";
191 is( $rv, "-$op", "correct -$op on string/-X overload" );
193 $rv = eval "-$op \$neither";
194 ok( !$@, "-$op succeeds with random overloading" )
196 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
198 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
199 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );