Be more robust on "extreme" (large absolute value)
[p5sagit/p5-mst-13.2.git] / t / lib / open3.t
index a4a978e..7cd0ca3 100755 (executable)
@@ -1,43 +1,65 @@
 #!./perl -w
-use strict;
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
+    unshift @INC, '../lib';
+    require Config; import Config;
+    if (!$Config{'d_fork'}
+       # open2/3 supported on win32 (but not Borland due to CRT bugs)
+       && ($^O ne 'MSWin32' || $Config{'cc'} =~ /^bcc/i))
+    {
+       print "1..0\n";
+       exit 0;
+    }
     # make warnings fatal
     $SIG{__WARN__} = sub { die @_ };
 }
 
+use strict;
 use IO::Handle;
 use IPC::Open3;
 #require 'open3.pl'; use subs 'open3';
 
+my $perl = './perl';
+
 sub ok {
     my ($n, $result, $info) = @_;
     if ($result) {
        print "ok $n\n";
     }
     else {
-       print "not ok $n\n";
+       print "not ok $n\n";
        print "# $info\n" if $info;
     }
 }
 
+sub cmd_line {
+       if ($^O eq 'MSWin32') {
+               my $cmd = shift;
+               $cmd =~ tr/\r\n//d;
+               $cmd =~ s/"/\\"/g;
+               return qq/"$cmd"/;
+       }
+       else {
+               return $_[0];
+       }
+}
+
 my ($pid, $reaped_pid);
 STDOUT->autoflush;
 STDERR->autoflush;
 
-print "1..21\n";
+print "1..22\n";
 
 # basic
-ok 1, $pid = open3 'WRITE', 'READ', 'ERROR', $^X, '-e', <<'EOF';
+ok 1, $pid = open3 'WRITE', 'READ', 'ERROR', $perl, '-e', cmd_line(<<'EOF');
     $| = 1;
     print scalar <STDIN>;
     print STDERR "hi error\n";
 EOF
 ok 2, print WRITE "hi kid\n";
-ok 3, <READ> eq "hi kid\n";
-ok 4, <ERROR> eq "hi error\n";
+ok 3, <READ> =~ /^hi kid\r?\n$/;
+ok 4, <ERROR> =~ /^hi error\r?\n$/;
 ok 5, close(WRITE), $!;
 ok 6, close(READ), $!;
 ok 7, close(ERROR), $!;
@@ -46,7 +68,7 @@ ok 8, $reaped_pid == $pid, $reaped_pid;
 ok 9, $? == 0, $?;
 
 # read and error together, both named
-$pid = open3 'WRITE', 'READ', 'READ', $^X, '-e', <<'EOF';
+$pid = open3 'WRITE', 'READ', 'READ', $perl, '-e', cmd_line(<<'EOF');
     $| = 1;
     print scalar <STDIN>;
     print STDERR scalar <STDIN>;
@@ -58,7 +80,7 @@ print scalar <READ>;
 waitpid $pid, 0;
 
 # read and error together, error empty
-$pid = open3 'WRITE', 'READ', '', $^X, '-e', <<'EOF';
+$pid = open3 'WRITE', 'READ', '', $perl, '-e', cmd_line(<<'EOF');
     $| = 1;
     print scalar <STDIN>;
     print STDERR scalar <STDIN>;
@@ -72,7 +94,7 @@ waitpid $pid, 0;
 # dup writer
 ok 14, pipe PIPE_READ, PIPE_WRITE;
 $pid = open3 '<&PIPE_READ', 'READ', '',
-                   $^X, '-e', 'print scalar <STDIN>';
+                   $perl, '-e', cmd_line('print scalar <STDIN>');
 close PIPE_READ;
 print PIPE_WRITE "ok 15\n";
 close PIPE_WRITE;
@@ -81,7 +103,7 @@ waitpid $pid, 0;
 
 # dup reader
 $pid = open3 'WRITE', '>&STDOUT', 'ERROR',
-                   $^X, '-e', 'print scalar <STDIN>';
+                   $perl, '-e', cmd_line('print scalar <STDIN>');
 print WRITE "ok 16\n";
 waitpid $pid, 0;
 
@@ -89,12 +111,12 @@ waitpid $pid, 0;
 # stdout but putting stdout somewhere else, is a good case because it
 # used not to work.
 $pid = open3 'WRITE', 'READ', '>&STDOUT',
-                   $^X, '-e', 'print STDERR scalar <STDIN>';
+                   $perl, '-e', cmd_line('print STDERR scalar <STDIN>');
 print WRITE "ok 17\n";
 waitpid $pid, 0;
 
 # dup reader and error together, both named
-$pid = open3 'WRITE', '>&STDOUT', '>&STDOUT', $^X, '-e', <<'EOF';
+$pid = open3 'WRITE', '>&STDOUT', '>&STDOUT', $perl, '-e', cmd_line(<<'EOF');
     $| = 1;
     print STDOUT scalar <STDIN>;
     print STDERR scalar <STDIN>;
@@ -104,7 +126,7 @@ print WRITE "ok 19\n";
 waitpid $pid, 0;
 
 # dup reader and error together, error empty
-$pid = open3 'WRITE', '>&STDOUT', '', $^X, '-e', <<'EOF';
+$pid = open3 'WRITE', '>&STDOUT', '', $perl, '-e', cmd_line(<<'EOF');
     $| = 1;
     print STDOUT scalar <STDIN>;
     print STDERR scalar <STDIN>;
@@ -112,3 +134,17 @@ EOF
 print WRITE "ok 20\n";
 print WRITE "ok 21\n";
 waitpid $pid, 0;
+
+# command line in single parameter variant of open3
+# for understanding of Config{'sh'} test see exec description in camel book
+my $cmd = 'print(scalar(<STDIN>))';
+$cmd = $Config{'sh'} =~ /sh/ ? "'$cmd'" : cmd_line($cmd);
+eval{$pid = open3 'WRITE', '>&STDOUT', 'ERROR', "$perl -e " . $cmd; };
+if ($@) {
+       print "error $@\n";
+       print "not ok 22\n";
+}
+else {
+       print WRITE "ok 22\n";
+       waitpid $pid, 0;
+}