-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotenv_test.go
More file actions
158 lines (149 loc) · 3.72 KB
/
dotenv_test.go
File metadata and controls
158 lines (149 loc) · 3.72 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
package envconfig
import (
"os"
"path/filepath"
"testing"
)
func TestEnvFileReader(t *testing.T) {
tempDir, err := os.MkdirTemp(t.TempDir(), "envconfig-test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
t.Cleanup(func() {
_ = os.RemoveAll(tempDir)
})
tests := []struct {
name string
fileContent string
lookupKey string
expected string
exists bool
}{
{
name: "basic_key_value",
fileContent: "KEY1=value1\nKEY2=value2",
lookupKey: "KEY1",
expected: "value1",
exists: true,
},
{
name: "export",
fileContent: "export KEY1=value1\nKEY2=value2",
lookupKey: "KEY1",
expected: "value1",
exists: true,
},
{
name: "with_spaces",
fileContent: "SPACED_KEY = spaced value ",
lookupKey: "SPACED_KEY",
expected: "spaced value",
exists: true,
},
{
name: "with_quotes",
fileContent: `QUOTED_KEY="quoted value"`,
lookupKey: "QUOTED_KEY",
expected: "quoted value",
exists: true,
},
{
name: "with_single_quotes",
fileContent: "SINGLE_QUOTED='single quoted'",
lookupKey: "SINGLE_QUOTED",
expected: "single quoted",
exists: true,
},
{
name: "with_quotes_and_comments",
fileContent: "SINGLE_QUOTED='single quoted' # This is comment",
lookupKey: "SINGLE_QUOTED",
expected: "single quoted",
exists: true,
},
{
name: "with_quotes_and_comments_inside",
fileContent: "SINGLE_QUOTED='single quoted # This is comment'",
lookupKey: "SINGLE_QUOTED",
expected: "single quoted # This is comment",
exists: true,
},
{
name: "comments_inside",
fileContent: "SINGLE_QUOTED=singlequoted#Thisis comment",
lookupKey: "SINGLE_QUOTED",
expected: "singlequoted#Thisis comment",
exists: true,
},
{
name: "comments_removed",
fileContent: "SINGLE_QUOTED=singlequoted #Thisis comment",
lookupKey: "SINGLE_QUOTED",
expected: "singlequoted",
exists: true,
},
{
name: "with_comments",
fileContent: "# This is a comment\nCOMMENTED=after comment\n# Another comment",
lookupKey: "COMMENTED",
expected: "after comment",
exists: true,
},
{
name: "with_empty_lines",
fileContent: "\n\nEMPTY_LINES=value\n\n",
lookupKey: "EMPTY_LINES",
expected: "value",
exists: true,
},
{
name: "invalid_line_format",
fileContent: "INVALID_LINE\nVALID_LINE=value",
lookupKey: "VALID_LINE",
expected: "value",
exists: true,
},
{
name: "missing_key",
fileContent: "OTHER_KEY=value",
lookupKey: "MISSING_KEY",
expected: "",
exists: false,
},
{
name: "empty_file",
fileContent: "",
lookupKey: "ANY_KEY",
expected: "",
exists: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
envFile := filepath.Join(tempDir, tc.name+".env")
if err := os.WriteFile(envFile, []byte(tc.fileContent), 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
lookupFn := EnvFileLookup(envFile)
value, exists := lookupFn(tc.lookupKey)
if exists != tc.exists {
t.Errorf("Expected exists=%v, got %v", tc.exists, exists)
}
if exists && value != tc.expected {
t.Errorf("Expected value=%q, got %q", tc.expected, value)
}
})
}
}
func TestEnvFileReaderUnknownFile(t *testing.T) {
const env = "__RANDOM_ENV__"
t.Setenv(env, "exists")
le := EnvFileLookup("non_existent.env")
v, ok := le("__RANDOM_ENV__")
if !ok {
t.Fatalf("Expected %q to exists", env)
}
if v != "exists" {
t.Fatalf("expected %q to have `exists` value, got: %q", env, v)
}
}