2009-02-15 20:35:52 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:27:16 -07:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2009-02-15 20:35:52 -07:00
|
|
|
)
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
func TestRunCat(t *testing.T) {
|
2010-02-18 19:32:33 -07:00
|
|
|
cmd, err := Run("/bin/cat", []string{"cat"}, nil, "",
|
2009-12-15 16:27:16 -07:00
|
|
|
Pipe, Pipe, DevNull)
|
2009-02-15 20:35:52 -07:00
|
|
|
if err != nil {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatal("run:", err)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
io.WriteString(cmd.Stdin, "hello, world\n")
|
|
|
|
cmd.Stdin.Close()
|
|
|
|
buf, err := ioutil.ReadAll(cmd.Stdout)
|
2009-06-22 14:26:13 -06:00
|
|
|
if err != nil {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatal("read:", err)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if string(buf) != "hello, world\n" {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatalf("read: got %q", buf)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if err = cmd.Close(); err != nil {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatal("close:", err)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
func TestRunEcho(t *testing.T) {
|
2010-02-18 19:32:33 -07:00
|
|
|
cmd, err := Run("/bin/echo", []string{"echo", "hello", "world"}, nil, "",
|
2009-12-15 16:27:16 -07:00
|
|
|
DevNull, Pipe, DevNull)
|
2009-02-15 20:35:52 -07:00
|
|
|
if err != nil {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatal("run:", err)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
buf, err := ioutil.ReadAll(cmd.Stdout)
|
2009-06-22 14:26:13 -06:00
|
|
|
if err != nil {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatal("read:", err)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if string(buf) != "hello world\n" {
|
2010-02-18 19:32:33 -07:00
|
|
|
t.Fatalf("read: got %q", buf)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if err = cmd.Close(); err != nil {
|
2010-02-18 19:32:33 -07:00
|
|
|
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)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
}
|