configs: record the source directory for modules

We can only do this when modules are loaded with Parser.LoadConfigDir,
but in practice this is the common case anyway.

This is important to support the path.module and path.root expressions in
configuration.
This commit is contained in:
Martin Atkins 2018-05-01 12:06:14 -07:00
parent fa2a76fa23
commit 70f1635416
3 changed files with 19 additions and 1 deletions

View File

@ -9,6 +9,18 @@ import (
// Module is a container for a set of configuration constructs that are
// evaluated within a common namespace.
type Module struct {
// SourceDir is the filesystem directory that the module was loaded from.
//
// This is populated automatically only for configurations loaded with
// LoadConfigDir. If the parser is using a virtual filesystem then the
// path here will be in terms of that virtual filesystem.
// Any other caller that constructs a module directly with NewModule may
// assign a suitable value to this attribute before using it for other
// purposes. It should be treated as immutable by all consumers of Module
// values.
SourceDir string
CoreVersionConstraints []VersionConstraint
Backend *Backend

View File

@ -41,6 +41,8 @@ func (p *Parser) LoadConfigDir(path string) (*Module, hcl.Diagnostics) {
mod, modDiags := NewModule(primary, override)
diags = append(diags, modDiags...)
mod.SourceDir = path
return mod, diags
}

View File

@ -30,13 +30,17 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
parser := NewParser(nil)
path := filepath.Join("test-fixtures/valid-modules", name)
_, diags := parser.LoadConfigDir(path)
mod, diags := parser.LoadConfigDir(path)
if len(diags) != 0 {
t.Errorf("unexpected diagnostics")
for _, diag := range diags {
t.Logf("- %s", diag)
}
}
if mod.SourceDir != path {
t.Errorf("wrong SourceDir value %q; want %s", mod.SourceDir, path)
}
})
}