aboutsummaryrefslogtreecommitdiff
path: root/config/config_test.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-06 21:19:56 +0800
committerGravatar Runxi Yu2026-03-07 00:34:30 +0800
commit01d15bccf3b1dcc51516b1f64d50950b31d7f8fb (patch)
treee491fcc762c67c1ef4ce54faafc5dafdb734ae8a /config/config_test.go
parentobjectstored/refstore: Weird ireturn behavior (diff)
signatureNo signature
Urgh I made some wrong amends and I'm too tired to separate the commits out this time
ancestor: Split out of reachability mergebase: Add merge base routines internal/commitquery: Add commit query context engine thingy internal/peel: Shared tag peeling errors: Shared object query errors internal/testgit: Add rooted repo helpers; remove raw path access objectstore/memory: Add in-memory object store objectid: Add Compare helper
Diffstat (limited to 'config/config_test.go')
-rw-r--r--config/config_test.go62
1 files changed, 19 insertions, 43 deletions
diff --git a/config/config_test.go b/config/config_test.go
index 8364b264..3f166202 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -3,8 +3,6 @@ package config_test
import (
"bytes"
"os"
- "os/exec"
- "path/filepath"
"strings"
"testing"
@@ -16,7 +14,9 @@ import (
func openConfig(t *testing.T, testRepo *testgit.TestRepo) *os.File {
t.Helper()
- cfgFile, err := os.Open(filepath.Join(testRepo.Dir(), "config"))
+ root := testRepo.OpenGitRoot(t)
+
+ cfgFile, err := root.Open("config")
if err != nil {
t.Fatalf("failed to open config: %v", err)
}
@@ -30,14 +30,10 @@ func gitConfigGet(t *testing.T, testRepo *testgit.TestRepo, key string) string {
return testRepo.Run(t, "config", "--get", key)
}
-func gitConfigGetE(testRepo *testgit.TestRepo, key string) (string, error) {
- //nolint:noctx
- cmd := exec.Command("git", "config", "--get", key) //#nosec G204
- cmd.Dir = testRepo.Dir()
- cmd.Env = testRepo.Env()
- out, err := cmd.CombinedOutput()
+func gitConfigGetE(t *testing.T, testRepo *testgit.TestRepo, key string) (string, error) {
+ t.Helper()
- return strings.TrimSpace(string(out)), err
+ return testRepo.RunE(t, "config", "--get", key)
}
func lookupValue(cfg *config.Config, section, subsection, key string) string {
@@ -463,20 +459,15 @@ func TestConfigErrorCases(t *testing.T) {
}
}
-func TestConfigEOFAfterKeyAgainstGit(t *testing.T) { //nolint:dupl
+func TestConfigEOFAfterKeyAgainstGit(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
- cfgPath := filepath.Join(testRepo.Dir(), "config")
-
cfgData := []byte("[Core]BAre")
- err := os.WriteFile(cfgPath, cfgData, 0o600)
- if err != nil {
- t.Fatalf("failed to write config: %v", err)
- }
+ testRepo.WriteFile(t, "config", cfgData, 0o600)
- gitValue, gitErr := gitConfigGetE(testRepo, "Core.BAre")
+ gitValue, gitErr := gitConfigGetE(t, testRepo, "Core.BAre")
furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
if (gitErr == nil) != (furErr == nil) {
@@ -493,20 +484,15 @@ func TestConfigEOFAfterKeyAgainstGit(t *testing.T) { //nolint:dupl
})
}
-func TestConfigNULValueAgainstGit(t *testing.T) { //nolint:dupl
+func TestConfigNULValueAgainstGit(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
- cfgPath := filepath.Join(testRepo.Dir(), "config")
-
cfgData := []byte("[Core]BAre=\x00")
- err := os.WriteFile(cfgPath, cfgData, 0o600)
- if err != nil {
- t.Fatalf("failed to write config: %v", err)
- }
+ testRepo.WriteFile(t, "config", cfgData, 0o600)
- gitValue, gitErr := gitConfigGetE(testRepo, "Core.BAre")
+ gitValue, gitErr := gitConfigGetE(t, testRepo, "Core.BAre")
furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
if (gitErr == nil) != (furErr == nil) {
@@ -523,20 +509,15 @@ func TestConfigNULValueAgainstGit(t *testing.T) { //nolint:dupl
})
}
-func TestConfigCarriageReturnSeparatorAgainstGit(t *testing.T) { //nolint:dupl
+func TestConfigCarriageReturnSeparatorAgainstGit(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
- cfgPath := filepath.Join(testRepo.Dir(), "config")
-
cfgData := []byte("[Core \"sub\"]\rBAre")
- err := os.WriteFile(cfgPath, cfgData, 0o600)
- if err != nil {
- t.Fatalf("failed to write config: %v", err)
- }
+ testRepo.WriteFile(t, "config", cfgData, 0o600)
- gitValue, gitErr := gitConfigGetE(testRepo, "Core.sub.BAre")
+ gitValue, gitErr := gitConfigGetE(t, testRepo, "Core.sub.BAre")
furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
if (gitErr == nil) != (furErr == nil) {
@@ -559,16 +540,14 @@ func FuzzConfig(f *testing.F) {
f.Add([]byte("[core \"sub\"]\nbare = true"), "core.sub.bare")
type fuzzRepoState struct {
- repo *testgit.TestRepo
- cfgPath string
+ repo *testgit.TestRepo
}
repos := make(map[objectid.Algorithm]fuzzRepoState, len(objectid.SupportedAlgorithms()))
for _, algo := range objectid.SupportedAlgorithms() {
testRepo := testgit.NewRepo(f, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
repos[algo] = fuzzRepoState{
- repo: testRepo,
- cfgPath: filepath.Join(testRepo.Dir(), "config"),
+ repo: testRepo,
}
}
@@ -579,12 +558,9 @@ func FuzzConfig(f *testing.F) {
t.Fatalf("missing fuzz repo state for %v", algo)
}
- err := os.WriteFile(state.cfgPath, cfgData, 0o600)
- if err != nil {
- t.Fatalf("failed to write config: %v", err)
- }
+ state.repo.WriteFile(t, "config", cfgData, 0o600)
- gitValue, gitErr := gitConfigGetE(state.repo, gitKey)
+ gitValue, gitErr := gitConfigGetE(t, state.repo, gitKey)
furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
if furErr == nil && furConfig == nil {