-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoenv_test.go
More file actions
97 lines (85 loc) · 2 KB
/
goenv_test.go
File metadata and controls
97 lines (85 loc) · 2 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
package goenv
import (
"os"
"strings"
"testing"
)
func writeTempEnvFile(t *testing.T, contents string) string {
t.Helper()
tmpFile, err := os.CreateTemp("", ".env.test")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
_, err = tmpFile.WriteString(contents)
if err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
tmpFile.Close()
return tmpFile.Name()
}
func TestLoadEnvBasic(t *testing.T) {
content := `
# This is a comment
APP_ENV=production
DB_HOST=localhost
DB_USER=root
DB_PASS="supersecret"
# Another comment
`
file := writeTempEnvFile(t, content)
defer os.Remove(file)
LoadEnv(file, false)
tests := map[string]string{
"APP_ENV": "production",
"DB_HOST": "localhost",
"DB_USER": "root",
"DB_PASS": "supersecret",
}
for key, expected := range tests {
value := os.Getenv(key)
if value != expected {
t.Errorf("Expected %s = %s, got %s", key, expected, value)
}
}
}
func TestLoadEnvWithMoreThan20Lines(t *testing.T) {
var builder strings.Builder
for i := 0; i < 25; i++ {
builder.WriteString("KEY")
builder.WriteString(string('A' + rune(i)))
builder.WriteString("=VALUE")
builder.WriteString(string('A' + rune(i)))
builder.WriteString("\n")
}
file := writeTempEnvFile(t, builder.String())
defer os.Remove(file)
LoadEnv(file, false)
for i := 0; i < 25; i++ {
key := "KEY" + string('A'+rune(i))
expected := "VALUE" + string('A'+rune(i))
value := os.Getenv(key)
if value != expected {
t.Errorf("Expected %s = %s, got %s", key, expected, value)
}
}
}
func TestLoadEnvIgnoresInvalidLines(t *testing.T) {
content := `
VALID_KEY=valid_value
INVALIDLINE
# Comment
ANOTHER_VALID=42
`
file := writeTempEnvFile(t, content)
defer os.Remove(file)
LoadEnv(file, false)
if os.Getenv("VALID_KEY") != "valid_value" {
t.Error("VALID_KEY should be set to valid_value")
}
if os.Getenv("ANOTHER_VALID") != "42" {
t.Error("ANOTHER_VALID should be set to 42")
}
if os.Getenv("INVALIDLINE") != "" {
t.Error("INVALIDLINE should not be set")
}
}