-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenvgo_test.go
62 lines (55 loc) · 1.3 KB
/
dotenvgo_test.go
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
package dotenvgo
import (
"log"
"os"
"testing"
)
func TestLoad(t *testing.T) {
envContent := `
HOST='localhost'
PORT=8080
SUPPORT_EMAIL="[email protected]"
LUCKY_NUMBER=420
SALT=496d3c88-e4f5-4ff1-8eb8-e4c0a8ac12f2
TEXT=aaa bbb CCC
`
tmpFile, err := os.CreateTemp("./", ".env_testfile")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
tmpFileName := tmpFile.Name()
defer os.Remove(tmpFileName)
if _, err := tmpFile.Write([]byte(envContent)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
tmpFile.Close()
if err := Load(&tmpFileName); err != nil {
log.Fatal(err.Error())
}
tests := []struct {
varName string
expectedVal string
}{
{"HOST", "localhost"},
{"PORT", "8080"},
{"SUPPORT_EMAIL", "[email protected]"},
{"LUCKY_NUMBER", "420"},
{"SALT", "496d3c88-e4f5-4ff1-8eb8-e4c0a8ac12f2"},
{"TEXT", "aaa bbb CCC"},
}
for _, test := range tests {
val, err := Get(test.varName)
if err != nil {
t.Errorf("Error getting variable %s: %v", test.varName, err)
}
if val != test.expectedVal {
t.Errorf("Expected %s for variable %s, got %s", test.expectedVal, test.varName, val)
}
}
}
func TestGetNonExistentVariable(t *testing.T) {
_, err := Get("NON_EXISTENT")
if err == nil {
t.Error("Expected an error for non-existent variable, got nil")
}
}