From 017c437bd234d36eb44dc483c08b93e95e0f5ae0 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Tue, 12 May 2026 13:44:48 +0200 Subject: [PATCH] Add test case for variable value without config --- internal/command/init_test.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/command/init_test.go b/internal/command/init_test.go index a501643e61..38b5577a1e 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -6110,7 +6110,7 @@ func TestInit_configErrorsImpactingStateStore(t *testing.T) { code := initCmd.Run(args) testOutput := done(t) if code != 1 { - t.Fatalf("expected apply to fail with code 1, got code %d: \n%s", code, testOutput.All()) + t.Fatalf("expected init to fail with code 1, got code %d: \n%s", code, testOutput.All()) } log.Printf("[TRACE] TestInit_configErrorsImpactingStateStore: init complete") t.Logf("init output:\n%s", testOutput.Stdout()) @@ -6130,6 +6130,36 @@ func TestInit_configErrorsImpactingStateStore(t *testing.T) { } } +func TestInit_varValueWithoutConfig(t *testing.T) { + td := t.TempDir() + t.Chdir(td) + cfg1 := `terraform {}` + + if err := os.WriteFile(filepath.Join(td, "main.tf"), []byte(cfg1), 0644); err != nil { + t.Fatalf("err: %s", err) + } + + ui := cli.NewMockUi() + view, done := testView(t) + initCmd := &InitCommand{ + Meta: Meta{ + Ui: ui, + View: view, + }, + } + + code := initCmd.Run([]string{"-var", "foo=bar"}) + testOutput := done(t) + if code != 1 { + t.Fatalf("expected init to fail with code 1, got code %d: \n%s", code, testOutput.All()) + } + + expectedErr := "Error: Value for undeclared variable" + if !strings.Contains(cleanString(testOutput.Stderr()), expectedErr) { + t.Fatalf("unexpected error, expected %q, given: %s", expectedErr, testOutput.Stderr()) + } +} + // newMockProviderSource is a helper to succinctly construct a mock provider // source that contains a set of packages matching the given provider versions // that are available for installation (from temporary local files).