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