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
3 changes: 3 additions & 0 deletions assertion/json/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ var (
// JSONPathNotFound returns an ErrFailure when a JSONPath expression could not
// evaluate to a found element.
func JSONPathNotFound(path string, err error) error {
if err == nil {
return fmt.Errorf("%w: %s", ErrJSONPathNotFound, path)
}
return fmt.Errorf("%w: %s: %s", ErrJSONPathNotFound, path, err)
}

Expand Down
19 changes: 16 additions & 3 deletions assertion/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strconv"
"strings"

"github.com/PaesslerAG/jsonpath"
"github.com/theory/jsonpath"
gjs "github.com/xeipuuv/gojsonschema"

"github.com/gdt-dev/core/api"
Expand Down Expand Up @@ -119,13 +119,19 @@ func (a *assertions) pathsOK() bool {
return false
}
for path, expVal := range a.exp.Paths {
got, err := jsonpath.Get(path, v)
p, err := jsonpath.Parse(path)
if err != nil {
// Not terminal because during parse we validate the JSONPath
// expression is valid.
a.Fail(JSONPathNotFound(path, err))
return false
}
nodes := p.Select(v)
if len(nodes) == 0 {
a.Fail(JSONPathNotFound(path, err))
return false
}
got := nodes[0]
switch got := got.(type) {
case string:
if expVal != got {
Expand Down Expand Up @@ -166,6 +172,7 @@ func (a *assertions) pathsOK() bool {
a.Fail(JSONPathConversionError(path, expVal, got))
return false
}

}
return true
}
Expand All @@ -185,13 +192,19 @@ func (a *assertions) pathFormatsOK() bool {
return false
}
for path, format := range a.exp.PathFormats {
got, err := jsonpath.Get(path, v)
p, err := jsonpath.Parse(path)
if err != nil {
// Not terminal because during parse we validate the JSONPath
// expression is valid.
a.Fail(JSONPathNotFound(path, err))
return false
}
nodes := p.Select(v)
if len(nodes) == 0 {
a.Fail(JSONPathNotFound(path, err))
return false
}
got := nodes[0]
ok, err := isFormatted(format, got)
if err != nil {
a.Fail(JSONFormatError(format, err))
Expand Down
33 changes: 31 additions & 2 deletions assertion/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ func TestJSONPathConversionError(t *testing.T) {

exp := gdtjson.Expect{
Paths: map[string]string{
"1234": "foo",
"$.1234": "foo",
},
}

a := gdtjson.New(&exp, c)
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONPathConversionError)
require.Error(failures[0], &parse.Error{})
}

func TestJSONPathNotEqual(t *testing.T) {
Expand Down Expand Up @@ -253,3 +253,32 @@ func TestJSONPathFormatNotEqual(t *testing.T) {
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONFormatNotEqual)
}

func TestJSONPathKeyWithDot(t *testing.T) {
require := require.New(t)

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

exp := gdtjson.Expect{
Paths: map[string]string{
"$[0].publisher.address['postal.code']": "10010",
},
}

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

exp = gdtjson.Expect{
Paths: map[string]string{
"$[0].publisher.address['postal.code']": "42",
},
}

a = gdtjson.New(&exp, c)
require.False(a.OK(ctx))
failures := a.Failures()
require.Len(failures, 1)
require.ErrorIs(failures[0], gdtjson.ErrJSONPathNotEqual)
}
12 changes: 3 additions & 9 deletions assertion/json/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ import (
"runtime"
"strings"

"github.com/PaesslerAG/jsonpath"
"github.com/theory/jsonpath"
"gopkg.in/yaml.v3"

"github.com/gdt-dev/core/parse"
)

var (
// defining the JSONPath language here allows us to disaggregate parse
// errors from runtime errors when evaluating a JSONPath expression.
lang = jsonpath.Language()
)

// UnsupportedJSONSchemaReference returns ErrUnsupportedJSONSchemaReference for
// a supplied URL.
func UnsupportedJSONSchemaReference(url string, node *yaml.Node) error {
Expand Down Expand Up @@ -143,7 +137,7 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error {
if len(path) == 0 || path[0] != '$' {
return JSONPathInvalidNoRoot(path, valNode)
}
if _, err := lang.NewEvaluable(path); err != nil {
if _, err := jsonpath.Parse(path); err != nil {
return JSONPathInvalid(path, err, valNode)
}
}
Expand All @@ -160,7 +154,7 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error {
if len(pathFormat) == 0 || pathFormat[0] != '$' {
return JSONPathInvalidNoRoot(pathFormat, valNode)
}
if _, err := lang.NewEvaluable(pathFormat); err != nil {
if _, err := jsonpath.Parse(pathFormat); err != nil {
return JSONPathInvalid(pathFormat, err, valNode)
}
}
Expand Down
4 changes: 2 additions & 2 deletions assertion/json/testdata/books.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"address": "153–157 Fifth Avenue",
"city": "New York City",
"state": "NY",
"postal_code": "10010",
"country_code": "US"
"postal.code": "10010",
"country.code": "US"
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions fixture/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"io"
"strconv"

"github.com/PaesslerAG/jsonpath"
"github.com/theory/jsonpath"

"github.com/gdt-dev/core/api"
)
Expand All @@ -29,14 +29,12 @@ func (f *jsonFixture) HasState(path string) bool {
if f.data == nil {
return false
}
got, err := jsonpath.Get(path, f.data)
p, err := jsonpath.Parse(path)
if err != nil {
return false
}
if got == nil {
return false
}
return true
nodes := p.Select(f.data)
return len(nodes) == 1
}

// GetState returns the value at supplied JSONPath expression or nil if the
Expand All @@ -45,10 +43,15 @@ func (f *jsonFixture) State(path string) interface{} {
if f.data == nil {
return nil
}
got, err := jsonpath.Get(path, f.data)
p, err := jsonpath.Parse(path)
if err != nil {
return nil
}
nodes := p.Select(f.data)
if len(nodes) == 0 {
return nil
}
got := nodes[0]
switch got := got.(type) {
case string:
return got
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module github.com/gdt-dev/core
go 1.24.3

require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/samber/lo v1.51.0
github.com/stretchr/testify v1.11.1
github.com/theory/jsonpath v0.10.1
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand Down
17 changes: 11 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/samber/lo v1.51.0 h1:kysRYLbHy/MB7kQZf5DSN50JHmMsNEdeY24VzJFu7wI=
github.com/samber/lo v1.51.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/theory/jsonpath v0.10.1 h1:Qa3alEtTTLIy2s60U2XzamS0XgQmF9zWIg42mEkSRVg=
github.com/theory/jsonpath v0.10.1/go.mod h1:ZOz+y6MxTEDcN/FOxf9AOgeHSoKHx2B+E0nD3HOtzGE=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
Expand All @@ -28,7 +32,8 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion parse/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (e *Error) SetContents() {
}
_, _ = fmt.Fprintf(b, "%03d: %s\n", x, line)
if x == e.Line {
_, _ = fmt.Fprintf(b, " %s^", strings.Repeat(" ", e.Column))
_, _ = fmt.Fprintf(b, " %s^\n", strings.Repeat(" ", e.Column))
}
}
if err := sc.Err(); err != nil {
Expand Down
21 changes: 20 additions & 1 deletion plugin/exec/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,28 @@ func (a *Action) Do(
args = []string{"-c", a.Exec}
}

origTarget := target
target = gdtcontext.ReplaceVariables(ctx, target)
if target != origTarget {
if origTarget != target {
debug.Printf(
ctx,
"exec: replaced target: %s -> %s",
origTarget, target,
)
}
}
args = lo.Map(args, func(arg string, _ int) string {
return gdtcontext.ReplaceVariables(ctx, arg)
origArg := arg
arg = gdtcontext.ReplaceVariables(ctx, arg)
if origArg != arg {
debug.Printf(
ctx,
"exec: replaced arg: %s -> %s",
origArg, arg,
)
}
return arg
})

debug.Printf(ctx, "exec: %s %s", target, args)
Expand Down
34 changes: 31 additions & 3 deletions plugin/exec/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/gdt-dev/core/api"
gdtcontext "github.com/gdt-dev/core/context"
"github.com/gdt-dev/core/debug"
)

// Expect contains the assertions about an Exec Spec's actions
Expand Down Expand Up @@ -76,7 +77,16 @@ func (a *pipeAssertions) OK(ctx context.Context) bool {
if a.ContainsAll != nil {
vals := a.ContainsAll.Values()
vals = lo.Map(vals, func(val string, _ int) string {
return gdtcontext.ReplaceVariables(ctx, val)
origVal := val
val = gdtcontext.ReplaceVariables(ctx, val)
if origVal != val {
debug.Printf(
ctx,
"exec.assert.contains: replaced var: %s -> %s",
origVal, val,
)
}
return val
})
for _, find := range vals {
if !strings.Contains(contents, find) {
Expand All @@ -89,7 +99,16 @@ func (a *pipeAssertions) OK(ctx context.Context) bool {
found := false
vals := a.ContainsAny.Values()
vals = lo.Map(vals, func(val string, _ int) string {
return gdtcontext.ReplaceVariables(ctx, val)
origVal := val
val = gdtcontext.ReplaceVariables(ctx, val)
if origVal != val {
debug.Printf(
ctx,
"exec.assert.contains-any: replaced var: %s -> %s",
origVal, val,
)
}
return val
})
for _, find := range vals {
if idx := strings.Index(contents, find); idx > -1 {
Expand All @@ -105,7 +124,16 @@ func (a *pipeAssertions) OK(ctx context.Context) bool {
if a.ContainsNone != nil {
vals := a.ContainsNone.Values()
vals = lo.Map(vals, func(val string, _ int) string {
return gdtcontext.ReplaceVariables(ctx, val)
origVal := val
val = gdtcontext.ReplaceVariables(ctx, val)
if origVal != val {
debug.Printf(
ctx,
"exec.assert.contains-none: replaced var: %s -> %s",
origVal, val,
)
}
return val
})
for _, find := range vals {
if strings.Contains(contents, find) {
Expand Down
Loading