Fix ext/POSIX/t/sysconf.t failures on Cygwin.
[p5sagit/p5-mst-13.2.git] / ext / POSIX / t / sysconf.t
1 #!perl -T
2
3 BEGIN {
4     if ($ENV{PERL_CORE}) {
5         chdir 't';
6         @INC = '../lib';
7     }
8
9     use Config;
10     use Test::More;
11     plan skip_all => "POSIX is unavailable" if $Config{'extensions'} !~ m!\bPOSIX\b!;
12 }
13
14 use strict;
15 use File::Spec;
16 use POSIX;
17 use Scalar::Util qw(looks_like_number);
18
19 sub check(@) {
20     grep { eval "&$_;1" or $@!~/vendor has not defined POSIX macro/ } @_
21 }       
22
23 my @path_consts = check qw(
24     _PC_CHOWN_RESTRICTED _PC_LINK_MAX _PC_NAME_MAX
25     _PC_NO_TRUNC _PC_PATH_MAX
26 );
27
28 my @path_consts_terminal = check qw(
29     _PC_MAX_CANON _PC_MAX_INPUT _PC_VDISABLE
30 );
31
32 my @path_consts_fifo = check qw(
33     _PC_PIPE_BUF
34 );
35
36 my @sys_consts = check qw(
37     _SC_ARG_MAX _SC_CHILD_MAX _SC_CLK_TCK _SC_JOB_CONTROL
38     _SC_NGROUPS_MAX _SC_OPEN_MAX _SC_PAGESIZE _SC_SAVED_IDS
39     _SC_STREAM_MAX _SC_VERSION _SC_TZNAME_MAX
40 );
41
42 my $tests = 2 * 3 * @path_consts +
43             2 * 3 * @path_consts_terminal +
44             2 * 3 * @path_consts_fifo +
45                 3 * @sys_consts;
46 plan $tests 
47      ? (tests => $tests) 
48      : (skip_all => "No tests to run on this OS")
49 ;
50
51 my $curdir = File::Spec->curdir;
52 $curdir = VMS::Filespec::fileify($curdir) if $^O eq 'VMS';
53
54 my $r;
55
56 sub _check_and_report {
57     my ($eval_status, $return_val, $description) = @_;
58     my $success = defined($return_val) || $! == 0;
59     is( $eval_status, '', $description );
60     ok( $success, "\tchecking that the returned value is defined (" 
61                     . (defined($return_val) ? "yes, it's $return_val)" : "it isn't)"
62                     . " or that errno is clear ("
63                     . (!($!+0) ? "it is)" : "it isn't, it's $!)"))
64                     );
65     SKIP: {
66         skip "constant not implemented on $^O or no limit in effect", 1 
67             if $success && !defined($return_val);
68         ok( looks_like_number($return_val), "\tchecking that the returned value looks like a number" );
69     }
70 }
71
72 # testing fpathconf() on a non-terminal file
73 SKIP: {
74     my $fd = POSIX::open($curdir, O_RDONLY)
75         or skip "could not open current directory ($!)", 3 * @path_consts;
76
77     for my $constant (@path_consts) {
78             $! = 0;
79             $r = eval { fpathconf( $fd, eval "$constant()" ) };
80             _check_and_report( $@, $r, "calling fpathconf($fd, $constant) " );
81     }
82     
83     POSIX::close($fd);
84 }
85
86 # testing pathconf() on a non-terminal file
87 for my $constant (@path_consts) {
88         $! = 0;
89         $r = eval { pathconf( $curdir, eval "$constant()" ) };
90         _check_and_report( $@, $r, qq[calling pathconf("$curdir", $constant)] );
91 }
92
93 SKIP: {
94     my $TTY = "/dev/tty";
95
96     my $n = 2 * 3 * @path_consts_terminal;
97
98     -c $TTY
99         or skip("$TTY not a character file", $n);
100     open(TTY, $TTY)
101         or skip("failed to open $TTY: $!", $n);
102     -t TTY
103         or skip("TTY ($TTY) not a terminal file", $n);
104
105     my $fd = fileno(TTY);
106
107     # testing fpathconf() on a terminal file
108     for my $constant (@path_consts_terminal) {
109         $! = 0;
110         $r = eval { fpathconf( $fd, eval "$constant()" ) };
111         _check_and_report( $@, $r, qq[calling fpathconf($fd, $constant) ($TTY)] );
112     }
113     
114     close($fd);
115     # testing pathconf() on a terminal file
116     for my $constant (@path_consts_terminal) {
117         $! = 0;
118         $r = eval { pathconf( $TTY, eval "$constant()" ) };
119         _check_and_report( $@, $r, qq[calling pathconf($TTY, $constant)] );
120     }
121 }
122
123 my $fifo = "fifo$$";
124
125 SKIP: {
126     eval { mkfifo($fifo, 0666) }
127         or skip("could not create fifo $fifo ($!)", 2 * 3 * @path_consts_fifo);
128
129   SKIP: {
130       my $fd = POSIX::open($fifo, O_RDWR)
131           or skip("could not open $fifo ($!)", 3 * @path_consts_fifo);
132
133       for my $constant (@path_consts_fifo) {
134           $! = 0;
135           $r = eval { fpathconf( $fd, eval "$constant()" ) };
136           _check_and_report( $@, $r, "calling fpathconf($fd, $constant) ($fifo)" );
137       }
138     
139       POSIX::close($fd);
140   }
141
142   # testing pathconf() on a fifo file
143   for my $constant (@path_consts_fifo) {
144       $! = 0;
145       $r = eval { pathconf( $fifo, eval "$constant()" ) };
146       _check_and_report( $@, $r, qq[calling pathconf($fifo, $constant)] );
147   }
148 }
149
150 END {
151     1 while unlink($fifo);
152 }
153
154 SKIP: {
155     if($^O eq 'cygwin') {
156         pop @sys_consts;
157         skip("No _SC_TZNAME_MAX on Cygwin", 3);
158     }
159         
160 }
161 # testing sysconf()
162 for my $constant (@sys_consts) {
163         $! = 0;
164         $r = eval { sysconf( eval "$constant()" ) };
165         _check_and_report( $@, $r, "calling sysconf($constant)" );
166 }
167