-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotenv_test.go
More file actions
123 lines (95 loc) · 3.28 KB
/
Copy pathdotenv_test.go
File metadata and controls
123 lines (95 loc) · 3.28 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
package dotenv_test
import (
"errors"
"os"
"testing"
"github.com/rfberaldo/dotenv"
)
func TestLoad(t *testing.T) {
t.Setenv("ENV", "dev") // predefined var
t.Setenv("PORT", "8000") // predefined var
err := dotenv.LoadTesting(t, "testdata/.env", "testdata/.env.production")
if err != nil {
t.Fatalf("error not expected: %s", err)
}
if got, want := os.Getenv("ENV"), "dev"; got != want {
t.Log("should not override predefined var")
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := os.Getenv("HOST"), "localhost:8000"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := os.Getenv("URL"), "http://localhost:8000"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := os.Getenv("ANYTHING"), "what ever have after the equal sign =/\\\"`'||; works"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
}
func TestLoad_Override(t *testing.T) {
t.Setenv("ENV", "dev") // predefined var
t.Cleanup(func() { dotenv.SetOverride(false) })
dotenv.SetOverride(true)
err := dotenv.LoadTesting(t, "testdata/.env", "testdata/.env.production")
if err != nil {
t.Fatalf("error not expected: %s", err)
}
if got, want := os.Getenv("ENV"), "production"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := os.Getenv("HOST"), "example.com"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := os.Getenv("URL"), "https://example.com"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
}
func TestRead(t *testing.T) {
t.Setenv("ENV", "dev") // predefined var is not considered
t.Setenv("PORT", "8000") // predefined var is considered when expanding
kv, err := dotenv.Read("testdata/.env", "testdata/.env.production")
if err != nil {
t.Fatalf("error not expected: %s", err)
}
if got, want := kv["ENV"], "test"; got != want {
t.Log("should not override predefined var")
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := kv["HOST"], "localhost:8000"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := kv["URL"], "http://localhost:8000"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := kv["ANYTHING"], "what ever have after the equal sign =/\\\"`'||; works"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
}
func TestRead_Override(t *testing.T) {
t.Setenv("ENV", "dev") // predefined var is not considered
t.Cleanup(func() { dotenv.SetOverride(false) })
dotenv.SetOverride(true)
kv, err := dotenv.Read("testdata/.env", "testdata/.env.production")
if err != nil {
t.Fatalf("error not expected: %s", err)
}
if got, want := kv["ENV"], "production"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := kv["HOST"], "example.com"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
if got, want := kv["URL"], "https://example.com"; got != want {
t.Errorf("expected=%s, got=%s", want, got)
}
}
func TestLoad_FileNotExists(t *testing.T) {
if err := dotenv.LoadTesting(t); err != nil {
t.Fatalf("error not expected: %s", err)
}
t.Cleanup(func() { dotenv.SetRequireFileExists(false) })
dotenv.SetRequireFileExists(true)
if err := dotenv.LoadTesting(t); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected os.ErrNotExist: %s", err)
}
}