Skip to content

Commit 787385c

Browse files
committed
omit test cleanup when state is empty
1 parent 309005b commit 787385c

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

helper/resource/testing_new.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ import (
1414
tftest "github.com/hashicorp/terraform-plugin-test"
1515
)
1616

17-
func getState(t *testing.T, wd *tftest.WorkingDir) *terraform.State {
18-
jsonState := wd.RequireState(t)
19-
state, err := shimStateFromJson(jsonState)
20-
if err != nil {
21-
t.Fatal(err)
17+
func runPostTestDestroy(t *testing.T, c TestCase, wd *tftest.WorkingDir) error {
18+
wd.RequireDestroy(t)
19+
20+
if c.CheckDestroy != nil {
21+
statePostDestroy := getState(t, wd)
22+
23+
if err := c.CheckDestroy(statePostDestroy); err != nil {
24+
return err
25+
}
2226
}
23-
return state
27+
28+
return nil
2429
}
2530

2631
func RunNewTest(t *testing.T, c TestCase, providers map[string]terraform.ResourceProvider) {
@@ -29,15 +34,12 @@ func RunNewTest(t *testing.T, c TestCase, providers map[string]terraform.Resourc
2934
wd := acctest.TestHelper.RequireNewWorkingDir(t)
3035

3136
defer func() {
32-
wd.RequireDestroy(t)
37+
statePreDestroy := getState(t, wd)
3338

34-
if c.CheckDestroy != nil {
35-
statePostDestroy := getState(t, wd)
36-
37-
if err := c.CheckDestroy(statePostDestroy); err != nil {
38-
t.Fatal(err)
39-
}
39+
if !stateIsEmpty(statePreDestroy) {
40+
runPostTestDestroy(t, c, wd)
4041
}
42+
4143
wd.Close()
4244
}()
4345

@@ -98,6 +100,19 @@ func RunNewTest(t *testing.T, c TestCase, providers map[string]terraform.Resourc
98100
}
99101
}
100102

103+
func getState(t *testing.T, wd *tftest.WorkingDir) *terraform.State {
104+
jsonState := wd.RequireState(t)
105+
state, err := shimStateFromJson(jsonState)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
return state
110+
}
111+
112+
func stateIsEmpty(state *terraform.State) bool {
113+
return state.Empty() || !state.HasResources()
114+
}
115+
101116
func planIsEmpty(plan *tfjson.Plan) bool {
102117
for _, rc := range plan.ResourceChanges {
103118
if rc.Mode == tfjson.DataResourceMode {
@@ -114,6 +129,7 @@ func planIsEmpty(plan *tfjson.Plan) bool {
114129
}
115130
return true
116131
}
132+
117133
func testIDRefresh(c TestCase, t *testing.T, wd *tftest.WorkingDir, step TestStep, r *terraform.ResourceState) error {
118134
spewConf := spew.NewDefaultConfig()
119135
spewConf.SortKeys = true

0 commit comments

Comments
 (0)