-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_test.go
155 lines (131 loc) · 3.1 KB
/
import_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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2022 Jonas Kwiedor. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package dotenv_test
import (
"errors"
"github.com/gowizzard/dotenv/v2"
"os"
"path/filepath"
"reflect"
"syscall"
"testing"
)
// TestImport is to test the Import function with table driven tests.
func TestImport(t *testing.T) {
tests := []struct {
name string
path string
perm os.FileMode
data []byte
write bool
error error
expected map[string]string
}{
{
name: "WITHOUT_QUOTES",
path: filepath.Join(t.TempDir(), ".env"),
perm: os.ModePerm,
data: []byte("TEST1=value\nTEST2=25"),
write: true,
error: nil,
expected: map[string]string{
"TEST1": "value",
"TEST2": "25",
},
},
{
name: "SINGLE_QUOTES",
path: filepath.Join(t.TempDir(), ".env"),
perm: os.ModePerm,
data: []byte("TEST1='value'\nTEST2='25'\nTEST3='42.5'\nTEST4='true'"),
write: true,
error: nil,
expected: map[string]string{
"TEST1": "value",
"TEST2": "25",
"TEST3": "42.5",
"TEST4": "true",
},
},
{
name: "DOUBLE_QUOTES",
path: filepath.Join(t.TempDir(), ".env"),
perm: os.ModePerm,
data: []byte("# This is a test command.\nTEST1=\"value\""),
write: true,
error: nil,
expected: map[string]string{
"TEST1": "value",
},
},
{
name: "FILE_NOT_EXIST",
path: "",
perm: os.ModePerm,
data: nil,
write: false,
error: errors.New("open : no such file or directory"),
expected: nil,
},
{
name: "EMPTY_FILE",
path: filepath.Join(t.TempDir(), ".env"),
perm: os.ModePerm,
data: []byte(""),
write: true,
error: errors.New("file is empty"),
expected: nil,
},
{
name: "NO_MATCHES",
path: filepath.Join(t.TempDir(), ".env"),
perm: os.ModePerm,
data: []byte("=value\n=25"),
write: true,
error: errors.New("no matches found"),
expected: nil,
},
}
for _, value := range tests {
t.Run(value.name, func(t *testing.T) {
if value.write {
err := os.WriteFile(value.path, value.data, value.perm)
if err != nil {
t.Error(err)
}
}
err := dotenv.Import(value.path)
if err != nil && !reflect.DeepEqual(value.error.Error(), err.Error()) {
t.Errorf("expected error: \"%v\", got \"%s\"", value.error, err)
}
for index, value := range value.expected {
result, ok := syscall.Getenv(index)
if ok && !reflect.DeepEqual(value, result) {
t.Errorf("expected: \"%s\", got \"%s\"", value, result)
}
}
})
}
t.Cleanup(func() {
syscall.Clearenv()
})
}
// BenchmarkImport is to test the Import function benchmark timing.
func BenchmarkImport(b *testing.B) {
path, perm, data := filepath.Join(b.TempDir(), ".env"), os.ModePerm, []byte("TEST1=\"value\"\nTEST2=\"value\"")
err := os.WriteFile(path, data, perm)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := dotenv.Import(path)
if err != nil {
b.Error(err)
}
}
b.Cleanup(func() {
syscall.Clearenv()
})
}