aboutsummaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
authorGravatar Runxi Yu2025-11-16 00:00:00 +0000
committerGravatar Runxi Yu2025-11-16 00:00:00 +0000
commitbad0f9715556a470d0de2a22c7040181e3a033ba (patch)
tree21463072ce5bc85682a887ce0cae26d833941af3 /config
parentEntryRecursive should return ErrNotFound instead of nil, nil (diff)
signature
Use actual git for tests and enhance Head
Diffstat (limited to 'config')
-rw-r--r--config/config_test.go500
1 files changed, 229 insertions, 271 deletions
diff --git a/config/config_test.go b/config/config_test.go
index f863c230..4296535f 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -1,365 +1,323 @@
package config
import (
+ "os"
+ "os/exec"
+ "path/filepath"
"strings"
"testing"
)
-func TestParseConfigSimple(t *testing.T) {
- input := `
-[core]
- repositoryformatversion = 0
- filemode = true
- bare = false
-
-[user]
- name = Alice Example
- email = alice@example.com
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+func setupTestRepo(t *testing.T) (string, func()) {
+ t.Helper()
+ tempDir, err := os.MkdirTemp("", "furgit-config-test-*")
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
- }
-
- if got := cfg.Get("core", "", "repositoryformatversion"); got != "0" {
- t.Errorf("core.repositoryformatversion = %q, want %q", got, "0")
+ t.Fatalf("failed to create temp dir: %v", err)
}
- if got := cfg.Get("core", "", "filemode"); got != "true" {
- t.Errorf("core.filemode = %q, want %q", got, "true")
+ cleanup := func() {
+ _ = os.RemoveAll(tempDir)
}
- if got := cfg.Get("user", "", "name"); got != "Alice Example" {
- t.Errorf("user.name = %q, want %q", got, "Alice Example")
- }
- if got := cfg.Get("user", "", "email"); got != "alice@example.com" {
- t.Errorf("user.email = %q, want %q", got, "alice@example.com")
- }
-}
-func TestParseConfigSubsection(t *testing.T) {
- input := `
-[remote "origin"]
- url = https://villosa.example.org/group1/group2//repos/repo
- fetch = +refs/heads/*:refs/remotes/origin/*
-`
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ 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)
}
- if got := cfg.Get("remote", "origin", "url"); got != "https://villosa.example.org/group1/group2//repos/repo" {
- t.Errorf("remote.origin.url = %q, want %q", got, "https://villosa.example.org/group1/group2//repos/repo")
- }
- if got := cfg.Get("remote", "origin", "fetch"); got != "+refs/heads/*:refs/remotes/origin/*" {
- t.Errorf("remote.origin.fetch = %q, want %q", got, "+refs/heads/*:refs/remotes/origin/*")
+ 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 TestParseConfigCaseInsensitive(t *testing.T) {
- input := `
-[Core]
- FileMode = true
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+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 {
- t.Fatalf("ParseConfig error: %v", err)
+ return ""
}
+ return strings.TrimSpace(string(output))
+}
- if got := cfg.Get("core", "", "filemode"); got != "true" {
- t.Errorf("core.filemode = %q, want %q", got, "true")
- }
- if got := cfg.Get("CORE", "", "FILEMODE"); got != "true" {
- t.Errorf("CORE.FILEMODE = %q, want %q", got, "true")
+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() }()
-func TestParseConfigBooleanKeys(t *testing.T) {
- input := `
-[core]
- bare
- ignorecase
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+ cfg, err := ParseConfig(cfgFile)
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ t.Fatalf("ParseConfig failed: %v", err)
}
if got := cfg.Get("core", "", "bare"); got != "true" {
- t.Errorf("core.bare = %q, want %q", got, "true")
- }
- if got := cfg.Get("core", "", "ignorecase"); got != "true" {
- t.Errorf("core.ignorecase = %q, want %q", got, "true")
+ t.Errorf("core.bare: got %q, want %q", got, "true")
}
-}
-
-func TestParseConfigQuotedValues(t *testing.T) {
- input := `
-[user]
- name = "Bob Smith"
- comment = "Has a \"quoted\" word"
-`
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ 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 != "Bob Smith" {
- t.Errorf("user.name = %q, want %q", got, "Bob Smith")
+ 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", "", "comment"); got != `Has a "quoted" word` {
- t.Errorf("user.comment = %q, want %q", got, `Has a "quoted" word`)
+ if got := cfg.Get("user", "", "email"); got != "john@example.com" {
+ t.Errorf("user.email: got %q, want %q", got, "john@example.com")
}
}
-func TestParseConfigEscapeSequences(t *testing.T) {
- input := `
-[test]
- newline = "line1\nline2"
- tab = "col1\tcol2"
- backslash = "path\\to\\file"
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+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("ParseConfig error: %v", err)
+ t.Fatalf("failed to open config: %v", err)
}
+ defer func() { _ = cfgFile.Close() }()
- if got := cfg.Get("test", "", "newline"); got != "line1\nline2" {
- t.Errorf("test.newline = %q, want %q", got, "line1\nline2")
+ cfg, err := ParseConfig(cfgFile)
+ if err != nil {
+ t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("test", "", "tab"); got != "col1\tcol2" {
- t.Errorf("test.tab = %q, want %q", got, "col1\tcol2")
+
+ 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("test", "", "backslash"); got != "path\\to\\file" {
- t.Errorf("test.backslash = %q, want %q", got, "path\\to\\file")
+ 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 TestParseConfigComments(t *testing.T) {
- input := `
-# This is a comment
-; This is also a comment
-[core]
- # Comment in section
- bare = false # inline comment
- filemode = true ; another inline comment
-`
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
- }
+func TestConfigMultiValueAgainstGit(t *testing.T) {
+ repoPath, cleanup := setupTestRepo(t)
+ defer cleanup()
- if got := cfg.Get("core", "", "bare"); got != "false" {
- t.Errorf("core.bare = %q, want %q", got, "false")
- }
- if got := cfg.Get("core", "", "filemode"); got != "true" {
- t.Errorf("core.filemode = %q, want %q", got, "true")
+ 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() }()
-func TestParseConfigMultipleValues(t *testing.T) {
- input := `
-[remote "origin"]
- fetch = +refs/heads/main:refs/remotes/origin/main
- fetch = +refs/heads/dev:refs/remotes/origin/dev
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+ cfg, err := ParseConfig(cfgFile)
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ t.Fatalf("ParseConfig failed: %v", err)
}
- values := cfg.GetAll("remote", "origin", "fetch")
- if len(values) != 2 {
- t.Fatalf("expected 2 values, got %d", len(values))
+ fetches := cfg.GetAll("remote", "origin", "fetch")
+ if len(fetches) != 3 {
+ t.Fatalf("expected 3 fetch values, got %d", len(fetches))
}
- if values[0] != "+refs/heads/main:refs/remotes/origin/main" {
- t.Errorf("fetch[0] = %q", values[0])
+
+ expected := []string{
+ "+refs/heads/main:refs/remotes/origin/main",
+ "+refs/heads/dev:refs/remotes/origin/dev",
+ "+refs/tags/*:refs/tags/*",
}
- if values[1] != "+refs/heads/dev:refs/remotes/origin/dev" {
- t.Errorf("fetch[1] = %q", values[1])
+ for i, want := range expected {
+ if fetches[i] != want {
+ t.Errorf("fetch[%d]: got %q, want %q", i, fetches[i], want)
+ }
}
}
-func TestParseConfigSubsectionWithEscapes(t *testing.T) {
- input := `
-[branch "feature/my-branch"]
- remote = origin
- merge = refs/heads/feature/my-branch
-`
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
- }
+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")
- if got := cfg.Get("branch", "feature/my-branch", "remote"); got != "origin" {
- t.Errorf("branch.feature/my-branch.remote = %q, want %q", got, "origin")
+ cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
+ if err != nil {
+ t.Fatalf("failed to open config: %v", err)
}
-}
+ defer func() { _ = cfgFile.Close() }()
-func TestParseConfigEmptyValue(t *testing.T) {
- input := `
-[core]
- empty =
- whitespace =
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+ cfg, err := ParseConfig(cfgFile)
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("core", "", "empty"); got != "" {
- t.Errorf("core.empty = %q, want empty string", got)
+ 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", "", "whitespace"); got != "" {
- t.Errorf("core.whitespace = %q, want empty string", got)
+ if got := cfg.Get("core", "", "filemode"); got != gitVerifyFilemode {
+ t.Errorf("core.filemode: got %q, want %q (from git)", got, gitVerifyFilemode)
}
}
-func TestParseConfigInvalidInputs(t *testing.T) {
- cases := []struct {
- name string
- input string
- }{
- {
- name: "key before section",
- input: "key = value\n",
- },
- {
- name: "invalid section no closing bracket",
- input: "[section\n",
- },
- {
- name: "invalid escape in value",
- input: "[test]\nkey = \"invalid\\x\"\n",
- },
- {
- name: "unclosed quote in value",
- input: "[test]\nkey = \"unclosed\n",
- },
- {
- name: "unclosed quote in subsection",
- input: "[section \"unclosed]\n",
- },
- }
+func TestConfigBooleanAgainstGit(t *testing.T) {
+ repoPath, cleanup := setupTestRepo(t)
+ defer cleanup()
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- _, err := ParseConfig(strings.NewReader(tc.input))
- if err == nil {
- t.Errorf("expected error for %q, got nil", tc.name)
- }
- })
- }
-}
+ gitConfig(t, repoPath, "test.flag1", "true")
+ gitConfig(t, repoPath, "test.flag2", "false")
+ gitConfig(t, repoPath, "test.flag3", "yes")
+ gitConfig(t, repoPath, "test.flag4", "no")
-func TestParseConfigEntries(t *testing.T) {
- input := `
-[core]
- bare = false
-[user]
- name = Alice
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+ cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ t.Fatalf("failed to open config: %v", err)
}
+ defer func() { _ = cfgFile.Close() }()
- entries := cfg.Entries()
- if len(entries) != 2 {
- t.Fatalf("expected 2 entries, got %d", len(entries))
+ cfg, err := ParseConfig(cfgFile)
+ if err != nil {
+ t.Fatalf("ParseConfig failed: %v", err)
}
- if entries[0].Section != "core" || entries[0].Key != "bare" || entries[0].Value != "false" {
- t.Errorf("entry[0] = %+v", entries[0])
+ 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")},
}
- if entries[1].Section != "user" || entries[1].Key != "name" || entries[1].Value != "Alice" {
- t.Errorf("entry[1] = %+v", entries[1])
+
+ 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 TestParseConfigGetNotFound(t *testing.T) {
- input := `
-[core]
- bare = false
-`
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
- }
+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")
- if got := cfg.Get("nonexistent", "", "key"); got != "" {
- t.Errorf("expected empty string for nonexistent key, got %q", got)
+ cfgFile, err := os.Open(filepath.Join(repoPath, "config"))
+ if err != nil {
+ t.Fatalf("failed to open config: %v", err)
}
-}
+ defer func() { _ = cfgFile.Close() }()
-func TestParseConfigComplexSubsection(t *testing.T) {
- input := `
-[url "https://villosa.example.org/"]
- insteadOf = gh:
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+ cfg, err := ParseConfig(cfgFile)
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("url", "https://villosa.example.org/", "insteadof"); got != "gh:" {
- t.Errorf("url.https://villosa.example.org/.insteadof = %q, want %q", got, "gh:")
+ 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 TestParseConfigBooleanKeyWithInlineComment(t *testing.T) {
- input := `
-[core]
- bare ; this is a comment
- filemode # another comment
-`
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
- }
+func TestConfigEntriesAgainstGit(t *testing.T) {
+ repoPath, cleanup := setupTestRepo(t)
+ defer cleanup()
- if got := cfg.Get("core", "", "bare"); got != "true" {
- t.Errorf("core.bare = %q, want %q", got, "true")
- }
- if got := cfg.Get("core", "", "filemode"); got != "true" {
- t.Errorf("core.filemode = %q, want %q", got, "true")
+ 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() }()
-func TestParseConfigLineContinuation(t *testing.T) {
- input := `[section]
- # Quoted value with line continuation
- quoted = "line1\
-line2\
-line3"
-
- # Unquoted value with line continuation
- unquoted = one\
-two\
-three
-`
- cfg, err := ParseConfig(strings.NewReader(input))
+ cfg, err := ParseConfig(cfgFile)
if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+ t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("section", "", "quoted"); got != "line1line2line3" {
- t.Errorf("section.quoted = %q, want %q", got, "line1line2line3")
+ entries := cfg.Entries()
+ if len(entries) < 3 {
+ t.Errorf("expected at least 3 entries, got %d", len(entries))
}
- if got := cfg.Get("section", "", "unquoted"); got != "onetwothree" {
- t.Errorf("section.unquoted = %q, want %q", got, "onetwothree")
+
+ 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 TestParseConfigDOSLineEndings(t *testing.T) {
- input := "[core]\r\n\tbare = true\r\n\tfilemode = false\r\n"
- cfg, err := ParseConfig(strings.NewReader(input))
- if err != nil {
- t.Fatalf("ParseConfig error: %v", err)
+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\"",
+ },
}
- if got := cfg.Get("core", "", "bare"); got != "true" {
- t.Errorf("core.bare = %q, want %q", got, "true")
- }
- if got := cfg.Get("core", "", "filemode"); got != "false" {
- t.Errorf("core.filemode = %q, want %q", got, "false")
+ 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)
+ }
+ })
}
}