This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lib/File/stat.t should also test the -X operators on directories.
[perl5.git] / lib / File / stat.t
CommitLineData
15b7a6a8 1#!./perl
f7a45afb
JH
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
d219c4fb
NC
8use strict;
9use warnings;
2f173a71 10use Test::More;
61a7d4fd 11use Config qw( %Config );
81a201f4 12use File::Temp qw( tempfile tempdir );
c69df70f 13
c239c67c 14require File::stat;
f7a45afb 15
17c017a5 16my (undef, $file) = tempfile();
2f173a71 17
cf23dc19
NC
18{
19 my @stat = CORE::stat $file;
20 my $stat = File::stat::stat($file);
21 isa_ok($stat, 'File::stat', 'should build a stat object');
22
23 my $i = 0;
24 foreach ([dev => 'device number'],
25 [ino => 'inode number'],
26 [mode => 'file mode'],
27 [nlink => 'number of links'],
28 [uid => 'owner uid'],
29 [gid => 'group id'],
30 [rdev => 'device identifier'],
31 [size => 'file size'],
32 [atime => 'last access time'],
33 [mtime => 'last modify time'],
34 [ctime => 'change time'],
35 [blksize => 'IO block size'],
36 [blocks => 'number of blocks']) {
37 my ($meth, $desc) = @$_;
38 # On OS/2 (fake) ino is not constant, it is incremented each time
39 SKIP: {
40 skip('inode number is not constant on OS/2', 1)
41 if $i == 1 && $^O eq 'os2';
42 is($stat->$meth, $stat[$i], "$desc in position $i");
db8b1169 43 }
cf23dc19
NC
44 ++$i;
45 }
46}
db8b1169 47
cf23dc19
NC
48sub test_X_ops {
49 my ($file, $desc_tail) = @_;
50 my @stat = CORE::stat $file;
51 my $stat = File::stat::stat($file);
52 isa_ok($stat, 'File::stat', 'should build a stat object');
53
54 for my $op (split //, "rwxoRWXOezsfdlpSbcugkMCA") {
55 for my $access ('', 'use filetest "access";') {
56 my ($warnings, $awarn, $vwarn, $rv);
57 my $desc = $access
58 ? "for -$op under use filetest 'access' $desc_tail"
59 : "for -$op $desc_tail";
60 {
61 local $SIG{__WARN__} = sub {
62 my $w = shift;
63 if ($w =~ /^File::stat ignores VMS ACLs/) {
64 ++$vwarn;
65 } elsif ($w =~ /^File::stat ignores use filetest 'access'/) {
66 ++$awarn;
67 } else {
68 $warnings .= $w;
69 }
70 };
71 $rv = eval "$access; -$op \$stat";
72 }
73 is($@, '', "Overload succeeds $desc");
74
75 if ($^O eq "VMS" && $op =~ /[rwxRWX]/) {
76 is($vwarn, 1, "warning about VMS ACLs $desc");
77 } else {
78 is($rv, eval "-$op \$file", "correct overload $desc")
79 unless $access;
80 is($vwarn, undef, "no warnings about VMS ACLs $desc");
81 }
82
83 # 111640 - File::stat bogus index check in overload
84 if ($access && $op =~ /[rwxRXW]/) {
85 # these should all warn with filetest access
86 is($awarn, 1,
87 "produced the right warning $desc");
88 } else {
89 # -d and others shouldn't warn
90 is($awarn, undef, "should be no warning $desc")
91 }
92
93 is($warnings, undef, "no other warnings seen $desc");
db8b1169 94 }
cd22a09c
BM
95 }
96}
97
81a201f4
NC
98foreach ([file => $file],
99 [dir => tempdir(CLEANUP => 1)]) {
100 my ($what, $pathname) = @$_;
101 test_X_ops($pathname, "for $what $pathname");
cf23dc19 102
b050b2f8
NC
103 my $mode = 01000;
104 while ($mode) {
105 $mode >>= 1;
106 my $mode_oct = sprintf "0%03o", $mode;
81a201f4
NC
107 chmod $mode, $pathname or die "Can't chmod $mode_oct $pathname: $!";
108 test_X_ops($pathname, "for $what with mode=$mode_oct");
b050b2f8 109 }
81a201f4
NC
110 chmod 0600, $pathname
111 or die "Can't restore permissions on $pathname to 0600";
b050b2f8
NC
112}
113
3c1f5943 114SKIP: {
cf23dc19 115 -e $^X && -x $^X or skip "$^X is not present and executable", 4;
3c1f5943
TR
116 $^O eq "VMS" and skip "File::stat ignores VMS ACLs", 4;
117
cf23dc19 118 test_X_ops($^X, "for $^X");
3c1f5943
TR
119}
120
121
cf23dc19
NC
122my $stat = File::stat::stat($file);
123isa_ok($stat, 'File::stat', 'should build a stat object');
124
cd22a09c
BM
125for (split //, "tTB") {
126 eval "-$_ \$stat";
127 like( $@, qr/\Q-$_ is not implemented/, "-$_ overload fails" );
128}
129
83716b1e 130SKIP: {
131 local *STAT;
61a7d4fd 132 skip("Could not open file: $!", 2) unless open(STAT, $file);
cf23dc19
NC
133 isa_ok(File::stat::stat('STAT'), 'File::stat',
134 '... should be able to find filehandle');
83716b1e 135
136 package foo;
137 local *STAT = *main::STAT;
cf23dc19
NC
138 my $stat2 = File::stat::stat('STAT');
139 main::isa_ok($stat2, 'File::stat',
140 '... and filehandle in another package');
83716b1e 141 close STAT;
142
659293e7
PG
143# VOS open() updates atime; ignore this error (posix-975).
144 my $stat3 = $stat2;
145 if ($^O eq 'vos') {
146 $$stat3[8] = $$stat[8];
147 }
148
378bd967 149 main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
afdf87ad 150 main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
378bd967 151
d51a9dd5
JH
152 main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
153
cf23dc19 154 main::is_deeply($stat, $stat3, '... and must match normal stat');
83716b1e 155}
156
e0b1616f
TC
157SKIP:
158{ # RT #111638
159 skip "We can't check for FIFOs", 2 unless defined &Fcntl::S_ISFIFO;
160 skip "No pipes", 2 unless defined $Config{d_pipe};
161 pipe my ($rh, $wh)
162 or skip "Couldn't create a pipe: $!", 2;
163 skip "Built-in -p doesn't detect a pipe", 2 unless -p $rh;
164
165 my $pstat = File::stat::stat($rh);
166 ok(!-p($stat), "-p should be false on a file");
e0b1616f
TC
167 ok(-p($pstat), "check -p detects a pipe");
168}
169
f7a45afb 170# Testing pretty much anything else is unportable.
c239c67c
NC
171
172done_testing;
173
174# Local variables:
175# cperl-indent-level: 4
176# indent-tabs-mode: nil
177# End:
178#
179# ex: set ts=8 sts=4 sw=4 et: