config: fix provider version constraint validation

Previously we were using the "semver" library to parse version
constraints, but we switched over to go-version and encapsulated it
inside our own plugin/discovery package to reduce dependency sprawl in
the code.

This particular situation was missed when updating references to the new
path, which meant that our validation code disagreed with the rest of
the code about what is considered a valid version constraint string.
By using the correct function, we ensure that we catch early any invalid
versions.
This commit is contained in:
Martin Atkins 2017-06-01 17:51:07 -07:00
parent dbbafbd43f
commit f70318097a
3 changed files with 12 additions and 2 deletions

View File

@ -8,11 +8,11 @@ import (
"strconv"
"strings"
"github.com/blang/semver"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/hashicorp/terraform/helper/hilmapstructure"
"github.com/hashicorp/terraform/plugin/discovery"
"github.com/mitchellh/reflectwalk"
)
@ -391,7 +391,7 @@ func (c *Config) Validate() error {
}
if p.Version != "" {
_, err := semver.ParseRange(p.Version)
_, err := discovery.ConstraintStr(p.Version).Parse()
if err != nil {
errs = append(errs, fmt.Errorf(
"provider.%s: invalid version constraint %q: %s",

View File

@ -626,6 +626,13 @@ func TestConfigValidate_varModuleInvalid(t *testing.T) {
}
}
func TestConfigValidate_varProviderVersionInvalid(t *testing.T) {
c := testConfig(t, "validate-provider-version-invalid")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestNameRegexp(t *testing.T) {
cases := []struct {
Input string

View File

@ -0,0 +1,3 @@
provider "test" {
version = "bananas!"
}