Skip to content

Commit

Permalink
add context.Context arg to assertions.OK()
Browse files Browse the repository at this point in the history
**BREAKING API CHANGE for plugin developers**

This changes the `gdt.Assertions.OK()` interface signature to accept a
`context.Context` object. This context object is required for debug
output/inspection in the kube plugin.

Signed-off-by: Jay Pipes <[email protected]>
  • Loading branch information
jaypipes committed May 27, 2024
1 parent 50d0e7f commit 3205888
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
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
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
2 changes: 1 addition & 1 deletion 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 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

0 comments on commit 3205888

Please sign in to comment.