@@ -3,6 +3,7 @@ package plugintest
33import (
44 "bytes"
55 "context"
6+ "encoding/json"
67 "errors"
78 "fmt"
89 "io/ioutil"
@@ -15,8 +16,9 @@ import (
1516)
1617
1718const (
18- ConfigFileName = "terraform_plugin_test.tf"
19- PlanFileName = "tfplan"
19+ ConfigFileName = "terraform_plugin_test.tf"
20+ ConfigFileNameJSON = ConfigFileName + ".json"
21+ PlanFileName = "tfplan"
2022)
2123
2224// WorkingDir represents a distinct working directory that can be used for
@@ -29,6 +31,10 @@ type WorkingDir struct {
2931 // baseDir is the root of the working directory tree
3032 baseDir string
3133
34+ // configFilename is the full filename where the latest configuration
35+ // was stored; empty until SetConfig is called.
36+ configFilename string
37+
3238 // baseArgs is arguments that should be appended to all commands
3339 baseArgs []string
3440
@@ -85,11 +91,20 @@ func (wd *WorkingDir) GetHelper() *Helper {
8591// Destroy to establish the configuration. Any previously-set configuration is
8692// discarded and any saved plan is cleared.
8793func (wd * WorkingDir ) SetConfig (ctx context.Context , cfg string ) error {
88- configFilename := filepath .Join (wd .baseDir , ConfigFileName )
89- err := ioutil .WriteFile (configFilename , []byte (cfg ), 0700 )
94+ outFilename := filepath .Join (wd .baseDir , ConfigFileName )
95+ rmFilename := filepath .Join (wd .baseDir , ConfigFileNameJSON )
96+ bCfg := []byte (cfg )
97+ if json .Valid (bCfg ) {
98+ outFilename , rmFilename = rmFilename , outFilename
99+ }
100+ if err := os .Remove (rmFilename ); err != nil && ! os .IsNotExist (err ) {
101+ return fmt .Errorf ("unable to remove %q: %w" , rmFilename , err )
102+ }
103+ err := ioutil .WriteFile (outFilename , bCfg , 0700 )
90104 if err != nil {
91105 return err
92106 }
107+ wd .configFilename = outFilename
93108
94109 var mismatch * tfexec.ErrVersionMismatch
95110 err = wd .tf .SetDisablePluginTLS (true )
@@ -158,11 +173,16 @@ func (wd *WorkingDir) ClearPlan(ctx context.Context) error {
158173 return nil
159174}
160175
176+ var errWorkingDirSetConfigNotCalled = fmt .Errorf ("must call SetConfig before Init" )
177+
161178// Init runs "terraform init" for the given working directory, forcing Terraform
162179// to use the current version of the plugin under test.
163180func (wd * WorkingDir ) Init (ctx context.Context ) error {
164- if _ , err := os .Stat (wd .configFilename ()); err != nil {
165- return fmt .Errorf ("must call SetConfig before Init" )
181+ if wd .configFilename == "" {
182+ return errWorkingDirSetConfigNotCalled
183+ }
184+ if _ , err := os .Stat (wd .configFilename ); err != nil {
185+ return errWorkingDirSetConfigNotCalled
166186 }
167187
168188 logging .HelperResourceTrace (ctx , "Calling Terraform CLI init command" )
@@ -174,10 +194,6 @@ func (wd *WorkingDir) Init(ctx context.Context) error {
174194 return err
175195}
176196
177- func (wd * WorkingDir ) configFilename () string {
178- return filepath .Join (wd .baseDir , ConfigFileName )
179- }
180-
181197func (wd * WorkingDir ) planFilename () string {
182198 return filepath .Join (wd .baseDir , PlanFileName )
183199}
0 commit comments