don't build a config if it didn't load properly

This commit is contained in:
James Bardin 2021-05-07 11:52:06 -04:00
parent 8d4d333efe
commit 655f18c393
3 changed files with 20 additions and 1 deletions

View File

@ -20,7 +20,7 @@ import (
// required to process the individual modules
func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) {
rootMod, diags := l.parser.LoadConfigDir(rootDir)
if rootMod == nil {
if rootMod == nil || diags.HasErrors() {
return nil, diags
}

View File

@ -80,3 +80,19 @@ func TestLoaderLoadConfig_addVersion(t *testing.T) {
t.Fatalf("wrong error\ngot:\n%s\n\nwant: containing %q", got, want)
}
}
func TestLoaderLoadConfig_loadDiags(t *testing.T) {
// building a config which didn't load correctly may cause configs to panic
fixtureDir := filepath.Clean("testdata/invalid-names")
loader, err := NewLoader(&Config{
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
})
if err != nil {
t.Fatalf("unexpected error from NewLoader: %s", err)
}
_, diags := loader.LoadConfig(fixtureDir)
if !diags.HasErrors() {
t.Fatalf("success; want error")
}
}

View File

@ -0,0 +1,3 @@
provider "42_bad!" {
invalid_provider_name = "yes"
}