1 //:wasmtest skip
2 // Native-only: a browser domain has no child processes to fork. os/exec's
3 // functions exist for the wasm target as stubs (syscall's js files), so the
4 // package compiles there, but running one cannot work and this fixture says so
5 // rather than pretending.
6 /*
7 os/exec works: Command, Run, Output, CombinedOutput, Dir, and the exit status.
8 9 The package held only ExitError, and it could not have held more: three layers
10 below it were TODO stubs.
11 12 - os.Process.Wait returned ErrNotImplemented, so nothing could learn how a
13 child ended;
14 - os.ProcessState answered "" and false for every process, so even a
15 successful child looked failed;
16 - os.StartProcess REFUSED ProcAttr.Files and ProcAttr.Dir outright, so a pipe
17 could not be handed to a child and Output had no way to capture anything.
18 19 All three are implemented now: Wait4 fills a real ProcessState (Exited,
20 ExitCode, Success, String, Sys), and forkExec's child chdirs to Dir and dup2s
21 each Files[i] onto fd i before execve - which is what makes a captured pipe
22 possible at all. The child cannot report a dup2 failure to the parent yet
23 (there is no error pipe), so it exits 127, the same code a failed execve gives.
24 25 This fixture is the end-to-end evidence: a real pipe is read, a real exit code
26 comes back, and Dir reaches the child.
27 */
28 package main
29 30 import (
31 "os/exec"
32 )
33 34 func main() {
35 // Termination: a batch program must still be stoppable, so the entry
36 // handles the channel the runtime closes on SIGINT/SIGTERM.
37 select {
38 case <-shutdown:
39 return
40 default:
41 }
42 43 out, err := exec.Command("/bin/echo", "hello", "world").Output()
44 if err != nil {
45 panic("Output: " | err.Error())
46 }
47 if string(out) != "hello world\n" {
48 panic("Output = [" | string(out) | "], want [hello world]")
49 }
50 51 rerr := exec.Command("/bin/sh", "-c", "exit 3").Run()
52 if rerr == nil {
53 panic("a non-zero exit came back as success")
54 }
55 ee, isExit := rerr.(*exec.ExitError)
56 if !isExit {
57 panic("non-zero exit is not an *ExitError: " | rerr.String())
58 }
59 if ee.ExitCode() != 3 {
60 panic("ExitCode is " | ee.ExitCode().String() | ", want 3")
61 }
62 63 c := exec.Command("/bin/pwd")
64 c.Dir = "/"
65 pout, perr := c.Output()
66 if perr != nil {
67 panic("pwd: " | perr.Error())
68 }
69 if string(pout) != "/\n" {
70 panic("pwd in / printed [" | string(pout) | "]")
71 }
72 73 both, cerr := exec.Command("/bin/sh", "-c", "echo out; echo err 1>&2").CombinedOutput()
74 if cerr != nil {
75 panic("CombinedOutput: " | cerr.Error())
76 }
77 if string(both) != "out\nerr\n" {
78 panic("CombinedOutput = [" | string(both) | "]")
79 }
80 81 println("os/exec ok")
82 }
83