@@ -3,6 +3,7 @@ package plugintest
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "encoding/json"
6
7
"errors"
7
8
"fmt"
8
9
"io/ioutil"
@@ -15,8 +16,9 @@ import (
15
16
)
16
17
17
18
const (
18
- ConfigFileName = "terraform_plugin_test.tf"
19
- PlanFileName = "tfplan"
19
+ ConfigFileName = "terraform_plugin_test.tf"
20
+ ConfigFileNameJSON = ConfigFileName + ".json"
21
+ PlanFileName = "tfplan"
20
22
)
21
23
22
24
// WorkingDir represents a distinct working directory that can be used for
@@ -29,6 +31,10 @@ type WorkingDir struct {
29
31
// baseDir is the root of the working directory tree
30
32
baseDir string
31
33
34
+ // configFilename is the full filename where the latest configuration
35
+ // was stored; empty until SetConfig is called.
36
+ configFilename string
37
+
32
38
// baseArgs is arguments that should be appended to all commands
33
39
baseArgs []string
34
40
@@ -85,11 +91,20 @@ func (wd *WorkingDir) GetHelper() *Helper {
85
91
// Destroy to establish the configuration. Any previously-set configuration is
86
92
// discarded and any saved plan is cleared.
87
93
func (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 )
90
104
if err != nil {
91
105
return err
92
106
}
107
+ wd .configFilename = outFilename
93
108
94
109
var mismatch * tfexec.ErrVersionMismatch
95
110
err = wd .tf .SetDisablePluginTLS (true )
@@ -158,11 +173,16 @@ func (wd *WorkingDir) ClearPlan(ctx context.Context) error {
158
173
return nil
159
174
}
160
175
176
+ var errWorkingDirSetConfigNotCalled = fmt .Errorf ("must call SetConfig before Init" )
177
+
161
178
// Init runs "terraform init" for the given working directory, forcing Terraform
162
179
// to use the current version of the plugin under test.
163
180
func (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
166
186
}
167
187
168
188
logging .HelperResourceTrace (ctx , "Calling Terraform CLI init command" )
@@ -174,10 +194,6 @@ func (wd *WorkingDir) Init(ctx context.Context) error {
174
194
return err
175
195
}
176
196
177
- func (wd * WorkingDir ) configFilename () string {
178
- return filepath .Join (wd .baseDir , ConfigFileName )
179
- }
180
-
181
197
func (wd * WorkingDir ) planFilename () string {
182
198
return filepath .Join (wd .baseDir , PlanFileName )
183
199
}
0 commit comments