This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Swap logic in BEGIN blocks to have Cwd's abs_path do the
[perl5.git] / lib / IPC / Run / t / binmode.t
CommitLineData
f75b6f69
JB
1#!/usr/bin/perl -w
2
3=head1 NAME
4
5binary.t - Test suite for IPC::Run binary functionality
6
7=cut
8
9BEGIN {
10 if( $ENV{PERL_CORE} ) {
f75b6f69
JB
11 use Cwd;
12 $^X = Cwd::abs_path($^X);
13 $^X = qq("$^X") if $^X =~ /\s/;
ced0984d
MB
14 chdir '../lib/IPC/Run' if -d '../lib/IPC/Run';
15 unshift @INC, 'lib', '../..';
f75b6f69
JB
16 }
17}
18
19## Handy to have when our output is intermingled with debugging output sent
20## to the debugging fd.
21$| = 1 ;
22select STDERR ; $| = 1 ; select STDOUT ;
23
24use strict ;
25
26use Test ;
27
28use IPC::Run qw( harness run binary ) ;
29
30sub Win32_MODE() ;
31*Win32_MODE = \&IPC::Run::Win32_MODE ;
32
33my $crlf_text = "Hello World\r\n" ;
34
35my $text = $crlf_text ;
36$text =~ s/\r//g if Win32_MODE ;
37
38my $nl_text = $crlf_text ;
39$nl_text =~ s/\r//g ;
40
41my @perl = ( $^X ) ;
42
43my $emitter_script = q{ binmode STDOUT ; print "Hello World\r\n" } ;
44my @emitter = ( @perl, '-e', $emitter_script ) ;
45
46my $reporter_script =
47 q{ binmode STDIN ; $_ = join "", <>; s/([\000-\037])/sprintf "\\\\0x%02x", ord $1/ge; print } ;
48my @reporter = ( @perl, '-e', $reporter_script ) ;
49
50my $in ;
51my $out ;
52my $err ;
53
54sub f($) {
55 my $s = shift ;
56 $s =~ s/([\000-\027])/sprintf "\\0x%02x", ord $1/ge ;
57 $s
58}
59
60my @tests = (
61## Parsing tests
62sub { ok eval { harness [], '>', binary, \$out } ? 1 : $@, 1 } ,
63sub { ok eval { harness [], '>', binary, "foo" } ? 1 : $@, 1 },
64sub { ok eval { harness [], '<', binary, \$in } ? 1 : $@, 1 },
65sub { ok eval { harness [], '<', binary, "foo" } ? 1 : $@, 1 },
66
67## Testing from-kid now so we can use it to test stdin later
68sub { ok run \@emitter, ">", \$out },
69sub { ok f $out, f $text, "no binary" },
70
71sub { ok run \@emitter, ">", binary, \$out },
72sub { ok f $out, f $crlf_text, "out binary" },
73
74sub { ok run \@emitter, ">", binary( 0 ), \$out },
75sub { ok f $out, f $text, "out binary 0" },
76
77sub { ok run \@emitter, ">", binary( 1 ), \$out },
78sub { ok f $out, f $crlf_text, "out binary 1" },
79
80## Test to-kid
81sub { ok run \@reporter, "<", \$nl_text, ">", \$out },
82sub { ok $out, "Hello World" . ( Win32_MODE ? "\\0x0d" : "" ) . "\\0x0a", "reporter < \\n" },
83
84sub { ok run \@reporter, "<", binary, \$nl_text, ">", \$out },
85sub { ok $out, "Hello World\\0x0a", "reporter < binary \\n" },
86
87sub { ok run \@reporter, "<", binary, \$crlf_text, ">", \$out },
88sub { ok $out, "Hello World\\0x0d\\0x0a", "reporter < binary \\r\\n" },
89
90sub { ok run \@reporter, "<", binary( 0 ), \$nl_text, ">", \$out },
91sub { ok $out, "Hello World" . ( Win32_MODE ? "\\0x0d" : "" ) . "\\0x0a", "reporter < binary(0) \\n" },
92
93sub { ok run \@reporter, "<", binary( 1 ), \$nl_text, ">", \$out },
94sub { ok $out, "Hello World\\0x0a", "reporter < binary(1) \\n" },
95
96sub { ok run \@reporter, "<", binary( 1 ), \$crlf_text, ">", \$out },
97sub { ok $out, "Hello World\\0x0d\\0x0a", "reporter < binary(1) \\r\\n" },
98) ;
99
100plan tests => scalar @tests ;
101
102$_->() for ( @tests ) ;