forked from dhamidi/leader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_config_file_test.go
151 lines (135 loc) · 3.75 KB
/
load_config_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
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
package main_test
import (
"bytes"
"io"
"os"
"testing"
"github.com/dhamidi/leader"
"github.com/stretchr/testify/assert"
)
// writeableTestFile wraps *bytes.Buffer by updating a given string after each call to Write.
type writeableTestFile struct {
*bytes.Buffer
updateAfterWrite *string
}
func (w *writeableTestFile) Write(p []byte) (n int, err error) {
n, err = w.Buffer.Write(p)
*w.updateAfterWrite = w.Buffer.String()
return
}
// testFileSystem returns files from predefined buffers holding the file's contents
type testFileSystem struct {
files []*struct{ name, contents string }
}
// newTestFileSystem returns a new, empty test file system.
func newTestFileSystem() *testFileSystem {
return &testFileSystem{
files: []*struct{ name, contents string }{},
}
}
// Open implements main.FileSystem by returning a handle to an internal buffer.
func (fs *testFileSystem) Open(name string) (io.Reader, error) {
for _, f := range fs.files {
if f.name == name {
return bytes.NewBufferString(f.contents), nil
}
}
return nil, os.ErrNotExist
}
// Create implements main.FileSystem by returning a handle to an
// internal buffer that can be used for writing.
//
// The internal representation of that buffer is updated after every
// call to Write. This ensures that a reader of the same file in the
// test file system sees changes made by a writer at the point in time
// the reader is opened.
func (fs *testFileSystem) Create(name string) (io.Writer, error) {
var file *struct{ name, contents string }
fs.Define(name, "")
for _, f := range fs.files {
if f.name == name {
file = f
break
}
}
result := &writeableTestFile{
Buffer: bytes.NewBufferString(""),
updateAfterWrite: &file.contents,
}
return result, nil
}
// Rename changes the name of the given file. If the given source
// does not exist, os.ErrNotExist is returned.
func (fs *testFileSystem) Rename(src, dest string) error {
var destFile, srcFile *struct{ name, contents string }
var srcIndex int
for i, f := range fs.files {
if f.name == src {
srcFile = f
srcIndex = i
continue
}
if f.name == dest {
destFile = f
continue
}
}
if srcFile == nil {
return os.ErrNotExist
}
if destFile == nil {
srcFile.name = dest
return nil
}
destFile.contents = srcFile.contents
if len(fs.files) == srcIndex+1 {
fs.files = fs.files[0:srcIndex]
} else {
fs.files = append(fs.files[0:srcIndex], fs.files[srcIndex+1:]...)
}
return nil
}
// Define sets the contents for a given file in this test file system
func (fs *testFileSystem) Define(path string, contents string) *testFileSystem {
var file struct{ name, contents string }
for _, f := range fs.files {
if f.name == path {
f.contents = contents
return fs
}
}
file.name = path
file.contents = contents
fs.files = append(fs.files, &file)
return fs
}
func TestLoadConfigFile_Execute_merges_key_bindings_from_config_file(t *testing.T) {
configFile := `
{
"keys": {
"d": "date",
"g": {
"name": "go",
"loopingKeys": ["t"],
"keys": {
"t": "go test -v ."
}
}
}
}
`
keymap := main.NewKeyMap("root")
context := newTestContext(t, keymap, bytes.NewBufferString(""), nil)
context.Files.(*testFileSystem).Define(".leaderrc", configFile)
loadConfig := main.NewLoadConfigFile(context, ".leaderrc")
keymap.Bind('d').Describe("do nothing")
keymap.Bind('g').Children().Bind('t').Describe("go test .")
assert.NoError(t, loadConfig.Execute(), "loadConfig.Execute()")
keyD := keymap.LookupKey('d')
keyG := keymap.LookupKey('g')
assert.Equal(t, "[d] date", keyD.String())
assert.Equal(t, "[g] <keymap go>", keyG.String())
keyGT := keyG.Children().LookupKey('t')
assert.True(t, keyGT.IsLooping(), "keyGT.IsLooping()")
assert.Equal(t, "[t] go test -v .", keyGT.String())
}