-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
163 lines (138 loc) · 4.45 KB
/
config_test.go
File metadata and controls
163 lines (138 loc) · 4.45 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestGetConfigPathPrefersXdgConfigHome(t *testing.T) {
xdgConfigHome := filepath.Join(t.TempDir(), "xdg")
t.Setenv("XDG_CONFIG_HOME", xdgConfigHome)
configPath := getConfigPath()
expected := filepath.Join(xdgConfigHome, configDirName, configFileName)
if configPath != expected {
t.Fatalf("expected config path %q, got %q", expected, configPath)
}
}
func TestGetConfigPathFallsBackToUserConfigDir(t *testing.T) {
// Clear XDG_CONFIG_HOME to test fallback
t.Setenv("XDG_CONFIG_HOME", "")
configPath := getConfigPath()
// Should contain the config dir name and file name
if !strings.Contains(configPath, configDirName) {
t.Errorf("config path %q should contain %q", configPath, configDirName)
}
if !strings.HasSuffix(configPath, configFileName) {
t.Errorf("config path %q should end with %q", configPath, configFileName)
}
}
func TestConfigDefaultValues(t *testing.T) {
config := &Config{}
// Empty config should use defaults via getter methods
if got := config.getGitTimeout(); got != gitCmdTimeout {
t.Errorf("getGitTimeout() = %v, want %v", got, gitCmdTimeout)
}
if got := config.getGitSlowTimeout(); got != gitCmdSlowTimeout {
t.Errorf("getGitSlowTimeout() = %v, want %v", got, gitCmdSlowTimeout)
}
// Empty fields should be empty
if config.WorktreeDir != "" {
t.Errorf("WorktreeDir should be empty, got %q", config.WorktreeDir)
}
if config.Shell != "" {
t.Errorf("Shell should be empty, got %q", config.Shell)
}
if config.PostCreate != "" {
t.Errorf("PostCreate should be empty, got %q", config.PostCreate)
}
if config.DefaultMergeStrategy != "" {
t.Errorf("DefaultMergeStrategy should be empty, got %q", config.DefaultMergeStrategy)
}
}
func TestLoadConfigWithAllFields(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", tmpDir)
configDir := filepath.Join(tmpDir, configDirName)
if err := os.MkdirAll(configDir, 0755); err != nil {
t.Fatalf("create config dir: %v", err)
}
configData := `{
"worktree_dir": "../my-worktrees",
"shell": "/bin/fish",
"post_create": "npm ci && npm run build",
"default_merge_strategy": "squash",
"git_timeout": 15,
"git_slow_timeout": 30
}`
configPath := filepath.Join(configDir, configFileName)
if err := os.WriteFile(configPath, []byte(configData), 0644); err != nil {
t.Fatalf("write config: %v", err)
}
config, err := loadConfig()
if err != nil {
t.Fatalf("loadConfig: %v", err)
}
if config.WorktreeDir != "../my-worktrees" {
t.Errorf("WorktreeDir = %q, want ../my-worktrees", config.WorktreeDir)
}
if config.Shell != "/bin/fish" {
t.Errorf("Shell = %q, want /bin/fish", config.Shell)
}
if config.PostCreate != "npm ci && npm run build" {
t.Errorf("PostCreate = %q, want 'npm ci && npm run build'", config.PostCreate)
}
if config.DefaultMergeStrategy != "squash" {
t.Errorf("DefaultMergeStrategy = %q, want squash", config.DefaultMergeStrategy)
}
if config.GitTimeout != 15 {
t.Errorf("GitTimeout = %d, want 15", config.GitTimeout)
}
if config.GitSlowTimeout != 30 {
t.Errorf("GitSlowTimeout = %d, want 30", config.GitSlowTimeout)
}
}
func TestLoadConfigIgnoresExtraFields(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", tmpDir)
configDir := filepath.Join(tmpDir, configDirName)
if err := os.MkdirAll(configDir, 0755); err != nil {
t.Fatalf("create config dir: %v", err)
}
// Config with unknown fields should still load known fields
configData := `{
"worktree_dir": "known-dir",
"unknown_field": "should be ignored",
"another_unknown": 42
}`
configPath := filepath.Join(configDir, configFileName)
if err := os.WriteFile(configPath, []byte(configData), 0644); err != nil {
t.Fatalf("write config: %v", err)
}
config, err := loadConfig()
if err != nil {
t.Fatalf("loadConfig: %v", err)
}
if config.WorktreeDir != "known-dir" {
t.Errorf("WorktreeDir = %q, want known-dir", config.WorktreeDir)
}
}
func TestSaveConfigFormatsNicely(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", tmpDir)
config := &Config{
WorktreeDir: "test-dir",
}
if err := saveConfig(config); err != nil {
t.Fatalf("saveConfig: %v", err)
}
configPath := filepath.Join(tmpDir, configDirName, configFileName)
data, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("read config: %v", err)
}
content := string(data)
// Should be indented (pretty printed)
if !strings.Contains(content, " ") {
t.Errorf("expected indented JSON, got:\n%s", content)
}
}