From d1ea20ac1ae305679fb6654d868a0dfce7e801dd Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 30 Apr 2026 12:30:14 +0100 Subject: [PATCH] command/output: Add regression test Co-authored-by: Daniel Schmidt --- internal/command/output_test.go | 82 +++++++++++++++++++ .../.terraform/terraform.tfstate | 12 +++ .../output-backend-with-deprecation/main.tf | 7 ++ 3 files changed, 101 insertions(+) create mode 100644 internal/command/testdata/output-backend-with-deprecation/.terraform/terraform.tfstate create mode 100644 internal/command/testdata/output-backend-with-deprecation/main.tf diff --git a/internal/command/output_test.go b/internal/command/output_test.go index 5cff3f4e6f..3b0666bbd8 100644 --- a/internal/command/output_test.go +++ b/internal/command/output_test.go @@ -13,9 +13,13 @@ import ( "github.com/zclconf/go-cty/cty" "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/backend/remote-state/inmem" "github.com/hashicorp/terraform/internal/providers" "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/states/statefile" + "github.com/hashicorp/terraform/internal/tfdiags" ) func TestOutput(t *testing.T) { @@ -381,3 +385,81 @@ func TestOutput_stateDefault(t *testing.T) { t.Fatalf("bad: %#v", actual) } } + +// deprecatedInmemBackend wraps the inmem backend and injects a deprecation +// warning from PrepareConfig, simulating a backend with deprecated attributes +// (like the S3 backend's dynamodb_table). +type deprecatedInmemBackend struct { + backend.Backend +} + +func (b *deprecatedInmemBackend) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) { + newObj, diags := b.Backend.PrepareConfig(obj) + diags = diags.Append(tfdiags.SimpleWarning(`The attribute "deprecated_attr" is deprecated.`)) + return newObj, diags +} + +func TestOutputRaw_warningsSuppressed(t *testing.T) { + // Pre-populate the inmem backend with a state containing an output value + inmem.Reset() + originalState := states.BuildState(func(s *states.SyncState) { + s.SetOutputValue( + addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance), + cty.StringVal("bar"), + false, + ) + }) + + // Register a backend that wraps inmem with a deprecation warning, + // simulating a backend like S3 whose PrepareConfig warns about + // deprecated attributes (e.g. dynamodb_table). + backendInit.Set("inmem", func() backend.Backend { + return &deprecatedInmemBackend{Backend: inmem.New()} + }) + defer backendInit.Set("inmem", inmem.New) + + td := t.TempDir() + testCopyDir(t, testFixturePath("output-backend-with-deprecation"), td) + t.Chdir(td) + + // Write the state into the inmem backend's default workspace + b := inmem.New() + b.Configure(cty.ObjectVal(map[string]cty.Value{ + "lock_id": cty.NullVal(cty.String), + })) + sMgr, sDiags := b.StateMgr(backend.DefaultStateName) + if sDiags.HasErrors() { + t.Fatalf("unexpected error: %s", sDiags.Err()) + } + sMgr.WriteState(originalState) + if err := sMgr.PersistState(nil); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + view, done := testView(t) + c := &OutputCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + View: view, + }, + } + + args := []string{"-raw", "foo"} + code := c.Run(args) + output := done(t) + if code != 0 { + t.Fatalf("unexpected exit code %d\nstderr:\n%s", code, output.Stderr()) + } + + // The key assertion: warnings must not appear in raw output + // as they would be indistinguishable from the value. + stderr := output.Stderr() + if strings.Contains(stderr, "deprecated") { + t.Fatalf("warnings should be suppressed, got:\n%s", stderr) + } + + actual := strings.TrimSpace(output.Stdout()) + if actual != `bar` { + t.Fatalf("expected output \"bar\", got: %#v", actual) + } +} diff --git a/internal/command/testdata/output-backend-with-deprecation/.terraform/terraform.tfstate b/internal/command/testdata/output-backend-with-deprecation/.terraform/terraform.tfstate new file mode 100644 index 0000000000..1c555fbae0 --- /dev/null +++ b/internal/command/testdata/output-backend-with-deprecation/.terraform/terraform.tfstate @@ -0,0 +1,12 @@ +{ + "version": 3, + "serial": 0, + "lineage": "test-output-deprecation", + "backend": { + "type": "inmem", + "config": { + "lock_id": null + }, + "hash": 3947750061 + } +} diff --git a/internal/command/testdata/output-backend-with-deprecation/main.tf b/internal/command/testdata/output-backend-with-deprecation/main.tf new file mode 100644 index 0000000000..cf5976f8d5 --- /dev/null +++ b/internal/command/testdata/output-backend-with-deprecation/main.tf @@ -0,0 +1,7 @@ +terraform { + backend "inmem" {} +} + +output "foo" { + value = "bar" +}