This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make utf8::encode respect magic
[perl5.git] / t / io / fflush.t
index 8c6bd08..4570f89 100644 (file)
@@ -3,6 +3,7 @@
 BEGIN {
     chdir 't' if -d 't';
     @INC = '../lib';
+    require './test.pl';
 }
 
 # Script to test auto flush on fork/exec/system/qx.  The idea is to
@@ -16,25 +17,19 @@ use strict;
 # This attempts to mirror the #ifdef forest found in perl.h so that we
 # know when to run these tests.  If that forest ever changes, change
 # it here too or expect test gratuitous test failures.
-if ($Config{useperlio} || $Config{fflushNULL} || $Config{d_sfio}) {
-    print "1..4\n";
-} else {
-    if ($Config{fflushall}) {
-       print "1..4\n";
-    } else {
-       print "1..0 # Skip: fflush(NULL) or equivalent not available\n";
-        exit;
-    }
-}
+my $useperlio = defined $Config{useperlio} ? $Config{useperlio} eq 'define' ? 1 : 0 : 0;
+my $fflushNULL = defined $Config{fflushNULL} ? $Config{fflushNULL} eq 'define' ? 1 : 0 : 0;
+my $d_sfio = defined $Config{d_sfio} ? $Config{d_sfio} eq 'define' ? 1 : 0 : 0;
+my $fflushall = defined $Config{fflushall} ? $Config{fflushall} eq 'define' ? 1 : 0 : 0;
+my $d_fork = defined $Config{d_fork} ? $Config{d_fork} eq 'define' ? 1 : 0 : 0;
 
-my $runperl = qq{$^X "-I../lib"};
-my @delete;
+skip_all('fflush(NULL) or equivalent not available')
+    unless $useperlio || $fflushNULL || $d_sfio || $fflushall;
 
-END {
-    for (@delete) {
-       unlink $_ or warn "unlink $_: $!";
-    }
-}
+plan(tests => 7);
+
+my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
+$runperl .= qq{ "-I../lib"};
 
 sub file_eq {
     my $f   = shift;
@@ -51,7 +46,8 @@ sub file_eq {
 
 # This script will be used as the command to execute from
 # child processes
-open PROG, "> ff-prog" or die "open ff-prog: $!";
+my $ffprog = tempfile();
+open PROG, "> $ffprog" or die "open $ffprog: $!";
 print PROG <<'EOF';
 my $f = shift;
 my $str = shift;
@@ -60,16 +56,15 @@ print OUT $str;
 close OUT;
 EOF
     ;
-close PROG;
-push @delete, "ff-prog";
+close PROG or die "close $ffprog: $!";;
 
 $| = 0; # we want buffered output
 
 # Test flush on fork/exec
-if ($Config{d_fork} ne "define") {
+if (!$d_fork) {
     print "ok 1 # skipped: no fork\n";
 } else {
-    my $f = "ff-fork-$$";
+    my $f = tempfile();
     open OUT, "> $f" or die "open $f: $!";
     print OUT "Pe";
     my $pid = fork;
@@ -80,7 +75,7 @@ if ($Config{d_fork} ne "define") {
     } elsif (defined $pid) {
        # Kid
        print OUT "r";
-       my $command = qq{$runperl "ff-prog" "$f" "l"};
+       my $command = qq{$runperl "$ffprog" "$f" "l"};
        print "# $command\n";
        exec $command or die $!;
        exit;
@@ -90,7 +85,6 @@ if ($Config{d_fork} ne "define") {
     }
 
     print file_eq($f, "Perl") ? "ok 1\n" : "not ok 1\n";
-    push @delete, $f;
 }
 
 # Test flush on system/qx/pipe open
@@ -112,14 +106,27 @@ my %subs = (
 my $t = 2;
 for (qw(system qx popen)) {
     my $code    = $subs{$_};
-    my $f       = "ff-$_-$$";
-    my $command = qq{$runperl "ff-prog" "$f" "rl"};
+    my $f       = tempfile();
+    my $command = qq{$runperl $ffprog "$f" "rl"};
     open OUT, "> $f" or die "open $f: $!";
     print OUT "Pe";
+    close OUT or die "close $f: $!";;
     print "# $command\n";
     $code->($command);
-    close OUT;
     print file_eq($f, "Perl") ? "ok $t\n" : "not ok $t\n";
-    push @delete, $f;
     ++$t;
 }
+
+my $cmd = _create_runperl(
+                         switches => ['-l'],
+                         prog =>
+                         sprintf('print qq[ok $_] for (%d..%d)', $t, $t+2));
+print "# cmd = '$cmd'\n";
+open my $CMD, "$cmd |" or die "Can't open pipe to '$cmd': $!";
+while (<$CMD>) {
+    system("$runperl -e 0");
+    print;
+}
+close $CMD;
+$t += 3;
+curr_test($t);