This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
docs patch: 'unicode_strings' doesn't change utf8ness
[perl5.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 use Config qw( %Config );
10
11 BEGIN {
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 ./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.
21     our $file = '../lib/File/stat.t';
22     if ( $Dmksymlinks ) {
23         $file = readlink $file;
24         die "Can't readlink(../lib/File/stat.t): $!" if ! defined $file;
25     }
26
27     our $hasst;
28     eval { my @n = stat $file };
29     $hasst = 1 unless $@ && $@ =~ /unimplemented/;
30     unless ($hasst) { plan skip_all => "no stat"; exit 0 }
31     use Config;
32     $hasst = 0 unless $Config{'i_sysstat'} eq 'define';
33     unless ($hasst) { plan skip_all => "no sys/stat.h"; exit 0 }
34 }
35
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.
42 our @stat = stat $file; # This is the function stat.
43 unless (@stat) { plan skip_all => "1..0 # Skip: no file $file"; exit 0 }
44
45 plan tests => 19 + 24*2 + 4 + 3 + 7 + 2;
46
47 use_ok( 'File::stat' );
48
49 my $stat = File::stat::stat( $file ); # This is the OO stat.
50 ok( ref($stat), 'should build a stat object' );
51
52 is( $stat->dev, $stat[0], "device number in position 0" );
53
54 # On OS/2 (fake) ino is not constant, it is incremented each time
55 SKIP: {
56         skip('inode number is not constant on OS/2', 1) if $^O eq 'os2';
57         is( $stat->ino, $stat[1], "inode number in position 1" );
58 }
59
60 is( $stat->mode, $stat[2], "file mode in position 2" );
61
62 is( $stat->nlink, $stat[3], "number of links in position 3" );
63
64 is( $stat->uid, $stat[4], "owner uid in position 4" );
65
66 is( $stat->gid, $stat[5], "group id in position 5" );
67
68 is( $stat->rdev, $stat[6], "device identifier in position 6" );
69
70 is( $stat->size, $stat[7], "file size in position 7" );
71
72 is( $stat->atime, $stat[8], "last access time in position 8" );
73
74 is( $stat->mtime, $stat[9], "last modify time in position 9" );
75
76 is( $stat->ctime, $stat[10], "change time in position 10" );
77
78 is( $stat->blksize, $stat[11], "IO block size in position 11" );
79
80 is( $stat->blocks, $stat[12], "number of blocks in position 12" );
81
82 for (split //, "rwxoRWXOezsfdlpSbcugkMCA") {
83     SKIP: {
84         $^O eq "VMS" and index("rwxRWX", $_) >= 0
85             and skip "File::stat ignores VMS ACLs", 2;
86
87         my $rv = eval "-$_ \$stat";
88         ok( !$@,                            "-$_ overload succeeds" )
89             or diag( $@ );
90         is( $rv, eval "-$_ \$file",         "correct -$_ overload" );
91     }
92 }
93
94 SKIP: {
95     my $file = '../perl';
96     -e $file && -x $file or skip "$file is not present and executable", 4;
97     $^O eq "VMS" and skip "File::stat ignores VMS ACLs", 4;
98
99     my $stat = File::stat::stat( $file ); # This is the OO stat.
100     foreach (qw/x X/) {
101     my $rv = eval "-$_ \$stat";
102     ok( !$@,                            "-$_ overload succeeds" )
103       or diag( $@ );
104     is( $rv, eval "-$_ \$file",         "correct -$_ overload" );
105   }
106 }
107
108
109 for (split //, "tTB") {
110     eval "-$_ \$stat";
111     like( $@, qr/\Q-$_ is not implemented/, "-$_ overload fails" );
112 }
113
114 SKIP: {
115         local *STAT;
116         skip("Could not open file: $!", 2) unless open(STAT, $file);
117         ok( File::stat::stat('STAT'), '... should be able to find filehandle' );
118
119         package foo;
120         local *STAT = *main::STAT;
121         main::ok( my $stat2 = File::stat::stat('STAT'), 
122                 '... and filehandle in another package' );
123         close STAT;
124
125 #       VOS open() updates atime; ignore this error (posix-975).
126         my $stat3 = $stat2;
127         if ($^O eq 'vos') {
128                 $$stat3[8] = $$stat[8];
129         }
130
131         main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
132         main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
133
134         main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
135
136         main::is( "@$stat", "@$stat3", '... and must match normal stat' );
137 }
138
139 {   # 111640 - File::stat bogus index check in overload
140     # 7 tests in this block
141
142     use filetest "access";
143     use warnings;
144     for my $op (split //, "rwxRXW") {
145         # these should all warn with filetest access
146         my $w;
147         local $SIG{__WARN__} = sub { $w .= shift };
148         eval "-$op \$stat";
149         like($w, qr/^File::stat ignores use filetest 'access'/,
150              "-$op produced the right warning under use filetest 'access'");
151     }
152
153     {
154         # -d and others shouldn't warn
155         my $w;
156         local $SIG{__WARN__} = sub { $w = shift };
157         eval '-d $stat';
158         is($w, undef, "Should be no warning from -d under filetest access");
159     }
160 }
161
162 SKIP:
163 {   # RT #111638
164     skip "We can't check for FIFOs", 2 unless defined &Fcntl::S_ISFIFO;
165     skip "No pipes", 2 unless defined $Config{d_pipe};
166     pipe my ($rh, $wh)
167       or skip "Couldn't create a pipe: $!", 2;
168     skip "Built-in -p doesn't detect a pipe", 2 unless -p $rh;
169
170     my $pstat = File::stat::stat($rh);
171     ok(!-p($stat), "-p should be false on a file");
172     ok(-p($pstat), "check -p detects a pipe");
173 }
174
175 local $!;
176 $stat = stat '/notafile';
177 isnt( $!, '', 'should populate $!, given invalid file' );
178
179 # Testing pretty much anything else is unportable.