Skip to content

Commit

Permalink
fix: move state_alias to run block + rename to state_key
Browse files Browse the repository at this point in the history
  • Loading branch information
kevineor committed Dec 11, 2024
1 parent 3a17ab1 commit fd155ad
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 23 deletions.
4 changes: 2 additions & 2 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 8 additions & 8 deletions internal/configs/test_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type TestRun struct {
// run.
ExpectFailures []hcl.Traversal

StateKey string

NameDeclRange hcl.Range
VariablesDeclRange hcl.Range
DeclRange hcl.Range
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -814,6 +814,7 @@ var testRunBlockSchema = &hcl.BodySchema{
{Name: "command"},
{Name: "providers"},
{Name: "expect_failures"},
{Name: "state_key"},
},
Blocks: []hcl.BlockHeaderSchema{
{
Expand Down Expand Up @@ -846,7 +847,6 @@ var testRunOptionsBlockSchema = &hcl.BodySchema{
{Name: "refresh"},
{Name: "replace"},
{Name: "target"},
{Name: "state_alias"},
},
}

Expand Down

0 comments on commit fd155ad

Please sign in to comment.