-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers_test.go
207 lines (185 loc) · 4.07 KB
/
helpers_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package sicher
import (
"encoding/hex"
"fmt"
"os"
"testing"
)
func TestCleanUpFile(t *testing.T) {
f, err := os.CreateTemp("", "*tempfile.env")
if err != nil {
t.Errorf("Unable to create temporary test file; %v", err)
}
_, err = f.WriteString("Hello World")
if err != nil {
t.Errorf("Unable to write to temporary test file; %v", err)
}
// clean up test, not to be mistaken with the cleanup file function
t.Cleanup(func() {
f.Close()
})
cleanUpFile(f.Name())
_, err = os.Open(f.Name())
if err == nil {
t.Errorf("file cleanup unsuccesssful")
}
}
func TestGenerateKey(t *testing.T) {
key := generateKey()
_, err := hex.DecodeString(key)
if err != nil {
t.Errorf("Generated key not a valid hex string")
}
}
func TestBasicParseConfig(t *testing.T) {
enMap := make(map[string]string)
cfg := []byte(`
PORT=8080
URI=localhost
#OLD_PORT=5000
`)
err := parseConfig(cfg, enMap, "dotenv")
if err != nil {
t.Errorf("Unable to parse config; %v", err)
}
port, ok := enMap["PORT"]
if !ok {
t.Errorf("Expected config to have been marshalled into map")
}
if port != "8080" {
t.Errorf("Expected value to be %s, got %s", "8080", port)
}
if enMap["OLD_PORT"] != "" {
t.Errorf("Expected ignored value to not be parsed")
}
enMap = make(map[string]string)
parseConfig(cfg, enMap, "yaml")
if len(enMap) != 0 {
t.Errorf("Expected dotenv style env not be be parseable with yaml envType")
}
}
func TestParseConfig(t *testing.T) {
tests := []struct {
text string
expected map[string]string
envType string
}{
{
text: `
PORT:8080
URI:localhost
#OLD_PORT:5000
`,
expected: map[string]string{
"PORT": "8080",
"URI": "localhost",
},
envType: "yaml",
},
{
text: `
PORT=8080
URI=localhost
#OLD_PORT=5000
`,
expected: map[string]string{
"PORT": "8080",
"URI": "localhost",
},
envType: "dotenv",
},
{
text: `
PORT=8080
URI=localhost
#OLD_PORT=5000
KEY=value=ndsjhjdghdhg
`,
expected: map[string]string{
"PORT": "8080",
"URI": "localhost",
"KEY": "value=ndsjhjdghdhg",
},
envType: "dotenv",
},
{
text: `
PORT:8080
URI=localhost
`,
expected: map[string]string{
"PORT": "8080",
},
envType: "yaml",
},
{
text: `
PORT:8080
URI=localhost
SOME_KEY:somevalue=jsfhjdghdhg
`,
expected: map[string]string{
"URI": "localhost",
},
envType: "dotenv",
},
}
for _, val := range tests {
enMap := make(map[string]string)
if err := parseConfig([]byte(val.text), enMap, EnvStyle(val.envType)); err != nil {
t.Errorf("Unable to parse config; %v", err)
}
t.Run(fmt.Sprintf("Envtype %s", val.envType), func(t *testing.T) {
for key, value := range val.expected {
if enMap[key] != value {
t.Errorf("Expected value to be %s, got %s", value, enMap[key])
}
}
for key, value := range enMap {
if val.expected[key] != value {
t.Errorf("Expected value to be %s, got %s", value, val.expected[key])
}
}
})
}
}
func TestYamlParseConfigError(t *testing.T) {
enMap := make(map[string]string)
cfg := []byte(`
PORT:8080
URI:localhost
#OLD_PORT:5000
`)
err := parseConfig(cfg, enMap, "wrong")
if err == nil {
t.Errorf("Expected error to be thrown when parsing wrong envType")
}
}
func TestCanIgnore(t *testing.T) {
data := []struct {
text string
expected bool
}{
{text: "# url", expected: true},
{text: " # url", expected: true},
{text: "url", expected: false},
{text: " url", expected: false},
}
for _, val := range data {
if canIgnore(val.text) != val.expected {
t.Errorf("Expected canIgnore(%s) to be %v, got %v", val.text, val.expected, canIgnore(val.text))
}
}
}
func TestDecodeHex(t *testing.T) {
_nonce, _text := generateKey(), generateKey()
hexString := _text + delimiter + _nonce
_, _, err := decodeFile(hexString)
if err != nil {
t.Errorf("Unable to decode valid hex string, got error %v", err)
}
nonce, text, err := decodeFile("invalidhex")
if err == nil {
t.Errorf("Expected invalid hex file to not decode, got values %s, %s", nonce, text)
}
}