Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![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)

# TSTR: your ultimate testing library!
# tstr: your ultimate testing library

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

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

#### Cmd

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.
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.

##### cmd.WithCommand

`cmd.WithCommand` allows using binaries from

```go
func TestMain(m *testing.M) {
tstr.RunMain(m, tstr.WithDeps(
cmd.New(
cmd.WithCommand("my-app", "--listen", ":8080"),
cmd.WithWaitMatchingLine("Server ready"),
),
))
}
```

##### cmd.WithGoCode

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

Expand Down
26 changes: 20 additions & 6 deletions dep/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ErrBadRegexp = strerr.Error("bad regular expression for matching line")
ErrOutputPipe = strerr.Error("failed to aquire output pipe for command")
ErrBuildFailed = strerr.Error("failed to build go binary")
ErrCreateCoverDir = strerr.Error("failed create coverage dir")
)

type Cmd struct {
Expand All @@ -49,7 +50,7 @@
func (c *Cmd) Start() error {
for _, opt := range c.opts {
if err := opt(c); err != nil {
return fmt.Errorf("failed to apply option: %w", err)
return fmt.Errorf("%w: %w", ErrOptApply, err)
}
}

Expand Down Expand Up @@ -133,20 +134,20 @@
}
}

// WithEnv sets environment variables for the command.
// By default, the command inherits the environment of the current process and setting this option will override it.
func WithEnv(env ...string) Opt {
// WithEnvSet sets environment variables for the command.
// By default the command inherits the environment of the current process and setting this option will override it.
func WithEnvSet(env ...string) Opt {
return func(c *Cmd) error {
c.cmd.Env = env
return nil
}
}

// WithEnvAppend adds environment variables to commands current env.
// By default, the command inherits the environment of the current process and setting this option will override it.
// By default the command inherits the environment of the current process and setting this option will override it.
func WithEnvAppend(env ...string) Opt {
return func(c *Cmd) error {
c.cmd.Env = env
c.cmd.Env = append(c.cmd.Env, env...)
return nil
}
}
Expand Down Expand Up @@ -229,6 +230,18 @@
}

c.cmd = exec.Command(target)
c.cmd.Stdout = os.Stdout
c.cmd.Stderr = os.Stderr
return nil
}
}

func WithGoCoverDir(dir string) Opt {
return func(c *Cmd) error {
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("%w: %w", ErrCreateCoverDir, err)
}

Check warning on line 243 in dep/cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

dep/cmd/cmd.go#L242-L243

Added lines #L242 - L243 were not covered by tests
c.cmd.Env = append(c.cmd.Env, "GOCOVERDIR="+dir)
return nil
}
}
Expand Down Expand Up @@ -259,6 +272,7 @@
return nil, fmt.Errorf("%w: %w", ErrBadRegexp, err)
}

cmd.Stdout = nil
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrOutputPipe, err)
Expand Down
26 changes: 11 additions & 15 deletions dep/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"bytes"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -96,7 +95,16 @@ func TestCmd(t *testing.T) {
name: "WithEnv",
cmd: cmd.New(
cmd.WithCommand("go", "env", "GOPRIVATE"),
cmd.WithEnv("GOPRIVATE=foo"),
cmd.WithEnvSet("GOPRIVATE=foo"),
cmd.WithWaitMatchingLine("foo"),
cmd.WithStopFn(func(c *exec.Cmd) error { return nil }),
),
},
{
name: "WithEnv",
cmd: cmd.New(
cmd.WithCommand("go", "env", "GOPRIVATE"),
cmd.WithEnvAppend("GOPRIVATE=foo"),
cmd.WithWaitMatchingLine("foo"),
cmd.WithStopFn(func(c *exec.Cmd) error { return nil }),
),
Expand Down Expand Up @@ -147,18 +155,6 @@ func TestCmd(t *testing.T) {
),
err: cmd.ErrBadRegexp,
},
{
name: "StdoutPipeErr",
cmd: cmd.New(
cmd.WithExecCmd(func() *exec.Cmd {
c := exec.Command("go", "version")
c.Stdout = bytes.NewBuffer(nil)
return c
}()),
cmd.WithWaitMatchingLine("lol"),
),
err: cmd.ErrOutputPipe,
},
{
name: "WithGoCode_BuildFailure",
cmd: cmd.New(
Expand Down Expand Up @@ -195,7 +191,7 @@ func TestCmd_WithGoCode_Coverage(t *testing.T) {
c := cmd.New(
cmd.WithGoCode(waitPkg, "./"),
cmd.WithWaitMatchingLine("Waiting for signal"),
cmd.WithEnvAppend("GOCOVERDIR="+coverDir),
cmd.WithGoCoverDir(coverDir),
)
deptest.ErrorIs(t, c, nil, nil)

Expand Down