-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.go
113 lines (91 loc) · 2.45 KB
/
utils.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package config
import (
"io/ioutil"
"os"
"testing"
"github.com/zclconf/go-cty/cty"
)
// ParseVars converts a map[string]cty.Value into map[string]interface
// where the interface are generic go types like string, number, bool, slice, map
func ParseVars(value map[string]cty.Value) map[string]interface{} {
vars := map[string]interface{}{}
for k, v := range value {
vars[k] = castVar(v)
}
return vars
}
func castVar(v cty.Value) interface{} {
if v.Type() == cty.String {
return v.AsString()
} else if v.Type() == cty.Bool {
return v.True()
} else if v.Type() == cty.Number {
return v.AsBigFloat()
} else if v.Type().IsObjectType() || v.Type().IsMapType() {
return ParseVars(v.AsValueMap())
} else if v.Type().IsTupleType() || v.Type().IsListType() {
i := v.ElementIterator()
vars := []interface{}{}
for {
if !i.Next() {
// cant iterate
break
}
_, value := i.Element()
vars = append(vars, castVar(value))
}
return vars
}
return nil
}
// createsTestFiles creates a temporary directory and
// stores temp files into it
// returns directory containing files
// cleanup function
// usage:
// d, cleanup := createTestFiles(t, `cluster "abc" {}`, `docs "bcdf" {}`)
// defer cleanup()
func CreateTestFiles(t *testing.T, contents ...string) string {
dir := createTempDirectory(t)
for _, x := range contents {
createNamedFile(t, dir, "*.hcl", x)
}
t.Cleanup(func() {
removeTestFiles(t, dir)
})
return dir
}
// createTestFile creates a hcl file from the given contents
func CreateTestFile(t *testing.T, contents string) string {
dir := createTempDirectory(t)
t.Cleanup(func() {
removeTestFiles(t, dir)
})
return createNamedFile(t, dir, "*.hcl", contents)
}
// create a temporary directory
func createTempDirectory(t *testing.T) string {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unable to create temporary directory: %s", err)
}
return dir
}
func createNamedFile(t *testing.T, dir, name, contents string) string {
f, err := ioutil.TempFile(dir, name)
if err != nil {
t.Fatalf("Error creating temp file %s", err)
}
defer f.Close()
if _, err := f.WriteString(contents); err != nil {
t.Fatalf("Error writing temp file contents: %s", err)
}
return f.Name()
}
// remove test files cleans up any temporary files created
// with createTestFile
func removeTestFiles(t *testing.T, dir string) {
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("Unable to remove temporary files %s", err)
}
}