Improve `--from-module` test coverage

Clean up and old todo and add a test for the simplified error message.
sams/tf-test-parallel-deps-cleanup
Daniel Banck 5 days ago committed by Daniel Banck
parent eb374193aa
commit 5d289cc1a2

@ -217,9 +217,7 @@ func DirFromModule(ctx context.Context, loader *configload.Loader, rootDir, modu
mod, _ := loader.Parser().LoadConfigDir(rootDir) // ignore diagnostics since we're just doing value-add here anyway
if mod != nil {
for _, mc := range mod.ModuleCalls {
// TODO improve this
sourceVal, _ := mc.SourceExpr.Value(nil)
if !sourceVal.IsKnown() {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,

@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/configs/configload"
"github.com/hashicorp/terraform/internal/copy"
@ -358,3 +359,37 @@ func TestDirFromModule_rel_submodules(t *testing.T) {
})
assertResultDeepEqual(t, gotTraces, wantTraces)
}
func TestDirFromModule_submodulesWithDynamicSources(t *testing.T) {
fixtureDir := filepath.Clean("testdata/empty")
fromModuleDir, err := filepath.Abs("./testdata/local-module-dynamic-sources")
if err != nil {
t.Fatal(err)
}
tmpDir, done := tempChdir(t, fixtureDir)
defer done()
hooks := &testInstallHooks{}
dir, err := filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Error(err)
}
modInstallDir := filepath.Join(dir, ".terraform/modules")
loader, cleanup := configload.NewLoaderForTests(t)
defer cleanup()
diags := DirFromModule(context.Background(), loader, dir, modInstallDir, fromModuleDir, nil, hooks)
wantDiags := tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unknown module source",
Detail: "Dynamic module sources cannot be used in conjunction with -from-module",
Subject: &hcl.Range{
Filename: filepath.Join(dir, "main.tf"),
Start: hcl.Pos{Line: 2, Column: 12, Byte: 27},
End: hcl.Pos{Line: 2, Column: 27, Byte: 42},
},
})
tfdiags.AssertDiagnosticsMatch(t, diags, wantDiags)
}

@ -0,0 +1,8 @@
module "name" {
source = "./${var.path}"
}
variable "path" {
type = string
default = "child"
}
Loading…
Cancel
Save