This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Correct test output for t/op/eval.t (missing newline)
[perl5.git] / t / op / filetest.t
1 #!./perl
2
3 # There are few filetest operators that are portable enough to test.
4 # See pod/perlport.pod for details.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib';
9     require './test.pl';
10 }
11
12 use Config;
13 plan(tests => 28 + 27*14);
14
15 ok( -d 'op' );
16 ok( -f 'TEST' );
17 ok( !-f 'op' );
18 ok( !-d 'TEST' );
19 ok( -r 'TEST' );
20
21 # make sure TEST is r-x
22 eval { chmod 0555, 'TEST' or die "chmod 0555, 'TEST' failed: $!" };
23 chomp ($bad_chmod = $@);
24
25 $oldeuid = $>;          # root can read and write anything
26 eval '$> = 1';          # so switch uid (may not be implemented)
27
28 print "# oldeuid = $oldeuid, euid = $>\n";
29
30 SKIP: {
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) {
38         skip( $bad_chmod );
39     }
40     else {
41         ok( !-w 'TEST' );
42     }
43 }
44
45 # Scripts are not -x everywhere so cannot test that.
46
47 eval '$> = $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...)
51 ok( -r 'op' );
52
53 # this would fail for the euid 1
54 # (unless we have unpacked the source code as uid 1...)
55 SKIP: {
56     if ($Config{d_seteuid}) {
57         ok( -w 'op' );
58     } else {
59         skip('no seteuid');
60     }
61 }
62
63 ok( -x 'op' ); # Hohum.  Are directories -x everywhere?
64
65 is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
66
67 # Test stackability of filetest operators
68
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' ) );
73 ok( -f -e 'TEST' );
74 ok( -e -f 'TEST' );
75 ok( defined(-d -e 'TEST') );
76 ok( defined(-e -d 'TEST') );
77 ok( ! -f -d 'op' );
78 ok( -x -d -x 'op' );
79 ok( (-s -f 'TEST' > 1), "-s returns real size" );
80 ok( -f -s 'TEST' == 1 );
81
82 # now with an empty file
83 my $tempfile = tempfile();
84 open my $fh, ">", $tempfile;
85 close $fh;
86 ok( -f $tempfile );
87 is( -s $tempfile, 0 );
88 is( -f -s $tempfile, 0 );
89 is( -s -f $tempfile, 0 );
90 unlink $tempfile;
91
92 # test that _ is a bareword after filetest operators
93
94 -f 'TEST';
95 ok( -f _ );
96 sub _ { "this is not a file name" }
97 ok( -f _ );
98
99 my $over;
100 {
101     package OverFtest;
102
103     use overload 
104         -X => sub { 
105             $over = [overload::StrVal($_[0]), $_[1]];
106             "-$_[1]"; 
107         };
108 }
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
126     # Need fallback. Previous versions of perl required 'fallback' to do
127     # -X operations on an object with no "" overload.
128     use overload 
129         '+' => sub { 1 },
130         fallback => 1;
131 }
132
133 my $ft = bless [], "OverFtest";
134 my $ftstr = overload::StrVal($ft);
135 my $str = bless [], "OverString";
136 my $both = bless [], "OverBoth";
137 my $neither = bless [], "OverNeither";
138 my $nstr = overload::StrVal($neither);
139
140 open my $gv, "<", "TEST";
141 bless $gv, "OverString";
142 open my $io, "<", "TEST";
143 $io = *{$io}{IO};
144 bless $io, "OverString";
145
146 my $fcntl_not_available;
147 eval { require Fcntl } or $fcntl_not_available = 1;
148
149 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
150     $over = [];
151     ok( my $rv = eval "-$op \$ft",  "overloaded -$op succeeds" )
152         or diag( $@ );
153     is( $over->[0], $ftstr,         "correct object for overloaded -$op" );
154     is( $over->[1], $op,            "correct op for overloaded -$op" );
155     is( $rv,        "-$op",         "correct return value for overloaded -$op");
156
157     my ($exp, $is) = (1, "is");
158     if (
159         !$fcntl_not_available and (
160         $op eq "u" and not eval { Fcntl::S_ISUID() } or
161         $op eq "g" and not eval { Fcntl::S_ISGID() } or
162         $op eq "k" and not eval { Fcntl::S_ISVTX() }
163         )
164     ) {
165         ($exp, $is) = (0, "not");
166     }
167
168     $over = 0;
169     $rv = eval "-$op \$str";
170     ok( !$@,                        "-$op succeeds with string overloading" )
171         or diag( $@ );
172     is( $rv, eval "-$op 'TEST'",    "correct -$op on string overload" );
173     is( $over,      $exp,           "string overload $is called for -$op" );
174
175     ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
176
177     $over = 0;
178     eval "-$op \$gv";
179     is( $over,      $exp,   "string overload $is called for -$op on GLOB" );
180
181     # IO refs always get string overload called. This might be a bug.
182     $op eq "t" || $op eq "T" || $op eq "B"
183         and ($exp, $is) = (1, "is");
184
185     $over = 0;
186     eval "-$op \$io";
187     is( $over,      $exp,   "string overload $is called for -$op on IO");
188
189     $rv = eval "-$op \$both";
190     is( $rv,        "-$op",         "correct -$op on string/-X overload" );
191
192     $rv = eval "-$op \$neither";
193     ok( !$@,                        "-$op succeeds with random overloading" )
194         or diag( $@ );
195     is( $rv, eval "-$op \$nstr",    "correct -$op with random overloading" );
196
197     is( eval "-r -$op \$ft", "-r",      "stacked overloaded -$op" );
198     is( eval "-$op -r \$ft", "-$op",    "overloaded stacked -$op" );
199 }