Skip to content

Commit

Permalink
Truncate pre-release version before comparing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
minamijoyo committed Oct 5, 2023
1 parent ae31322 commit bb079fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
14 changes: 3 additions & 11 deletions tfexec/terraform_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package tfexec

import (
"context"
"fmt"
"reflect"
"testing"

"github.com/hashicorp/go-version"
)

var providersStdout = `
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion tfexec/terraform_state_replace_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 22 additions & 0 deletions tfexec/terraform_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"regexp"
"strings"

"github.com/hashicorp/go-version"
)
Expand All @@ -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
}

0 comments on commit bb079fd

Please sign in to comment.