magic.t tweak
[p5sagit/p5-mst-13.2.git] / t / op / exec.t
1 #!./perl
2
3 BEGIN: {
4     chdir 't' if -d 't';
5     @INC = ('../lib');
6     require './test.pl';
7 }
8
9 # supress VMS whinging about bad execs.
10 use vmsish qw(hushed);
11
12 $| = 1;                         # flush stdout
13
14 $ENV{LC_ALL}   = 'C';           # Forge English error messages.
15 $ENV{LANGUAGE} = 'C';           # Ditto in GNU.
16
17 my $Is_VMS   = $^O eq 'VMS';
18 my $Is_Win32 = $^O eq 'MSWin32';
19
20 plan(tests => 20);
21
22 my $Perl = which_perl();
23
24 my $exit;
25 SKIP: {
26     skip("bug/feature of pdksh", 2) if $^O eq 'os2';
27
28     my $tnum = curr_test();
29     $exit = system qq{$Perl -le "print q{ok $tnum - interp system(EXPR)"}};
30     next_test();
31     is( $exit, 0, '  exited 0' );
32 }
33
34 my $tnum = curr_test();
35 $exit = system qq{$Perl -le "print q{ok $tnum - split & direct system(EXPR)"}};
36 next_test();
37 is( $exit, 0, '  exited 0' );
38
39 # On VMS and Win32 you need the quotes around the program or it won't work.
40 # On Unix its the opposite.
41 my $quote = $Is_VMS || $Is_Win32 ? '"' : '';
42 $tnum = curr_test();
43 $exit = system $Perl, '-le', 
44                "${quote}print q{ok $tnum - system(PROG, LIST)}${quote}";
45 next_test();
46 is( $exit, 0, '  exited 0' );
47
48
49 # Some basic piped commands.  Some OS's have trouble with "helpfully"
50 # putting newlines on the end of piped output.  So we split this into
51 # newline insensitive and newline sensitive tests.
52 my $echo_out = `$Perl -e "print 'ok'" | $Perl -le "print <STDIN>"`;
53 $echo_out =~ s/\n\n/\n/g;
54 is( $echo_out, "ok\n", 'piped echo emulation');
55
56 {
57     # here we check if extra newlines are going to be slapped on
58     # piped output.
59     local $TODO = 'VMS sticks newlines on everything' if $Is_VMS;
60
61     is( scalar `$Perl -e "print 'ok'"`,
62         "ok", 'no extra newlines on ``' );
63
64     is( scalar `$Perl -e "print 'ok'" | $Perl -e "print <STDIN>"`, 
65         "ok", 'no extra newlines on pipes');
66
67     is( scalar `$Perl -le "print 'ok'" | $Perl -le "print <STDIN>"`, 
68         "ok\n\n", 'doubled up newlines');
69
70     is( scalar `$Perl -e "print 'ok'" | $Perl -le "print <STDIN>"`, 
71         "ok\n", 'extra newlines on inside pipes');
72
73     is( scalar `$Perl -le "print 'ok'" | $Perl -e "print <STDIN>"`, 
74         "ok\n", 'extra newlines on outgoing pipes');
75 }
76
77
78 is( system(qq{$Perl -e "exit 0"}), 0,     'Explicit exit of 0' );
79
80 my $exit_one = $Is_VMS ? 4 << 8 : 1 << 8;
81 is( system(qq{$Perl "-I../lib" -e "use vmsish qw(hushed); exit 1"}), $exit_one,
82     'Explicit exit of 1' );
83
84
85 $rc = system "lskdfj";
86 unless( ok($rc == 255 << 8 or $rc == -1 or $rc == 256) ) {
87     print "# \$rc == $rc\n";
88 }
89
90 unless ( ok( $! == 2  or  $! =~ /\bno\b.*\bfile/i or  
91              $! == 13 or  $! =~ /permission denied/i or
92              $! == 22 or  $! =~ /invalid argument/           ) ) {
93     printf "# \$! eq %d, '%s'\n", $!, $!;
94 }
95
96
97 is( `$Perl -le "print 'ok'"`,   "ok\n",     'basic ``' );
98 is( <<`END`,                    "ok\n",     '<<`HEREDOC`' );
99 $Perl -le "print 'ok'"
100 END
101
102
103 TODO: {
104     my $tnum = curr_test();
105     if( $^O =~ /Win32/ ) {
106         print "not ok $tnum - exec failure doesn't terminate process " .
107               "# TODO Win32 exec failure waits for user input\n";
108         next_test();
109         last TODO;
110     }
111
112     ok( !exec("lskdjfalksdjfdjfkls"), 
113         "exec failure doesn't terminate process");
114 }
115
116 my $test = curr_test();
117 exec $Perl, '-le', qq{${quote}print 'ok $test - exec PROG, LIST'${quote}};
118 fail("This should never be reached if the exec() worked");