Skip to content

Commit b094a05

Browse files
committed
Set stdout and err for cmd
1 parent d4e729d commit b094a05

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![Go Reference](https://pkg.go.dev/badge/github.com/go-tstr/tstr.svg)](https://pkg.go.dev/github.com/go-tstr/tstr) [![codecov](https://codecov.io/github/go-tstr/tstr/graph/badge.svg?token=H3u7Ui9PfC)](https://codecov.io/github/go-tstr/tstr) ![main](https://github.com/go-tstr/tstr/actions/workflows/go.yaml/badge.svg?branch=main)
22

3-
# TSTR: your ultimate testing library!
3+
# tstr: your ultimate testing library
44

55
tstr is testing library allows you to write integration and black-box tests like normal unit tests in Go.
66

@@ -151,7 +151,24 @@ func TestMain(m *testing.M) {
151151

152152
#### Cmd
153153

154-
Cmd dependecy is the most versatile one. It can be used for running any binary or even compiling a Go application and running it as dependency.
154+
Cmd dependecy is the most versatile dependency. It can be used for running any binary or even compiling a Go application and running it as dependency.
155+
156+
##### cmd.WithCommand
157+
158+
`cmd.WithCommand` allows using binaries from
159+
160+
```go
161+
func TestMain(m *testing.M) {
162+
tstr.RunMain(m, tstr.WithDeps(
163+
cmd.New(
164+
cmd.WithCommand("my-app", "--listen", ":8080"),
165+
cmd.WithWaitMatchingLine("Server ready"),
166+
),
167+
))
168+
}
169+
```
170+
171+
##### cmd.WithGoCode
155172

156173
This example compiles `my-app` Go application, instruments it for coverage collections, waits for it to be ready and finally starts running tests.
157174

dep/cmd/cmd.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
ErrBadRegexp = strerr.Error("bad regular expression for matching line")
2626
ErrOutputPipe = strerr.Error("failed to aquire output pipe for command")
2727
ErrBuildFailed = strerr.Error("failed to build go binary")
28+
ErrCreateCoverDir = strerr.Error("failed create coverage dir")
2829
)
2930

3031
type Cmd struct {
@@ -49,7 +50,7 @@ func New(opts ...Opt) *Cmd {
4950
func (c *Cmd) Start() error {
5051
for _, opt := range c.opts {
5152
if err := opt(c); err != nil {
52-
return fmt.Errorf("failed to apply option: %w", err)
53+
return fmt.Errorf("%w: %w", ErrOptApply, err)
5354
}
5455
}
5556

@@ -133,20 +134,20 @@ func WithStopFn(fn func(*exec.Cmd) error) Opt {
133134
}
134135
}
135136

136-
// WithEnv sets environment variables for the command.
137-
// By default, the command inherits the environment of the current process and setting this option will override it.
138-
func WithEnv(env ...string) Opt {
137+
// WithEnvSet sets environment variables for the command.
138+
// By default the command inherits the environment of the current process and setting this option will override it.
139+
func WithEnvSet(env ...string) Opt {
139140
return func(c *Cmd) error {
140141
c.cmd.Env = env
141142
return nil
142143
}
143144
}
144145

145146
// WithEnvAppend adds environment variables to commands current env.
146-
// By default, the command inherits the environment of the current process and setting this option will override it.
147+
// By default the command inherits the environment of the current process and setting this option will override it.
147148
func WithEnvAppend(env ...string) Opt {
148149
return func(c *Cmd) error {
149-
c.cmd.Env = env
150+
c.cmd.Env = append(c.cmd.Env, env...)
150151
return nil
151152
}
152153
}
@@ -229,6 +230,18 @@ func WithGoCode(modulePath, mainPkg string) Opt {
229230
}
230231

231232
c.cmd = exec.Command(target)
233+
c.cmd.Stdout = os.Stdout
234+
c.cmd.Stderr = os.Stderr
235+
return nil
236+
}
237+
}
238+
239+
func WithGoCoverDir(dir string) Opt {
240+
return func(c *Cmd) error {
241+
if err := os.MkdirAll(dir, 0o755); err != nil {
242+
return fmt.Errorf("%w: %w", ErrCreateCoverDir, err)
243+
}
244+
c.cmd.Env = append(c.cmd.Env, "GOCOVERDIR="+dir)
232245
return nil
233246
}
234247
}
@@ -259,6 +272,7 @@ func MatchingLine(exp string, cmd *exec.Cmd) (func(*exec.Cmd) error, error) {
259272
return nil, fmt.Errorf("%w: %w", ErrBadRegexp, err)
260273
}
261274

275+
cmd.Stdout = nil
262276
stdout, err := cmd.StdoutPipe()
263277
if err != nil {
264278
return nil, fmt.Errorf("%w: %w", ErrOutputPipe, err)

dep/cmd/cmd_test.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd_test
22

33
import (
4-
"bytes"
54
"net/http"
65
"net/http/httptest"
76
"os"
@@ -96,7 +95,16 @@ func TestCmd(t *testing.T) {
9695
name: "WithEnv",
9796
cmd: cmd.New(
9897
cmd.WithCommand("go", "env", "GOPRIVATE"),
99-
cmd.WithEnv("GOPRIVATE=foo"),
98+
cmd.WithEnvSet("GOPRIVATE=foo"),
99+
cmd.WithWaitMatchingLine("foo"),
100+
cmd.WithStopFn(func(c *exec.Cmd) error { return nil }),
101+
),
102+
},
103+
{
104+
name: "WithEnv",
105+
cmd: cmd.New(
106+
cmd.WithCommand("go", "env", "GOPRIVATE"),
107+
cmd.WithEnvAppend("GOPRIVATE=foo"),
100108
cmd.WithWaitMatchingLine("foo"),
101109
cmd.WithStopFn(func(c *exec.Cmd) error { return nil }),
102110
),
@@ -147,18 +155,6 @@ func TestCmd(t *testing.T) {
147155
),
148156
err: cmd.ErrBadRegexp,
149157
},
150-
{
151-
name: "StdoutPipeErr",
152-
cmd: cmd.New(
153-
cmd.WithExecCmd(func() *exec.Cmd {
154-
c := exec.Command("go", "version")
155-
c.Stdout = bytes.NewBuffer(nil)
156-
return c
157-
}()),
158-
cmd.WithWaitMatchingLine("lol"),
159-
),
160-
err: cmd.ErrOutputPipe,
161-
},
162158
{
163159
name: "WithGoCode_BuildFailure",
164160
cmd: cmd.New(
@@ -195,7 +191,7 @@ func TestCmd_WithGoCode_Coverage(t *testing.T) {
195191
c := cmd.New(
196192
cmd.WithGoCode(waitPkg, "./"),
197193
cmd.WithWaitMatchingLine("Waiting for signal"),
198-
cmd.WithEnvAppend("GOCOVERDIR="+coverDir),
194+
cmd.WithGoCoverDir(coverDir),
199195
)
200196
deptest.ErrorIs(t, c, nil, nil)
201197

0 commit comments

Comments
 (0)