diff --git a/pkg/execpipe/execpipe_example_test.go b/pkg/execpipe/execpipe_example_test.go new file mode 100644 index 0000000..dd45783 --- /dev/null +++ b/pkg/execpipe/execpipe_example_test.go @@ -0,0 +1,26 @@ +package execpipe_test + +import ( + "bytes" + "fmt" + "io" + "strings" + + "github.com/docker-library/go-dockerlibrary/pkg/execpipe" +) + +func Example() { + pipe, err := execpipe.RunCommand("go", "version") + if err != nil { + panic(err) + } + defer pipe.Close() + + var buf bytes.Buffer + io.Copy(&buf, pipe) + + fmt.Println(strings.SplitN(buf.String(), " version ", 2)[0]) + + // Output: + // go +} diff --git a/pkg/execpipe/execpipe_test.go b/pkg/execpipe/execpipe_test.go new file mode 100644 index 0000000..b8452ac --- /dev/null +++ b/pkg/execpipe/execpipe_test.go @@ -0,0 +1,31 @@ +package execpipe_test + +import ( + "os" + "os/exec" + "testing" + + "github.com/docker-library/go-dockerlibrary/pkg/execpipe" +) + +func TestStdoutPipeError(t *testing.T) { + cmd := exec.Command("nothing", "really", "matters", "in", "the", "end") + + // set "Stdout" so that "cmd.StdoutPipe" fails + // https://golang.org/src/os/exec/exec.go?s=16834:16883#L587 + cmd.Stdout = os.Stdout + + _, err := execpipe.Run(cmd) + if err == nil { + t.Errorf("Expected execpipe.Run to fail -- it did not") + } +} + +func TestStartError(t *testing.T) { + // craft a definitely-invalid command so that "cmd.Start" fails + // https://golang.org/src/os/exec/exec.go?s=8739:8766#L303 + _, err := execpipe.RunCommand("nothing-really-matters-in-the-end--bogus-command") + if err == nil { + t.Errorf("Expected execpipe.RunCommand to fail -- it did not") + } +}