|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBranchStatusReportsExactAheadBehindCounts(t *testing.T) { |
| 12 | + remoteDir := filepath.Join(t.TempDir(), "origin.git") |
| 13 | + runGitCommand(t, t.TempDir(), "git", "init", "--bare", remoteDir) |
| 14 | + |
| 15 | + repoRoot := t.TempDir() |
| 16 | + runGitCommand(t, repoRoot, "git", "init", "-b", "main") |
| 17 | + runGitCommand(t, repoRoot, "git", "config", "user.name", "Test User") |
| 18 | + runGitCommand(t, repoRoot, "git", "config", "user.email", "test@example.com") |
| 19 | + runGitCommand(t, repoRoot, "git", "config", "core.hooksPath", ".git/hooks") |
| 20 | + |
| 21 | + writeFile(t, filepath.Join(repoRoot, "README.md"), "# test\n") |
| 22 | + runGitCommand(t, repoRoot, "git", "add", "README.md") |
| 23 | + runGitCommand(t, repoRoot, "git", "commit", "-m", "initial") |
| 24 | + runGitCommand(t, repoRoot, "git", "remote", "add", "origin", remoteDir) |
| 25 | + runGitCommand(t, repoRoot, "git", "push", "-u", "origin", "main") |
| 26 | + |
| 27 | + peerClone := filepath.Join(t.TempDir(), "peer") |
| 28 | + runGitCommand(t, t.TempDir(), "git", "clone", remoteDir, peerClone) |
| 29 | + runGitCommand(t, peerClone, "git", "config", "user.name", "Peer User") |
| 30 | + runGitCommand(t, peerClone, "git", "config", "user.email", "peer@example.com") |
| 31 | + runGitCommand(t, peerClone, "git", "config", "core.hooksPath", ".git/hooks") |
| 32 | + writeFile(t, filepath.Join(peerClone, "peer.txt"), "peer\n") |
| 33 | + runGitCommand(t, peerClone, "git", "add", "peer.txt") |
| 34 | + runGitCommand(t, peerClone, "git", "commit", "-m", "peer change") |
| 35 | + runGitCommand(t, peerClone, "git", "push", "origin", "main") |
| 36 | + |
| 37 | + writeFile(t, filepath.Join(repoRoot, "local-one.txt"), "one\n") |
| 38 | + runGitCommand(t, repoRoot, "git", "add", "local-one.txt") |
| 39 | + runGitCommand(t, repoRoot, "git", "commit", "-m", "local one") |
| 40 | + writeFile(t, filepath.Join(repoRoot, "local-two.txt"), "two\n") |
| 41 | + runGitCommand(t, repoRoot, "git", "add", "local-two.txt") |
| 42 | + runGitCommand(t, repoRoot, "git", "commit", "-m", "local two") |
| 43 | + runGitCommand(t, repoRoot, "git", "fetch", "origin") |
| 44 | + |
| 45 | + status, err := NewEngine(repoRoot).BranchStatus("main", "main") |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("BranchStatus: %v", err) |
| 48 | + } |
| 49 | + if status.AheadCount != 2 || status.BehindCount != 1 { |
| 50 | + t.Fatalf("expected ahead=2 behind=1, got %+v", status) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func runGitCommand(t *testing.T, dir string, name string, args ...string) string { |
| 55 | + t.Helper() |
| 56 | + |
| 57 | + cmd := exec.Command(name, args...) |
| 58 | + cmd.Dir = dir |
| 59 | + |
| 60 | + var stdout bytes.Buffer |
| 61 | + var stderr bytes.Buffer |
| 62 | + cmd.Stdout = &stdout |
| 63 | + cmd.Stderr = &stderr |
| 64 | + |
| 65 | + if err := cmd.Run(); err != nil { |
| 66 | + t.Fatalf("%s %v failed: %v: %s", name, args, err, stderr.String()) |
| 67 | + } |
| 68 | + |
| 69 | + return stdout.String() |
| 70 | +} |
| 71 | + |
| 72 | +func writeFile(t *testing.T, path string, contents string) { |
| 73 | + t.Helper() |
| 74 | + |
| 75 | + if err := os.WriteFile(path, []byte(contents), 0o644); err != nil { |
| 76 | + t.Fatalf("WriteFile(%s): %v", path, err) |
| 77 | + } |
| 78 | +} |
0 commit comments