-
Notifications
You must be signed in to change notification settings - Fork 17
query: refactor query test step logic into testStepNewQuery()
#568
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1488e4e
Add test for list validation diagnostics
SBGoods e447089
Move query error handling to `RunQueryChecks()`
SBGoods 2f5a039
Remove query error filtering from `wd.Query()`
SBGoods 18dffe0
Refactor query check logic into `testStepNewQuery()`
SBGoods 13b7a39
Merge branch 'main' into SBGoods/query-expect-error
SBGoods 7c7e498
Add copyright headers
SBGoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package resource | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| tfjson "github.com/hashicorp/terraform-json" | ||
| "github.com/mitchellh/go-testing-interface" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource/query" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" | ||
| "github.com/hashicorp/terraform-plugin-testing/internal/teststep" | ||
| ) | ||
|
|
||
| func testStepNewQuery(ctx context.Context, t testing.T, wd *plugintest.WorkingDir, step TestStep, providers *providerFactories) error { | ||
| t.Helper() | ||
|
|
||
| queryConfigRequest := teststep.ConfigurationRequest{ | ||
| Raw: &step.Config, | ||
| } | ||
| err := wd.SetQuery(ctx, teststep.Configuration(queryConfigRequest), step.ConfigVariables) | ||
| if err != nil { | ||
| return fmt.Errorf("Error setting query config: %w", err) | ||
| } | ||
|
|
||
| err = runProviderCommand(ctx, t, wd, providers, func() error { | ||
| return wd.Init(ctx) | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Error getting init: %s", err) | ||
| } | ||
|
|
||
| var queryOut []tfjson.LogMsg | ||
| err = runProviderCommand(ctx, t, wd, providers, func() error { | ||
| var err error | ||
| queryOut, err = wd.Query(ctx) | ||
| return err | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return query.RunQueryChecks(ctx, t, queryOut, step.QueryResultChecks) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
Queryis using the machine readable output, the error message it is running the regex on with that mode is the formatted%+vof the diagnostics, which is different then other modes that use the formatted Terraform CLI output of the diagnostics. That can make the output hard to read / different to match than the other modes:--- FAIL: TestQuery_ExpectError_ValidationError (0.49s) /Users/austin.valle/code/terraform-plugin-testing/helper/resource/query/query_test.go:111: Step 2/2 error running query, expected an error with pattern (Not the correct diagnostic summary), no match on: running terraform query command returned diagnostics: [{baseLogMessage:{Lvl:error Msg:Error: Diagnostic summary Time:2025-10-29 09:10:02.151561 -0400 EDT} Diagnostic:{Severity:error Summary:Diagnostic summary Detail:Diagnostic details Range:0x140004547c0 Snippet:0x14000030640}} {baseLogMessage:{Lvl:error Msg:Error: Diagnostic summary Time:2025-10-29 09:10:02.151735 -0400 EDT} Diagnostic:{Severity:error Summary:Diagnostic summary Detail:Diagnostic details Range:0x14000454900 Snippet:0x14000030690}}]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a good point. Maybe we should introduce a string formatter for machine readable error messaging in a follow-up PR.