-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgonfig_test.go
176 lines (161 loc) · 5.36 KB
/
gonfig_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gonfig_test
import (
"encoding/json"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/luizvnasc/gonfig"
)
const (
validJsonFile = "./test/config.json"
invalidJsonFile = "./test/invalid.json"
invalidJsonBodyFile = "./test/invalid_config.json"
validXMLFile = "./test/config.xml"
invalidXMLBodyFile = "./test/invalid_config.xml"
unsupportedFile = "./test/config.xyz"
validYamlFile = "./test/config.yaml"
invalidYamlBodyFile = "./test/invalid_config.yaml"
validTomlFile = "./test/config.toml"
invalidTomlBodyFile = "./test/invalid_config.toml"
)
type SomeConfiguration struct {
Version string `json:"version" xml:"version" yaml:"version" toml:"version" env:"VERSION"`
ProjectName string `json:"project_name" xml:"project-name" yaml:"project_name" toml:"project_name" env:"PROJECT_NAME"`
}
var configValid SomeConfiguration
func init() {
file, err := os.Open(validJsonFile)
if err != nil {
panic("Error Loading teste sample")
}
b, err := ioutil.ReadAll(file)
if err != nil {
panic("Error Loading teste sample")
}
json.Unmarshal(b, &configValid)
}
func TestGonfig(t *testing.T) {
t.Run("JSON tests", func(t *testing.T) {
t.Run("Load a configuration from a valid json file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, validJsonFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, invalidJsonFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, invalidJsonBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
})
t.Run("XML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid xml file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, validXMLFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid xml body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, invalidXMLBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
})
t.Run("YAML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid yaml file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, validYamlFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid yaml body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, invalidYamlBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
})
t.Run("TOML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid toml file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, validTomlFile)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid toml body", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, invalidTomlBodyFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
})
t.Run("ENV tests", func(t *testing.T) {
t.Run("Load a configuration from ENV", func(t *testing.T) {
config := SomeConfiguration{}
os.Setenv("VERSION", configValid.Version)
os.Setenv("PROJECT_NAME", configValid.ProjectName)
err := gonfig.Load(&config)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
})
t.Run("Unsupported file", func(t *testing.T) {
config := SomeConfiguration{}
err := gonfig.Load(&config, unsupportedFile)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.UnsupportedFileError {
t.Errorf("Expected the error %v, got %v", gonfig.UnsupportedFileError, err)
}
})
}