-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutils_test.go
118 lines (91 loc) · 2.76 KB
/
testutils_test.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
114
115
116
117
118
package hclconfig
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"
)
func TestProcessesTypes(t *testing.T) {
vars := map[string]cty.Value{}
vars["string"] = cty.StringVal("abc")
vars["number"] = cty.NumberIntVal(23)
vars["bool"] = cty.BoolVal(true)
vars["array"] = cty.ListVal(
[]cty.Value{
cty.StringVal("abc"),
cty.StringVal("123"),
})
vars["map"] = cty.MapVal(map[string]cty.Value{
"foo": cty.StringVal("abc"),
})
output := ParseVars(vars)
require.Equal(t, "abc", output["string"])
num := int64(output["number"].(float64))
require.Equal(t, int64(23), num)
require.True(t, output["bool"].(bool))
require.Equal(t, "abc", output["array"].([]interface{})[0])
require.Equal(t, "123", output["array"].([]interface{})[1])
require.Equal(t, "abc", output["map"].(map[string]interface{})["foo"])
}
// CreateConfigFromStrings is a test helper function that
// parses the given contents strings as HCL and returns a Shipyard Config
func CreateConfigFromStrings(t *testing.T, contents ...string) (*Config, string) {
//dir := CreateTestFiles(t, contents...)
//c := resources.NewConfig()
//err := ParseFolder(dir, c, false, "", false, []string{}, nil, "")
//require.NoError(t, err)
//return c, dir
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)
}
}