-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_test.go
153 lines (121 loc) · 3.87 KB
/
env_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
package confy
import (
"os"
"reflect"
"strings"
"testing"
"time"
)
func TestEnvBasicTypes(t *testing.T) {
os.Setenv("thing", "helloworld")
os.Setenv("b_bool", "true")
os.Setenv("thonku_complex_Mff", "toaster")
dummyConfig, err := LoadEnv[testStruct](ENVDelimiter)
if err != nil {
t.Fatal(err)
}
if !dummyConfig.B {
t.Fatalf("%+v", dummyConfig)
}
if dummyConfig.Thing != "helloworld" {
t.Fatalf("%+v", dummyConfig)
}
if dummyConfig.Thonku.Mff != "toaster" {
t.Fatalf("%+v", dummyConfig)
}
}
func TestEnvComplexTypes(t *testing.T) {
os.Setenv("marshal", "test marshalling")
os.Setenv("thonku_complex_Mff", "innername:42")
os.Setenv("my_boy", "2024-11-09T15:04:05Z")
os.Setenv("basic_array", "item1,item2,item3")
os.Setenv("complex_array", "text1,text2,text3")
dummyConfig, err := LoadEnv[testCliStruct](ENVDelimiter)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if dummyConfig.ImplementsMarshaller.content != "test marshalling" {
t.Errorf("Expected ImplementsMarshaller content 'test marshalling', got '%s'", dummyConfig.ImplementsMarshaller.content)
}
if dummyConfig.Thonku.Mff != "innername:42" {
t.Errorf("Expected Thonku Mff innername:42 got %s", dummyConfig.Thonku.Mff)
}
expectedTime := time.Date(2024, time.November, 9, 15, 4, 5, 0, time.UTC)
if !dummyConfig.ItsTime.Equal(expectedTime) {
t.Errorf("Expected ItsTime to be '%v', got '%v'", expectedTime, dummyConfig.ItsTime)
}
expectedBasicArray := []string{"item1", "item2", "item3"}
if !equalStringSlices(dummyConfig.BasicArray, expectedBasicArray) {
t.Errorf("Expected BasicArray to be '%v', got '%v'", expectedBasicArray, dummyConfig.BasicArray)
}
expectedComplexArray := []implementsTextUnmarshaler{
{content: "text1"},
{content: "text2"},
{content: "text3"},
}
for i, v := range dummyConfig.ComplexArray {
if v.content != expectedComplexArray[i].content {
t.Errorf("Expected ComplexArray[%d] to be '%s', got '%s'", i, expectedComplexArray[i].content, v.content)
}
}
}
func TestEnvHelperMethod(t *testing.T) {
type Small struct {
Thing string
Nested struct {
NestedVal string
}
}
os.Args = []string{
"dummy",
}
expectedContents := []string{
"Thing",
"Nested_NestedVal",
}
vals := GetGeneratedEnv[Small](ENVDelimiter)
if !reflect.DeepEqual(expectedContents, vals) {
t.Fatalf("expected %v got %v", expectedContents, vals)
}
}
func TestEnvTransform(t *testing.T) {
os.Setenv("MARSHAL", "test marshalling")
os.Setenv("THONKU_COMPLEX_MFF", "innername:42")
os.Setenv("MY_BOY", "2024-11-09T15:04:05Z")
os.Setenv("BASIC_ARRAY", "item1,item2,item3")
os.Setenv("COMPLEX_ARRAY", "text1,text2,text3")
dummyConfig, err := LoadEnvWithTransform[testCliStruct](ENVDelimiter, strings.ToUpper)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if dummyConfig.ImplementsMarshaller.content != "test marshalling" {
t.Errorf("Expected ImplementsMarshaller content 'test marshalling', got '%s'", dummyConfig.ImplementsMarshaller.content)
}
if dummyConfig.Thonku.Mff != "innername:42" {
t.Errorf("Expected Thonku Mff innername:42 got %s", dummyConfig.Thonku.Mff)
}
expectedTime := time.Date(2024, time.November, 9, 15, 4, 5, 0, time.UTC)
if !dummyConfig.ItsTime.Equal(expectedTime) {
t.Errorf("Expected ItsTime to be '%v', got '%v'", expectedTime, dummyConfig.ItsTime)
}
expectedBasicArray := []string{"item1", "item2", "item3"}
if !equalStringSlices(dummyConfig.BasicArray, expectedBasicArray) {
t.Errorf("Expected BasicArray to be '%v', got '%v'", expectedBasicArray, dummyConfig.BasicArray)
}
expectedComplexArray := []implementsTextUnmarshaler{
{content: "text1"},
{content: "text2"},
{content: "text3"},
}
for i, v := range dummyConfig.ComplexArray {
if v.content != expectedComplexArray[i].content {
t.Errorf("Expected ComplexArray[%d] to be '%s', got '%s'", i, expectedComplexArray[i].content, v.content)
}
}
}