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

Ensure matching modules with metadata when requested #21640

Merged
merged 8 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions config/module/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package module
import (
"errors"
"fmt"
"regexp"
"sort"
"strings"

version "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/registry/response"
)

const anyVersion = ">=0.0.0"

var explicitEqualityConstraint = regexp.MustCompile("^=[0-9]")

// return the newest version that satisfies the provided constraint
func newest(versions []string, constraint string) (string, error) {
if constraint == "" {
Expand Down Expand Up @@ -50,6 +54,11 @@ func newest(versions []string, constraint string) (string, error) {
return iv.GreaterThan(jv)
})

// Store whether the constraint contains a metadata requirement,
// so we can check if we should be returning a specific metadata version
constraintMeta := strings.SplitAfterN(cs.String(), "+", 2)
pselle marked this conversation as resolved.
Show resolved Hide resolved
equalsConstraint := explicitEqualityConstraint.MatchString(cs.String())

// versions are now in order, so just find the first which satisfies the
// constraint
for i := range versions {
Expand All @@ -58,6 +67,12 @@ func newest(versions []string, constraint string) (string, error) {
continue
}
if cs.Check(v) {
// Constraint has metadata and is explicit equality
if len(constraintMeta) > 1 && equalsConstraint {
if constraintMeta[1] != v.Metadata() {
continue
}
}
return versions[i], nil
}
}
Expand Down
26 changes: 26 additions & 0 deletions config/module/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ func TestNewestInvalidModuleVersion(t *testing.T) {
t.Fatalf("expected version %q, got %q", expected, m.Version)
}
}

func TestNewestModulesWithMetadata(t *testing.T) {
mpv := &response.ModuleProviderVersions{
Source: "registry/test/module",
Versions: []*response.ModuleVersion{
{Version: "0.9.0"},
{Version: "0.9.0+def"},
{Version: "0.9.0+abc"},
{Version: "0.9.0+xyz"},
},
}

// with metadata and explicit version request
expected := "0.9.0+def"
m, _ := newestVersion(mpv.Versions, "=0.9.0+def")
if m.Version != expected {
t.Fatalf("expected version %q, got %q", expected, m.Version)
}

// respect explicit equality, but >/</~ will be ignored
expected = "0.9.0"
m, _ = newestVersion(mpv.Versions, "~>0.9.0+abc")
pselle marked this conversation as resolved.
Show resolved Hide resolved
if m.Version != expected {
t.Fatalf("expected version %q, got %q", expected, m.Version)
}
}