@@ -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