Repository navigation
Expand file tree
/
Copy pathparse_test.go
More file actions
126 lines (106 loc) · 2.79 KB
/
Copy pathparse_test.go
File metadata and controls
126 lines (106 loc) · 2.79 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
package envconfig
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func writeTempEnvFile(t *testing.T, contents string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, ".env")
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
t.Fatalf("failed to write temp env file: %v", err)
}
return path
}
func TestParse(t *testing.T) {
t.Run("comments and blank lines are skipped", func(t *testing.T) {
contents := `
# This is a comment
FOO=bar
# Another comment
BAZ=qux
`
path := writeTempEnvFile(t, contents)
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse returned error: %v", err)
}
want := map[string]string{
"FOO": "bar",
"BAZ": "qux",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Parse() = %#v, want %#v", got, want)
}
})
t.Run("unquoted values are trimmed", func(t *testing.T) {
path := writeTempEnvFile(t, "KEY= hello world \n")
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse returned error: %v", err)
}
if got["KEY"] != "hello world" {
t.Errorf("KEY = %q, want %q", got["KEY"], "hello world")
}
})
t.Run("double-quoted values with escapes", func(t *testing.T) {
path := writeTempEnvFile(t, `MSG="line one\nline two\ttabbed \"quoted\" and \\backslash"`+"\n")
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse returned error: %v", err)
}
want := "line one\nline two\ttabbed \"quoted\" and \\backslash"
if got["MSG"] != want {
t.Errorf("MSG = %q, want %q", got["MSG"], want)
}
})
t.Run("single-quoted values are literal", func(t *testing.T) {
path := writeTempEnvFile(t, `RAW='no \n escapes here'`+"\n")
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse returned error: %v", err)
}
want := `no \n escapes here`
if got["RAW"] != want {
t.Errorf("RAW = %q, want %q", got["RAW"], want)
}
})
t.Run("multi-line fixture with mixed forms", func(t *testing.T) {
contents := `# Application configuration
APP_NAME=MyApp
APP_ENV="production"
DEBUG=false
# Database
DB_HOST=localhost
DB_PORT=5432
DB_PASSWORD='p@ss "word"'
# A trailing comment line
TAGS=alpha,beta,gamma
`
path := writeTempEnvFile(t, contents)
got, err := Parse(path)
if err != nil {
t.Fatalf("Parse returned error: %v", err)
}
want := map[string]string{
"APP_NAME": "MyApp",
"APP_ENV": "production",
"DEBUG": "false",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_PASSWORD": `p@ss "word"`,
"TAGS": "alpha,beta,gamma",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Parse() = %#v, want %#v", got, want)
}
})
t.Run("nonexistent file returns error", func(t *testing.T) {
_, err := Parse(filepath.Join(t.TempDir(), "does-not-exist.env"))
if err == nil {
t.Fatal("expected error for nonexistent file, got nil")
}
})
}