From d074d148e844d1844738bc92ca9687dd50165a5f Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Mon, 13 Jan 2025 16:16:33 +0100 Subject: [PATCH] Hide ephemeral values in diagnostics output (#36314) * Hide ephemeral values in diagnostics output This is more in line with the rest of the implementation * Add test for CompactValueStr --- internal/tfdiags/format.go | 4 +- internal/tfdiags/format_test.go | 110 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 internal/tfdiags/format_test.go diff --git a/internal/tfdiags/format.go b/internal/tfdiags/format.go index c7e6bdc97c..b696f570d0 100644 --- a/internal/tfdiags/format.go +++ b/internal/tfdiags/format.go @@ -39,10 +39,10 @@ func CompactValueStr(val cty.Value) string { // that are both ephemeral and sensitive should have both markings // and should therefore get caught by the marks.Sensitive case // above. - continue + return "(ephemeral value)" default: // We don't know about any other marks, so we'll be conservative. - // This shouldn't actuallyr eachable since the caller should've + // This shouldn't actually reachable since the caller should've // checked this and skipped calling compactValueStr anyway. return "value with unrecognized marks (this is a bug in Terraform)" } diff --git a/internal/tfdiags/format_test.go b/internal/tfdiags/format_test.go new file mode 100644 index 0000000000..b88e6b886b --- /dev/null +++ b/internal/tfdiags/format_test.go @@ -0,0 +1,110 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package tfdiags + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/internal/lang/marks" + "github.com/zclconf/go-cty/cty" +) + +func TestCompactValueStr(t *testing.T) { + tests := []struct { + Val cty.Value + Want string + }{ + { + cty.NullVal(cty.DynamicPseudoType), + "null", + }, + { + cty.UnknownVal(cty.DynamicPseudoType), + "(not yet known)", + }, + { + cty.False, + "false", + }, + { + cty.True, + "true", + }, + { + cty.NumberIntVal(5), + "5", + }, + { + cty.NumberFloatVal(5.2), + "5.2", + }, + { + cty.StringVal(""), + `""`, + }, + { + cty.StringVal("hello"), + `"hello"`, + }, + { + cty.ListValEmpty(cty.String), + "empty list of string", + }, + { + cty.SetValEmpty(cty.String), + "empty set of string", + }, + { + cty.EmptyTupleVal, + "empty tuple", + }, + { + cty.MapValEmpty(cty.String), + "empty map of string", + }, + { + cty.EmptyObjectVal, + "object with no attributes", + }, + { + cty.ListVal([]cty.Value{cty.StringVal("a")}), + "list of string with 1 element", + }, + { + cty.ListVal([]cty.Value{cty.StringVal("a"), cty.StringVal("b")}), + "list of string with 2 elements", + }, + { + cty.ObjectVal(map[string]cty.Value{ + "a": cty.StringVal("b"), + }), + `object with 1 attribute "a"`, + }, + { + cty.ObjectVal(map[string]cty.Value{ + "a": cty.StringVal("b"), + "c": cty.StringVal("d"), + }), + "object with 2 attributes", + }, + { + cty.StringVal("a sensitive value").Mark(marks.Sensitive), + "(sensitive value)", + }, + { + cty.StringVal("an ephemeral value").Mark(marks.Ephemeral), + "(ephemeral value)", + }, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("%#v", test.Val), func(t *testing.T) { + got := CompactValueStr(test.Val) + if got != test.Want { + t.Errorf("wrong result\nvalue: %#v\ngot: %s\nwant: %s", test.Val, got, test.Want) + } + }) + } +}