Skip to content

Commit

Permalink
Add Stdout Stderr opiton
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Nov 30, 2022
1 parent 99776b2 commit a71e711
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
4 changes: 4 additions & 0 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type book struct {
beforeFuncs []func(*RunResult) error
afterFuncs []func(*RunResult) error
capturers capturers
stdout io.Writer
stderr io.Writer
}

func LoadBook(path string) (*book, error) {
Expand Down Expand Up @@ -385,6 +387,8 @@ func newBook() *book {
sshRunners: map[string]*sshRunner{},
interval: 0 * time.Second,
runnerErrs: map[string]error{},
stdout: os.Stdout,
stderr: os.Stderr,
}
}

Expand Down
12 changes: 6 additions & 6 deletions book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ func TestLoadBook(t *testing.T) {
t.Setenv("DEBUG", strconv.FormatBool(debug))
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
o, err := LoadBook(tt.path)
bk, err := LoadBook(tt.path)
if err != nil {
t.Fatal(err)
}
if want := debug; o.debug != want {
t.Errorf("got %v\nwant %v", o.debug, want)
if want := debug; bk.debug != want {
t.Errorf("got %v\nwant %v", bk.debug, want)
}
if want := "5"; o.intervalStr != want {
t.Errorf("got %v\nwant %v", o.intervalStr, want)
if want := "5"; bk.intervalStr != want {
t.Errorf("got %v\nwant %v", bk.intervalStr, want)
}
got := o.vars
got := bk.vars
var want map[string]interface{}
if err := json.Unmarshal(tt.varsBytes, &want); err != nil {
panic(err)
Expand Down
7 changes: 4 additions & 3 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//go:build integration
////go:build integration

package runn

import (
"context"
"io"
"os"
"strconv"
"testing"
Expand Down Expand Up @@ -46,11 +47,11 @@ func TestRunUsingHTTPBin(t *testing.T) {
for _, tt := range tests {
t.Run(tt.book, func(t *testing.T) {
ctx := context.Background()
f, err := New(Book(tt.book))
o, err := New(Book(tt.book), Stdout(io.Discard), Stderr(io.Discard))
if err != nil {
t.Fatal(err)
}
if err := f.Run(ctx); err != nil {
if err := o.Run(ctx); err != nil {
t.Error(err)
}
})
Expand Down
4 changes: 2 additions & 2 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func New(opts ...Option) (*operator, error) {
included: bk.included,
ifCond: bk.ifCond,
skipTest: bk.skipTest,
stdout: os.Stdout,
stderr: os.Stderr,
stdout: bk.stdout,
stderr: bk.stderr,
bookPath: bk.path,
beforeFuncs: bk.beforeFuncs,
afterFuncs: bk.afterFuncs,
Expand Down
3 changes: 1 addition & 2 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ func TestDump(t *testing.T) {
}
ctx := context.Background()
for _, tt := range tests {
o, err := New(Book(tt.book), Func("upcase", strings.ToUpper))
o.stdout = io.Discard
o, err := New(Book(tt.book), Func("upcase", strings.ToUpper), Stdout(io.Discard), Stderr(io.Discard))
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 19 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -188,6 +189,8 @@ func Underlay(path string) Option {
if bk.intervalStr == "" {
bk.interval = loaded.interval
}
bk.stdout = loaded.stdout
bk.stderr = loaded.stderr
return nil
}
}
Expand Down Expand Up @@ -693,6 +696,22 @@ func RunRandom(n int) Option {
}
}

// Stdout - Set STDOUT
func Stdout(w io.Writer) Option {
return func(bk *book) error {
bk.stdout = w
return nil
}
}

// Stderr - Set STDERR
func Stderr(w io.Writer) Option {
return func(bk *book) error {
bk.stderr = w
return nil
}
}

// setupBuiltinFunctions - Set up built-in functions to runner
func setupBuiltinFunctions(opts ...Option) []Option {
// Built-in functions are added at the beginning of an option and are overridden by subsequent options
Expand Down
4 changes: 2 additions & 2 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestOptionOverlay(t *testing.T) {
}
opts := []cmp.Option{
cmp.AllowUnexported(book{}, httpRunner{}, dbRunner{}),
cmpopts.IgnoreFields(book{}, "funcs"),
cmpopts.IgnoreFields(book{}, "funcs", "stdout", "stderr"),
cmpopts.IgnoreFields(httpRunner{}, "endpoint", "client", "validator"),
cmpopts.IgnoreFields(dbRunner{}, "client"),
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestOptionUnderlay(t *testing.T) {
}
opts := []cmp.Option{
cmp.AllowUnexported(book{}, httpRunner{}, dbRunner{}),
cmpopts.IgnoreFields(book{}, "funcs"),
cmpopts.IgnoreFields(book{}, "funcs", "stdout", "stderr"),
cmpopts.IgnoreFields(httpRunner{}, "endpoint", "client", "validator"),
cmpopts.IgnoreFields(dbRunner{}, "client"),
}
Expand Down

0 comments on commit a71e711

Please sign in to comment.