command/init: Don't panic if go-getter-fetched module has version constraint

This commit is contained in:
Matt Morrison 2019-05-18 08:19:31 +12:00 committed by Martin Atkins
parent 742deca3e9
commit cbebb7cdf1
4 changed files with 37 additions and 0 deletions

View File

@ -480,6 +480,15 @@ func (i *ModuleInstaller) installGoGetterModule(req *earlyconfig.ModuleRequest,
packageAddr, _ := splitAddrSubdir(req.SourceAddr)
hooks.Download(key, packageAddr, nil)
if len(req.VersionConstraints) != 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid version constraint",
fmt.Sprintf("Cannot apply a version constraint to module %q (at %s:%d) because it has a non Registry URL.", req.Name, req.CallPos.Filename, req.CallPos.Line),
))
return nil, diags
}
modDir, err := getter.getWithGoGetter(instPath, req.SourceAddr)
if err != nil {
if _, ok := err.(*MaybeRelativePathErr); ok {

View File

@ -111,6 +111,24 @@ func TestModuleInstaller_error(t *testing.T) {
}
}
func TestModuleInstaller_invalid_version_constraint_error(t *testing.T) {
fixtureDir := filepath.Clean("test-fixtures/invalid-version-constraint")
dir, done := tempChdir(t, fixtureDir)
defer done()
hooks := &testInstallHooks{}
modulesDir := filepath.Join(dir, ".terraform/modules")
inst := NewModuleInstaller(modulesDir, nil)
_, diags := inst.InstallModules(".", false, hooks)
if !diags.HasErrors() {
t.Fatal("expected error")
} else {
assertDiagnosticSummary(t, diags, "Invalid version constraint")
}
}
func TestModuleInstaller_symlink(t *testing.T) {
fixtureDir := filepath.Clean("test-fixtures/local-module-symlink")
dir, done := tempChdir(t, fixtureDir)

View File

@ -0,0 +1 @@
.terraform/*

View File

@ -0,0 +1,9 @@
# This fixture references the github repo at:
# https://github.com/hashicorp/terraform-aws-module-installer-acctest
# However, due to the nature of this test (verifying early error), the URL will not be contacted,
# and the test is safe to execute as part of the normal test suite.
module "acctest_root" {
source = "github.com/hashicorp/terraform-aws-module-installer-acctest"
version = "0.0.1"
}