From cbebb7cdf16bd47d051fd1553ab06f645c3fa4ce Mon Sep 17 00:00:00 2001 From: Matt Morrison Date: Sat, 18 May 2019 08:19:31 +1200 Subject: [PATCH] command/init: Don't panic if go-getter-fetched module has version constraint --- internal/initwd/module_install.go | 9 +++++++++ internal/initwd/module_install_test.go | 18 ++++++++++++++++++ .../invalid-version-constraint/.gitignore | 1 + .../invalid-version-constraint/root.tf | 9 +++++++++ 4 files changed, 37 insertions(+) create mode 100644 internal/initwd/test-fixtures/invalid-version-constraint/.gitignore create mode 100644 internal/initwd/test-fixtures/invalid-version-constraint/root.tf diff --git a/internal/initwd/module_install.go b/internal/initwd/module_install.go index 822bc4f1e..531310ab8 100644 --- a/internal/initwd/module_install.go +++ b/internal/initwd/module_install.go @@ -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 { diff --git a/internal/initwd/module_install_test.go b/internal/initwd/module_install_test.go index bd44ac344..0eca62fc3 100644 --- a/internal/initwd/module_install_test.go +++ b/internal/initwd/module_install_test.go @@ -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) diff --git a/internal/initwd/test-fixtures/invalid-version-constraint/.gitignore b/internal/initwd/test-fixtures/invalid-version-constraint/.gitignore new file mode 100644 index 000000000..6e0db03a8 --- /dev/null +++ b/internal/initwd/test-fixtures/invalid-version-constraint/.gitignore @@ -0,0 +1 @@ +.terraform/* diff --git a/internal/initwd/test-fixtures/invalid-version-constraint/root.tf b/internal/initwd/test-fixtures/invalid-version-constraint/root.tf new file mode 100644 index 000000000..e7dbaa323 --- /dev/null +++ b/internal/initwd/test-fixtures/invalid-version-constraint/root.tf @@ -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" +} \ No newline at end of file