Commit | Line | Data |
---|---|---|
a43cb6b7 BS |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
220939e6 | 5 | require './test.pl'; |
624c42e2 | 6 | set_up_inc('../lib'); |
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... | |
13 | use Config; | |
14 | use warnings; | |
15 | use 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 |
20 | my $useperlio = defined $Config{useperlio} ? $Config{useperlio} eq 'define' ? 1 : 0 : 0; |
21 | my $fflushNULL = defined $Config{fflushNULL} ? $Config{fflushNULL} eq 'define' ? 1 : 0 : 0; | |
375927eb PK |
22 | my $fflushall = defined $Config{fflushall} ? $Config{fflushall} eq 'define' ? 1 : 0 : 0; |
23 | my $d_fork = defined $Config{d_fork} ? $Config{d_fork} eq 'define' ? 1 : 0 : 0; | |
24 | ||
991b8780 | 25 | skip_all('fflush(NULL) or equivalent not available') |
97cb92d6 | 26 | unless $useperlio || $fflushNULL || $fflushall; |
991b8780 NC |
27 | |
28 | plan(tests => 7); | |
a43cb6b7 | 29 | |
a6dd0448 GS |
30 | my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X; |
31 | $runperl .= qq{ "-I../lib"}; | |
32 | ||
a43cb6b7 BS |
33 | sub file_eq { |
34 | my $f = shift; | |
35 | my $val = shift; | |
36 | ||
37 | open IN, $f or die "open $f: $!"; | |
38 | chomp(my $line = <IN>); | |
39 | close IN; | |
40 | ||
41 | print "# got $line\n"; | |
42 | print "# expected $val\n"; | |
43 | return $line eq $val; | |
44 | } | |
45 | ||
46 | # This script will be used as the command to execute from | |
47 | # child processes | |
62a28c97 NC |
48 | my $ffprog = tempfile(); |
49 | open PROG, "> $ffprog" or die "open $ffprog: $!"; | |
a43cb6b7 BS |
50 | print PROG <<'EOF'; |
51 | my $f = shift; | |
52 | my $str = shift; | |
53 | open OUT, ">> $f" or die "open $f: $!"; | |
54 | print OUT $str; | |
55 | close OUT; | |
56 | EOF | |
57 | ; | |
62a28c97 | 58 | close PROG or die "close $ffprog: $!";; |
a43cb6b7 BS |
59 | |
60 | $| = 0; # we want buffered output | |
61 | ||
62 | # Test flush on fork/exec | |
375927eb | 63 | if (!$d_fork) { |
a43cb6b7 BS |
64 | print "ok 1 # skipped: no fork\n"; |
65 | } else { | |
62a28c97 | 66 | my $f = tempfile(); |
a43cb6b7 BS |
67 | open OUT, "> $f" or die "open $f: $!"; |
68 | print OUT "Pe"; | |
69 | my $pid = fork; | |
70 | if ($pid) { | |
71 | # Parent | |
72 | wait; | |
73 | close OUT or die "close $f: $!"; | |
74 | } elsif (defined $pid) { | |
75 | # Kid | |
76 | print OUT "r"; | |
62a28c97 | 77 | my $command = qq{$runperl "$ffprog" "$f" "l"}; |
a43cb6b7 BS |
78 | print "# $command\n"; |
79 | exec $command or die $!; | |
80 | exit; | |
81 | } else { | |
82 | # Bang | |
83 | die "fork: $!"; | |
84 | } | |
85 | ||
86 | print file_eq($f, "Perl") ? "ok 1\n" : "not ok 1\n"; | |
a43cb6b7 BS |
87 | } |
88 | ||
89 | # Test flush on system/qx/pipe open | |
90 | my %subs = ( | |
91 | "system" => sub { | |
92 | my $c = shift; | |
93 | system $c; | |
94 | }, | |
95 | "qx" => sub { | |
96 | my $c = shift; | |
97 | qx{$c}; | |
98 | }, | |
99 | "popen" => sub { | |
100 | my $c = shift; | |
101 | open PIPE, "$c|" or die "$c: $!"; | |
102 | close PIPE; | |
103 | }, | |
104 | ); | |
105 | my $t = 2; | |
106 | for (qw(system qx popen)) { | |
107 | my $code = $subs{$_}; | |
62a28c97 NC |
108 | my $f = tempfile(); |
109 | my $command = qq{$runperl $ffprog "$f" "rl"}; | |
a43cb6b7 BS |
110 | open OUT, "> $f" or die "open $f: $!"; |
111 | print OUT "Pe"; | |
f126f811 | 112 | close OUT or die "close $f: $!";; |
a43cb6b7 BS |
113 | print "# $command\n"; |
114 | $code->($command); | |
a43cb6b7 | 115 | print file_eq($f, "Perl") ? "ok $t\n" : "not ok $t\n"; |
a43cb6b7 BS |
116 | ++$t; |
117 | } | |
a6dd0448 | 118 | |
220939e6 JH |
119 | my $cmd = _create_runperl( |
120 | switches => ['-l'], | |
121 | prog => | |
122 | sprintf('print qq[ok $_] for (%d..%d)', $t, $t+2)); | |
123 | print "# cmd = '$cmd'\n"; | |
a6dd0448 GS |
124 | open my $CMD, "$cmd |" or die "Can't open pipe to '$cmd': $!"; |
125 | while (<$CMD>) { | |
126 | system("$runperl -e 0"); | |
127 | print; | |
128 | } | |
129 | close $CMD; | |
130 | $t += 3; | |
991b8780 | 131 | curr_test($t); |