Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions configs/config_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func buildChildModules(parent *Config, walker ModuleWalker) (map[string]*Config,
}

child.Children, modDiags = buildChildModules(child, walker)
diags = append(diags, modDiags...)

ret[call.Name] = child
}
Expand Down
45 changes: 45 additions & 0 deletions configs/config_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,48 @@ func TestBuildConfig(t *testing.T) {
t.Fatalf("child_a.child_c is same object as child_b.child_c; should not be")
}
}

func TestBuildConfigDiags(t *testing.T) {
parser := NewParser(nil)
mod, diags := parser.LoadConfigDir("testdata/nested-errors")
assertNoDiagnostics(t, diags)
if mod == nil {
t.Fatal("got nil root module; want non-nil")
}

versionI := 0
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
// For the sake of this test we're going to just treat our
// SourceAddr as a path relative to our fixture directory.
// A "real" implementation of ModuleWalker should accept the
// various different source address syntaxes Terraform supports.
sourcePath := filepath.Join("testdata/nested-errors", req.SourceAddr)

mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
versionI++
return mod, version, diags
},
))

wantDiag := `testdata/nested-errors/child_c/child_c.tf:5,1-8: ` +
`Unsupported block type; Blocks of type "invalid" are not expected here.`
assertExactDiagnostics(t, diags, []string{wantDiag})

// we should still have module structure loaded
var got []string
cfg.DeepEach(func(c *Config) {
got = append(got, fmt.Sprintf("%s %s", strings.Join(c.Path, "."), c.Version))
})
sort.Strings(got)
want := []string{
" <nil>",
"child_a 1.0.0",
"child_a.child_c 1.0.1",
}

if !reflect.DeepEqual(got, want) {
t.Fatalf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
}
}
4 changes: 4 additions & 0 deletions configs/testdata/nested-errors/child_a/child_a.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

module "child_c" {
source = "child_c"
}
6 changes: 6 additions & 0 deletions configs/testdata/nested-errors/child_c/child_c.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
output "hello" {
value = "hello"
}

invalid "block" "type " {
}
3 changes: 3 additions & 0 deletions configs/testdata/nested-errors/root.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "child_a" {
source = "child_a"
}