From 63fbbf172bb11c6fbd5571a3b18d92d94d82ee4f Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 20 Mar 2026 15:56:35 +0100 Subject: [PATCH] command: test `state replace-provider` with const variables --- .../command/state_replace_provider_test.go | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/internal/command/state_replace_provider_test.go b/internal/command/state_replace_provider_test.go index 042586a163..f30ad07580 100644 --- a/internal/command/state_replace_provider_test.go +++ b/internal/command/state_replace_provider_test.go @@ -11,7 +11,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" @@ -378,6 +382,125 @@ func TestStateReplaceProvider_stateStore(t *testing.T) { } } +func TestStateReplaceProvider_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 := &StateReplaceProviderCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + View: view, + WorkingDir: wd, + }, + }, + } + + args := []string{ + "-auto-approve", + "hashicorp/test", + "testing-corp/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 := &StateReplaceProviderCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + View: view, + WorkingDir: wd, + }, + }, + } + + args := []string{ + "-auto-approve", + "-var", "module_name=child", + "hashicorp/test", + "testing-corp/test", + } + if code := c.Run(args); code != 0 { + t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + actual := strings.TrimSpace(testStateRead(t, "terraform.tfstate").String()) + expected := strings.TrimSpace(` + +module.child: + test_instance.test: + ID = + provider = provider["registry.terraform.io/testing-corp/test"] +`) + 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 := &StateReplaceProviderCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + View: view, + WorkingDir: wd, + }, + }, + } + + args := []string{ + "-auto-approve", + "hashicorp/test", + "testing-corp/test", + } + if code := c.Run(args); code != 0 { + t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + actual := strings.TrimSpace(testStateRead(t, "terraform.tfstate").String()) + expected := strings.TrimSpace(` + +module.child: + test_instance.test: + ID = + provider = provider["registry.terraform.io/testing-corp/test"] +`) + if diff := cmp.Diff(expected, actual); diff != "" { + t.Fatalf("unexpected state output\n%s", diff) + } + }) +} + func TestStateReplaceProvider_docs(t *testing.T) { c := &StateReplaceProviderCommand{}