-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
testing.go
43 lines (37 loc) · 1.06 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package configload
import (
"io/ioutil"
"os"
"testing"
)
// NewLoaderForTests is a variant of NewLoader that is intended to be more
// convenient for unit tests.
//
// The loader's modules directory is a separate temporary directory created
// for each call. Along with the created loader, this function returns a
// cleanup function that should be called before the test completes in order
// to remove that temporary directory.
//
// In the case of any errors, t.Fatal (or similar) will be called to halt
// execution of the test, so the calling test does not need to handle errors
// itself.
func NewLoaderForTests(t *testing.T) (*Loader, func()) {
t.Helper()
modulesDir, err := ioutil.TempDir("", "tf-configs")
if err != nil {
t.Fatalf("failed to create temporary modules dir: %s", err)
return nil, func() {}
}
cleanup := func() {
os.RemoveAll(modulesDir)
}
loader, err := NewLoader(&Config{
ModulesDir: modulesDir,
})
if err != nil {
cleanup()
t.Fatalf("failed to create config loader: %s", err)
return nil, func() {}
}
return loader, cleanup
}