hashicorp / hashicorp/terraform-plugin-testing

knownvalue: Consider Pointer-Based Checks

Open
#302 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
68
Forks
22
Avg merge
2d 12h
Merged PRs (30d)
1

Description

### terraform-plugin-testing version

```
v1.7.0
```

### Use cases

The v1.7.0 release introduced the experimental [`knownvalue` package](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue) for writing value-specific assertions against known resource attribute and output values. This initially exposed checks such as:

* `Bool(value bool)`
* `Float64Exact(value float64)`
* `Int64Exact(value int64)`
* `StringExact(value string)`

The values for these checks may not be known upfront with a developer hardcoding the value. For example in #295, computed attribute values are compared to an API response type where the struct fields are pointers. Attempting to declaratively dereference the field value pointer to use the existing known value checks would almost undoubtedly cause a panic when running the test since the dereferencing operation would occur during compilation while its still `nil`.

### Proposal

_Caveat: This proposal only suggests **asserting** values that are already pointers to Go built-in types, not potentially **extracting** values into the pointer. That proposal should be submitted separately and has many more considerations of whether it should be offered natively or what form it should take._

Verify whether this testing pattern is prevalent in the ecosystem or might become prevalent with the newer checks. If not, do nothing.

Introduce the following into the `knownvalue` package:

* `BoolPointer(value *bool)`
* `Float64PointerExact(value *float64)`
* `Int64PointerExact(value *int64)`
* `StringPointerExact(value *string)`

If the given `value` argument is `nil`, return an error that alludes to using the `Null` check instead. This would be intended to prevent developers from testing implementation errors and from relying on these checks for null checking.

Otherwise, the check logic can dereference the pointer and perform an exact equality check.

### References

- https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.