-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathkvfile_test.go
More file actions
220 lines (193 loc) · 6.56 KB
/
kvfile_test.go
File metadata and controls
220 lines (193 loc) · 6.56 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
package kvfile
import (
"bufio"
"os"
"path/filepath"
"strings"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
// Test Parse for a non existent file.
func TestParseNonExistentFile(t *testing.T) {
_, err := Parse("no_such_file", nil)
assert.Check(t, is.ErrorType(err, os.IsNotExist))
}
// Test Parse from a file with a lookup function.
func TestParseWithLookup(t *testing.T) {
content := `# comment=
VAR=VAR_VALUE
EMPTY_VAR=
UNDEFINED_VAR
DEFINED_VAR
`
vars := map[string]string{
"DEFINED_VAR": "defined-value",
}
lookupFn := func(name string) (string, bool) {
v, ok := vars[name]
return v, ok
}
fileName := filepath.Join(t.TempDir(), "envfile")
err := os.WriteFile(fileName, []byte(content), 0o644)
assert.NilError(t, err)
variables, err := Parse(fileName, lookupFn)
assert.NilError(t, err)
expectedLines := []string{"VAR=VAR_VALUE", "EMPTY_VAR=", "DEFINED_VAR=defined-value"}
assert.Check(t, is.DeepEqual(variables, expectedLines))
}
// Test ParseEnvFile for a file with a few well formatted lines
func TestParseFromReaderGoodFile(t *testing.T) {
content := `foo=bar
baz=quux
# comment
_foobar=foobaz
with.dots=working
and_underscore=working too
`
// Adding a newline + a line with pure whitespace.
// This is being done like this instead of the block above
// because it's common for editors to trim trailing whitespace
// from lines, which becomes annoying since that's the
// exact thing we need to test.
content += "\n \t "
lines, err := ParseFromReader(strings.NewReader(content), nil)
assert.NilError(t, err)
expectedLines := []string{
"foo=bar",
"baz=quux",
"_foobar=foobaz",
"with.dots=working",
"and_underscore=working too",
}
assert.Check(t, is.DeepEqual(lines, expectedLines))
}
// Test ParseFromReader for an empty file
func TestParseFromReaderEmptyFile(t *testing.T) {
lines, err := ParseFromReader(strings.NewReader(""), nil)
assert.NilError(t, err)
assert.Check(t, is.Len(lines, 0))
}
// Test ParseFromReader for a badly formatted file
func TestParseFromReaderBadlyFormattedFile(t *testing.T) {
content := `foo=bar
f =quux
`
_, err := ParseFromReader(strings.NewReader(content), nil)
const expectedMessage = "variable 'f ' contains whitespaces"
assert.Check(t, is.ErrorContains(err, expectedMessage))
}
// Test ParseFromReader for a file with a line exceeding bufio.MaxScanTokenSize
func TestParseFromReaderLineTooLongFile(t *testing.T) {
content := "foo=" + strings.Repeat("a", bufio.MaxScanTokenSize+42)
_, err := ParseFromReader(strings.NewReader(content), nil)
const expectedMessage = "bufio.Scanner: token too long"
assert.Check(t, is.ErrorContains(err, expectedMessage))
}
// ParseEnvFile with a random file, pass through
func TestParseFromReaderRandomFile(t *testing.T) {
content := `first line
another invalid line`
_, err := ParseFromReader(strings.NewReader(content), nil)
const expectedMessage = "variable 'first line' contains whitespaces"
assert.Check(t, is.ErrorContains(err, expectedMessage))
}
// Test ParseFromReader with a lookup function.
func TestParseFromReaderWithLookup(t *testing.T) {
content := `# comment=
VAR=VAR_VALUE
EMPTY_VAR=
UNDEFINED_VAR
DEFINED_VAR
`
vars := map[string]string{
"DEFINED_VAR": "defined-value",
}
lookupFn := func(name string) (string, bool) {
v, ok := vars[name]
return v, ok
}
variables, err := ParseFromReader(strings.NewReader(content), lookupFn)
assert.NilError(t, err)
expectedLines := []string{"VAR=VAR_VALUE", "EMPTY_VAR=", "DEFINED_VAR=defined-value"}
assert.Check(t, is.DeepEqual(variables, expectedLines))
}
// Test ParseFromReader with empty variable name
func TestParseFromReaderWithNoName(t *testing.T) {
content := `# comment=
=blank variable names are an error case
`
_, err := ParseFromReader(strings.NewReader(content), nil)
const expectedMessage = "no variable name on line '=blank variable names are an error case'"
assert.Check(t, is.ErrorContains(err, expectedMessage))
}
// Test trimQuotes helper function
func TestTrimQuotes(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{name: "double quotes", input: `"bar"`, expected: "bar"},
{name: "single quotes", input: "'bar'", expected: "bar"},
{name: "no quotes", input: "bar", expected: "bar"},
{name: "empty string", input: "", expected: ""},
{name: "mismatched quotes double-single", input: `"bar'`, expected: `"bar'`},
{name: "mismatched quotes single-double", input: `'bar"`, expected: `'bar"`},
{name: "only opening double quote", input: `"bar`, expected: `"bar`},
{name: "only closing double quote", input: `bar"`, expected: `bar"`},
{name: "only opening single quote", input: "'bar", expected: "'bar"},
{name: "only closing single quote", input: "bar'", expected: "bar'"},
{name: "empty double quotes", input: `""`, expected: ""},
{name: "empty single quotes", input: "''", expected: ""},
{name: "nested double in single", input: `'"bar"'`, expected: `"bar"`},
{name: "nested single in double", input: `"'bar'"`, expected: "'bar'"},
{name: "single char", input: "a", expected: "a"},
{name: "single double quote", input: `"`, expected: `"`},
{name: "single single quote", input: "'", expected: "'"},
{name: "spaces inside double quotes", input: `"hello world"`, expected: "hello world"},
{name: "spaces inside single quotes", input: "'hello world'", expected: "hello world"},
{name: "value with internal quotes", input: `"say \"hello\""`, expected: `say \"hello\"`},
{name: "triple double quotes", input: `"""`, expected: `"`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := trimQuotes(tc.input)
assert.Check(t, is.Equal(result, tc.expected))
})
}
}
// Test ParseFromReader strips surrounding quotes from values
func TestParseFromReaderQuotedValues(t *testing.T) {
content := `# Quoted values should have surrounding quotes stripped
DOUBLE_QUOTED="hello world"
SINGLE_QUOTED='hello world'
NO_QUOTES=hello
EMPTY_DOUBLE=""
EMPTY_SINGLE=''
MISMATCHED="hello'
NESTED_DOUBLE='"hello"'
NESTED_SINGLE="'hello'"
UNQUOTED_SPACES=hello world
INTERNAL_QUOTES=he"ll"o
ONLY_OPENING="hello
VALUE_WITH_EQUALS="foo=bar"
`
lines, err := ParseFromReader(strings.NewReader(content), nil)
assert.NilError(t, err)
expectedLines := []string{
"DOUBLE_QUOTED=hello world",
"SINGLE_QUOTED=hello world",
"NO_QUOTES=hello",
"EMPTY_DOUBLE=",
"EMPTY_SINGLE=",
`MISMATCHED="hello'`,
`NESTED_DOUBLE="hello"`,
"NESTED_SINGLE='hello'",
"UNQUOTED_SPACES=hello world",
`INTERNAL_QUOTES=he"ll"o`,
`ONLY_OPENING="hello`,
"VALUE_WITH_EQUALS=foo=bar",
}
assert.Check(t, is.DeepEqual(lines, expectedLines))
}