@@ -54,6 +54,9 @@ type CheckFuncCtx struct {
54
54
55
55
// Scaleway client
56
56
Client * scw.Client
57
+
58
+ // OverrideEnv passed in the TestConfig
59
+ OverrideEnv map [string ]string
57
60
}
58
61
59
62
// TestCheck is a function that perform assertion on a CheckFuncCtx
@@ -64,18 +67,20 @@ type BeforeFunc func(ctx *BeforeFuncCtx) error
64
67
type AfterFunc func (ctx * AfterFuncCtx ) error
65
68
66
69
type BeforeFuncCtx struct {
67
- T * testing.T
68
- Client * scw.Client
69
- ExecuteCmd func (args []string ) interface {}
70
- Meta map [string ]interface {}
70
+ T * testing.T
71
+ Client * scw.Client
72
+ ExecuteCmd func (args []string ) interface {}
73
+ Meta map [string ]interface {}
74
+ OverrideEnv map [string ]string
71
75
}
72
76
73
77
type AfterFuncCtx struct {
74
- T * testing.T
75
- Client * scw.Client
76
- ExecuteCmd func (args []string ) interface {}
77
- Meta map [string ]interface {}
78
- CmdResult interface {}
78
+ T * testing.T
79
+ Client * scw.Client
80
+ ExecuteCmd func (args []string ) interface {}
81
+ Meta map [string ]interface {}
82
+ CmdResult interface {}
83
+ OverrideEnv map [string ]string
79
84
}
80
85
81
86
// TestConfig contain configuration that can be used with the Test function
@@ -116,8 +121,15 @@ type TestConfig struct {
116
121
// Fake build info for this test.
117
122
BuildInfo BuildInfo
118
123
124
+ // If set, it will create a temporary home directory during the tests.
125
+ // Get this folder with ExtractUserHomeDir()
126
+ TmpHomeDir bool
127
+
119
128
// OverrideEnv contains environment variables that will be overridden during the test.
120
129
OverrideEnv map [string ]string
130
+
131
+ // Custom client to use for test, if none are provided will create one automatically
132
+ Client * scw.Client
121
133
}
122
134
123
135
// getTestFilePath returns a valid filename path based on the go test name and suffix. (Take care of non fs friendly char)
@@ -135,7 +147,7 @@ func getTestFilePath(t *testing.T, suffix string) string {
135
147
return filepath .Join ("." , "testdata" , fileName )
136
148
}
137
149
138
- func getTestClient (t * testing.T , testConfig * TestConfig ) (client * scw.Client , cleanup func ()) {
150
+ func createTestClient (t * testing.T , testConfig * TestConfig ) (client * scw.Client , cleanup func ()) {
139
151
var err error
140
152
cleanup = func () {}
141
153
@@ -199,11 +211,32 @@ func Test(config *TestConfig) func(t *testing.T) {
199
211
return "few seconds ago" , nil
200
212
})
201
213
202
- client , cleanup := getTestClient (t , config )
203
- defer cleanup ()
214
+ // We try to use the client provided in the config
215
+ // if no client is provided in the config we create a test client
216
+ client := config .Client
217
+ if client == nil {
218
+ var cleanup func ()
219
+ client , cleanup = createTestClient (t , config )
220
+ defer cleanup ()
221
+ }
204
222
205
223
meta := map [string ]interface {}{}
206
224
225
+ overideEnv := config .OverrideEnv
226
+ if overideEnv == nil {
227
+ overideEnv = map [string ]string {}
228
+ }
229
+
230
+ if config .TmpHomeDir {
231
+ dir , err := ioutil .TempDir (os .TempDir (), "scw" )
232
+ require .NoError (t , err )
233
+ defer func () {
234
+ err = os .RemoveAll (dir )
235
+ assert .NoError (t , err )
236
+ }()
237
+ overideEnv ["HOME" ] = dir
238
+ }
239
+
207
240
executeCmd := func (args []string ) interface {} {
208
241
stdoutBuffer := & bytes.Buffer {}
209
242
stderrBuffer := & bytes.Buffer {}
@@ -216,7 +249,7 @@ func Test(config *TestConfig) func(t *testing.T) {
216
249
Stderr : stderrBuffer ,
217
250
Client : client ,
218
251
DisableTelemetry : true ,
219
- OverrideEnv : config . OverrideEnv ,
252
+ OverrideEnv : overideEnv ,
220
253
})
221
254
require .NoError (t , err , "stdout: %s\n stderr: %s" , stdoutBuffer .String (), stderrBuffer .String ())
222
255
@@ -226,10 +259,11 @@ func Test(config *TestConfig) func(t *testing.T) {
226
259
// Run config.BeforeFunc
227
260
if config .BeforeFunc != nil {
228
261
require .NoError (t , config .BeforeFunc (& BeforeFuncCtx {
229
- T : t ,
230
- Client : client ,
231
- ExecuteCmd : executeCmd ,
232
- Meta : meta ,
262
+ T : t ,
263
+ Client : client ,
264
+ ExecuteCmd : executeCmd ,
265
+ Meta : meta ,
266
+ OverrideEnv : overideEnv ,
233
267
}))
234
268
}
235
269
@@ -253,29 +287,31 @@ func Test(config *TestConfig) func(t *testing.T) {
253
287
Stderr : stderr ,
254
288
Client : client ,
255
289
DisableTelemetry : true ,
256
- OverrideEnv : config . OverrideEnv ,
290
+ OverrideEnv : overideEnv ,
257
291
})
258
292
259
293
meta ["CmdResult" ] = result
260
294
config .Check (t , & CheckFuncCtx {
261
- ExitCode : exitCode ,
262
- Stdout : stdout .Bytes (),
263
- Stderr : stderr .Bytes (),
264
- Meta : meta ,
265
- Result : result ,
266
- Err : err ,
267
- Client : client ,
295
+ ExitCode : exitCode ,
296
+ Stdout : stdout .Bytes (),
297
+ Stderr : stderr .Bytes (),
298
+ Meta : meta ,
299
+ Result : result ,
300
+ Err : err ,
301
+ Client : client ,
302
+ OverrideEnv : overideEnv ,
268
303
})
269
304
}
270
305
271
306
// Run config.AfterFunc
272
307
if config .AfterFunc != nil {
273
308
require .NoError (t , config .AfterFunc (& AfterFuncCtx {
274
- T : t ,
275
- Client : client ,
276
- ExecuteCmd : executeCmd ,
277
- Meta : meta ,
278
- CmdResult : result ,
309
+ T : t ,
310
+ Client : client ,
311
+ ExecuteCmd : executeCmd ,
312
+ Meta : meta ,
313
+ CmdResult : result ,
314
+ OverrideEnv : overideEnv ,
279
315
}))
280
316
}
281
317
}
0 commit comments