Skip to content

Commit

Permalink
Modifying ExpectKnown<Value|OutputValue|OutputValueAtPath> to allow f…
Browse files Browse the repository at this point in the history
…or checking of null values (#266)
  • Loading branch information
bendbennett committed Jan 15, 2024
1 parent d93aa82 commit 598dac8
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 356 deletions.
34 changes: 34 additions & 0 deletions knownvalue/null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package knownvalue

import (
"fmt"
)

var _ Check = NullValue{}

// NullValue is a Check for asserting equality between the value supplied
// to NullValueExact and the value passed to the CheckValue method.
type NullValue struct{}

// CheckValue determines whether the passed value is of nil.
func (v NullValue) CheckValue(other any) error {
if other != nil {
return fmt.Errorf("expected value nil for NullValue check, got: %T", other)
}

return nil
}

// String returns the string representation of nil.
func (v NullValue) String() string {
return "nil"
}

// NullValueExact returns a Check for asserting equality nil
// and the value passed to the CheckValue method.
func NullValueExact() NullValue {
return NullValue{}
}
61 changes: 61 additions & 0 deletions knownvalue/null_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package knownvalue_test

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/hashicorp/terraform-plugin-testing/knownvalue"
)

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

testCases := map[string]struct {
self knownvalue.NullValue
other any
expectedError error
}{
"zero-nil": {},
"zero-other": {
other: nil, // checking against the underlying value field zero-value
},
"not-nil": {
self: knownvalue.NullValueExact(),
other: false,
expectedError: fmt.Errorf("expected value nil for NullValue check, got: bool"),
},
"equal": {
self: knownvalue.NullValueExact(),
other: nil,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

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

got := testCase.self.CheckValue(testCase.other)

if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

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

got := knownvalue.NullValueExact().String()

if diff := cmp.Diff(got, "nil"); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
}
24 changes: 2 additions & 22 deletions statecheck/expect_known_output_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package statecheck
import (
"context"
"fmt"
"reflect"

tfjson "github.com/hashicorp/terraform-json"

Expand Down Expand Up @@ -56,27 +55,8 @@ func (e expectKnownOutputValue) CheckState(ctx context.Context, req CheckStateRe
return
}

if result == nil {
resp.Error = fmt.Errorf("value is null")

return
}

switch reflect.TypeOf(result).Kind() {
case reflect.Bool,
reflect.Map,
reflect.Slice,
reflect.String:
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = err

return
}
default:
errorStr := fmt.Sprintf("unrecognised output type: %T, known value type is %T", result, e.knownValue)
errorStr += "\n\nThis is an error in statecheck.ExpectKnownOutputValue.\nPlease report this to the maintainers."

resp.Error = fmt.Errorf(errorStr)
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = err

return
}
Expand Down
24 changes: 2 additions & 22 deletions statecheck/expect_known_output_value_at_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package statecheck
import (
"context"
"fmt"
"reflect"

tfjson "github.com/hashicorp/terraform-json"

Expand Down Expand Up @@ -57,27 +56,8 @@ func (e expectKnownOutputValueAtPath) CheckState(ctx context.Context, req CheckS
return
}

if result == nil {
resp.Error = fmt.Errorf("value is null")

return
}

switch reflect.TypeOf(result).Kind() {
case reflect.Bool,
reflect.Map,
reflect.Slice,
reflect.String:
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = err

return
}
default:
errorStr := fmt.Sprintf("unrecognised output type: %T, known value type is %T", result, e.knownValue)
errorStr += "\n\nThis is an error in statecheck.ExpectKnownOutputValueAtPath.\nPlease report this to the maintainers."

resp.Error = fmt.Errorf(errorStr)
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = err

return
}
Expand Down
Loading

0 comments on commit 598dac8

Please sign in to comment.