-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
81 lines (72 loc) · 1.91 KB
/
file_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
package copre
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestConfigFileOptions struct {
A string `json:"a"`
B string `json:"b"`
C string `json:"c"`
}
func TestFileOptions(t *testing.T) {
require := require.New(t)
assert := assert.New(t)
dir, err := ioutil.TempDir("", "fileoptions")
require.NoError(err)
defer os.RemoveAll(dir)
pa := filepath.Join(dir, "a")
pb := filepath.Join(dir, "b")
pc := filepath.Join(dir, "c")
for _, p := range []string{pa, pb, pc} {
err = os.Mkdir(p, 0700)
require.NoError(err)
}
fa := filepath.Join(pa, "d.json")
fb := filepath.Join(pb, "e.json")
fc := filepath.Join(pc, "f.json")
err = os.WriteFile(fa, []byte(`{ "a": "a"}`), 0600)
require.NoError(err)
err = os.WriteFile(fb, []byte(`{ "b": "b"}`), 0600)
require.NoError(err)
err = os.WriteFile(fc, []byte(`{ "c": "c"}`), 0600)
require.NoError(err)
t.Run("MergeAll", func(t *testing.T) {
result := TestConfigFileOptions{}
err = File(fa, json.Unmarshal,
AppendFilePaths(fb, fc),
MergeFiles(),
).Process(&result)
require.NoError(err)
assert.Equal("a", result.A)
assert.Equal("b", result.B)
assert.Equal("c", result.C)
})
t.Run("FirstFound", func(t *testing.T) {
result := TestConfigFileOptions{}
err = File(fa, json.Unmarshal,
AppendFilePaths(fb, fc),
).Process(&result)
require.NoError(err)
assert.Equal("a", result.A)
assert.Equal("", result.B)
assert.Equal("", result.C)
})
}
func TestFileIgnoreNotFound(t *testing.T) {
fp := path.Join("does", "not", "exist")
result := TestConfigFileOptions{}
t.Run("WithOptionNoError", func(t *testing.T) {
err := File(fp, json.Unmarshal, IgnoreNotFound()).Process(&result)
require.NoError(t, err)
})
t.Run("WithoutOptionError", func(t *testing.T) {
err := File(fp, json.Unmarshal).Process(&result)
require.Error(t, err)
})
}