This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Correct typo in comment.
[perl5.git] / t / op / exec.t
CommitLineData
8d063cd8
LW
1#!./perl
2
853410bb 3BEGIN {
9247405f 4 chdir 't' if -d 't';
972e7321 5 require './test.pl';
624c42e2 6 set_up_inc('../lib');
9247405f
MS
7}
8
e08e1e1d
JM
9my $vms_exit_mode = 0;
10
11if ($^O eq 'VMS') {
12 if (eval 'require VMS::Feature') {
13 $vms_exit_mode = !(VMS::Feature::current("posix_exit"));
14 } else {
15 my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
16 my $env_posix_ex = $ENV{'PERL_VMS_POSIX_EXIT'} || '';
17 my $unix_rpt = $env_unix_rpt =~ /^[ET1]/i;
18 my $posix_ex = $env_posix_ex =~ /^[ET1]/i;
19 if (($unix_rpt || $posix_ex) ) {
20 $vms_exit_mode = 0;
21 } else {
22 $vms_exit_mode = 1;
23 }
24 }
25}
26
27
93f09d7b 28# suppress VMS whinging about bad execs.
9247405f
MS
29use vmsish qw(hushed);
30
8d063cd8 31$| = 1; # flush stdout
68dc0745 32
cc2768c2 33$ENV{LC_ALL} = 'C'; # Force English error messages.
9429f27a
JH
34$ENV{LANGUAGE} = 'C'; # Ditto in GNU.
35
e0d72a37
JH
36my $Is_VMS = $^O eq 'VMS';
37my $Is_Win32 = $^O eq 'MSWin32';
b6345914 38
64def2ae 39plan(tests => 41);
972e7321
MS
40
41my $Perl = which_perl();
68dc0745 42
9247405f
MS
43my $exit;
44SKIP: {
45 skip("bug/feature of pdksh", 2) if $^O eq 'os2';
46
b6345914
JH
47 my $tnum = curr_test();
48 $exit = system qq{$Perl -le "print q{ok $tnum - interp system(EXPR)"}};
9247405f
MS
49 next_test();
50 is( $exit, 0, ' exited 0' );
95e8664e
CN
51}
52
b6345914
JH
53my $tnum = curr_test();
54$exit = system qq{$Perl -le "print q{ok $tnum - split & direct system(EXPR)"}};
9247405f
MS
55next_test();
56is( $exit, 0, ' exited 0' );
57
e0d72a37 58# On VMS and Win32 you need the quotes around the program or it won't work.
9247405f 59# On Unix its the opposite.
e0d72a37 60my $quote = $Is_VMS || $Is_Win32 ? '"' : '';
b6345914 61$tnum = curr_test();
972e7321 62$exit = system $Perl, '-le',
b6345914 63 "${quote}print q{ok $tnum - system(PROG, LIST)}${quote}";
9247405f
MS
64next_test();
65is( $exit, 0, ' exited 0' );
66
8d063cd8 67
b6345914
JH
68# Some basic piped commands. Some OS's have trouble with "helpfully"
69# putting newlines on the end of piped output. So we split this into
70# newline insensitive and newline sensitive tests.
71my $echo_out = `$Perl -e "print 'ok'" | $Perl -le "print <STDIN>"`;
72$echo_out =~ s/\n\n/\n/g;
73is( $echo_out, "ok\n", 'piped echo emulation');
74
75{
76 # here we check if extra newlines are going to be slapped on
77 # piped output.
78 local $TODO = 'VMS sticks newlines on everything' if $Is_VMS;
79
80 is( scalar `$Perl -e "print 'ok'"`,
81 "ok", 'no extra newlines on ``' );
82
83 is( scalar `$Perl -e "print 'ok'" | $Perl -e "print <STDIN>"`,
84 "ok", 'no extra newlines on pipes');
85
86 is( scalar `$Perl -le "print 'ok'" | $Perl -le "print <STDIN>"`,
87 "ok\n\n", 'doubled up newlines');
88
89 is( scalar `$Perl -e "print 'ok'" | $Perl -le "print <STDIN>"`,
90 "ok\n", 'extra newlines on inside pipes');
91
92 is( scalar `$Perl -le "print 'ok'" | $Perl -e "print <STDIN>"`,
93 "ok\n", 'extra newlines on outgoing pipes');
fa326138
RG
94
95 {
96 local($/) = \2;
97 $out = runperl(prog => 'print q{1234}');
98 is($out, "1234", 'ignore $/ when capturing output in scalar context');
99 }
b6345914
JH
100}
101
102
972e7321 103is( system(qq{$Perl -e "exit 0"}), 0, 'Explicit exit of 0' );
9247405f 104
e08e1e1d 105my $exit_one = $vms_exit_mode ? 4 << 8 : 1 << 8;
972e7321 106is( system(qq{$Perl "-I../lib" -e "use vmsish qw(hushed); exit 1"}), $exit_one,
9247405f
MS
107 'Explicit exit of 1' );
108
c623ac67 109$rc = system { "lskdfj" } "lskdfj";
0dd6fc14 110unless( ok($rc == 255 << 8 or $rc == -1 or $rc == 256 or $rc == 512) ) {
9247405f 111 print "# \$rc == $rc\n";
ec40c0cd 112}
8d063cd8 113
9247405f
MS
114unless ( ok( $! == 2 or $! =~ /\bno\b.*\bfile/i or
115 $! == 13 or $! =~ /permission denied/i or
250d67eb 116 $! == 22 or $! =~ /invalid argument/i ) ) {
4cdca267 117 diag sprintf "\$! eq %d, '%s'\n", $!, $!;
9247405f
MS
118}
119
972e7321
MS
120
121is( `$Perl -le "print 'ok'"`, "ok\n", 'basic ``' );
122is( <<`END`, "ok\n", '<<`HEREDOC`' );
123$Perl -le "print 'ok'"
124END
125
cdd6375d
MH
126is( <<~`END`, "ok\n", '<<~`HEREDOC`' );
127 $Perl -le "print 'ok'"
128 END
129
8d7403e6 130{
397baf23
Z
131 sub rpecho { qq($Perl -le "print '$_[0]'") }
132 is scalar(readpipe(rpecho("b"))), "b\n",
133 "readpipe with one argument in scalar context";
134 is join(",", "a", readpipe(rpecho("b")), "c"), "a,b\n,c",
135 "readpipe with one argument in list context";
136 local $_ = rpecho("f");
137 is scalar(readpipe), "f\n",
138 "readpipe default argument in scalar context";
139 is join(",", "a", readpipe, "c"), "a,f\n,c",
140 "readpipe default argument in list context";
141 sub rpechocxt {
142 rpecho(wantarray ? "list" : defined(wantarray) ? "scalar" : "void");
143 }
144 is scalar(readpipe(rpechocxt())), "scalar\n",
145 "readpipe argument context in scalar context";
146 is join(",", "a", readpipe(rpechocxt()), "b"), "a,scalar\n,b",
147 "readpipe argument context in list context";
148 foreach my $args ("(\$::p,\$::q)", "((\$::p,\$::q))") {
149 foreach my $lvalue ("my \$r", "my \@r") {
150 eval("$lvalue = readpipe$args if 0");
151 like $@, qr/\AToo many arguments for /;
152 }
153 }
8d7403e6 154}
972e7321 155
6a5c965b
FC
156package o {
157 use subs "readpipe";
158 sub readpipe { pop }
159 ::is `${\"hello"}`, 'hello',
160 'overridden `` interpolates [perl #115330]';
161 ::is <<`119827`, "ls\n",
162l${\"s"}
163119827
164 '<<`` respects overrides and interpolates [perl #119827]';
165}
166
9247405f 167TODO: {
b6345914 168 my $tnum = curr_test();
9247405f 169 if( $^O =~ /Win32/ ) {
e0d72a37
JH
170 print "not ok $tnum - exec failure doesn't terminate process " .
171 "# TODO Win32 exec failure waits for user input\n";
172 next_test();
9247405f
MS
173 last TODO;
174 }
378cc40b 175
9247405f
MS
176 ok( !exec("lskdjfalksdjfdjfkls"),
177 "exec failure doesn't terminate process");
0994c4d0 178}
378cc40b 179
2fcab330
DIM
180{
181 local $! = 0;
182 ok !exec(), 'empty exec LIST fails';
183 ok $! == 2 || $! =~ qr/\bno\b.*\bfile\b/i, 'errno = ENOENT'
4cdca267 184 or diag sprintf "\$! eq %d, '%s'\n", $!, $!;
2fcab330
DIM
185
186}
187{
188 local $! = 0;
189 my $err = $!;
190 ok !(exec {""} ()), 'empty exec PROGRAM LIST fails';
f47d3375 191 ok $! == 2 || $! =~ qr/\bno\b.*\bfile\b/i, 'errno = ENOENT'
4cdca267 192 or diag sprintf "\$! eq %d, '%s'\n", $!, $!;
2fcab330
DIM
193}
194
64def2ae
Z
195package CountRead {
196 sub TIESCALAR { bless({ n => 0 }, $_[0]) }
197 sub FETCH { ++$_[0]->{n} }
198}
199my $cr;
200tie $cr, "CountRead";
4cafcffa
CB
201my $exit_statement = "exit(\$ARGV[0] eq '1' ? 0 : 1)";
202$exit_statement = qq/"$exit_statement"/ if $^O eq 'VMS';
203is system($^X, "-e", $exit_statement, $cr), 0,
64def2ae
Z
204 "system args have magic processed exactly once";
205is tied($cr)->{n}, 1, "system args have magic processed before fork";
206
4cafcffa
CB
207$exit_statement = "exit(\$ARGV[0] eq \$ARGV[1] ? 0 : 1)";
208$exit_statement = qq/"$exit_statement"/ if $^O eq 'VMS';
209is system($^X, "-e", $exit_statement, "$$", $$), 0,
64def2ae
Z
210 "system args have magic processed before fork";
211
972e7321
MS
212my $test = curr_test();
213exec $Perl, '-le', qq{${quote}print 'ok $test - exec PROG, LIST'${quote}};
9247405f 214fail("This should never be reached if the exec() worked");