-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_test.go
More file actions
45 lines (41 loc) · 954 Bytes
/
options_test.go
File metadata and controls
45 lines (41 loc) · 954 Bytes
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
package confix
import (
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWithValidationAfterInitializing(t *testing.T) {
var before = "before"
var after = "after"
assert.NotEqual(t, before, after)
cfg := config[string]{
cfg: &before,
}
opt := WithValidation[string](func(cfg *string) error {
*cfg = after
return nil
})
assert.Equal(t, before, *cfg.cfg)
err := opt.apply(&cfg)
assert.NoError(t, err)
assert.Equal(t, before, after)
assert.Equal(t, after, *cfg.cfg)
}
func TestWithWritingConfigToFile(t *testing.T) {
fpath := path.Join(t.TempDir(), "config.yaml")
assert.NoFileExists(t, fpath)
tCfg := &testConfig{
A: generateRandom(t, 100),
}
cfg := config[testConfig]{
cfg: tCfg,
}
opt := WithWritingConfigToFile[testConfig](fpath)
err := opt.apply(&cfg)
assert.NoError(t, err)
if assert.FileExists(t, fpath) {
require.NoError(t, os.Remove(fpath))
}
}