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