Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add state_key for test run blocks #36185

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,16 @@ func (runner *TestFileRunner) Test(file *moduletest.File) {
file.Status = moduletest.Error
continue // Abort!
}
}

if _, exists := runner.RelevantStates[key]; !exists {
runner.RelevantStates[key] = &TestFileState{
Run: nil,
State: states.NewState(),
}
if run.Config.StateKey != "" {
key = run.Config.StateKey
}

if _, exists := runner.RelevantStates[key]; !exists {
runner.RelevantStates[key] = &TestFileState{
Run: nil,
State: states.NewState(),
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ func TestTest_Runs(t *testing.T) {
expectedErr: []string{"Ephemeral resource instance has expired", "Ephemeral resources cannot be asserted"},
code: 1,
},
"with_state_key": {
expectedOut: []string{"3 passed, 1 failed."},
expectedErr: []string{"Test assertion failed", "resource renamed without moved block"},
code: 1,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
run "old_version" {
state_key = "test1"
}

run "new_code" {
state_key = "test1"
module {
source = "./breaking_change"
}
assert {
condition = test_resource.renamed_without_move.id == run.old_version.test_id
error_message = "resource renamed without moved block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "test_resource" "renamed_without_move" {
value = "test"
}

output "test_id" {
value = test_resource.renamed_without_move.id
}
11 changes: 11 additions & 0 deletions internal/command/testdata/test/with_state_key/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "test_resource" "test_id_moved" {
}

output "test_id" {
value = test_resource.test_id_moved.id
}

moved {
from = test_resource.test_id
to = test_resource.test_id_moved
}
14 changes: 14 additions & 0 deletions internal/command/testdata/test/with_state_key/moved.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
run "old_version" {
state_key = "test1"
module {
source = "./old_version"
}
}

run "new_code" {
state_key = "test1"
assert {
condition = test_resource.test_id_moved.id == run.old_version.test_id
error_message = "ressource_id differed"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "test_resource" "test_id" {
value = "test"
}

output "test_id" {
value = test_resource.test_id.id
}
8 changes: 8 additions & 0 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 @@ -606,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 @@ -807,6 +814,7 @@ var testRunBlockSchema = &hcl.BodySchema{
{Name: "command"},
{Name: "providers"},
{Name: "expect_failures"},
{Name: "state_key"},
},
Blocks: []hcl.BlockHeaderSchema{
{
Expand Down
Loading