Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maed223 committed Apr 19, 2024
1 parent db0f78d commit 5471fdf
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/checks/state_test.go
Expand Up @@ -19,7 +19,7 @@ func TestChecksHappyPath(t *testing.T) {
loader, close := configload.NewLoaderForTests(t)
defer close()
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, nil)
_, instDiags := inst.InstallModules(context.Background(), fixtureDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), fixtureDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/command_test.go
Expand Up @@ -159,7 +159,7 @@ func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *config
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/graph_test.go
Expand Up @@ -107,7 +107,7 @@ func TestGraph_resourcesOnly(t *testing.T) {
t.Fatal(err)
}
inst := initwd.NewModuleInstaller(".terraform/modules", loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), ".", "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), ".", "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down
41 changes: 21 additions & 20 deletions internal/configs/config_build_test.go
Expand Up @@ -4,6 +4,7 @@
package configs

import (
"context"
"fmt"
"io/ioutil"
"path"
Expand All @@ -29,8 +30,8 @@ func TestBuildConfig(t *testing.T) {
}

versionI := 0
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {
// 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
Expand All @@ -40,7 +41,7 @@ func TestBuildConfig(t *testing.T) {
mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
versionI++
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
Expand Down Expand Up @@ -88,8 +89,8 @@ func TestBuildConfigDiags(t *testing.T) {
}

versionI := 0
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {
// 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
Expand All @@ -99,7 +100,7 @@ func TestBuildConfigDiags(t *testing.T) {
mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
versionI++
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
Expand Down Expand Up @@ -135,8 +136,8 @@ func TestBuildConfigChildModuleBackend(t *testing.T) {
t.Fatal("got nil root module; want non-nil")
}

cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {
// 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
Expand All @@ -145,7 +146,7 @@ func TestBuildConfigChildModuleBackend(t *testing.T) {

mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion("1.0.0")
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
Expand Down Expand Up @@ -216,14 +217,14 @@ func TestBuildConfigInvalidModules(t *testing.T) {
expectedErrs := readDiags(ioutil.ReadFile(filepath.Join(testDir, name, "errors")))
expectedWarnings := readDiags(ioutil.ReadFile(filepath.Join(testDir, name, "warnings")))

_, buildDiags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
_, buildDiags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {
// for simplicity, these tests will treat all source
// addresses as relative to the root module
sourcePath := filepath.Join(path, req.SourceAddr.String())
mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion("1.0.0")
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
Expand Down Expand Up @@ -306,7 +307,7 @@ func TestBuildConfig_WithMockDataSources(t *testing.T) {
t.Fatal("got nil root module; want non-nil")
}

cfg, diags := BuildConfig(mod, nil, MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, nil, MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
sourcePath := filepath.Join("testdata/valid-modules/with-mock-sources", provider.MockDataExternalSource)
return parser.LoadMockDataDir(sourcePath, hcl.Range{})
}))
Expand Down Expand Up @@ -337,7 +338,7 @@ func TestBuildConfig_WithMockDataSourcesInline(t *testing.T) {
t.Fatal("got nil root module; want non-nil")
}

cfg, diags := BuildConfig(mod, nil, MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, nil, MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
sourcePath := filepath.Join("testdata/valid-modules/with-mock-sources-inline", provider.MockDataExternalSource)
return parser.LoadMockDataDir(sourcePath, hcl.Range{})
}))
Expand Down Expand Up @@ -368,8 +369,8 @@ func TestBuildConfig_WithNestedTestModules(t *testing.T) {
t.Fatal("got nil root module; want non-nil")
}

cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {

// 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
Expand All @@ -385,7 +386,7 @@ func TestBuildConfig_WithNestedTestModules(t *testing.T) {

mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion("1.0.0")
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
Expand Down Expand Up @@ -451,8 +452,8 @@ func TestBuildConfig_WithTestModule(t *testing.T) {
t.Fatal("got nil root module; want non-nil")
}

cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {
// 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
Expand All @@ -461,7 +462,7 @@ func TestBuildConfig_WithTestModule(t *testing.T) {

mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion("1.0.0")
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
Expand Down
6 changes: 2 additions & 4 deletions internal/configs/configload/loader_load.go
Expand Up @@ -24,15 +24,13 @@ import (
// LoadConfig performs the basic syntax and uniqueness validations that are
// required to process the individual modules
func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) {
mod, diags := l.parser.LoadConfigDir(rootDir)
return l.loadConfig(mod, diags)
return l.loadConfig(l.parser.LoadConfigDir(rootDir))
}

// LoadConfigWithTests matches LoadConfig, except the configs.Config contains
// any relevant .tftest.hcl files.
func (l *Loader) LoadConfigWithTests(rootDir string, testDir string) (*configs.Config, hcl.Diagnostics) {
mod, diags := l.parser.LoadConfigDirWithTests(rootDir, testDir)
return l.loadConfig(mod, diags)
return l.loadConfig(l.parser.LoadConfigDirWithTests(rootDir, testDir))
}

func (l *Loader) loadConfig(rootMod *configs.Module, diags hcl.Diagnostics) (*configs.Config, hcl.Diagnostics) {
Expand Down
13 changes: 8 additions & 5 deletions internal/configs/parser_test.go
Expand Up @@ -4,6 +4,7 @@
package configs

import (
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -48,7 +49,7 @@ func testModuleConfigFromFile(filename string) (*Config, hcl.Diagnostics) {
f, diags := parser.LoadConfigFile(filename)
mod, modDiags := NewModule([]*File{f}, nil)
diags = append(diags, modDiags...)
cfg, moreDiags := BuildConfig(mod, nil, nil)
cfg, moreDiags, _ := BuildConfig(context.Background(), mod, nil, nil)
return cfg, append(diags, moreDiags...)
}

Expand All @@ -64,7 +65,7 @@ func testModuleFromDir(path string) (*Module, hcl.Diagnostics) {
func testModuleConfigFromDir(path string) (*Config, hcl.Diagnostics) {
parser := NewParser(nil)
mod, diags := parser.LoadConfigDir(path)
cfg, moreDiags := BuildConfig(mod, nil, nil)
cfg, moreDiags, _ := BuildConfig(context.Background(), mod, nil, nil)
return cfg, append(diags, moreDiags...)
}

Expand Down Expand Up @@ -105,8 +106,8 @@ func testNestedModuleConfigFromDir(t *testing.T, path string) (*Config, hcl.Diag

func buildNestedModuleConfig(mod *Module, path string, parser *Parser) (*Config, hcl.Diagnostics) {
versionI := 0
return BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
cfg, diags, _ := BuildConfig(context.Background(), mod, ModuleWalkerFunc(
func(ctx context.Context, req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics, *ModuleDeprecationInfo) {
// For the sake of this test we're going to just treat our
// SourceAddr as a path relative to the calling module.
// A "real" implementation of ModuleWalker should accept the
Expand All @@ -124,12 +125,14 @@ func buildNestedModuleConfig(mod *Module, path string, parser *Parser) (*Config,
mod, diags := parser.LoadConfigDir(sourcePath)
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
versionI++
return mod, version, diags
return mod, version, diags, nil
}),
MockDataLoaderFunc(func(provider *Provider) (*MockData, hcl.Diagnostics) {
return nil, nil
}),
)

return cfg, diags
}

func assertNoDiagnostics(t *testing.T, diags hcl.Diagnostics) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/lang/globalref/analyzer_test.go
Expand Up @@ -25,7 +25,7 @@ func testAnalyzer(t *testing.T, fixtureName string) *Analyzer {
defer cleanup()

inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), configDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), configDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatalf("unexpected module installation errors: %s", instDiags.Err().Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/moduletest/eval_context_test.go
Expand Up @@ -824,7 +824,7 @@ func testModuleInline(t *testing.T, sources map[string]string) *configs.Config {
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), cfgPath, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), cfgPath, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/refactoring/move_validate_test.go
Expand Up @@ -520,7 +520,7 @@ func loadRefactoringFixture(t *testing.T, dir string) (*configs.Config, instance
defer cleanup()

inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/terraform_test.go
Expand Up @@ -68,7 +68,7 @@ func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *config
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func testModuleInline(t *testing.T, sources map[string]string) *configs.Config {
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
_, instDiags := inst.InstallModules(context.Background(), cfgPath, "tests", true, false, initwd.ModuleInstallHooksImpl{})
_, instDiags, _ := inst.InstallModules(context.Background(), cfgPath, "tests", true, false, initwd.ModuleInstallHooksImpl{})
if instDiags.HasErrors() {
t.Fatal(instDiags.Err())
}
Expand Down

0 comments on commit 5471fdf

Please sign in to comment.