From 2eb9fe90c8abc9aa148696a401ee9c40156530bd Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 20 Mar 2026 15:52:29 +0100 Subject: [PATCH] command: test `state rm` with const variables --- internal/command/state_rm_test.go | 98 +++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/internal/command/state_rm_test.go b/internal/command/state_rm_test.go index a29753af12..4dc8925a21 100644 --- a/internal/command/state_rm_test.go +++ b/internal/command/state_rm_test.go @@ -12,7 +12,11 @@ import ( "github.com/hashicorp/cli" + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform/internal/addrs" + "github.com/hashicorp/terraform/internal/backend" + backendInit "github.com/hashicorp/terraform/internal/backend/init" "github.com/hashicorp/terraform/internal/providers" "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/states/statefile" @@ -478,6 +482,100 @@ func TestStateRm_noState(t *testing.T) { } } +func TestStateRm_constVariable(t *testing.T) { + t.Run("missing value", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + view, _ := testView(t) + c := &StateRmCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + View: view, + WorkingDir: wd, + }, + }, + } + + args := []string{"module.child.test_instance.test"} + if code := c.Run(args); code == 0 { + t.Fatalf("expected error, got 0") + } + + errStr := ui.ErrorWriter.String() + if !strings.Contains(errStr, "No value for required variable") { + t.Fatalf("expected missing variable error, got: %s", errStr) + } + }) + + t.Run("value via cli", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + view, _ := testView(t) + c := &StateRmCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + View: view, + WorkingDir: wd, + }, + }, + } + + args := []string{"-var", "module_name=child", "module.child.test_instance.test"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + actual := strings.TrimSpace(testStateRead(t, "terraform.tfstate").String()) + expected := strings.TrimSpace(``) + if diff := cmp.Diff(expected, actual); diff != "" { + t.Fatalf("unexpected state output\n%s", diff) + } + }) + + t.Run("value via backend", func(t *testing.T) { + mockBackend := TestNewVariableBackend(map[string]string{ + "module_name": "child", + }) + backendInit.Set("local-vars", func() backend.Backend { return mockBackend }) + defer backendInit.Set("local-vars", nil) + + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var-backend") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + view, _ := testView(t) + c := &StateRmCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + View: view, + WorkingDir: wd, + }, + }, + } + + args := []string{"module.child.test_instance.test"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + actual := strings.TrimSpace(testStateRead(t, "terraform.tfstate").String()) + expected := strings.TrimSpace(``) + if diff := cmp.Diff(expected, actual); diff != "" { + t.Fatalf("unexpected state output\n%s", diff) + } + }) +} + func TestStateRm_needsInit(t *testing.T) { td := t.TempDir() testCopyDir(t, testFixturePath("backend-change"), td)