This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Removed a release-engineering step. (It's been replaced with automated tests)
[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
2f173a71 8use Test::More;
9
f7a45afb
JH
10BEGIN {
11 our $hasst;
12 eval { my @n = stat "TEST" };
13 $hasst = 1 unless $@ && $@ =~ /unimplemented/;
2f173a71 14 unless ($hasst) { plan skip_all => "no stat"; exit 0 }
f7a45afb
JH
15 use Config;
16 $hasst = 0 unless $Config{'i_sysstat'} eq 'define';
2f173a71 17 unless ($hasst) { plan skip_all => "no sys/stat.h"; exit 0 }
f7a45afb 18 our @stat = stat "TEST"; # This is the function stat.
83716b1e 19 unless (@stat) { plan skip_all => "1..0 # Skip: no file TEST"; exit 0 }
f7a45afb
JH
20}
21
cd22a09c 22plan tests => 19 + 24*2 + 3;
f7a45afb 23
2f173a71 24use_ok( 'File::stat' );
f7a45afb 25
2f173a71 26my $stat = File::stat::stat( "TEST" ); # This is the OO stat.
27ok( ref($stat), 'should build a stat object' );
f7a45afb 28
2f173a71 29is( $stat->dev, $stat[0], "device number in position 0" );
f7a45afb 30
4f0c37ba 31# On OS/2 (fake) ino is not constant, it is incremented each time
2f173a71 32SKIP: {
dfcfdb64 33 skip('inode number is not constant on OS/2', 1) if $^O eq 'os2';
2f173a71 34 is( $stat->ino, $stat[1], "inode number in position 1" );
35}
36
37is( $stat->mode, $stat[2], "file mode in position 2" );
f7a45afb 38
2f173a71 39is( $stat->nlink, $stat[3], "number of links in position 3" );
f7a45afb 40
2f173a71 41is( $stat->uid, $stat[4], "owner uid in position 4" );
f7a45afb 42
2f173a71 43is( $stat->gid, $stat[5], "group id in position 5" );
f7a45afb 44
2f173a71 45is( $stat->rdev, $stat[6], "device identifier in position 6" );
f7a45afb 46
2f173a71 47is( $stat->size, $stat[7], "file size in position 7" );
f7a45afb 48
2f173a71 49is( $stat->atime, $stat[8], "last access time in position 8" );
f7a45afb 50
2f173a71 51is( $stat->mtime, $stat[9], "last modify time in position 9" );
f7a45afb 52
2f173a71 53is( $stat->ctime, $stat[10], "change time in position 10" );
f7a45afb 54
2f173a71 55is( $stat->blksize, $stat[11], "IO block size in position 11" );
f7a45afb 56
2f173a71 57is( $stat->blocks, $stat[12], "number of blocks in position 12" );
f7a45afb 58
cd22a09c
BM
59for (split //, "rwxoRWXOezsfdlpSbcugkMCA") {
60 SKIP: {
e9fd6cc7
CB
61 $^O eq "VMS" and index("rwxRWX", $_) >= 0
62 and skip "File::stat ignores VMS ACLs", 2;
cd22a09c
BM
63
64 my $rv = eval "-$_ \$stat";
65 ok( !$@, "-$_ overload succeeds" )
66 or diag( $@ );
67 is( $rv, eval "-$_ 'TEST'", "correct -$_ overload" );
68 }
69}
70
71for (split //, "tTB") {
72 eval "-$_ \$stat";
73 like( $@, qr/\Q-$_ is not implemented/, "-$_ overload fails" );
74}
75
83716b1e 76SKIP: {
77 local *STAT;
378bd967 78 skip("Could not open file: $!", 2) unless open(STAT, 'TEST');
83716b1e 79 ok( File::stat::stat('STAT'), '... should be able to find filehandle' );
80
81 package foo;
82 local *STAT = *main::STAT;
83 main::ok( my $stat2 = File::stat::stat('STAT'),
84 '... and filehandle in another package' );
85 close STAT;
86
659293e7
PG
87# VOS open() updates atime; ignore this error (posix-975).
88 my $stat3 = $stat2;
89 if ($^O eq 'vos') {
90 $$stat3[8] = $$stat[8];
91 }
92
378bd967 93 main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
afdf87ad 94 main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
378bd967 95
d51a9dd5
JH
96 main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
97
659293e7 98 main::is( "@$stat", "@$stat3", '... and must match normal stat' );
83716b1e 99}
100
cd22a09c 101
2f173a71 102local $!;
103$stat = stat '/notafile';
cd22a09c 104isnt( $!, '', 'should populate $!, given invalid file' );
f7a45afb
JH
105
106# Testing pretty much anything else is unportable.