Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix debug handler and change assertions.OK() #18

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ tests:
headers:
- Location
- name: look up that created book
GET: $LOCATION
GET: $$LOCATION
response:
status: 200
json:
Expand Down
3 changes: 2 additions & 1 deletion assertion/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package json

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -170,7 +171,7 @@ func (a *assertions) Failures() []error {
}

// OK returns true if all contained assertions pass successfully
func (a *assertions) OK() bool {
func (a *assertions) OK(ctx context.Context) bool {
if a == nil || a.exp == nil {
return true
}
Expand Down
28 changes: 18 additions & 10 deletions assertion/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package json_test

import (
"context"
"io/ioutil"
"path/filepath"
"testing"
Expand Down Expand Up @@ -98,6 +99,7 @@ func content() []byte {
func TestLength(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := content()
expLen := len(c)

Expand All @@ -106,12 +108,12 @@ func TestLength(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.True(a.OK())
require.True(a.OK(ctx))
require.Empty(a.Failures())

expLen = 0
a = gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdterrors.ErrNotEqual)
Expand All @@ -120,6 +122,7 @@ func TestLength(t *testing.T) {
func TestJSONUnmarshalError(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := []byte(`not { value } json`)

exp := gdtjson.Expect{
Expand All @@ -129,7 +132,7 @@ func TestJSONUnmarshalError(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONUnmarshalError)
Expand All @@ -138,6 +141,7 @@ func TestJSONUnmarshalError(t *testing.T) {
func TestJSONPathError(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := content()

exp := gdtjson.Expect{
Expand All @@ -147,7 +151,7 @@ func TestJSONPathError(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONPathNotFound)
Expand All @@ -156,6 +160,7 @@ func TestJSONPathError(t *testing.T) {
func TestJSONPathConversionError(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := content()

exp := gdtjson.Expect{
Expand All @@ -165,7 +170,7 @@ func TestJSONPathConversionError(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONPathConversionError)
Expand All @@ -174,6 +179,7 @@ func TestJSONPathConversionError(t *testing.T) {
func TestJSONPathNotEqual(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := content()

exp := gdtjson.Expect{
Expand All @@ -183,7 +189,7 @@ func TestJSONPathNotEqual(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.True(a.OK())
require.True(a.OK(ctx))
require.Empty(a.Failures())

exp = gdtjson.Expect{
Expand All @@ -193,7 +199,7 @@ func TestJSONPathNotEqual(t *testing.T) {
}

a = gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONPathNotEqual)
Expand All @@ -202,6 +208,7 @@ func TestJSONPathNotEqual(t *testing.T) {
func TestJSONPathFormatNotFound(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := content()

exp := gdtjson.Expect{
Expand All @@ -211,7 +218,7 @@ func TestJSONPathFormatNotFound(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONPathNotFound)
Expand All @@ -220,6 +227,7 @@ func TestJSONPathFormatNotFound(t *testing.T) {
func TestJSONPathFormatNotEqual(t *testing.T) {
require := require.New(t)

ctx := context.TODO()
c := content()

exp := gdtjson.Expect{
Expand All @@ -229,7 +237,7 @@ func TestJSONPathFormatNotEqual(t *testing.T) {
}

a := gdtjson.New(&exp, c)
require.True(a.OK())
require.True(a.OK(ctx))
require.Empty(a.Failures())

exp = gdtjson.Expect{
Expand All @@ -239,7 +247,7 @@ func TestJSONPathFormatNotEqual(t *testing.T) {
}

a = gdtjson.New(&exp, c)
require.False(a.OK())
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONFormatNotEqual)
Expand Down
10 changes: 2 additions & 8 deletions debug/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ func Printf(
) {
t.Helper()
writers := gdtcontext.Debug(ctx)
if writers == nil {
return
}
t.Logf(format, args...)
if len(writers) == 0 {
return
}
t.Logf(format, args...)

if !strings.HasPrefix(format, "[gdt] ") {
format = "[gdt] " + t.Name() + " " + format
Expand All @@ -50,14 +47,11 @@ func Println(
) {
t.Helper()
writers := gdtcontext.Debug(ctx)
if writers == nil {
if len(writers) == 0 {
return
}
// NOTE(jaypipes): T.Logf() automatically adds newlines...
t.Logf(format, args...)
if len(writers) == 0 {
return
}

if !strings.HasPrefix(format, "[gdt] ") {
format = "[gdt] " + t.Name() + " " + format
Expand Down
6 changes: 6 additions & 0 deletions plugin/exec/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ func (a *Action) Do(
}
if outbuf != nil {
outbuf.ReadFrom(outpipe)
if outbuf.Len() > 0 {
debug.Println(ctx, t, "exec: stdout: %s", outbuf.String())
}
}
if errbuf != nil {
errbuf.ReadFrom(errpipe)
if errbuf.Len() > 0 {
debug.Println(ctx, t, "exec: stderr: %s", errbuf.String())
}
}

err = cmd.Wait()
Expand Down
9 changes: 5 additions & 4 deletions plugin/exec/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package exec

import (
"bytes"
"context"
"strings"

"github.com/gdt-dev/gdt/errors"
Expand Down Expand Up @@ -76,7 +77,7 @@ func (a *pipeAssertions) Terminal() bool {

// OK checks all the assertions in the pipeAssertions against the supplied pipe
// contents and returns true if all assertions pass.
func (a *pipeAssertions) OK() bool {
func (a *pipeAssertions) OK(ctx context.Context) bool {
if a == nil || a.pipe == nil {
return true
}
Expand Down Expand Up @@ -167,17 +168,17 @@ func (a *assertions) Terminal() bool {

// OK checks all the assertions against the supplied arguments and returns true
// if all assertions pass.
func (a *assertions) OK() bool {
func (a *assertions) OK(ctx context.Context) bool {
res := true
if a.expExitCode != a.exitCode {
a.Fail(errors.NotEqual(a.expExitCode, a.exitCode))
res = false
}
if !a.expOutPipe.OK() {
if !a.expOutPipe.OK(ctx) {
a.failures = append(a.failures, a.expOutPipe.Failures()...)
res = false
}
if !a.expErrPipe.OK() {
if !a.expErrPipe.OK(ctx) {
a.failures = append(a.failures, a.expErrPipe.Failures()...)
res = false
}
Expand Down
12 changes: 1 addition & 11 deletions plugin/exec/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *Spec) Eval(ctx context.Context, t *testing.T) *result.Result {
return result.New(result.WithRuntimeError(ExecRuntimeError(err)))
}
a := newAssertions(s.Assert, ec, outbuf, errbuf)
if !a.OK() {
if !a.OK(ctx) {
for _, fail := range a.Failures() {
t.Error(fail)
}
Expand All @@ -42,16 +42,6 @@ func (s *Spec) Eval(ctx context.Context, t *testing.T) *result.Result {
if err != nil {
debug.Println(ctx, t, "error in on.fail.exec: %s", err)
}
if outbuf.Len() > 0 {
debug.Println(
ctx, t, "on.fail.exec: stdout: %s", outbuf.String(),
)
}
if errbuf.Len() > 0 {
debug.Println(
ctx, t, "on.fail.exec: stderr: %s", errbuf.String(),
)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions plugin/exec/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ func TestDebugWriter(t *testing.T) {
require.NotEqual(b.Len(), 0)
debugout := b.String()
require.Contains(debugout, "exec: echo [cat]")
require.Contains(debugout, "exec: stdout: cat")
require.Contains(debugout, "exec: sh [-c echo cat 1>&2]")
require.Contains(debugout, "exec: stderr: cat")
}

func TestWait(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions scenario/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package scenario_test

import (
"bufio"
"bytes"
"context"
"os"
"path/filepath"
Expand Down Expand Up @@ -72,16 +74,21 @@ func TestDebugFlushing(t *testing.T) {
f, err := os.Open(fp)
require.Nil(err)

ctx := gdtcontext.New(
gdtcontext.WithDebug(),
)
var b bytes.Buffer
w := bufio.NewWriter(&b)
ctx := gdtcontext.New(gdtcontext.WithDebug(w))

s, err := scenario.FromReader(f, scenario.WithPath(fp))
require.Nil(err)
require.NotNil(s)

err = s.Run(ctx, t)
require.Nil(err)
require.False(t.Failed())
w.Flush()
require.NotEqual(b.Len(), 0)
debugout := b.String()
require.Contains(debugout, "TestDebugFlushing/foo-debug-wait-flush wait: 250ms before")
}

func TestTimeoutCascade(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion types/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

package types

import "context"

// Assertions track zero or more assertions about some result
type Assertions interface {
// OK returns true if all contained assertions pass successfully, false
// otherwise. If false is returned, Failures() is guaranteed to be
// non-empty.
OK() bool
OK(context.Context) bool
// Fail appends a supplied error to the set of failed assertions
Fail(error)
// Failures returns a slice of failure messages indicating which assertions
Expand Down
Loading