Skip to content

Commit b240685

Browse files
committed
replace unreleased plugin testing code w/ TODOs and normal Check funcs
1 parent e69be50 commit b240685

File tree

5 files changed

+67
-58
lines changed

5 files changed

+67
-58
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
3939
with:
4040
# TODO: Remove once Terraform 1.14 GA is released
41-
terraform_version: 1.14.0-rc1
41+
terraform_version: 1.14.0-rc2
4242
terraform_wrapper: false
4343

4444
- name: Generate

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ go 1.24.0
44

55
require (
66
github.com/google/go-cmp v0.7.0
7+
github.com/hashicorp/go-version v1.7.0
78
github.com/hashicorp/terraform-plugin-framework v1.16.1
89
github.com/hashicorp/terraform-plugin-framework-validators v0.19.0
910
github.com/hashicorp/terraform-plugin-go v0.29.0
1011
github.com/hashicorp/terraform-plugin-log v0.9.0
11-
github.com/hashicorp/terraform-plugin-testing v1.14.0-beta.1.0.20251029152858-203e6cc410a0
12+
github.com/hashicorp/terraform-plugin-testing v1.13.3
1213
)
1314

1415
require (
@@ -27,7 +28,6 @@ require (
2728
github.com/hashicorp/go-plugin v1.7.0 // indirect
2829
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
2930
github.com/hashicorp/go-uuid v1.0.3 // indirect
30-
github.com/hashicorp/go-version v1.7.0 // indirect
3131
github.com/hashicorp/hc-install v0.9.2 // indirect
3232
github.com/hashicorp/hcl/v2 v2.24.0 // indirect
3333
github.com/hashicorp/logutils v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9T
9393
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
9494
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1 h1:mlAq/OrMlg04IuJT7NpefI1wwtdpWudnEmjuQs04t/4=
9595
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1/go.mod h1:GQhpKVvvuwzD79e8/NZ+xzj+ZpWovdPAe8nfV/skwNU=
96-
github.com/hashicorp/terraform-plugin-testing v1.14.0-beta.1.0.20251029152858-203e6cc410a0 h1:2ZfVb9DwefNk/aN3uJZLIADETfQOdxtOrDZ6iLGtx8o=
97-
github.com/hashicorp/terraform-plugin-testing v1.14.0-beta.1.0.20251029152858-203e6cc410a0/go.mod h1:UrIjRAJLN0kygs0miY1Moy4PxUzy2e9R5WxyRk8aliI=
96+
github.com/hashicorp/terraform-plugin-testing v1.13.3 h1:QLi/khB8Z0a5L54AfPrHukFpnwsGL8cwwswj4RZduCo=
97+
github.com/hashicorp/terraform-plugin-testing v1.13.3/go.mod h1:WHQ9FDdiLoneey2/QHpGM/6SAYf4A7AZazVg7230pLE=
9898
github.com/hashicorp/terraform-registry-address v0.4.0 h1:S1yCGomj30Sao4l5BMPjTGZmCNzuv7/GDTDX99E9gTk=
9999
github.com/hashicorp/terraform-registry-address v0.4.0/go.mod h1:LRS1Ay0+mAiRkUyltGT+UHWkIqTFvigGn/LbMshfflE=
100100
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=

internal/provider/action_local_command.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func (a *localCommandAction) Metadata(ctx context.Context, req action.MetadataRe
3535

3636
func (a *localCommandAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
3737
resp.Schema = schema.Schema{
38-
// TODO: Once we have a local_command data source, reference that to be used if the user needs to the consume the output of the command (and it's idempotent)
3938
MarkdownDescription: "Invokes an executable on the local machine. All environment variables visible to the Terraform process are passed through " +
4039
"to the child process. After the child process successfully executes, the `stdout` will be returned for Terraform to display to the user.\n\n" +
4140
"Any non-zero exit code will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available.",

internal/provider/action_local_command_test.go

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"testing"
1414

1515
"github.com/google/go-cmp/cmp"
16+
"github.com/hashicorp/go-version"
1617
"github.com/hashicorp/terraform-plugin-testing/config"
1718
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-testing/terraform"
1820
"github.com/hashicorp/terraform-plugin-testing/tfversion"
1921
)
2022

@@ -35,7 +37,7 @@ func TestLocalCommandAction_bash(t *testing.T) {
3537
resource.UnitTest(t, resource.TestCase{
3638
// Actions are only available in 1.14 and later
3739
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
38-
tfversion.SkipBelow(tfversion.Version1_14_0),
40+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
3941
},
4042
ProtoV5ProviderFactories: protoV5ProviderFactories(),
4143
Steps: []resource.TestStep{
@@ -45,13 +47,12 @@ func TestLocalCommandAction_bash(t *testing.T) {
4547
"scripts_folder_path": config.StringVariable(testScriptsDir),
4648
},
4749
ConfigDirectory: config.StaticDirectory(bashTestDirectory),
48-
// TODO: Currently action checks don't exist, but eventually we can run these on the progress messages
49-
// https://github.com/hashicorp/terraform-plugin-testing/pull/570
50-
// ActionChecks: []actioncheck.ActionCheck{
51-
// actioncheck.ExpectProgressCount("local_command", 1),
52-
// actioncheck.ExpectProgressMessageContains("local_command", "Hello !"),
53-
// },
54-
PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
50+
Check: func(s *terraform.State) error {
51+
return assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent)
52+
},
53+
// TODO: use this when PostApplyFunc is released in terraform-plugin-testing
54+
//
55+
// PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
5556
},
5657
},
5758
})
@@ -71,7 +72,7 @@ func TestLocalCommandAction_bash_stdin(t *testing.T) {
7172
resource.UnitTest(t, resource.TestCase{
7273
// Actions are only available in 1.14 and later
7374
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
74-
tfversion.SkipBelow(tfversion.Version1_14_0),
75+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
7576
},
7677
ProtoV5ProviderFactories: protoV5ProviderFactories(),
7778
Steps: []resource.TestStep{
@@ -82,13 +83,11 @@ func TestLocalCommandAction_bash_stdin(t *testing.T) {
8283
"scripts_folder_path": config.StringVariable(testScriptsDir),
8384
},
8485
ConfigDirectory: config.StaticDirectory(bashTestDirectory),
85-
// TODO: Currently action checks don't exist, but eventually we can run these on the progress messages
86-
// https://github.com/hashicorp/terraform-plugin-testing/pull/570
87-
// ActionChecks: []actioncheck.ActionCheck{
88-
// actioncheck.ExpectProgressCount("local_command", 1),
89-
// actioncheck.ExpectProgressMessageContains("local_command", fmt.Sprintf("Hello %s!", stdin)),
90-
// },
91-
PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
86+
Check: func(s *terraform.State) error {
87+
return assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent)
88+
},
89+
// TODO: use this when PostApplyFunc is released in terraform-plugin-testing
90+
// PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
9291
},
9392
},
9493
})
@@ -111,7 +110,7 @@ func TestLocalCommandAction_bash_all(t *testing.T) {
111110
resource.UnitTest(t, resource.TestCase{
112111
// Actions are only available in 1.14 and later
113112
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
114-
tfversion.SkipBelow(tfversion.Version1_14_0),
113+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
115114
},
116115
ProtoV5ProviderFactories: protoV5ProviderFactories(),
117116
Steps: []resource.TestStep{
@@ -127,13 +126,11 @@ func TestLocalCommandAction_bash_all(t *testing.T) {
127126
),
128127
},
129128
ConfigDirectory: config.StaticDirectory(bashTestDirectory),
130-
// TODO: Currently action checks don't exist, but eventually we can run these on the progress messages
131-
// https://github.com/hashicorp/terraform-plugin-testing/pull/570
132-
// ActionChecks: []actioncheck.ActionCheck{
133-
// actioncheck.ExpectProgressCount("local_command", 1),
134-
// actioncheck.ExpectProgressMessageContains("local_command", fmt.Sprintf("Hello %s!", stdin)),
135-
// },
136-
PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
129+
Check: func(s *terraform.State) error {
130+
return assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent)
131+
},
132+
// TODO: use this when PostApplyFunc is released in terraform-plugin-testing
133+
// PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
137134
},
138135
},
139136
})
@@ -155,7 +152,7 @@ func TestLocalCommandAction_bash_null_args(t *testing.T) {
155152
resource.UnitTest(t, resource.TestCase{
156153
// Actions are only available in 1.14 and later
157154
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
158-
tfversion.SkipBelow(tfversion.Version1_14_0),
155+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
159156
},
160157
ProtoV5ProviderFactories: protoV5ProviderFactories(),
161158
Steps: []resource.TestStep{
@@ -170,13 +167,11 @@ func TestLocalCommandAction_bash_null_args(t *testing.T) {
170167
),
171168
},
172169
ConfigDirectory: config.TestNameDirectory(),
173-
// TODO: Currently action checks don't exist, but eventually we can run these on the progress messages
174-
// https://github.com/hashicorp/terraform-plugin-testing/pull/570
175-
// ActionChecks: []actioncheck.ActionCheck{
176-
// actioncheck.ExpectProgressCount("local_command", 1),
177-
// actioncheck.ExpectProgressMessageContains("local_command", "Hello !"),
178-
// },
179-
PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
170+
Check: func(s *terraform.State) error {
171+
return assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent)
172+
},
173+
// TODO: use this when PostApplyFunc is released in terraform-plugin-testing
174+
// PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
180175
},
181176
},
182177
})
@@ -200,7 +195,7 @@ func TestLocalCommandAction_absolute_path_bash(t *testing.T) {
200195
resource.UnitTest(t, resource.TestCase{
201196
// Actions are only available in 1.14 and later
202197
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
203-
tfversion.SkipBelow(tfversion.Version1_14_0),
198+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
204199
},
205200
ProtoV5ProviderFactories: protoV5ProviderFactories(),
206201
Steps: []resource.TestStep{
@@ -211,13 +206,11 @@ func TestLocalCommandAction_absolute_path_bash(t *testing.T) {
211206
"scripts_folder_path": config.StringVariable(testScriptsDir),
212207
},
213208
ConfigDirectory: config.StaticDirectory(bashTestDirectory),
214-
// TODO: Currently action checks don't exist, but eventually we can run these on the progress messages
215-
// https://github.com/hashicorp/terraform-plugin-testing/pull/570
216-
// ActionChecks: []actioncheck.ActionCheck{
217-
// actioncheck.ExpectProgressCount("local_command", 1),
218-
// actioncheck.ExpectProgressMessageContains("local_command", "Hello !"),
219-
// },
220-
PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
209+
Check: func(s *terraform.State) error {
210+
return assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent)
211+
},
212+
// TODO: use this when PostApplyFunc is released in terraform-plugin-testing
213+
// PostApplyFunc: assertTestFile(t, filepath.Join(tempDir, "test_file.txt"), expectedFileContent),
221214
},
222215
},
223216
})
@@ -228,7 +221,7 @@ func TestLocalCommandAction_not_found(t *testing.T) {
228221
resource.UnitTest(t, resource.TestCase{
229222
// Actions are only available in 1.14 and later
230223
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
231-
tfversion.SkipBelow(tfversion.Version1_14_0),
224+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
232225
},
233226
ProtoV5ProviderFactories: protoV5ProviderFactories(),
234227
Steps: []resource.TestStep{
@@ -265,7 +258,7 @@ func TestLocalCommandAction_stderr(t *testing.T) {
265258
resource.UnitTest(t, resource.TestCase{
266259
// Actions are only available in 1.14 and later
267260
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
268-
tfversion.SkipBelow(tfversion.Version1_14_0),
261+
tfversion.SkipBelow(version.Must(version.NewVersion("1.14.0"))), // TODO: replace with tfversion.Version1_14_0 when new plugin-testing version is released
269262
},
270263
ProtoV5ProviderFactories: protoV5ProviderFactories(),
271264
Steps: []resource.TestStep{
@@ -280,17 +273,34 @@ func TestLocalCommandAction_stderr(t *testing.T) {
280273
})
281274
}
282275

283-
func assertTestFile(t *testing.T, filePath, expectedContent string) func() {
284-
return func() {
285-
t.Helper()
276+
// TODO: use this function when PostApplyFunc is released in terraform-plugin-testing
277+
//
278+
// func assertTestFile(t *testing.T, filePath, expectedContent string) func() {
279+
// return func() {
280+
// t.Helper()
281+
282+
// testFile, err := os.ReadFile(filePath)
283+
// if err != nil {
284+
// t.Fatalf("error trying to read created test file: %s", err)
285+
// }
286286

287-
testFile, err := os.ReadFile(filePath)
288-
if err != nil {
289-
t.Fatalf("error trying to read created test file: %s", err)
290-
}
287+
// if diff := cmp.Diff(expectedContent, string(testFile)); diff != "" {
288+
// t.Fatalf("unexpected file diff (-expected, +got): %s", diff)
289+
// }
290+
// }
291+
// }
291292

292-
if diff := cmp.Diff(expectedContent, string(testFile)); diff != "" {
293-
t.Fatalf("unexpected file diff (-expected, +got): %s", diff)
294-
}
293+
func assertTestFile(t *testing.T, filePath, expectedContent string) error {
294+
t.Helper()
295+
296+
testFile, err := os.ReadFile(filePath)
297+
if err != nil {
298+
return fmt.Errorf("error trying to read created test file: %s", err)
295299
}
300+
301+
if diff := cmp.Diff(expectedContent, string(testFile)); diff != "" {
302+
return fmt.Errorf("unexpected file diff (-expected, +got): %s", diff)
303+
}
304+
305+
return nil
296306
}

0 commit comments

Comments
 (0)