forked from BinSquare/envmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvfile_test.go
More file actions
83 lines (73 loc) · 1.82 KB
/
envfile_test.go
File metadata and controls
83 lines (73 loc) · 1.82 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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestParseDotEnv(t *testing.T) {
content := `
# Database config
DB_HOST=localhost
DB_PORT=5432
DB_PASSWORD="quoted value"
API_KEY='single quoted'
# Empty and malformed lines
EMPTY=
NO_VALUE
WHITESPACE = spaced
`
dir := t.TempDir()
path := filepath.Join(dir, ".env")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
got, err := parseDotEnv(path)
if err != nil {
t.Fatalf("parseDotEnv: %v", err)
}
tests := []struct {
key string
expected string
exists bool
}{
{"DB_HOST", "localhost", true},
{"DB_PORT", "5432", true},
{"DB_PASSWORD", "quoted value", true}, // quotes stripped
{"API_KEY", "single quoted", true}, // single quotes stripped
{"EMPTY", "", true}, // empty value is valid
{"WHITESPACE", "spaced", true}, // whitespace trimmed
{"NO_VALUE", "", false}, // malformed, skipped
{"COMMENT", "", false}, // comments skipped
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
val, ok := got[tt.key]
if ok != tt.exists {
t.Errorf("key %q exists = %v, want %v", tt.key, ok, tt.exists)
}
if ok && val != tt.expected {
t.Errorf("got[%q] = %q, want %q", tt.key, val, tt.expected)
}
})
}
}
func TestParseDotEnvEmpty(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, ".env")
if err := os.WriteFile(path, []byte("# only comments\n\n"), 0644); err != nil {
t.Fatal(err)
}
got, err := parseDotEnv(path)
if err != nil {
t.Fatalf("parseDotEnv: %v", err)
}
if len(got) != 0 {
t.Errorf("expected empty map, got %d entries", len(got))
}
}
func TestParseDotEnvNotFound(t *testing.T) {
_, err := parseDotEnv("/nonexistent/.env")
if err == nil {
t.Error("expected error for missing file")
}
}