-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_handlers_test.go
More file actions
324 lines (261 loc) · 7.87 KB
/
Copy pathtypes_handlers_test.go
File metadata and controls
324 lines (261 loc) · 7.87 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package configurature_test
import (
"fmt"
"os"
fp "path/filepath"
"strings"
"testing"
co "github.com/imoore76/configurature"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
/*
Definitions for implementing the ImageFile custom type
*/
// type to be set as config struct field types
type ImageFile string
func (i *ImageFile) String() string {
return (string)(*i)
}
func (i *ImageFile) Set(v string) error {
// This will fail if the file does not exist or there is any other error
// accessing the file
if _, err := os.Stat(v); err != nil {
return err
}
// This will fail if the file is not of the supported type
switch ext := fp.Ext(strings.ToLower(v)); ext {
case ".png", ".jpg", ".jpeg", ".gif":
// ok
default:
return fmt.Errorf("file type \"%s\" not supported", ext)
}
*i = (ImageFile)(v)
return nil
}
func (i *ImageFile) Type() string {
return "imageFile"
}
func (i *ImageFile) Interface() any {
return ImageFile(*i)
}
type MyConfig struct {
StateFile string `help:"File in which to store lock state" short:"s"`
Image ImageFile `help:"Path to an image"`
Images []ImageFile `help:"Paths to images"`
Conf co.ConfigFile `help:"Configuration file"`
}
func addImageFileTypes() {
co.AddType[ImageFile]()
co.AddType[[]ImageFile]()
}
func TestRegisterCustomType(t *testing.T) {
addImageFileTypes()
}
func TestCustomType_Flag(t *testing.T) {
addImageFileTypes()
tmp, _ := os.CreateTemp("", "cfgr-test-*.png")
tmp.Close()
defer os.Remove(tmp.Name())
conf := co.Configure[MyConfig](&co.Options{
Args: []string{"--image=" + tmp.Name()},
Usage: func(_ *flag.FlagSet) {},
})
assert := assert.New(t)
assert.Equal(ImageFile(tmp.Name()), conf.Image)
}
func TestCustomType_Error(t *testing.T) {
addImageFileTypes()
if os.Getenv("TEST_PASSTHROUGH") == "1" {
co.Configure[MyConfig](&co.Options{
Args: []string{"--image", os.Getenv("TEST_TEMP_FILE_NAME")},
Usage: func(_ *flag.FlagSet) {},
})
os.Exit(0)
}
fileName := tmpFile(t, "mod")
stdout, stderr := runExternal(t)
assert := assert.New(t)
assert.Equal(`invalid argument "`+fileName+`" for "--image" flag: file type `+
`".mod" not supported`+"\n", stdout)
assert.Equal(`invalid argument "`+fileName+`" for "--image" flag: file type `+
`".mod" not supported`+"\n", stderr)
}
func TestCustomType_ErrorConfigFile(t *testing.T) {
addImageFileTypes()
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
fileName := tmpFile(t, "mod")
tmp.Write([]byte("image: " + fileName + "\n"))
tmp.Close()
co.Configure[MyConfig](&co.Options{
Args: []string{"--conf", tmp.Name()},
Usage: func(_ *flag.FlagSet) {},
})
os.Exit(1)
}
stdout, stderr := runExternal(t)
assert := assert.New(t)
assert.Equal("", stdout)
assert.Equal(`error parsing configuration: unable to set value for image: `+
`file type ".mod" not supported`+"\n", stderr)
}
func TestCustomSliceType_Flag(t *testing.T) {
addImageFileTypes()
files := make([]string, 3)
expected := make([]ImageFile, 3)
for idx := range 3 {
tmp, _ := os.CreateTemp("", "cfgr-test-*.png")
tmp.Close()
defer os.Remove(tmp.Name())
files[idx] = tmp.Name()
expected[idx] = ImageFile(tmp.Name())
}
conf := co.Configure[MyConfig](&co.Options{
Args: []string{"--images", strings.Join(files, ",")},
Usage: func(_ *flag.FlagSet) {},
})
assert := assert.New(t)
assert.Equal(expected, conf.Images)
}
func TestCustomSliceType_Error(t *testing.T) {
addImageFileTypes()
if os.Getenv("TEST_PASSTHROUGH") == "1" {
co.Configure[MyConfig](&co.Options{
Args: []string{"--images", os.Getenv("TEST_TEMP_FILE_NAME")},
Usage: func(_ *flag.FlagSet) {},
})
os.Exit(0)
}
fileName := tmpFile(t, "mod")
stdout, stderr := runExternal(t)
assert := assert.New(t)
assert.Equal(`invalid argument "`+fileName+`" for "--images" flag: file type `+
`".mod" not supported`+"\n", stdout)
assert.Equal(`invalid argument "`+fileName+`" for "--images" flag: file type `+
`".mod" not supported`+"\n", stderr)
}
func TestCustomSliceType_ConfigFile(t *testing.T) {
addImageFileTypes()
files := make([]string, 3)
expected := make([]ImageFile, 3)
for idx := range 3 {
tmp, _ := os.CreateTemp("", "cfgr-test-config-file*.png")
tmp.Close()
defer os.Remove(tmp.Name())
files[idx] = tmp.Name()
expected[idx] = ImageFile(tmp.Name())
}
tmp, _ := os.CreateTemp("", "cfgr-test-*.yaml")
tmp.Write([]byte("images:\n - " + strings.Join(files, "\n - ") + "\n"))
tmp.Close()
defer os.Remove(tmp.Name())
conf := co.Configure[MyConfig](&co.Options{
Args: []string{"--conf", tmp.Name()},
Usage: func(_ *flag.FlagSet) {},
})
assert.Equal(t, expected, conf.Images)
}
func TestCustomSliceType_ErrorConfigFile(t *testing.T) {
addImageFileTypes()
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
fileName := tmpFile(t, "mod")
tmp.Write([]byte("image: " + fileName + "\n"))
tmp.Close()
co.Configure[MyConfig](&co.Options{
Args: []string{"--conf", tmp.Name()},
Usage: func(_ *flag.FlagSet) {},
})
panic("Expected exit")
}
stdout, stderr := runExternal(t)
assert := assert.New(t)
assert.Equal("", stdout)
assert.Equal(`error parsing configuration: unable to set value for image: `+
`file type ".mod" not supported`+"\n", stderr)
}
func TestMapValueType(t *testing.T) {
type Color string
co.AddMapValueType("",
[]string{"red", "blue", "green"},
[]Color{"#ff0000", "#0000ff", "#00ff00"},
)
type CConf struct {
Background Color `help:"background color" default:"red"`
}
conf := co.Configure[CConf](&co.Options{
Args: []string{"--background", "blue"},
Usage: func(_ *flag.FlagSet) {},
})
assert.Equal(t, conf.Background, Color("#0000ff"))
}
func TestMapValueType_NoValue(t *testing.T) {
type Color string
co.AddMapValueType("",
[]string{"red", "blue", "green"},
[]Color{"#ff0000", "#0000ff", "#00ff00"},
)
type CConf struct {
Background Color `help:"background color"`
}
conf := co.Configure[CConf](&co.Options{})
assert.Equal(t, conf.Background, Color(""))
}
func TestMapValueType_BadValue(t *testing.T) {
type Color string
co.AddMapValueType("",
[]string{"red", "blue", "green"},
[]Color{"#ff0000", "#0000ff", "#00ff00"},
)
type CConf struct {
Background Color `help:"background color"`
}
if os.Getenv("TEST_PASSTHROUGH") == "1" {
co.Configure[CConf](&co.Options{
Args: []string{"--background", "yellow"},
Usage: func(_ *flag.FlagSet) {},
})
panic("Should have exited")
}
assert := assert.New(t)
stdout, stderr := runExternal(t)
assert.Equal(`invalid argument "yellow" for "--background" flag: `+
`invalid Color: "yellow"`+"\n", stdout)
assert.Equal(stdout, stderr)
}
func TestMapValueType_Usage(t *testing.T) {
type Color string
co.AddMapValueType("",
[]string{"red", "blue", "green"},
[]Color{"#ff0000", "#0000ff", "#00ff00"},
)
type MVT struct {
Background Color `help:"background color" default:"red"`
}
if os.Getenv("TEST_PASSTHROUGH") == "1" {
co.Configure[MVT](&co.Options{
Args: []string{"-h"},
})
panic("Should have exited")
}
assert := assert.New(t)
stdout, stderr := runExternal(t)
assert.Equal("", stderr)
assert.True(strings.Contains(stdout, `--background Color background color (red|blue|green) (default red)`), stdout)
}