aboutsummaryrefslogtreecommitdiff
path: root/config/config_test.go
blob: 4296535f804d8cbef55701aa105abb050d36e327 (about) (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package config

import (
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

func setupTestRepo(t *testing.T) (string, func()) {
	t.Helper()
	tempDir, err := os.MkdirTemp("", "furgit-config-test-*")
	if err != nil {
		t.Fatalf("failed to create temp dir: %v", err)
	}
	cleanup := func() {
		_ = os.RemoveAll(tempDir)
	}

	cmd := exec.Command("git", "init", "--object-format=sha256", "--bare", tempDir)
	cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null")
	if output, err := cmd.CombinedOutput(); err != nil {
		cleanup()
		t.Fatalf("failed to init git repo: %v\n%s", err, output)
	}

	return tempDir, cleanup
}

func gitConfig(t *testing.T, dir string, args ...string) {
	t.Helper()
	fullArgs := append([]string{"config"}, args...)
	cmd := exec.Command("git", fullArgs...)
	cmd.Dir = dir
	cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null")
	if output, err := cmd.CombinedOutput(); err != nil {
		t.Fatalf("git config %v failed: %v\n%s", args, err, output)
	}
}

func gitConfigGet(t *testing.T, dir, key string) string {
	t.Helper()
	cmd := exec.Command("git", "config", "--get", key)
	cmd.Dir = dir
	cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null")
	output, err := cmd.CombinedOutput()
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(output))
}

func TestConfigAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "core.bare", "true")
	gitConfig(t, repoPath, "core.filemode", "false")
	gitConfig(t, repoPath, "user.name", "John Doe")
	gitConfig(t, repoPath, "user.email", "john@example.com")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	if got := cfg.Get("core", "", "bare"); got != "true" {
		t.Errorf("core.bare: got %q, want %q", got, "true")
	}
	if got := cfg.Get("core", "", "filemode"); got != "false" {
		t.Errorf("core.filemode: got %q, want %q", got, "false")
	}
	if got := cfg.Get("user", "", "name"); got != "John Doe" {
		t.Errorf("user.name: got %q, want %q", got, "John Doe")
	}
	if got := cfg.Get("user", "", "email"); got != "john@example.com" {
		t.Errorf("user.email: got %q, want %q", got, "john@example.com")
	}
}

func TestConfigSubsectionAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "remote.origin.url", "https://example.com/repo.git")
	gitConfig(t, repoPath, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	if got := cfg.Get("remote", "origin", "url"); got != "https://example.com/repo.git" {
		t.Errorf("remote.origin.url: got %q, want %q", got, "https://example.com/repo.git")
	}
	if got := cfg.Get("remote", "origin", "fetch"); got != "+refs/heads/*:refs/remotes/origin/*" {
		t.Errorf("remote.origin.fetch: got %q, want %q", got, "+refs/heads/*:refs/remotes/origin/*")
	}
}

func TestConfigMultiValueAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "--add", "remote.origin.fetch", "+refs/heads/main:refs/remotes/origin/main")
	gitConfig(t, repoPath, "--add", "remote.origin.fetch", "+refs/heads/dev:refs/remotes/origin/dev")
	gitConfig(t, repoPath, "--add", "remote.origin.fetch", "+refs/tags/*:refs/tags/*")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	fetches := cfg.GetAll("remote", "origin", "fetch")
	if len(fetches) != 3 {
		t.Fatalf("expected 3 fetch values, got %d", len(fetches))
	}

	expected := []string{
		"+refs/heads/main:refs/remotes/origin/main",
		"+refs/heads/dev:refs/remotes/origin/dev",
		"+refs/tags/*:refs/tags/*",
	}
	for i, want := range expected {
		if fetches[i] != want {
			t.Errorf("fetch[%d]: got %q, want %q", i, fetches[i], want)
		}
	}
}

func TestConfigCaseInsensitiveAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "Core.Bare", "true")
	gitConfig(t, repoPath, "CORE.FileMode", "false")

	gitVerifyBare := gitConfigGet(t, repoPath, "core.bare")
	gitVerifyFilemode := gitConfigGet(t, repoPath, "core.filemode")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	if got := cfg.Get("core", "", "bare"); got != gitVerifyBare {
		t.Errorf("core.bare: got %q, want %q (from git)", got, gitVerifyBare)
	}
	if got := cfg.Get("CORE", "", "BARE"); got != gitVerifyBare {
		t.Errorf("CORE.BARE: got %q, want %q (from git)", got, gitVerifyBare)
	}
	if got := cfg.Get("core", "", "filemode"); got != gitVerifyFilemode {
		t.Errorf("core.filemode: got %q, want %q (from git)", got, gitVerifyFilemode)
	}
}

func TestConfigBooleanAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "test.flag1", "true")
	gitConfig(t, repoPath, "test.flag2", "false")
	gitConfig(t, repoPath, "test.flag3", "yes")
	gitConfig(t, repoPath, "test.flag4", "no")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	tests := []struct {
		key  string
		want string
	}{
		{"flag1", gitConfigGet(t, repoPath, "test.flag1")},
		{"flag2", gitConfigGet(t, repoPath, "test.flag2")},
		{"flag3", gitConfigGet(t, repoPath, "test.flag3")},
		{"flag4", gitConfigGet(t, repoPath, "test.flag4")},
	}

	for _, tt := range tests {
		if got := cfg.Get("test", "", tt.key); got != tt.want {
			t.Errorf("test.%s: got %q, want %q (from git)", tt.key, got, tt.want)
		}
	}
}

func TestConfigComplexValuesAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "test.spaced", "value with spaces")
	gitConfig(t, repoPath, "test.special", "value=with=equals")
	gitConfig(t, repoPath, "test.path", "/path/to/something")
	gitConfig(t, repoPath, "test.number", "12345")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	tests := []string{"spaced", "special", "path", "number"}
	for _, key := range tests {
		want := gitConfigGet(t, repoPath, "test."+key)
		if got := cfg.Get("test", "", key); got != want {
			t.Errorf("test.%s: got %q, want %q (from git)", key, got, want)
		}
	}
}

func TestConfigEntriesAgainstGit(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitConfig(t, repoPath, "core.bare", "true")
	gitConfig(t, repoPath, "core.filemode", "false")
	gitConfig(t, repoPath, "user.name", "Test User")

	cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
	if err != nil {
		t.Fatalf("failed to open config: %v", err)
	}
	defer func() { _ = cfgFile.Close() }()

	cfg, err := ParseConfig(cfgFile)
	if err != nil {
		t.Fatalf("ParseConfig failed: %v", err)
	}

	entries := cfg.Entries()
	if len(entries) < 3 {
		t.Errorf("expected at least 3 entries, got %d", len(entries))
	}

	found := make(map[string]bool)
	for _, entry := range entries {
		key := entry.Section + "." + entry.Key
		if entry.Subsection != "" {
			key = entry.Section + "." + entry.Subsection + "." + entry.Key
		}
		found[key] = true

		gitValue := gitConfigGet(t, repoPath, key)
		if entry.Value != gitValue {
			t.Errorf("entry %s: got value %q, git has %q", key, entry.Value, gitValue)
		}
	}
}

func TestConfigErrorCases(t *testing.T) {
	tests := []struct {
		name   string
		config string
	}{
		{
			name:   "key before section",
			config: "bare = true",
		},
		{
			name:   "invalid section character",
			config: "[core/invalid]",
		},
		{
			name:   "unterminated section",
			config: "[core",
		},
		{
			name:   "unterminated quote",
			config: "[core]\n\tbare = \"true",
		},
		{
			name:   "invalid escape",
			config: "[core]\n\tvalue = \"test\\x\"",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			r := strings.NewReader(tt.config)
			_, err := ParseConfig(r)
			if err == nil {
				t.Errorf("expected error for %s", tt.name)
			}
		})
	}
}