
Reference: https://github.com/hashicorp/terraform-provider-scaffolding-framework/pull/220 Consuming developers should not directly need to worry about `github.com/hashicorp/go-version` unless they want to use that Go module. Provider-defined functions testing was releasing in this template repository before the Terraform 1.8.0 version variable in the terraform-plugin-testing `tfversion` package was available. Now that the variable is present, we can remove go-version as a direct dependency, simplifying both the code and dependency management slightly.
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package provider
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
|
"github.com/hashicorp/terraform-plugin-testing/tfversion"
|
|
)
|
|
|
|
func TestExampleFunction_Known(t *testing.T) {
|
|
resource.UnitTest(t, resource.TestCase{
|
|
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
|
|
tfversion.SkipBelow(tfversion.Version1_8_0),
|
|
},
|
|
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: `
|
|
output "test" {
|
|
value = provider::scaffolding::example("testvalue")
|
|
}
|
|
`,
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckOutput("test", "testvalue"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestExampleFunction_Null(t *testing.T) {
|
|
resource.UnitTest(t, resource.TestCase{
|
|
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
|
|
tfversion.SkipBelow(tfversion.Version1_8_0),
|
|
},
|
|
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: `
|
|
output "test" {
|
|
value = provider::scaffolding::example(null)
|
|
}
|
|
`,
|
|
// The parameter does not enable AllowNullValue
|
|
ExpectError: regexp.MustCompile(`argument must not be null`),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestExampleFunction_Unknown(t *testing.T) {
|
|
resource.UnitTest(t, resource.TestCase{
|
|
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
|
|
tfversion.SkipBelow(tfversion.Version1_8_0),
|
|
},
|
|
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: `
|
|
resource "terraform_data" "test" {
|
|
input = "testvalue"
|
|
}
|
|
|
|
output "test" {
|
|
value = provider::scaffolding::example(terraform_data.test.output)
|
|
}
|
|
`,
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckOutput("test", "testvalue"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|