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

assert.one_not_null() treats missing keys as null #730

Merged
merged 2 commits into from
Sep 7, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ keys_are_specified:
one_value_not_null_is_ok: #@ assert.one_not_null(["foo", "bar", "baz"]).check(multiple_values_are_not_null())
non_string_keys_are_ok: #@ assert.one_not_null([0, False, 0.1, "foo"]).check(non_string_keys())
spec_is_not_a_sequence: #@ assert.try_to(lambda: assert.one_not_null(13).check(one_value_is_not_null()))
key_not_found: #@ assert.try_to(lambda: assert.one_not_null(["foo", "not-a-key"]).check(one_value_is_not_null()))
key_not_found: #@ assert.one_not_null(["foo", "not-a-key"]).check(one_value_is_not_null())
too_args: #@ assert.try_to(lambda: assert.one_not_null([], []).check({}))

+++
Expand Down Expand Up @@ -72,9 +72,7 @@ keys_are_specified:
spec_is_not_a_sequence:
- null
- 'assert.one_not_null: expected a sequence of keys, but was a ''int'''
key_not_found:
- null
- 'check: "not-a-key" is not present in value'
key_not_found: true
too_args:
- null
- 'assert.one_not_null: got 2 arguments, want 1'
18 changes: 2 additions & 16 deletions pkg/yttlibrary/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/k14s/starlark-go/syntax"
"github.com/vmware-tanzu/carvel-ytt/pkg/experiments"
"github.com/vmware-tanzu/carvel-ytt/pkg/orderedmap"
"github.com/vmware-tanzu/carvel-ytt/pkg/spell"
"github.com/vmware-tanzu/carvel-ytt/pkg/template/core"
)

Expand Down Expand Up @@ -359,8 +358,8 @@ func (m AssertModule) oneNotNullCheck(keys starlark.Sequence) core.StarlarkFunc
return nil, fmt.Errorf("check: unexpected error while looking up key %s in dict %s", key, dict)
}
if !found {
hint := m.maybeSuggestKey(key, *dict)
return nil, fmt.Errorf("check: %s is not present in value%s", key, hint)
// allow schema to catch this (see also https://github.com/vmware-tanzu/carvel-ytt/issues/722)
nulls = append(nulls, key.String())
}
if value == starlark.None {
nulls = append(nulls, key.String())
Expand Down Expand Up @@ -414,19 +413,6 @@ func (m AssertModule) OneOf(thread *starlark.Thread, f *starlark.Builtin, args s
return maxFunc, nil
}

func (m AssertModule) maybeSuggestKey(given starlark.Value, expected starlark.Dict) string {
var keysAsStrings []string
for _, k := range expected.Keys() {
keysAsStrings = append(keysAsStrings, k.String())
}
mostSimilarKey := spell.Nearest(given.String(), keysAsStrings)
var hint string
if mostSimilarKey != "" {
hint = fmt.Sprintf(" (did you mean %s?)", mostSimilarKey)
}
return hint
}

func (m AssertModule) yamlEncodeDecode(val starlark.Value) (starlark.Value, error) {
yaml := yamlModule{}
value, err := core.NewStarlarkValue(val).AsGoValue()
Expand Down