github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/initwd/testing.go (about) 1 package initwd 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/hashicorp/terraform/internal/configs" 8 "github.com/hashicorp/terraform/internal/configs/configload" 9 "github.com/hashicorp/terraform/internal/registry" 10 "github.com/hashicorp/terraform/internal/tfdiags" 11 ) 12 13 // LoadConfigForTests is a convenience wrapper around configload.NewLoaderForTests, 14 // ModuleInstaller.InstallModules and configload.Loader.LoadConfig that allows 15 // a test configuration to be loaded in a single step. 16 // 17 // If module installation fails, t.Fatal (or similar) is called to halt 18 // execution of the test, under the assumption that installation failures are 19 // not expected. If installation failures _are_ expected then use 20 // NewLoaderForTests and work with the loader object directly. If module 21 // installation succeeds but generates warnings, these warnings are discarded. 22 // 23 // If installation succeeds but errors are detected during loading then a 24 // possibly-incomplete config is returned along with error diagnostics. The 25 // test run is not aborted in this case, so that the caller can make assertions 26 // against the returned diagnostics. 27 // 28 // As with NewLoaderForTests, a cleanup function is returned which must be 29 // called before the test completes in order to remove the temporary 30 // modules directory. 31 func LoadConfigForTests(t *testing.T, rootDir string) (*configs.Config, *configload.Loader, func(), tfdiags.Diagnostics) { 32 t.Helper() 33 34 var diags tfdiags.Diagnostics 35 36 loader, cleanup := configload.NewLoaderForTests(t) 37 inst := NewModuleInstaller(loader.ModulesDir(), registry.NewClient(nil, nil)) 38 39 _, moreDiags := inst.InstallModules(context.Background(), rootDir, true, ModuleInstallHooksImpl{}) 40 diags = diags.Append(moreDiags) 41 if diags.HasErrors() { 42 cleanup() 43 t.Fatal(diags.Err()) 44 return nil, nil, func() {}, diags 45 } 46 47 // Since module installer has modified the module manifest on disk, we need 48 // to refresh the cache of it in the loader. 49 if err := loader.RefreshModules(); err != nil { 50 t.Fatalf("failed to refresh modules after installation: %s", err) 51 } 52 53 config, hclDiags := loader.LoadConfig(rootDir) 54 diags = diags.Append(hclDiags) 55 return config, loader, cleanup, diags 56 } 57 58 // MustLoadConfigForTests is a variant of LoadConfigForTests which calls 59 // t.Fatal (or similar) if there are any errors during loading, and thus 60 // does not return diagnostics at all. 61 // 62 // This is useful for concisely writing tests that don't expect errors at 63 // all. For tests that expect errors and need to assert against them, use 64 // LoadConfigForTests instead. 65 func MustLoadConfigForTests(t *testing.T, rootDir string) (*configs.Config, *configload.Loader, func()) { 66 t.Helper() 67 68 config, loader, cleanup, diags := LoadConfigForTests(t, rootDir) 69 if diags.HasErrors() { 70 cleanup() 71 t.Fatal(diags.Err()) 72 } 73 return config, loader, cleanup 74 }