|
|
|
|
@ -6,6 +6,7 @@ package configs
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"sort"
|
|
|
|
|
@ -283,6 +284,86 @@ func TestBuildConfigInvalidModules(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildConfig_WithNestedTestModules(t *testing.T) {
|
|
|
|
|
parser := NewParser(nil)
|
|
|
|
|
mod, diags := parser.LoadConfigDirWithTests("testdata/valid-modules/with-tests-nested-module", "tests")
|
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
if mod == nil {
|
|
|
|
|
t.Fatal("got nil root module; want non-nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
|
|
|
|
|
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
|
|
|
|
|
|
|
|
|
|
// Bit of a hack to get the test working, but we know all the source
|
|
|
|
|
// addresses in this test are locals, so we can just treat them as
|
|
|
|
|
// paths in the filesystem.
|
|
|
|
|
|
|
|
|
|
addr := req.SourceAddr.String()
|
|
|
|
|
current := req.Parent
|
|
|
|
|
for current.SourceAddr != nil {
|
|
|
|
|
addr = path.Join(current.SourceAddr.String(), addr)
|
|
|
|
|
current = current.Parent
|
|
|
|
|
}
|
|
|
|
|
sourcePath := filepath.Join("testdata/valid-modules/with-tests-nested-module", addr)
|
|
|
|
|
|
|
|
|
|
mod, diags := parser.LoadConfigDir(sourcePath)
|
|
|
|
|
version, _ := version.NewVersion("1.0.0")
|
|
|
|
|
return mod, version, diags
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
if cfg == nil {
|
|
|
|
|
t.Fatal("got nil config; want non-nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We should have loaded our test case, and one of the test runs should
|
|
|
|
|
// have loaded an alternate module.
|
|
|
|
|
|
|
|
|
|
if len(cfg.Module.Tests) != 1 {
|
|
|
|
|
t.Fatalf("expected exactly one test case but found %d", len(cfg.Module.Tests))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test := cfg.Module.Tests["main.tftest.hcl"]
|
|
|
|
|
if len(test.Runs) != 1 {
|
|
|
|
|
t.Fatalf("expected two test runs but found %d", len(test.Runs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run := test.Runs[0]
|
|
|
|
|
if run.ConfigUnderTest == nil {
|
|
|
|
|
t.Fatalf("the first test run should have loaded config but did not")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if run.ConfigUnderTest.Parent != nil {
|
|
|
|
|
t.Errorf("config under test should not have a parent")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if run.ConfigUnderTest.Root != run.ConfigUnderTest {
|
|
|
|
|
t.Errorf("config under test root should be itself")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(run.ConfigUnderTest.Path) > 0 {
|
|
|
|
|
t.Errorf("config under test path should be the root module")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We should also have loaded a single child underneath the config under
|
|
|
|
|
// test, and it should have valid paths.
|
|
|
|
|
|
|
|
|
|
child := run.ConfigUnderTest.Children["child"]
|
|
|
|
|
|
|
|
|
|
if child.Parent != run.ConfigUnderTest {
|
|
|
|
|
t.Errorf("child should point back to root")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(child.Path) != 1 || child.Path[0] != "child" {
|
|
|
|
|
t.Errorf("child should have rebased against virtual root")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if child.Root != run.ConfigUnderTest {
|
|
|
|
|
t.Errorf("child root should be main config under test")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildConfig_WithTestModule(t *testing.T) {
|
|
|
|
|
parser := NewParser(nil)
|
|
|
|
|
mod, diags := parser.LoadConfigDirWithTests("testdata/valid-modules/with-tests-module", "tests")
|
|
|
|
|
|