This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Setup @INC at compile time because commit ec34a119 needs to load utf8.pm
[perl5.git] / t / io / fflush.t
CommitLineData
a43cb6b7
BS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
220939e6 6 require './test.pl';
a43cb6b7
BS
7}
8
9# Script to test auto flush on fork/exec/system/qx. The idea is to
10# print "Pe" to a file from a parent process and "rl" to the same file
11# from a child process. If buffers are flushed appropriately, the
12# file should contain "Perl". We'll see...
13use Config;
14use warnings;
15use strict;
16
17# This attempts to mirror the #ifdef forest found in perl.h so that we
18# know when to run these tests. If that forest ever changes, change
19# it here too or expect test gratuitous test failures.
375927eb
PK
20my $useperlio = defined $Config{useperlio} ? $Config{useperlio} eq 'define' ? 1 : 0 : 0;
21my $fflushNULL = defined $Config{fflushNULL} ? $Config{fflushNULL} eq 'define' ? 1 : 0 : 0;
22my $d_sfio = defined $Config{d_sfio} ? $Config{d_sfio} eq 'define' ? 1 : 0 : 0;
23my $fflushall = defined $Config{fflushall} ? $Config{fflushall} eq 'define' ? 1 : 0 : 0;
24my $d_fork = defined $Config{d_fork} ? $Config{d_fork} eq 'define' ? 1 : 0 : 0;
25
26if ($useperlio || $fflushNULL || $d_sfio) {
a6dd0448 27 print "1..7\n";
a43cb6b7 28} else {
375927eb 29 if ($fflushall) {
a6dd0448 30 print "1..7\n";
a43cb6b7
BS
31 } else {
32 print "1..0 # Skip: fflush(NULL) or equivalent not available\n";
33 exit;
34 }
35}
36
a6dd0448
GS
37my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
38$runperl .= qq{ "-I../lib"};
39
a43cb6b7
BS
40sub file_eq {
41 my $f = shift;
42 my $val = shift;
43
44 open IN, $f or die "open $f: $!";
45 chomp(my $line = <IN>);
46 close IN;
47
48 print "# got $line\n";
49 print "# expected $val\n";
50 return $line eq $val;
51}
52
53# This script will be used as the command to execute from
54# child processes
62a28c97
NC
55my $ffprog = tempfile();
56open PROG, "> $ffprog" or die "open $ffprog: $!";
a43cb6b7
BS
57print PROG <<'EOF';
58my $f = shift;
59my $str = shift;
60open OUT, ">> $f" or die "open $f: $!";
61print OUT $str;
62close OUT;
63EOF
64 ;
62a28c97 65close PROG or die "close $ffprog: $!";;
a43cb6b7
BS
66
67$| = 0; # we want buffered output
68
69# Test flush on fork/exec
375927eb 70if (!$d_fork) {
a43cb6b7
BS
71 print "ok 1 # skipped: no fork\n";
72} else {
62a28c97 73 my $f = tempfile();
a43cb6b7
BS
74 open OUT, "> $f" or die "open $f: $!";
75 print OUT "Pe";
76 my $pid = fork;
77 if ($pid) {
78 # Parent
79 wait;
80 close OUT or die "close $f: $!";
81 } elsif (defined $pid) {
82 # Kid
83 print OUT "r";
62a28c97 84 my $command = qq{$runperl "$ffprog" "$f" "l"};
a43cb6b7
BS
85 print "# $command\n";
86 exec $command or die $!;
87 exit;
88 } else {
89 # Bang
90 die "fork: $!";
91 }
92
93 print file_eq($f, "Perl") ? "ok 1\n" : "not ok 1\n";
a43cb6b7
BS
94}
95
96# Test flush on system/qx/pipe open
97my %subs = (
98 "system" => sub {
99 my $c = shift;
100 system $c;
101 },
102 "qx" => sub {
103 my $c = shift;
104 qx{$c};
105 },
106 "popen" => sub {
107 my $c = shift;
108 open PIPE, "$c|" or die "$c: $!";
109 close PIPE;
110 },
111 );
112my $t = 2;
113for (qw(system qx popen)) {
114 my $code = $subs{$_};
62a28c97
NC
115 my $f = tempfile();
116 my $command = qq{$runperl $ffprog "$f" "rl"};
a43cb6b7
BS
117 open OUT, "> $f" or die "open $f: $!";
118 print OUT "Pe";
f126f811 119 close OUT or die "close $f: $!";;
a43cb6b7
BS
120 print "# $command\n";
121 $code->($command);
a43cb6b7 122 print file_eq($f, "Perl") ? "ok $t\n" : "not ok $t\n";
a43cb6b7
BS
123 ++$t;
124}
a6dd0448 125
220939e6
JH
126my $cmd = _create_runperl(
127 switches => ['-l'],
128 prog =>
129 sprintf('print qq[ok $_] for (%d..%d)', $t, $t+2));
130print "# cmd = '$cmd'\n";
a6dd0448
GS
131open my $CMD, "$cmd |" or die "Can't open pipe to '$cmd': $!";
132while (<$CMD>) {
133 system("$runperl -e 0");
134 print;
135}
136close $CMD;
137$t += 3;