Repository: warrensbox/terraform-switcher (go)

Issue:
We are facing the issue with the latest version(v1.1.0) of tfswitcher
Our pipeline was running fine with v1.0.2, but suddenly in one step our pipeline download the latest tfswitcher(v1.1.0) and then pipeline got failed. We are facing the below error :

FATAL Error parsing configuration file: "exact constraint (\"= 1.1.9, = 1.1.9\") cannot be combined with other conditions" 


Discussion:

Patch:
diff --git a/lib/param_parsing/versiontf.go b/lib/param_parsing/versiontf.go
--- a/lib/param_parsing/versiontf.go
+++ b/lib/param_parsing/versiontf.go
@@ -1,7 +1,7 @@
 package param_parsing
 
 import (
-	"fmt"
+	semver "github.com/hashicorp/go-version"
 	"os"
 	"path/filepath"
 	"strings"
@@ -12,7 +12,6 @@ import (
 
 func GetVersionFromVersionsTF(params Params) (Params, error) {
 	var tfConstraints []string
-	var exactConstraints []string
 
 	curDir, err := os.Getwd()
 	if err != nil {
@@ -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, ", ")


Test patch:
diff --git a/lib/param_parsing/versiontf_test.go b/lib/param_parsing/versiontf_test.go
--- a/lib/param_parsing/versiontf_test.go
+++ b/lib/param_parsing/versiontf_test.go
@@ -4,7 +4,6 @@ import (
 	"fmt"
 	"github.com/hashicorp/go-version"
 	"github.com/warrensbox/terraform-switcher/lib"
-	"strings"
 	"testing"
 )
 
@@ -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)
 		}
 	}
 }
@@ -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)
 		}
diff --git a/test-data/integration-tests/test_versiontf/version.tf b/test-data/integration-tests/test_versiontf/version.tf
--- a/test-data/integration-tests/test_versiontf/version.tf
+++ b/test-data/integration-tests/test_versiontf/version.tf
@@ -3,5 +3,5 @@ terraform {
 }
 
 terraform {
-  required_version = "<= 1.0.5"
+  required_version = "<=1.0.5"
 }
diff --git a/test-data/skip-integration-tests/test_versiontf_non_matching_constraints/version.tf b/test-data/skip-integration-tests/test_versiontf_non_matching_constraints/version.tf
--- a/test-data/skip-integration-tests/test_versiontf_non_matching_constraints/version.tf
+++ b/test-data/skip-integration-tests/test_versiontf_non_matching_constraints/version.tf
@@ -3,7 +3,7 @@ terraform {
 }
 
 terraform {
-  required_version = "= 1.0.5"
+  required_version = "=1.0.5"
 }
 
 terraform {