-
Notifications
You must be signed in to change notification settings - Fork 3
/
load_config_test.go
39 lines (31 loc) · 1.56 KB
/
load_config_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
package main_test
import (
"os"
"path/filepath"
"testing"
"github.com/dhamidi/leader"
"github.com/stretchr/testify/assert"
)
func TestLoadConfig_Execute_loads_less_specific_config_files_first(t *testing.T) {
homeRC := `{"keys": {"h": "home", "p": "home", "c": "home"}}`
parentRC := `{"keys": {"h": "home", "p": "parent", "c": "parent"}}`
childRC := `{"keys": {"h": "home", "p": "parent", "c": "child"}}`
context := newTestContextForConfig(t)
defineTestFile(context, os.ExpandEnv("${HOME}/.leaderrc"), homeRC)
defineTestFile(context, filepath.Clean(os.ExpandEnv("${PWD}/../.leaderrc")), parentRC)
defineTestFile(context, os.ExpandEnv("${PWD}/.leaderrc"), childRC)
main.NewLoadConfig(context, os.ExpandEnv("${PWD}"), os.ExpandEnv("${HOME}")).Execute()
assert.Equal(t, "home", context.CurrentKeyMap.LookupKey('h').Description())
assert.Equal(t, "parent", context.CurrentKeyMap.LookupKey('p').Description())
assert.Equal(t, "child", context.CurrentKeyMap.LookupKey('c').Description())
}
func TestLoadConfig_Execute_tries_to_load_home_leaderrc_even_when_outside_of_home_directory(t *testing.T) {
homeRC := `{"keys": {"h": "home", "c": "home"}}`
currentRC := `{"keys": {"c": "child"}}`
context := newTestContextForConfig(t)
defineTestFile(context, os.ExpandEnv("${HOME}/.leaderrc"), homeRC)
defineTestFile(context, "/tmp/.leaderrc", currentRC)
main.NewLoadConfig(context, "/tmp", os.ExpandEnv("${HOME}")).Execute()
assert.Equal(t, "home", context.CurrentKeyMap.LookupKey('h').Description())
assert.Equal(t, "child", context.CurrentKeyMap.LookupKey('c').Description())
}