From fd155ad890b9c95e6e5f5bcd9175a941d41d9281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Andrieux?= Date: Wed, 11 Dec 2024 16:05:40 +0100 Subject: [PATCH] fix: move `state_alias` to run block + rename to `state_key` --- internal/backend/local/test.go | 4 ++-- internal/command/test_test.go | 2 +- .../breaking_change.tftest.hcl | 8 ++------ .../breaking_change/main.tf | 0 .../{with_state_alias => with_state_key}/main.tf | 0 .../moved.tftest.hcl | 8 ++------ .../old_version/main.tf | 0 internal/configs/test_file.go | 16 ++++++++-------- 8 files changed, 15 insertions(+), 23 deletions(-) rename internal/command/testdata/test/{with_state_alias => with_state_key}/breaking_change.tftest.hcl (72%) rename internal/command/testdata/test/{with_state_alias => with_state_key}/breaking_change/main.tf (100%) rename internal/command/testdata/test/{with_state_alias => with_state_key}/main.tf (100%) rename internal/command/testdata/test/{with_state_alias => with_state_key}/moved.tftest.hcl (69%) rename internal/command/testdata/test/{with_state_alias => with_state_key}/old_version/main.tf (100%) diff --git a/internal/backend/local/test.go b/internal/backend/local/test.go index 6272fb6c67f1..9f95f139cc7a 100644 --- a/internal/backend/local/test.go +++ b/internal/backend/local/test.go @@ -348,8 +348,8 @@ func (runner *TestFileRunner) Test(file *moduletest.File) { } } - if run.Config.Options.StateAlias != "" { - key = run.Config.Options.StateAlias + if run.Config.StateKey != "" { + key = run.Config.StateKey } if _, exists := runner.RelevantStates[key]; !exists { diff --git a/internal/command/test_test.go b/internal/command/test_test.go index d714c4dd64da..06663e505e10 100644 --- a/internal/command/test_test.go +++ b/internal/command/test_test.go @@ -275,7 +275,7 @@ func TestTest_Runs(t *testing.T) { expectedErr: []string{"Ephemeral resource instance has expired", "Ephemeral resources cannot be asserted"}, code: 1, }, - "with_state_alias": { + "with_state_key": { expectedOut: []string{"3 passed, 1 failed."}, expectedErr: []string{"Test assertion failed", "resource renamed without moved block"}, code: 1, diff --git a/internal/command/testdata/test/with_state_alias/breaking_change.tftest.hcl b/internal/command/testdata/test/with_state_key/breaking_change.tftest.hcl similarity index 72% rename from internal/command/testdata/test/with_state_alias/breaking_change.tftest.hcl rename to internal/command/testdata/test/with_state_key/breaking_change.tftest.hcl index a8301aa7c09f..645788826124 100644 --- a/internal/command/testdata/test/with_state_alias/breaking_change.tftest.hcl +++ b/internal/command/testdata/test/with_state_key/breaking_change.tftest.hcl @@ -1,16 +1,12 @@ run "old_version" { - plan_options { - state_alias = "test1" - } + state_key = "test1" } run "new_code" { + state_key = "test1" module { source = "./breaking_change" } - plan_options { - state_alias = "test1" - } assert { condition = test_resource.renamed_without_move.id == run.old_version.test_id error_message = "resource renamed without moved block" diff --git a/internal/command/testdata/test/with_state_alias/breaking_change/main.tf b/internal/command/testdata/test/with_state_key/breaking_change/main.tf similarity index 100% rename from internal/command/testdata/test/with_state_alias/breaking_change/main.tf rename to internal/command/testdata/test/with_state_key/breaking_change/main.tf diff --git a/internal/command/testdata/test/with_state_alias/main.tf b/internal/command/testdata/test/with_state_key/main.tf similarity index 100% rename from internal/command/testdata/test/with_state_alias/main.tf rename to internal/command/testdata/test/with_state_key/main.tf diff --git a/internal/command/testdata/test/with_state_alias/moved.tftest.hcl b/internal/command/testdata/test/with_state_key/moved.tftest.hcl similarity index 69% rename from internal/command/testdata/test/with_state_alias/moved.tftest.hcl rename to internal/command/testdata/test/with_state_key/moved.tftest.hcl index ddfdca26a860..a8749c3b6a9f 100644 --- a/internal/command/testdata/test/with_state_alias/moved.tftest.hcl +++ b/internal/command/testdata/test/with_state_key/moved.tftest.hcl @@ -1,16 +1,12 @@ run "old_version" { + state_key = "test1" module { source = "./old_version" } - plan_options { - state_alias = "test1" - } } run "new_code" { - plan_options { - state_alias = "test1" - } + state_key = "test1" assert { condition = test_resource.test_id_moved.id == run.old_version.test_id error_message = "ressource_id differed" diff --git a/internal/command/testdata/test/with_state_alias/old_version/main.tf b/internal/command/testdata/test/with_state_key/old_version/main.tf similarity index 100% rename from internal/command/testdata/test/with_state_alias/old_version/main.tf rename to internal/command/testdata/test/with_state_key/old_version/main.tf diff --git a/internal/configs/test_file.go b/internal/configs/test_file.go index f3c2d995fa1e..6451939946a7 100644 --- a/internal/configs/test_file.go +++ b/internal/configs/test_file.go @@ -133,6 +133,8 @@ type TestRun struct { // run. ExpectFailures []hcl.Traversal + StateKey string + NameDeclRange hcl.Range VariablesDeclRange hcl.Range DeclRange hcl.Range @@ -275,8 +277,6 @@ type TestRunOptions struct { // Refresh is analogous to the -refresh=false Terraform plan option. Refresh bool - StateAlias string - // Replace is analogous to the -replace=ADDRESS Terraform plan option. Replace []hcl.Traversal @@ -608,6 +608,11 @@ func decodeTestRunBlock(block *hcl.Block) (*TestRun, hcl.Diagnostics) { r.ExpectFailures = failures } + if attr, exists := content.Attributes["state_key"]; exists { + rawDiags := gohcl.DecodeExpression(attr.Expr, nil, &r.StateKey) + diags = append(diags, rawDiags...) + } + return &r, diags } @@ -762,11 +767,6 @@ func decodeTestRunOptionsBlock(block *hcl.Block) (*TestRunOptions, hcl.Diagnosti opts.Target = tars } - if attr, exists := content.Attributes["state_alias"]; exists { - rawDiags := gohcl.DecodeExpression(attr.Expr, nil, &opts.StateAlias) - diags = append(diags, rawDiags...) - } - if !opts.Refresh && opts.Mode == RefreshOnlyTestMode { // These options are incompatible. diags = append(diags, &hcl.Diagnostic{ @@ -814,6 +814,7 @@ var testRunBlockSchema = &hcl.BodySchema{ {Name: "command"}, {Name: "providers"}, {Name: "expect_failures"}, + {Name: "state_key"}, }, Blocks: []hcl.BlockHeaderSchema{ { @@ -846,7 +847,6 @@ var testRunOptionsBlockSchema = &hcl.BodySchema{ {Name: "refresh"}, {Name: "replace"}, {Name: "target"}, - {Name: "state_alias"}, }, }