forked from muja/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_test.go
103 lines (92 loc) · 2.06 KB
/
cache_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
package goconfig
import (
"context"
"os"
"testing"
testspace "github.com/Jiu2015/gotestspace"
"github.com/stretchr/testify/assert"
)
var cacheTestSpace testspace.Space
func TestCacheCreateConfig(t *testing.T) {
var err error
cacheTestSpace, err = testspace.Create(
testspace.WithPathOption("testspace-*"),
testspace.WithShellOption(`
git config -f config.test a.b "value of a.b"
git config -f config.test a.b.c "value of a/b/c"
`),
)
assert.Nil(t, err)
}
func TestCacheSet(t *testing.T) {
fn := cacheTestSpace.GetPath("config.test")
fi, err := os.Stat(fn)
if assert.Nil(t, err) {
cfg, err := loadConfigFile(fn)
if assert.Nil(t, err) {
CacheSet(fn, cfg, fi.Size(), fi.ModTime())
}
}
}
func TestCacheGet(t *testing.T) {
fn := cacheTestSpace.GetPath("config.test")
cfg, ok := CacheGet(fn)
if assert.True(t, ok) {
for _, tc := range []struct {
Key string
Value string
}{
{"a.b", "value of a.b"},
{"a.b.c", "value of a/b/c"},
} {
assert.Equal(t,
tc.Value,
cfg.Get(tc.Key),
)
}
}
}
func TestCacheUpdateConfig(t *testing.T) {
cancelCtx, cancel := context.WithCancel(context.Background())
defer cancel()
stdout, stderr, err := cacheTestSpace.Execute(
cancelCtx,
`git config -f config.test foo.bar "foo bar"`,
)
assert.Nil(t, err, "stdout: %s\nstderr: %s", stdout, stderr)
}
func TestCacheOutOfDate(t *testing.T) {
fn := cacheTestSpace.GetPath("config.test")
cfg, ok := CacheGet(fn)
assert.False(t, ok)
assert.Nil(t, cfg)
}
func TestCacheSetAgain(t *testing.T) {
fn := cacheTestSpace.GetPath("config.test")
fi, err := os.Stat(fn)
if assert.Nil(t, err) {
cfg, err := loadConfigFile(fn)
if assert.Nil(t, err) {
CacheSet(fn, cfg, fi.Size(), fi.ModTime())
}
}
cfg, ok := CacheGet(fn)
if assert.True(t, ok) {
for _, tc := range []struct {
Key string
Value string
}{
{"a.b", "value of a.b"},
{"a.b.c", "value of a/b/c"},
{"foo.bar", "foo bar"},
} {
assert.Equal(t,
tc.Value,
cfg.Get(tc.Key),
)
}
}
cfg, ok = CacheGet(fn)
assert.True(t, ok)
assert.NotNil(t, cfg)
}