11 use Config qw( %Config );
12 use File::Temp qw( tempfile tempdir );
16 my (undef, $file) = tempfile();
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 is_deeply($stat, \@stat, '... and matches the builtin');
25 foreach ([dev => 'device number'],
26 [ino => 'inode number'],
27 [mode => 'file mode'],
28 [nlink => 'number of links'],
31 [rdev => 'device identifier'],
32 [size => 'file size'],
33 [atime => 'last access time'],
34 [mtime => 'last modify time'],
35 [ctime => 'change time'],
36 [blksize => 'IO block size'],
37 [blocks => 'number of blocks']) {
38 my ($meth, $desc) = @$_;
39 # On OS/2 (fake) ino is not constant, it is incremented each time
41 skip('inode number is not constant on OS/2', 1)
42 if $i == 1 && $^O eq 'os2';
43 is($stat->$meth, $stat[$i], "$desc in position $i");
48 my $stat2 = stat $file;
49 isa_ok($stat2, 'File::stat',
50 'File::stat exports stat, overriding the builtin');
51 is_deeply($stat2, $stat, '... and matches the direct call');
55 my ($file, $desc_tail, $skip) = @_;
56 my @stat = CORE::stat $file;
57 my $stat = File::stat::stat($file);
58 isa_ok($stat, 'File::stat', 'should build a stat object');
60 for my $op (split //, "rwxoRWXOezsfdlpSbcugkMCA") {
61 if ($skip && $op =~ $skip) {
62 note("Not testing -A $desc_tail");
65 for my $access ('', 'use filetest "access";') {
66 my ($warnings, $awarn, $vwarn, $rv);
68 ? "for -$op under use filetest 'access' $desc_tail"
69 : "for -$op $desc_tail";
71 local $SIG{__WARN__} = sub {
73 if ($w =~ /^File::stat ignores VMS ACLs/) {
75 } elsif ($w =~ /^File::stat ignores use filetest 'access'/) {
81 $rv = eval "$access; -$op \$stat";
83 is($@, '', "Overload succeeds $desc");
85 if ($^O eq "VMS" && $op =~ /[rwxRWX]/) {
86 is($vwarn, 1, "warning about VMS ACLs $desc");
88 is($rv, eval "-$op \$file", "correct overload $desc")
90 is($vwarn, undef, "no warnings about VMS ACLs $desc");
93 # 111640 - File::stat bogus index check in overload
94 if ($access && $op =~ /[rwxRXW]/) {
95 # these should all warn with filetest access
97 "produced the right warning $desc");
99 # -d and others shouldn't warn
100 is($awarn, undef, "should be no warning $desc")
103 is($warnings, undef, "no other warnings seen $desc");
108 foreach ([file => $file],
109 [dir => tempdir(CLEANUP => 1)]) {
110 my ($what, $pathname) = @$_;
111 test_X_ops($pathname, "for $what $pathname");
116 my $mode_oct = sprintf "0%03o", $mode;
117 chmod $mode, $pathname or die "Can't chmod $mode_oct $pathname: $!";
118 test_X_ops($pathname, "for $what with mode=$mode_oct");
120 chmod 0600, $pathname
121 or die "Can't restore permissions on $pathname to 0600";
125 -e $^X && -x $^X or skip "$^X is not present and executable", 4;
126 $^O eq "VMS" and skip "File::stat ignores VMS ACLs", 4;
128 # Other tests running in parallel mean that $^X is read, updating its atime
129 test_X_ops($^X, "for $^X", qr/A/);
133 my $stat = File::stat::stat($file);
134 isa_ok($stat, 'File::stat', 'should build a stat object');
136 for (split //, "tTB") {
138 like( $@, qr/\Q-$_ is not implemented/, "-$_ overload fails" );
143 skip("Could not open file: $!", 2) unless open(STAT, $file);
144 isa_ok(File::stat::stat('STAT'), 'File::stat',
145 '... should be able to find filehandle');
148 local *STAT = *main::STAT;
149 my $stat2 = File::stat::stat('STAT');
150 main::isa_ok($stat2, 'File::stat',
151 '... and filehandle in another package');
154 # VOS open() updates atime; ignore this error (posix-975).
157 $$stat3[8] = $$stat[8];
160 main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
161 main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
163 main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
165 main::is_deeply($stat, $stat3, '... and must match normal stat');
170 skip "We can't check for FIFOs", 2 unless defined &Fcntl::S_ISFIFO;
171 skip "No pipes", 2 unless defined $Config{d_pipe};
173 or skip "Couldn't create a pipe: $!", 2;
174 skip "Built-in -p doesn't detect a pipe", 2 unless -p $rh;
176 my $pstat = File::stat::stat($rh);
177 ok(!-p($stat), "-p should be false on a file");
178 ok(-p($pstat), "check -p detects a pipe");
181 # Testing pretty much anything else is unportable.
186 # cperl-indent-level: 4
187 # indent-tabs-mode: nil
190 # ex: set ts=8 sts=4 sw=4 et: