forked from dhamidi/leader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_test.go
111 lines (96 loc) · 3.24 KB
/
bind_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
package main_test
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/dhamidi/leader"
"github.com/stretchr/testify/assert"
)
func TestBind_Execute_creates_local_configuration_file_if_it_does_not_exist_yet(t *testing.T) {
context := newTestContextForConfig(t)
assert.NoError(t, main.NewBind(context, "d", "date").Execute())
expectedConfig, _ := json.MarshalIndent(map[string]interface{}{
"keys": map[string]interface{}{
"d": "date",
},
}, "", " ")
configFile, err := context.Files.Open(".leaderrc")
assert.NoError(t, err)
actualConfig, _ := ioutil.ReadAll(configFile)
assert.Equal(t, string(expectedConfig), string(actualConfig))
}
func TestBind_Execute_adds_a_binding_to_local_configuration_by_default(t *testing.T) {
childRC := `{"keys": {"h": "home", "c": "child"}}`
context := newTestContextForConfig(t)
defineTestFile(context, ".leaderrc", childRC)
main.NewBind(context, "c", "bind").Execute()
expectedConfig, _ := json.MarshalIndent(map[string]interface{}{
"keys": map[string]interface{}{
"h": "home",
"c": "bind",
},
}, "", " ")
configFile, err := context.Files.Open(".leaderrc")
assert.NoError(t, err)
actualConfig, _ := ioutil.ReadAll(configFile)
assert.Equal(t, string(expectedConfig), string(actualConfig))
}
func TestBind_Execute_supports_nested_bindings(t *testing.T) {
childRC := `{"keys": {"h": "home", "c": "child"}}`
context := newTestContextForConfig(t)
defineTestFile(context, ".leaderrc", childRC)
main.NewBind(context, "cc", "bind").Execute()
expectedConfig, _ := json.MarshalIndent(map[string]interface{}{
"keys": map[string]interface{}{
"h": "home",
"c": map[string]interface{}{
"keys": map[string]interface{}{
"c": "bind",
},
},
},
}, "", " ")
configFile, err := context.Files.Open(".leaderrc")
assert.NoError(t, err)
actualConfig, _ := ioutil.ReadAll(configFile)
assert.Equal(t, string(expectedConfig), string(actualConfig))
}
func TestBind_Execute_writes_to_home_leaderrc_if_option_global_is_given(t *testing.T) {
homeRCPath := os.ExpandEnv("${HOME}/.leaderrc")
homeRC := `{"keys": {"h": "home", "c": "child"}}`
context := newTestContextForConfig(t)
defineTestFile(context, homeRCPath, homeRC)
main.NewBind(context, "cd", "bind").
SetGlobal(homeRCPath).
Execute()
expectedConfig, _ := json.MarshalIndent(map[string]interface{}{
"keys": map[string]interface{}{
"h": "home",
"c": map[string]interface{}{
"keys": map[string]interface{}{
"d": "bind",
},
},
},
}, "", " ")
configFile, err := context.Files.Open(homeRCPath)
assert.NoError(t, err)
actualConfig, _ := ioutil.ReadAll(configFile)
assert.Equal(t, string(expectedConfig), string(actualConfig))
}
func TestBind_Execute_removes_binding_if_unbind_option_is_set(t *testing.T) {
childRC := `{"keys": {"h": "home", "c": "child"}}`
context := newTestContextForConfig(t)
defineTestFile(context, ".leaderrc", childRC)
main.NewBind(context, "c", "").Unbind().Execute()
expectedConfig, _ := json.MarshalIndent(map[string]interface{}{
"keys": map[string]interface{}{
"h": "home",
},
}, "", " ")
configFile, err := context.Files.Open(".leaderrc")
assert.NoError(t, err)
actualConfig, _ := ioutil.ReadAll(configFile)
assert.Equal(t, string(expectedConfig), string(actualConfig))
}