Add examples/tests for "pkg/execpipe" (100% coverage!)

This commit is contained in:
Tianon Gravi 2017-04-03 15:02:26 -07:00
parent b7dd34a9b2
commit 818247f8b4
2 changed files with 57 additions and 0 deletions

View File

@ -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
}

View File

@ -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")
}
}