-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathconfig_test.go
More file actions
169 lines (142 loc) · 4.73 KB
/
Copy pathconfig_test.go
File metadata and controls
169 lines (142 loc) · 4.73 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package jid
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultConfig(t *testing.T) {
cfg := defaultConfig()
assert.Equal(t, historyMaxSize, cfg.History.MaxSize)
assert.Equal(t, "", cfg.History.Path)
assert.Equal(t, "tab", cfg.Keybindings.CandidateNext)
assert.Equal(t, "", cfg.Keybindings.CandidatePrev)
assert.Equal(t, "up", cfg.Keybindings.HistoryPrev)
assert.Equal(t, "down", cfg.Keybindings.HistoryNext)
assert.Equal(t, "ctrl+j", cfg.Keybindings.ScrollDown)
assert.Equal(t, "ctrl+k", cfg.Keybindings.ScrollUp)
assert.Equal(t, "ctrl+x", cfg.Keybindings.ToggleFuncHelp)
}
func TestLoadConfigMissingFile(t *testing.T) {
// point config path at a nonexistent file via env manipulation
// We test via a helper that accepts a path
cfg := loadConfigFromPath("/nonexistent/path/config.toml")
assert.Equal(t, defaultConfig(), cfg)
}
func TestLoadConfigFull(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
content := `
[history]
path = "/tmp/my_history"
max_size = 500
[keybindings]
candidate_next = "f1"
candidate_prev = "f2"
history_prev = "ctrl+p"
history_next = "ctrl+n"
scroll_down = "ctrl+j"
`
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
cfg := loadConfigFromPath(path)
assert.Equal(t, "/tmp/my_history", cfg.History.Path)
assert.Equal(t, 500, cfg.History.MaxSize)
assert.Equal(t, "f1", cfg.Keybindings.CandidateNext)
assert.Equal(t, "f2", cfg.Keybindings.CandidatePrev)
assert.Equal(t, "ctrl+p", cfg.Keybindings.HistoryPrev)
assert.Equal(t, "ctrl+n", cfg.Keybindings.HistoryNext)
// unspecified fields keep defaults
assert.Equal(t, "ctrl+x", cfg.Keybindings.ToggleFuncHelp)
}
func TestLoadConfigPartialOverride(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
content := `
[keybindings]
scroll_down = "ctrl+d"
`
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
cfg := loadConfigFromPath(path)
assert.Equal(t, "ctrl+d", cfg.Keybindings.ScrollDown)
// all other fields keep defaults
assert.Equal(t, "tab", cfg.Keybindings.CandidateNext)
assert.Equal(t, "up", cfg.Keybindings.HistoryPrev)
assert.Equal(t, historyMaxSize, cfg.History.MaxSize)
}
func TestLoadConfigInvalidTOML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
require.NoError(t, os.WriteFile(path, []byte("not valid toml :::"), 0644))
cfg := loadConfigFromPath(path)
assert.Equal(t, defaultConfig(), cfg)
}
func TestExpandPath(t *testing.T) {
home, err := os.UserHomeDir()
require.NoError(t, err)
assert.Equal(t, filepath.Join(home, ".jid_history"), expandPath("~/.jid_history"))
assert.Equal(t, "/absolute/path", expandPath("/absolute/path"))
assert.Equal(t, "relative/path", expandPath("relative/path"))
}
func TestHistoryPathFromConfig(t *testing.T) {
cfg := defaultConfig()
// no path configured → OS default
assert.Equal(t, historyFilePath(), cfg.HistoryPath())
// custom path
cfg.History.Path = "/custom/history"
assert.Equal(t, "/custom/history", cfg.HistoryPath())
// tilde path
home, _ := os.UserHomeDir()
cfg.History.Path = "~/.jid_history"
assert.Equal(t, filepath.Join(home, ".jid_history"), cfg.HistoryPath())
}
func TestMergeKeybindingsEmpty(t *testing.T) {
dst := defaultConfig().Keybindings
mergeKeybindings(&dst, KeybindingsConfig{})
// nothing should change
assert.Equal(t, defaultConfig().Keybindings, dst)
}
func TestDefaultConfigExitOnEnter(t *testing.T) {
cfg := defaultConfig()
// Default: ExitOnEnter not set (nil) → IsExitOnEnter returns true
assert.Nil(t, cfg.Behavior.ExitOnEnter)
assert.True(t, cfg.IsExitOnEnter())
assert.Equal(t, "ctrl+q", cfg.Keybindings.Quit)
}
func TestLoadConfigExitOnEnterFalse(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
content := `
[behavior]
exit_on_enter = false
`
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
cfg := loadConfigFromPath(path)
assert.NotNil(t, cfg.Behavior.ExitOnEnter)
assert.False(t, cfg.IsExitOnEnter())
}
func TestLoadConfigExitOnEnterTrue(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
content := `
[behavior]
exit_on_enter = true
`
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
cfg := loadConfigFromPath(path)
assert.NotNil(t, cfg.Behavior.ExitOnEnter)
assert.True(t, cfg.IsExitOnEnter())
}
func TestLoadConfigCustomQuitKey(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
content := `
[keybindings]
quit = "ctrl+d"
`
require.NoError(t, os.WriteFile(path, []byte(content), 0644))
cfg := loadConfigFromPath(path)
assert.Equal(t, "ctrl+d", cfg.Keybindings.Quit)
// other fields unchanged
assert.Equal(t, "tab", cfg.Keybindings.CandidateNext)
}