Skip to content

Commit

Permalink
Merge pull request #8 from gotwarlost/cue-test-fix
Browse files Browse the repository at this point in the history
Fix bugs in the cue unit tester after the refactor
  • Loading branch information
gotwarlost committed May 6, 2024
2 parents 73bcafa + 26f0051 commit ccc663e
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/cuetools/testdata/runtime/impl.cue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package runtime

response: desired:resources: main: resource: {
foo: request.observed.composite.resource.foo
foo: #request.observed.composite.resource.foo
bar: "baz"
}

2 changes: 1 addition & 1 deletion internal/cuetools/testdata/runtime/init.cue
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import(
"github.com/crossplane-contrib/function-cue/testdata/runtime/util"
)

request: util.#RequestSchema
#request: util.#RequestSchema
2 changes: 1 addition & 1 deletion internal/cuetools/testdata/runtime/tests/correct.cue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@if(correct)
package tests

request: observed: composite: resource: {
#request: observed: composite: resource: {
foo: "bar"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cuetools/testdata/runtime/tests/incorrect.cue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@if(incorrect)
package tests

request: observed: composite: resource: {
#request: observed: composite: resource: {
foo: "foo2"
}

Expand Down
7 changes: 7 additions & 0 deletions internal/cuetools/testdata/runtime2/impl.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package runtime

resources: main: resource: {
foo: _request.observed.composite.resource.foo
bar: "baz"
}

7 changes: 7 additions & 0 deletions internal/cuetools/testdata/runtime2/init.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package runtime

import(
"github.com/crossplane-contrib/function-cue/testdata/runtime/util"
)

_request: util.#RequestSchema
11 changes: 11 additions & 0 deletions internal/cuetools/testdata/runtime2/tests/correct.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@if(correct)
package tests

_request: observed: composite: resource: {
foo: "bar"
}

resources: main: resource: {
foo: "bar"
bar: "baz"
}
11 changes: 11 additions & 0 deletions internal/cuetools/testdata/runtime2/tests/incorrect.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@if(incorrect)
package tests

_request: observed: composite: resource: {
foo: "foo2"
}

resources: main: resource: {
foo: "bar"
bar: "baz"
}
3 changes: 3 additions & 0 deletions internal/cuetools/testdata/runtime2/util/util.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util

#RequestSchema: {...}
15 changes: 11 additions & 4 deletions internal/cuetools/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (t *Tester) runTest(f *fn.Cue, codeBytes []byte, tag string) (finalErr erro
}
}()

requestVar := "request"
requestVar := "#request"
if t.config.RequestVar != "" {
requestVar = t.config.RequestVar
}
Expand All @@ -199,7 +199,13 @@ func (t *Tester) runTest(f *fn.Cue, codeBytes []byte, tag string) (finalErr erro
}

var expected fnv1beta1.RunFunctionResponse
err := evalPackage(t.config.TestPackage, tag, responseVar, &expected)
var err error
if t.config.LegacyDesiredOnlyResponse {
expected.Desired = &fnv1beta1.State{}
err = evalPackage(t.config.TestPackage, tag, responseVar, expected.Desired)
} else {
err = evalPackage(t.config.TestPackage, tag, responseVar, &expected)
}
if err != nil {
return errors.Wrap(err, "evaluate expected")
}
Expand All @@ -210,12 +216,13 @@ func (t *Tester) runTest(f *fn.Cue, codeBytes []byte, tag string) (finalErr erro
return errors.Wrap(err, "evaluate request")
}

actual, err := f.Eval(&req, string(codeBytes), fn.EvalOptions{
opts := fn.EvalOptions{
RequestVar: requestVar,
ResponseVar: responseVar,
DesiredOnlyResponse: t.config.LegacyDesiredOnlyResponse,
Debug: fn.DebugOptions{Enabled: t.config.Debug},
})
}
actual, err := f.Eval(&req, string(codeBytes), opts)
if err != nil {
return errors.Wrap(err, "evaluate package with test request")
}
Expand Down
43 changes: 42 additions & 1 deletion internal/cuetools/tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,48 @@ func TestTester(t *testing.T) {
Package: "./runtime",
})
require.NoError(t, err)
envDiff := "XP_FUNCTION_CUE_DIFF"
envDiff := ExternalDiffEnvVar
diffProgram := os.Getenv(envDiff)
if diffProgram != "" {
err = os.Unsetenv(envDiff) // we expect a specific diff format
require.NoError(t, err)
defer func() { _ = os.Setenv(envDiff, diffProgram) }()
}
err = tester.Run()
expected := `
running test tags: correct, incorrect
> run test "correct"
PASS correct
> run test "incorrect"
diffs found:
--- expected
+++ actual
@@ -3,5 +3,5 @@
main:
resource:
bar: baz
- foo: bar
+ foo: foo2
FAIL incorrect: expected did not match actual
`
require.Error(t, err)
assert.Equal(t, "1 of 2 tests had errors", err.Error())
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buf.String()))
}

func TestTesterLegacyOptions(t *testing.T) {
fn := chdirCueRoot(t)
defer fn()
buf, reset := getOutput()
defer reset()
tester, err := NewTester(TestConfig{
Package: "./runtime2",
RequestVar: "_request",
ResponseVar: ".",
LegacyDesiredOnlyResponse: true,
})
require.NoError(t, err)
envDiff := ExternalDiffEnvVar
diffProgram := os.Getenv(envDiff)
if diffProgram != "" {
err = os.Unsetenv(envDiff) // we expect a specific diff format
Expand Down

0 comments on commit ccc663e

Please sign in to comment.