99 "reflect"
1010 "strings"
1111
12- "github.com/hashicorp/go-version"
13-
1412 tfjson "github.com/hashicorp/terraform-json"
1513
1614 "github.com/google/go-cmp/cmp"
@@ -32,17 +30,8 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
3230 kind := step .ImportStateKind
3331 importStatePersist := step .ImportStatePersist
3432
35- if kind .plannable () {
36- // Instead of calling [t.Fatal], return an error. This package's unit tests can use [TestStep.ExpectError] to match on the error message.
37- // An alternative, [plugintest.TestExpectTFatal], does not have access to logged error messages, so it is open to false positives on this
38- // complex code path.
39- if err := requirePlannableImport (t , * helper .TerraformVersion ()); err != nil {
40- return err
41- }
42-
43- if importStatePersist {
44- return fmt .Errorf ("ImportStatePersist is not supported with plannable import blocks" )
45- }
33+ if err := importStatePreconditions (ctx , t , helper , step , cfgRaw ); err != nil {
34+ return err
4635 }
4736
4837 configRequest := teststep.PrepareConfigurationRequest {
@@ -408,15 +397,28 @@ func appendImportBlock(config string, resourceName string, importID string) stri
408397 resourceName , importID )
409398}
410399
411- func requirePlannableImport ( t testing.T , versionUnderTest version. Version ) error {
400+ func importStatePreconditions ( ctx context. Context , t testing.T , helper * plugintest. Helper , step TestStep , cfgRaw string ) error {
412401 t .Helper ()
413402
414- if versionUnderTest .LessThan (tfversion .Version1_5_0 ) {
403+ kind := step .ImportStateKind
404+ versionUnderTest := * helper .TerraformVersion ()
405+
406+ // Instead of calling [t.Fatal], we return an error. This package's unit tests can use [TestStep.ExpectError] to match
407+ // on the error message. An alternative, [plugintest.TestExpectTFatal], does not have access to logged error messages,
408+ // so it is open to false positives on this complex code path.
409+ switch {
410+ case kind .plannable () && versionUnderTest .LessThan (tfversion .Version1_5_0 ):
415411 return fmt .Errorf (
416412 `ImportState steps using plannable import blocks require Terraform 1.5.0 or later. Either ` +
417413 `upgrade the Terraform version running the test or add a ` + "`TerraformVersionChecks`" + ` to ` +
418414 `the test case to skip this test.` + "\n \n " +
419415 `https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/tfversion-checks#skip-version-checks` )
416+
417+ case kind .plannable () && step .ImportStatePersist :
418+ return fmt .Errorf (`ImportStatePersist is not supported with plannable import blocks` )
419+
420+ case kind .plannable () && step .ImportStateVerify :
421+ return fmt .Errorf (`ImportStateVerify is not supported with plannable import blocks` )
420422 }
421423
422424 return nil
0 commit comments