sync blead with Update Archive::Extract 0.34
[p5sagit/p5-mst-13.2.git] / lib / File / stat.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use Test::More;
9
10 BEGIN {
11     our $hasst;
12     eval { my @n = stat "TEST" };
13     $hasst = 1 unless $@ && $@ =~ /unimplemented/;
14     unless ($hasst) { plan skip_all => "no stat"; exit 0 }
15     use Config;
16     $hasst = 0 unless $Config{'i_sysstat'} eq 'define';
17     unless ($hasst) { plan skip_all => "no sys/stat.h"; exit 0 }
18     our @stat = stat "TEST"; # This is the function stat.
19     unless (@stat) { plan skip_all => "1..0 # Skip: no file TEST"; exit 0 }
20 }
21
22 plan tests => 19 + 24*2 + 3;
23
24 use_ok( 'File::stat' );
25
26 my $stat = File::stat::stat( "TEST" ); # This is the OO stat.
27 ok( ref($stat), 'should build a stat object' );
28
29 is( $stat->dev, $stat[0], "device number in position 0" );
30
31 # On OS/2 (fake) ino is not constant, it is incremented each time
32 SKIP: {
33         skip('inode number is not constant on OS/2', 1) if $^O eq 'os2';
34         is( $stat->ino, $stat[1], "inode number in position 1" );
35 }
36
37 is( $stat->mode, $stat[2], "file mode in position 2" );
38
39 is( $stat->nlink, $stat[3], "number of links in position 3" );
40
41 is( $stat->uid, $stat[4], "owner uid in position 4" );
42
43 is( $stat->gid, $stat[5], "group id in position 5" );
44
45 is( $stat->rdev, $stat[6], "device identifier in position 6" );
46
47 is( $stat->size, $stat[7], "file size in position 7" );
48
49 is( $stat->atime, $stat[8], "last access time in position 8" );
50
51 is( $stat->mtime, $stat[9], "last modify time in position 9" );
52
53 is( $stat->ctime, $stat[10], "change time in position 10" );
54
55 is( $stat->blksize, $stat[11], "IO block size in position 11" );
56
57 is( $stat->blocks, $stat[12], "number of blocks in position 12" );
58
59 for (split //, "rwxoRWXOezsfdlpSbcugkMCA") {
60     SKIP: {
61         $^O eq "VMS" and index("rwxRWX", $_) >= 0
62             and skip "File::stat ignores VMS ACLs", 2;
63
64         my $rv = eval "-$_ \$stat";
65         ok( !$@,                            "-$_ overload succeeds" )
66             or diag( $@ );
67         is( $rv, eval "-$_ 'TEST'",         "correct -$_ overload" );
68     }
69 }
70
71 for (split //, "tTB") {
72     eval "-$_ \$stat";
73     like( $@, qr/\Q-$_ is not implemented/, "-$_ overload fails" );
74 }
75
76 SKIP: {
77         local *STAT;
78         skip("Could not open file: $!", 2) unless open(STAT, 'TEST');
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
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
93         main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
94         main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
95
96         main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
97
98         main::is( "@$stat", "@$stat3", '... and must match normal stat' );
99 }
100
101
102 local $!;
103 $stat = stat '/notafile';
104 isnt( $!, '', 'should populate $!, given invalid file' );
105
106 # Testing pretty much anything else is unportable.