-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
config_test.go
160 lines (147 loc) · 3.71 KB
/
config_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
/**
* This file is part of tz.
*
* tz is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* tz is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tz. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
"os"
"strings"
"testing"
"time"
)
func TestConfigKeysDuplicated(t *testing.T) {
tomlPath := "./testdata/config/config_test_keys_dup.toml"
_, err := LoadConfig(tomlPath, nil)
if err == nil {
t.Errorf("Expected error while reading %s, but didn’t get one", tomlPath)
}
expectedError := "Key(s) mapped multiple times in config: q"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected specific error while reading %s, but get a different one: %v", tomlPath, err)
}
}
func TestLoadConfig(t *testing.T) {
oldTzList, tzListWasSet := os.LookupEnv("TZ_LIST")
os.Unsetenv("TZ_LIST")
tomlPath := "./example-conf.toml"
_, err := LoadConfig(tomlPath, nil)
if err != nil {
t.Errorf("Could not read %s: %v", tomlPath, err)
}
if tzListWasSet {
os.Setenv("TZ_LIST", oldTzList)
SetupLogger()
}
}
func TestLoadConfigParser(t *testing.T) {
tests := []struct {
args []string
envArg string
expected string
}{
{nil, "", "Local;UTC"},
{[]string{"GMT"}, "UTC", "Local;GMT"},
{[]string{"", "GMT", ""}, "UTC", "Local;UTC;GMT;UTC"},
{nil, "GMT", "Local;GMT"},
{nil, ";GMT;", "Local;UTC;GMT;UTC"},
{nil, "Unknown", ""},
}
oldEnv := os.Getenv("TZ_LIST")
for _, test := range tests {
os.Setenv("TZ_LIST", test.envArg)
config, err := LoadDefaultConfig(test.args)
if err != nil {
if test.expected != "" {
t.Error(err)
}
} else {
names := make([]string, len(config.Zones))
for i, z := range config.Zones {
names[i] = z.Name
}
observed := strings.Join(names, ";")
if observed != test.expected {
t.Errorf("Expected '%v' to be: '%v'", observed, test.expected)
}
}
}
os.Setenv("TZ_LIST", oldEnv)
}
func TestLoadDefaultConfig(t *testing.T) {
_, err := LoadDefaultConfig(nil)
if err != nil {
t.Fatalf("Could not read default config file: %v", err)
}
}
func TestSetupZone(t *testing.T) {
now := time.Now()
tests := []struct {
zoneName string
ok bool
}{
{
zoneName: "Europe/Paris",
ok: true,
},
{
zoneName: "America/London",
ok: false,
},
{
// Names should be trimmed in the config, so this should be ok.
zoneName: " Australia/Sydney",
ok: true,
},
}
for _, test := range tests {
_, err := ReadZoneFromString(now, test.zoneName)
if test.ok != (err == nil) {
t.Errorf("Expected %v, but got: %v", test.ok, err)
}
}
}
func TestSetupZoneWithCustomNames(t *testing.T) {
now := time.Now()
tests := []struct {
zoneName string
shortName string
ok bool
}{
{
zoneName: "Europe/Paris,bonjour",
shortName: "bonjour",
ok: true,
},
{
zoneName: "America/Mexico_City,hola",
shortName: "hola",
ok: true,
},
{
zoneName: "America/Invalid",
shortName: "",
ok: false,
},
}
for _, test := range tests {
z, err := ReadZoneFromString(now, test.zoneName)
if test.ok != (err == nil) {
t.Errorf("Expected %v, but got: %v", test.ok, err)
}
if z != nil && !strings.Contains(z.Name, test.shortName) {
t.Errorf("Expected %v to contain: %v", z.Name, test.shortName)
}
}
}