Skip to content

Commit

Permalink
Merge pull request #730 from vmware-tanzu/722-missing-key-same-as-null
Browse files Browse the repository at this point in the history
assert.one_not_null() treats missing keys as null
  • Loading branch information
pivotaljohn authored Sep 7, 2022
2 parents 69e2bb5 + d552051 commit fe9ac47
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
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

0 comments on commit fe9ac47

Please sign in to comment.