diff options
| author | Russ Cox <rsc@golang.org> | 2010-02-18 18:32:33 -0800 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2010-02-18 18:32:33 -0800 |
| commit | c2dea2196c336ba195ee713fd0020031d473500e (patch) | |
| tree | 02cd76934724a16f9db94e8691bace5eb4040095 /src/pkg/exec | |
| parent | 4589c3458033d8797c34dedc5f72f534487d2cd6 (diff) | |
| download | go-c2dea2196c336ba195ee713fd0020031d473500e.tar.xz | |
exec: add dir argument to Run.
fix, test MergeWithStdout
R=r
CC=golang-dev
https://golang.org/cl/214046
Diffstat (limited to 'src/pkg/exec')
| -rw-r--r-- | src/pkg/exec/exec.go | 6 | ||||
| -rw-r--r-- | src/pkg/exec/exec_test.go | 57 |
2 files changed, 50 insertions, 13 deletions
diff --git a/src/pkg/exec/exec.go b/src/pkg/exec/exec.go index 8e959e03a3..a1b7bd6b9c 100644 --- a/src/pkg/exec/exec.go +++ b/src/pkg/exec/exec.go @@ -78,7 +78,7 @@ func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) { // If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr) // of the returned Cmd is the other end of the pipe. // Otherwise the field in Cmd is nil. -func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd, err os.Error) { +func Run(argv0 string, argv, envv []string, dir string, stdin, stdout, stderr int) (p *Cmd, err os.Error) { p = new(Cmd) var fd [3]*os.File @@ -89,13 +89,13 @@ func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd, goto Error } if stderr == MergeWithStdout { - p.Stderr = p.Stdout + fd[2] = fd[1] } else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil { goto Error } // Run command. - p.Pid, err = os.ForkExec(argv0, argv, envv, "", &fd) + p.Pid, err = os.ForkExec(argv0, argv, envv, dir, &fd) if err != nil { goto Error } diff --git a/src/pkg/exec/exec_test.go b/src/pkg/exec/exec_test.go index 9c4d2ee319..3e4ab7d780 100644 --- a/src/pkg/exec/exec_test.go +++ b/src/pkg/exec/exec_test.go @@ -11,39 +11,76 @@ import ( ) func TestRunCat(t *testing.T) { - cmd, err := Run("/bin/cat", []string{"cat"}, nil, + cmd, err := Run("/bin/cat", []string{"cat"}, nil, "", Pipe, Pipe, DevNull) if err != nil { - t.Fatalf("opencmd /bin/cat: %v", err) + t.Fatal("run:", err) } io.WriteString(cmd.Stdin, "hello, world\n") cmd.Stdin.Close() buf, err := ioutil.ReadAll(cmd.Stdout) if err != nil { - t.Fatalf("reading from /bin/cat: %v", err) + t.Fatal("read:", err) } if string(buf) != "hello, world\n" { - t.Fatalf("reading from /bin/cat: got %q", buf) + t.Fatalf("read: got %q", buf) } if err = cmd.Close(); err != nil { - t.Fatalf("closing /bin/cat: %v", err) + t.Fatal("close:", err) } } func TestRunEcho(t *testing.T) { - cmd, err := Run("/bin/echo", []string{"echo", "hello", "world"}, nil, + cmd, err := Run("/bin/echo", []string{"echo", "hello", "world"}, nil, "", DevNull, Pipe, DevNull) if err != nil { - t.Fatalf("opencmd /bin/echo: %v", err) + t.Fatal("run:", err) } buf, err := ioutil.ReadAll(cmd.Stdout) if err != nil { - t.Fatalf("reading from /bin/echo: %v", err) + t.Fatal("read:", err) } if string(buf) != "hello world\n" { - t.Fatalf("reading from /bin/echo: got %q", buf) + t.Fatalf("read: got %q", buf) } if err = cmd.Close(); err != nil { - t.Fatalf("closing /bin/echo: %v", err) + t.Fatal("close:", err) + } +} + +func TestStderr(t *testing.T) { + cmd, err := Run("/bin/sh", []string{"sh", "-c", "echo hello world 1>&2"}, nil, "", + DevNull, DevNull, Pipe) + if err != nil { + t.Fatal("run:", err) + } + buf, err := ioutil.ReadAll(cmd.Stderr) + if err != nil { + t.Fatal("read:", err) + } + if string(buf) != "hello world\n" { + t.Fatalf("read: got %q", buf) + } + if err = cmd.Close(); err != nil { + t.Fatal("close:", err) + } +} + + +func TestMergeWithStdout(t *testing.T) { + cmd, err := Run("/bin/sh", []string{"sh", "-c", "echo hello world 1>&2"}, nil, "", + DevNull, Pipe, MergeWithStdout) + if err != nil { + t.Fatal("run:", err) + } + buf, err := ioutil.ReadAll(cmd.Stdout) + if err != nil { + t.Fatal("read:", err) + } + if string(buf) != "hello world\n" { + t.Fatalf("read: got %q", buf) + } + if err = cmd.Close(); err != nil { + t.Fatal("close:", err) } } |
