Skip to content

Commit

Permalink
Fix for #400 - Whitespaces and Version Constraints (#403)
Browse files Browse the repository at this point in the history
* added fix for #400 when the comparator is not separated from the version number by a space
* use hashicorp version constraint checker for constraints
* added new testcase
  • Loading branch information
MatrixCrawler committed Apr 25, 2024
1 parent d6d6b94 commit 9f7bbaf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
28 changes: 8 additions & 20 deletions lib/param_parsing/versiontf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package param_parsing

import (
"fmt"
semver "github.com/hashicorp/go-version"
"os"
"path/filepath"
"strings"
Expand All @@ -12,7 +12,6 @@ import (

func GetVersionFromVersionsTF(params Params) (Params, error) {
var tfConstraints []string
var exactConstraints []string

curDir, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -41,25 +40,14 @@ func GetVersionFromVersionsTF(params Params) (Params, error) {
requiredVersions := module.RequiredCore

for key := range requiredVersions {
tfConstraint := requiredVersions[key]
tfConstraintParts := strings.Fields(tfConstraint)

if len(tfConstraintParts) > 2 {
logger.Fatalf("Invalid version constraint found: %q", tfConstraint)
} else if len(tfConstraintParts) == 1 {
exactConstraints = append(exactConstraints, tfConstraint)
tfConstraint = "= " + tfConstraintParts[0]
}

if tfConstraintParts[0] == "=" {
exactConstraints = append(exactConstraints, tfConstraint)
// Check if the version contraint is valid
constraint, constraintErr := semver.NewConstraint(requiredVersions[key])
if constraintErr != nil {
logger.Errorf("Invalid version constraint found: %q", requiredVersions[key])
return params, constraintErr
}

tfConstraints = append(tfConstraints, tfConstraint)
}

if len(exactConstraints) > 0 && len(tfConstraints) > 1 {
return params, fmt.Errorf("exact constraint (%q) cannot be combined with other conditions", strings.Join(exactConstraints, ", "))
// It's valid. Add to list
tfConstraints = append(tfConstraints, constraint.String())
}

tfConstraint := strings.Join(tfConstraints, ", ")
Expand Down
18 changes: 9 additions & 9 deletions lib/param_parsing/versiontf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/hashicorp/go-version"
"github.com/warrensbox/terraform-switcher/lib"
"strings"
"testing"
)

Expand All @@ -17,23 +16,24 @@ func TestGetVersionFromVersionsTF_matches_version(t *testing.T) {
v1, _ := version.NewVersion("1.0.5")
actualVersion, _ := version.NewVersion(params.Version)
if !actualVersion.Equal(v1) {
t.Error("Determined version is not 1.0.5")
t.Errorf("Determined version is not 1.0.5, but %s", params.Version)
}
}

func TestGetVersionFromVersionsTF_non_matching_constraints(t *testing.T) {
func TestGetVersionFromVersionsTF_impossible_constraints(t *testing.T) {
logger = lib.InitLogger("DEBUG")
var params Params
params = initParams(params)
params.ChDirPath = "../../test-data/skip-integration-tests/test_versiontf_non_matching_constraints"
params, err := GetVersionFromVersionsTF(params)
expectedError := "did not find version matching constraint: ~> 1.0.0, =1.0.5, <= 1.0.4"
if err == nil {
t.Error("Expected error but got nil")
t.Errorf("Expected error '%s', got nil", expectedError)
} else {
expected := "exact constraint ("
expected2 := ") cannot be combined with other conditions"
if !strings.Contains(fmt.Sprint(err), expected) || !strings.Contains(fmt.Sprint(err), expected2) {
t.Errorf("Expected error string containing %q and %q. Got %q", expected, expected2, err)
if err.Error() == expectedError {
t.Logf("Got expected error '%s'", err)
} else {
t.Errorf("Got unexpected error '%s'", err)
}
}
}
Expand All @@ -47,7 +47,7 @@ func TestGetVersionFromVersionsTF_erroneous_file(t *testing.T) {
if err == nil {
t.Error("Expected error got nil")
} else {
expected := "error parsing constraint: Malformed constraint: ~527> 1.0.0"
expected := "Malformed constraint: ~527> 1.0.0"
if fmt.Sprint(err) != expected {
t.Errorf("Expected error %q, got %q", expected, err)
}
Expand Down
2 changes: 1 addition & 1 deletion test-data/integration-tests/test_versiontf/version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ terraform {
}

terraform {
required_version = "<= 1.0.5"
required_version = "<=1.0.5"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
}

terraform {
required_version = "= 1.0.5"
required_version = "=1.0.5"
}

terraform {
Expand Down

0 comments on commit 9f7bbaf

Please sign in to comment.