Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #400 - Whitespaces and Version Constraints #403

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 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,7 @@ import (

func GetVersionFromVersionsTF(params Params) (Params, error) {
var tfConstraints []string
var exactConstraints []string
//var exactConstraints []string
MatrixCrawler marked this conversation as resolved.
Show resolved Hide resolved

curDir, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -41,25 +41,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