Skip to content

Commit

Permalink
Add eval details template for JQ
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias committed Oct 14, 2024
1 parent c4ebd6c commit 86fb53c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/engine/eval/jq/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

evalerrors "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/eval/templates"
engif "github.com/stacklok/minder/internal/engine/interfaces"
eoptions "github.com/stacklok/minder/internal/engine/options"
"github.com/stacklok/minder/internal/util"
Expand Down Expand Up @@ -124,7 +125,16 @@ func (jqe *Evaluator) Eval(ctx context.Context, pol map[string]any, _ protorefle
msg = fmt.Sprintf("%s\nassertion: %s", msg, string(marshalledAssertion))
}

return evalerrors.NewErrEvaluationFailed("%s", msg)
return evalerrors.NewDetailedErrEvaluationFailed(
templates.JqTemplate,
map[string]any{
"path": a.Ingested.Def,
"expected": expectedVal,
"actual": dataVal,
},
"%s",
msg,
)
}
}

Expand Down
64 changes: 64 additions & 0 deletions internal/engine/eval/jq/jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"

evalerrors "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/eval/jq"
"github.com/stacklok/minder/internal/engine/eval/templates"
engif "github.com/stacklok/minder/internal/engine/interfaces"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
Expand Down Expand Up @@ -664,3 +666,65 @@ func TestInvalidJQEvals(t *testing.T) {
})
}
}

func TestEvaluationDetailRendering(t *testing.T) {
t.Parallel()

tests := []struct {
name string
msg string
msgArgs []any
tmpl string
args any
error string
details string
}{
// JQ template
{
name: "JQ template with different actual and expected values",
msg: "this is the message",
tmpl: templates.JqTemplate,
args: map[string]any{
"path": ".simple",
"expected": true,
"actual": false,
},
error: "evaluation failure: this is the message",
details: "The detected configuration does not match the desired configuration:\n" +
"Expected \".simple\" to equal true, but was false.",
},
{
name: "JQ template with different actual value equal nil",
msg: "this is the message",
tmpl: templates.JqTemplate,
args: map[string]any{
"path": ".simple",
"expected": 5,
"actual": nil,
},
error: "evaluation failure: this is the message",
details: "The detected configuration does not match the desired configuration:\n" +
"Expected \".simple\" to equal 5, but was not set.",
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := evalerrors.NewDetailedErrEvaluationFailed(
tt.tmpl,
tt.args,
tt.msg,
tt.msgArgs...,
)

require.Equal(t, tt.error, err.Error())
evalErr, ok := err.(*evalerrors.EvaluationError)
require.True(t, ok)
require.Equal(t, tt.details, evalErr.Details())
})
}
}
2 changes: 2 additions & 0 deletions internal/engine/eval/templates/jq.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The detected configuration does not match the desired configuration:
Expected "{{.path}}" to equal {{.expected}}, but was {{if eq .actual nil}}not set{{else}}{{.actual}}{{end}}.
7 changes: 7 additions & 0 deletions internal/engine/eval/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ var MixedScriptsTemplate string
//
//go:embed invisibleCharactersTemplate.tmpl
var InvisibleCharactersTemplate string

// JqTemplate is the template for details of the `jq` evaluation engine.
//
// This template expects three parameters, `path`, `expected`, and `actual`, which are strings.
//
//go:embed jq.tmpl
var JqTemplate string

0 comments on commit 86fb53c

Please sign in to comment.