From 4d5e981e25920b50f6144e3df8797e4ea14e1189 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Tue, 3 Oct 2023 22:52:01 +0900 Subject: [PATCH] Truncate pre-release version before comparing The hashicorp/go-version returns false when comparing pre-releases, for example 1.6.0-rc1 >= 0.13. This is counter-intuitive for determining the presence or absence of a feature, so remove the pre-release information before comparing. --- tfexec/terraform_providers_test.go | 14 +++----------- tfexec/terraform_state_replace_provider.go | 7 ++++++- tfexec/terraform_version.go | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/tfexec/terraform_providers_test.go b/tfexec/terraform_providers_test.go index 90da214..924e4d4 100644 --- a/tfexec/terraform_providers_test.go +++ b/tfexec/terraform_providers_test.go @@ -2,11 +2,8 @@ package tfexec import ( "context" - "fmt" "reflect" "testing" - - "github.com/hashicorp/go-version" ) var providersStdout = ` @@ -100,18 +97,13 @@ resource "null_resource" "bar" {} t.Fatalf("failed to run terraform providers: %s", err) } - v, err := terraformCLI.Version(context.Background()) - if err != nil { - t.Fatalf("unexpected version error: %s", err) - } - - constraints, err := version.NewConstraint(fmt.Sprintf(">= %s", MinimumTerraformVersionForStateReplaceProvider)) + supportsStateReplaceProvider, _, err := terraformCLI.SupportsStateReplaceProvider(context.Background()) if err != nil { - t.Fatalf("unexpected version constraint error: %s", err) + t.Fatalf("failed to determine if Terraform version supports state replace-provider: %s", err) } want := providersStdout - if !constraints.Check(v) { + if !supportsStateReplaceProvider { want = legacyProvidersStdout } diff --git a/tfexec/terraform_state_replace_provider.go b/tfexec/terraform_state_replace_provider.go index 0c424f4..40c4eae 100644 --- a/tfexec/terraform_state_replace_provider.go +++ b/tfexec/terraform_state_replace_provider.go @@ -34,7 +34,12 @@ func (c *terraformCLI) SupportsStateReplaceProvider(ctx context.Context) (bool, return false, constraints, err } - if !constraints.Check(v) { + ver, err := truncatePreReleaseVersion(v) + if err != nil { + return false, constraints, err + } + + if !constraints.Check(ver) { return false, constraints, nil } diff --git a/tfexec/terraform_version.go b/tfexec/terraform_version.go index ea21b87..5fab486 100644 --- a/tfexec/terraform_version.go +++ b/tfexec/terraform_version.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "regexp" + "strings" "github.com/hashicorp/go-version" ) @@ -29,3 +30,24 @@ func (c *terraformCLI) Version(ctx context.Context) (*version.Version, error) { return version, nil } + +// truncatePreReleaseVersion is a helper function that removes +// pre-release information. +// The hashicorp/go-version returns false when comparing pre-releases, for +// example 1.6.0-rc1 >= 0.13. This is counter-intuitive for determining the +// presence or absence of a feature, so remove the pre-release information +// before comparing. +func truncatePreReleaseVersion(v *version.Version) (*version.Version, error) { + if v.Prerelease() == "" { + return v, nil + } + + vs, _, _ := strings.Cut(v.String(), "-") + + ver, err := version.NewVersion(vs) + if err != nil { + return nil, err + } + + return ver, nil +}