Skip to content

Commit 818247f

Browse files
committed
Add examples/tests for "pkg/execpipe" (100% coverage!)
1 parent b7dd34a commit 818247f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pkg/execpipe/execpipe_example_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package execpipe_test
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"strings"
8+
9+
"github.com/docker-library/go-dockerlibrary/pkg/execpipe"
10+
)
11+
12+
func Example() {
13+
pipe, err := execpipe.RunCommand("go", "version")
14+
if err != nil {
15+
panic(err)
16+
}
17+
defer pipe.Close()
18+
19+
var buf bytes.Buffer
20+
io.Copy(&buf, pipe)
21+
22+
fmt.Println(strings.SplitN(buf.String(), " version ", 2)[0])
23+
24+
// Output:
25+
// go
26+
}

pkg/execpipe/execpipe_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package execpipe_test
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/docker-library/go-dockerlibrary/pkg/execpipe"
9+
)
10+
11+
func TestStdoutPipeError(t *testing.T) {
12+
cmd := exec.Command("nothing", "really", "matters", "in", "the", "end")
13+
14+
// set "Stdout" so that "cmd.StdoutPipe" fails
15+
// https://golang.org/src/os/exec/exec.go?s=16834:16883#L587
16+
cmd.Stdout = os.Stdout
17+
18+
_, err := execpipe.Run(cmd)
19+
if err == nil {
20+
t.Errorf("Expected execpipe.Run to fail -- it did not")
21+
}
22+
}
23+
24+
func TestStartError(t *testing.T) {
25+
// craft a definitely-invalid command so that "cmd.Start" fails
26+
// https://golang.org/src/os/exec/exec.go?s=8739:8766#L303
27+
_, err := execpipe.RunCommand("nothing-really-matters-in-the-end--bogus-command")
28+
if err == nil {
29+
t.Errorf("Expected execpipe.RunCommand to fail -- it did not")
30+
}
31+
}

0 commit comments

Comments
 (0)