From f762271dbfc121eaac7eb59c0beb01620a09118f Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 30 Jan 2026 17:10:01 +0100 Subject: test: Make gitCmd accept an stdin []byte --- refs_test.go | 120 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 60 insertions(+), 60 deletions(-) (limited to 'refs_test.go') diff --git a/refs_test.go b/refs_test.go index 2d4a1532..e15fd15b 100644 --- a/refs_test.go +++ b/refs_test.go @@ -18,10 +18,10 @@ func TestResolveRef(t *testing.T) { if err != nil { t.Fatalf("Failed to write test.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "test") - commitHash := gitCmd(t, repoPath, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commitHash) + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "test") + commitHash := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commitHash) repo, err := OpenRepository(repoPath) if err != nil { @@ -42,7 +42,7 @@ func TestResolveRef(t *testing.T) { t.Errorf("resolved hash: got %s, want %s", resolved.Hash, hashObj) } - gitRevParse := gitCmd(t, repoPath, "rev-parse", "refs/heads/main") + gitRevParse := gitCmd(t, repoPath, nil, "rev-parse", "refs/heads/main") if resolved.Hash.String() != gitRevParse { t.Errorf("furgit resolved %s, git resolved %s", resolved.Hash, gitRevParse) } @@ -64,11 +64,11 @@ func TestResolveHEAD(t *testing.T) { if err != nil { t.Fatalf("failed to write test.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "test") - commitHash := gitCmd(t, repoPath, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commitHash) - gitCmd(t, repoPath, "symbolic-ref", "HEAD", "refs/heads/main") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "test") + commitHash := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commitHash) + gitCmd(t, repoPath, nil, "symbolic-ref", "HEAD", "refs/heads/main") repo, err := OpenRepository(repoPath) if err != nil { @@ -89,7 +89,7 @@ func TestResolveHEAD(t *testing.T) { t.Errorf("HEAD symbolic ref: got %q, want %q", ref.Ref, "refs/heads/main") } - gitSymRef := gitCmd(t, repoPath, "symbolic-ref", "HEAD") + gitSymRef := gitCmd(t, repoPath, nil, "symbolic-ref", "HEAD") if ref.Ref != gitSymRef { t.Errorf("furgit resolved %v, git resolved %s", ref.Ref, gitSymRef) } @@ -106,23 +106,23 @@ func TestPackedRefs(t *testing.T) { if err != nil { t.Fatalf("failed to write test.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "commit1") - commit1Hash := gitCmd(t, repoPath, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "commit1") + commit1Hash := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") err = os.WriteFile(filepath.Join(workDir, "test2.txt"), []byte("content2"), 0o644) if err != nil { t.Fatalf("failed to write test2.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "commit2") - commit2Hash := gitCmd(t, repoPath, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "commit2") + commit2Hash := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/branch1", commit1Hash) - gitCmd(t, repoPath, "update-ref", "refs/heads/branch2", commit2Hash) - gitCmd(t, repoPath, "update-ref", "refs/tags/v1.0", commit1Hash) + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/branch1", commit1Hash) + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/branch2", commit2Hash) + gitCmd(t, repoPath, nil, "update-ref", "refs/tags/v1.0", commit1Hash) - gitCmd(t, repoPath, "pack-refs", "--all") + gitCmd(t, repoPath, nil, "pack-refs", "--all") repo, err := OpenRepository(repoPath) if err != nil { @@ -141,7 +141,7 @@ func TestPackedRefs(t *testing.T) { t.Errorf("branch1: got %s, want %s", resolved1.Hash, hash1) } - gitResolved1 := gitCmd(t, repoPath, "rev-parse", "refs/heads/branch1") + gitResolved1 := gitCmd(t, repoPath, nil, "rev-parse", "refs/heads/branch1") if resolved1.Hash.String() != gitResolved1 { t.Errorf("furgit resolved %s, git resolved %s", resolved1.Hash, gitResolved1) } @@ -175,14 +175,14 @@ func TestResolveRefFully(t *testing.T) { if err != nil { t.Fatalf("failed to write file.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "init") - commit := gitCmd(t, repoPath, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "init") + commit := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") // Create two layers of symbolic refs - gitCmd(t, repoPath, "symbolic-ref", "refs/heads/level1", "refs/heads/level2") - gitCmd(t, repoPath, "symbolic-ref", "refs/heads/level2", "refs/heads/main") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commit) + gitCmd(t, repoPath, nil, "symbolic-ref", "refs/heads/level1", "refs/heads/level2") + gitCmd(t, repoPath, nil, "symbolic-ref", "refs/heads/level2", "refs/heads/main") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commit) repo, err := OpenRepository(repoPath) if err != nil { @@ -215,8 +215,8 @@ func TestResolveRefFullySymbolicCycle(t *testing.T) { } defer func() { _ = repo.Close() }() - gitCmd(t, repoPath, "symbolic-ref", "refs/heads/A", "refs/heads/B") - gitCmd(t, repoPath, "symbolic-ref", "refs/heads/B", "refs/heads/A") + gitCmd(t, repoPath, nil, "symbolic-ref", "refs/heads/A", "refs/heads/B") + gitCmd(t, repoPath, nil, "symbolic-ref", "refs/heads/B", "refs/heads/A") _, err = repo.ResolveRefFully("refs/heads/A") if err == nil { @@ -239,10 +239,10 @@ func TestResolveRefHashInput(t *testing.T) { if err != nil { t.Fatalf("failed to write file.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "init") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "init") - commitHash := gitCmd(t, repoPath, "rev-parse", "HEAD") + commitHash := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") repo, err := OpenRepository(repoPath) if err != nil { @@ -287,28 +287,28 @@ func TestListRefsLooseOverridesPacked(t *testing.T) { workDir, cleanupWork := setupWorkDir(t) defer cleanupWork() - gitCmd(t, repoPath, "symbolic-ref", "HEAD", "refs/heads/main") + gitCmd(t, repoPath, nil, "symbolic-ref", "HEAD", "refs/heads/main") err := os.WriteFile(filepath.Join(workDir, "file.txt"), []byte("one"), 0o644) if err != nil { t.Fatalf("failed to write file.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "c1") - commit1 := gitCmd(t, repoPath, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "c1") + commit1 := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commit1) - gitCmd(t, repoPath, "update-ref", "refs/heads/feature", commit1) - gitCmd(t, repoPath, "pack-refs", "--all", "--prune") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commit1) + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/feature", commit1) + gitCmd(t, repoPath, nil, "pack-refs", "--all", "--prune") err = os.WriteFile(filepath.Join(workDir, "file.txt"), []byte("two"), 0o644) if err != nil { t.Fatalf("failed to write file.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "c2") - commit2 := gitCmd(t, repoPath, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commit2) + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "c2") + commit2 := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commit2) repo, err := OpenRepository(repoPath) if err != nil { @@ -360,19 +360,19 @@ func TestListRefsPatternFiltering(t *testing.T) { workDir, cleanupWork := setupWorkDir(t) defer cleanupWork() - gitCmd(t, repoPath, "symbolic-ref", "HEAD", "refs/heads/main") + gitCmd(t, repoPath, nil, "symbolic-ref", "HEAD", "refs/heads/main") err := os.WriteFile(filepath.Join(workDir, "file.txt"), []byte("one"), 0o644) if err != nil { t.Fatalf("failed to write file.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "c1") - commit1 := gitCmd(t, repoPath, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "c1") + commit1 := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commit1) - gitCmd(t, repoPath, "update-ref", "refs/heads/feature", commit1) - gitCmd(t, repoPath, "pack-refs", "--all", "--prune") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commit1) + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/feature", commit1) + gitCmd(t, repoPath, nil, "pack-refs", "--all", "--prune") repo, err := OpenRepository(repoPath) if err != nil { @@ -404,21 +404,21 @@ func TestListRefsPackedPatterns(t *testing.T) { workDir, cleanupWork := setupWorkDir(t) defer cleanupWork() - gitCmd(t, repoPath, "symbolic-ref", "HEAD", "refs/heads/main") + gitCmd(t, repoPath, nil, "symbolic-ref", "HEAD", "refs/heads/main") err := os.WriteFile(filepath.Join(workDir, "file.txt"), []byte("one"), 0o644) if err != nil { t.Fatalf("failed to write file.txt: %v", err) } - gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".") - gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "c1") - commit := gitCmd(t, repoPath, "rev-parse", "HEAD") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "add", ".") + gitCmd(t, repoPath, nil, "--work-tree="+workDir, "commit", "-m", "c1") + commit := gitCmd(t, repoPath, nil, "rev-parse", "HEAD") - gitCmd(t, repoPath, "update-ref", "refs/heads/main", commit) - gitCmd(t, repoPath, "update-ref", "refs/heads/feature/one", commit) - gitCmd(t, repoPath, "update-ref", "refs/notes/review", commit) - gitCmd(t, repoPath, "update-ref", "refs/tags/v1", commit) - gitCmd(t, repoPath, "pack-refs", "--all", "--prune") + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/main", commit) + gitCmd(t, repoPath, nil, "update-ref", "refs/heads/feature/one", commit) + gitCmd(t, repoPath, nil, "update-ref", "refs/notes/review", commit) + gitCmd(t, repoPath, nil, "update-ref", "refs/tags/v1", commit) + gitCmd(t, repoPath, nil, "pack-refs", "--all", "--prune") repo, err := OpenRepository(repoPath) if err != nil { -- cgit v1.3.1-10-gc9f91