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

ensure all parse-time errors show line/col in YAML #14

Merged
merged 1 commit into from
Aug 12, 2023
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
40 changes: 30 additions & 10 deletions assertion/json/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

gdterrors "github.com/gdt-dev/gdt/errors"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -78,32 +79,51 @@ var (

// UnsupportedJSONSchemaReference returns ErrUnsupportedJSONSchemaReference for
// a supplied URL.
func UnsupportedJSONSchemaReference(url string) error {
return fmt.Errorf("%w: %s", ErrUnsupportedJSONSchemaReference, url)
func UnsupportedJSONSchemaReference(url string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrUnsupportedJSONSchemaReference, url, node.Line, node.Column,
)
}

// JSONSchemaFileNotFound returns ErrJSONSchemaFileNotFound for a supplied
// path.
func JSONSchemaFileNotFound(path string) error {
return fmt.Errorf("%w: %s", ErrJSONSchemaFileNotFound, path)
func JSONSchemaFileNotFound(path string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrJSONSchemaFileNotFound, path, node.Line, node.Column,
)
}

// JSONUnmarshalError returns an ErrFailure when JSON content cannot be
// decoded.
func JSONUnmarshalError(err error) error {
return fmt.Errorf("%w: %s", ErrJSONUnmarshalError, err)
func JSONUnmarshalError(err error, node *yaml.Node) error {
if node != nil {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrJSONUnmarshalError, err, node.Line, node.Column,
)
} else {
return fmt.Errorf("%w: %s", ErrJSONUnmarshalError, err)
}
}

// JSONPathInvalid returns an ErrParse when a JSONPath expression could not be
// parsed.
func JSONPathInvalid(path string, err error) error {
return fmt.Errorf("%w: %s: %s", ErrJSONPathInvalid, path, err)
func JSONPathInvalid(path string, err error, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s: %s at line %d, column %d",
ErrJSONPathInvalid, path, err, node.Line, node.Column,
)
}

// JSONPathInvalidNoRoot returns an ErrJSONPathInvalidNoRoot when a JSONPath
// expression does not start with '$'.
func JSONPathInvalidNoRoot(path string) error {
return fmt.Errorf("%w: %s", ErrJSONPathInvalidNoRoot, path)
func JSONPathInvalidNoRoot(path string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrJSONPathInvalidNoRoot, path, node.Line, node.Column,
)
}

// JSONPathNotFound returns an ErrFailure when a JSONPath expression could not
Expand Down
16 changes: 8 additions & 8 deletions assertion/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error {
schemaURL := valNode.Value
if strings.HasPrefix(schemaURL, "http://") || strings.HasPrefix(schemaURL, "https://") {
// TODO(jaypipes): Support network lookups?
return UnsupportedJSONSchemaReference(schemaURL)
return UnsupportedJSONSchemaReference(schemaURL, valNode)
}
// Convert relative filepaths to absolute filepaths rooted in the context's
// testdir after stripping any "file://" scheme prefix
Expand All @@ -84,7 +84,7 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error {

f, err := os.Open(schemaURL)
if err != nil {
return JSONSchemaFileNotFound(schemaURL)
return JSONSchemaFileNotFound(schemaURL, valNode)
}
defer f.Close()
if runtime.GOOS == "windows" {
Expand All @@ -105,10 +105,10 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error {
}
for path, _ := range paths {
if len(path) == 0 || path[0] != '$' {
return JSONPathInvalidNoRoot(path)
return JSONPathInvalidNoRoot(path, valNode)
}
if _, err := lang.NewEvaluable(path); err != nil {
return JSONPathInvalid(path, err)
return JSONPathInvalid(path, err, valNode)
}
}
e.Paths = paths
Expand All @@ -122,10 +122,10 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error {
}
for pathFormat, _ := range pathFormats {
if len(pathFormat) == 0 || pathFormat[0] != '$' {
return JSONPathInvalidNoRoot(pathFormat)
return JSONPathInvalidNoRoot(pathFormat, valNode)
}
if _, err := lang.NewEvaluable(pathFormat); err != nil {
return JSONPathInvalid(pathFormat, err)
return JSONPathInvalid(pathFormat, err, valNode)
}
}
e.PathFormats = pathFormats
Expand Down Expand Up @@ -228,7 +228,7 @@ func (a *assertions) pathsOK() bool {
}
v := interface{}(nil)
if err := json.Unmarshal(a.content, &v); err != nil {
a.Fail(JSONUnmarshalError(err))
a.Fail(JSONUnmarshalError(err, nil))
a.terminal = true
return false
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func (a *assertions) pathFormatsOK() bool {
}
v := interface{}(nil)
if e := json.Unmarshal(a.content, &v); e != nil {
a.Fail(JSONUnmarshalError(e))
a.Fail(JSONUnmarshalError(e, nil))
a.terminal = true
return false
}
Expand Down
7 changes: 5 additions & 2 deletions errors/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func UnknownSourceType(source interface{}) error {
}

// FileNotFound returns ErrFileNotFound for a given file path
func FileNotFound(path string) error {
return fmt.Errorf("%w: %s", ErrFileNotFound, path)
func FileNotFound(path string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrFileNotFound, path, node.Line, node.Column,
)
}
6 changes: 3 additions & 3 deletions plugin/exec/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func ExecEmpty(node *yaml.Node) error {

// ExecInvalidShellParse returns an ErrExecInvalid with the error from
// shlex.Split
func ExecInvalidShellParse(err error) error {
func ExecInvalidShellParse(err error, node *yaml.Node) error {
return fmt.Errorf(
"%w: cannot parse shell args: %s",
ErrExecInvalid, err,
"%w: cannot parse shell args: %s at line %d, column %d",
ErrExecInvalid, err, node.Line, node.Column,
)
}

Expand Down
4 changes: 3 additions & 1 deletion plugin/exec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (s *Spec) UnmarshalYAML(node *yaml.Node) error {
if node.Kind != yaml.MappingNode {
return errors.ExpectedMapAt(node)
}
var execValNode *yaml.Node
// maps/structs are stored in a top-level Node.Content field which is a
// concatenated slice of Node pointers in pairs of key/values.
for i := 0; i < len(node.Content); i += 2 {
Expand All @@ -59,6 +60,7 @@ func (s *Spec) UnmarshalYAML(node *yaml.Node) error {
if valNode.Kind != yaml.ScalarNode {
return errors.ExpectedScalarAt(valNode)
}
execValNode = valNode
s.Exec = strings.TrimSpace(valNode.Value)
if s.Exec == "" {
return ExecEmpty(valNode)
Expand Down Expand Up @@ -94,7 +96,7 @@ func (s *Spec) UnmarshalYAML(node *yaml.Node) error {
if s.Shell != "" {
_, err := shlex.Split(s.Exec)
if err != nil {
return ExecInvalidShellParse(err)
return ExecInvalidShellParse(err, execValNode)
}
}
return nil
Expand Down
Loading