-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimezone_unix_internal_test.go
232 lines (183 loc) · 4.79 KB
/
timezone_unix_internal_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//go:build darwin || linux
package timezone
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
tests := map[string]struct {
Filepath string
Expected string
}{
"inline": {
Filepath: "testdata/var/db/zoneinfo",
Expected: "America/Sao_Paulo",
},
"with comment": {
Filepath: "testdata/etc/timezone_comment",
Expected: "America/Sao_Paulo",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
tz := parseFromConfigFile([]string{test.Filepath})
assert.Equal(t, []string{test.Expected}, tz)
})
}
}
func TestParse_Empty(t *testing.T) {
tz := parseFromConfigFile([]string{"testdata/empty"})
assert.Equal(t, []string{}, tz)
}
func TestParseFromClock(t *testing.T) {
tests := map[string]struct {
Filepath string
Expected string
}{
"timezone": {
Filepath: "testdata/etc/conf.d/clock",
Expected: "America/Sao_Paulo",
},
"zone": {
Filepath: "testdata/etc/sysconfig/clock",
Expected: "America/Sao_Paulo",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
tz := parseFromClock([]string{test.Filepath})
assert.Equal(t, []string{test.Expected}, tz)
})
}
}
func TestParseFromClock_Empty(t *testing.T) {
tz := parseFromClock([]string{"testdata/empty"})
assert.Equal(t, []string{}, tz)
}
func TestParseSymlink(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
symlinkFile := filepath.Join(tmpDir, "localtime")
err = os.Symlink("testdata/zoneinfo/America/Sao_Paulo", symlinkFile)
require.NoError(t, err)
tz := parseSymlink(symlinkFile)
assert.Equal(t, "America/Sao_Paulo", tz)
}
func TestParseSymlink_NotSymlink(t *testing.T) {
tz := parseSymlink("testdata/zoneinfo/America/Sao_Paulo")
assert.Empty(t, tz)
}
func TestResolveTimezones(t *testing.T) {
tests := map[string]struct {
Timezones []string
Expected string
}{
"no timezone": {
Timezones: []string{},
Expected: "",
},
"only one timezone": {
Timezones: []string{"America/Sao_Paulo"},
Expected: "America/Sao_Paulo",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
tz, err := resolveTimezones(test.Timezones, "")
require.NoError(t, err)
assert.Equal(t, test.Expected, tz)
})
}
}
func TestResolveTimezones_Conflicting(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
timezones := []string{
"America/Sao_Paulo",
"America/Los_Angeles",
"Africa/Harare",
}
_, err = resolveTimezones(timezones, "testdata/zoneinfo")
require.Error(t, err)
assert.Contains(
t,
err.Error(),
"multiple conflicting time zone configurations found:\n",
fmt.Sprintf("error %q differs from the string set", err))
assert.Contains(
t,
err.Error(),
"America/Sao_Paulo\n",
fmt.Sprintf("error %q differs from the string set", err))
assert.Contains(
t,
err.Error(),
"America/Los_Angeles\n",
fmt.Sprintf("error %q differs from the string set", err))
assert.Contains(
t,
err.Error(),
"Africa/Harare\n",
fmt.Sprintf("error %q differs from the string set", err))
assert.Contains(
t,
err.Error(),
"Fix the configuration, or set the time zone in a TZ environment variable",
fmt.Sprintf("error %q differs from the string set", err))
}
func TestEnv(t *testing.T) {
err := os.Setenv("TZ", "America/Sao_Paulo")
require.NoError(t, err)
defer os.Unsetenv("TZ")
tz := parseEnv()
assert.Equal(t, "America/Sao_Paulo", tz)
}
func TestEnv_Filepath(t *testing.T) {
tests := map[string]struct {
Filepath string
Dir string
DestinationPath string
Expected string
}{
"America/Sao_Paulo": {
Filepath: "testdata/zoneinfo/America/Sao_Paulo",
Dir: "America",
DestinationPath: "America/Sao_Paulo",
Expected: "America/Sao_Paulo",
},
"UTC": {
Filepath: "testdata/zoneinfo/UTC",
Dir: "UTC",
DestinationPath: "UTC/UTC",
Expected: "UTC",
},
}
tmpDir, err := os.MkdirTemp(os.TempDir(), "")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err = os.Mkdir(filepath.Join(tmpDir, test.Dir), os.FileMode(int(0700)))
require.NoError(t, err)
tmpTimezonePath := filepath.Join(tmpDir, test.DestinationPath)
copyFile(t, test.Filepath, tmpTimezonePath)
err = os.Setenv("TZ", tmpTimezonePath)
require.NoError(t, err)
defer os.Unsetenv("TZ")
tz := parseEnv()
assert.Equal(t, test.Expected, tz)
})
}
}
func copyFile(t *testing.T, source, destination string) {
input, err := os.ReadFile(source)
require.NoError(t, err)
err = os.WriteFile(destination, input, 0600)
require.NoError(t, err)
}