返回 DeepSeek-Reasonix
gitstatus_test.go
根目录 / internal / cli / gitstatus_test.go
1 package cli
2
3 import (
4 "context"
5 "errors"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12
13 "github.com/charmbracelet/x/ansi"
14 )
15
16 func TestParseGitNumstat(t *testing.T) {
17 added, removed := parseGitNumstat("10\t2\ta.go\n-\t-\tasset.bin\n3\t0\tpath with spaces.go\n")
18 if added != 13 || removed != 2 {
19 t.Fatalf("parseGitNumstat = (+%d -%d), want (+13 -2)", added, removed)
20 }
21 }
22
23 func TestCountUntracked(t *testing.T) {
24 got := countUntracked(" M tracked.go\n?? new.go\n?? nested/\n")
25 if got != 2 {
26 t.Fatalf("countUntracked = %d, want 2", got)
27 }
28 }
29
30 func TestGitStatusRender(t *testing.T) {
31 got := ansi.Strip(gitStatus{Repo: "mySkills", Branch: "main", Added: 15}.Render())
32 if got != "mySkills@main +15 -0" {
33 t.Fatalf("Render = %q", got)
34 }
35
36 got = ansi.Strip(gitStatus{Repo: "repo", Branch: "abc1234", Detached: true, Untracked: 2}.Render())
37 if got != "repo@abc1234 ?2" {
38 t.Fatalf("detached Render = %q", got)
39 }
40 }
41
42 func TestGitStatusRenderRepoUsesSuppliedRepoStyle(t *testing.T) {
43 got := ansi.Strip(gitStatus{Repo: "repo", Branch: "main"}.RenderRepo("[repo]"))
44 if got != "[repo]@main" {
45 t.Fatalf("RenderRepo = %q, want styled repo name only", got)
46 }
47 }
48
49 func TestGitStatusRenderWithinCompactsRepoBeforeBranch(t *testing.T) {
50 status := gitStatus{Repo: "VeryLongDeepSeekReasonixWorkspace", Branch: "codex/cli-tui-status-row"}
51
52 full := ansi.Strip(status.RenderWithin(80, statusAutoColor))
53 if full != "VeryLongDeepSeekReasonixWorkspace@codex/cli-tui-status-row" {
54 t.Fatalf("wide RenderWithin = %q", full)
55 }
56
57 got := ansi.Strip(status.RenderWithin(46, statusAutoColor))
58 if ansi.StringWidth(got) > 46 {
59 t.Fatalf("compacted status width = %d, want <= 46: %q", ansi.StringWidth(got), got)
60 }
61 if !strings.Contains(got, "@codex/cli-tui-status-row") {
62 t.Fatalf("branch should stay intact while repo can be compacted: %q", got)
63 }
64 if !strings.Contains(got, "…") {
65 t.Fatalf("long repo should be compacted with ellipsis: %q", got)
66 }
67 }
68
69 func TestGitStatusRenderWithinKeepsDirtySuffix(t *testing.T) {
70 status := gitStatus{
71 Repo: "VeryLongDeepSeekReasonixWorkspace",
72 Branch: "codex/cli-tui-status-row",
73 Added: 12,
74 Removed: 3,
75 Untracked: 4,
76 }
77
78 got := ansi.Strip(status.RenderWithin(35, statusAutoColor))
79 if ansi.StringWidth(got) > 35 {
80 t.Fatalf("compacted dirty status width = %d, want <= 35: %q", ansi.StringWidth(got), got)
81 }
82 if !strings.Contains(got, "+12 -3 ?4") {
83 t.Fatalf("dirty suffix should be preserved: %q", got)
84 }
85 if !strings.Contains(got, "@") || !strings.Contains(got, "…") {
86 t.Fatalf("identity should remain segmented and compacted: %q", got)
87 }
88 }
89
90 func TestLoadGitStatus(t *testing.T) {
91 if _, err := exec.LookPath("git"); err != nil {
92 t.Skip("git not found")
93 }
94
95 root := t.TempDir()
96 runGitForTest(t, root, "init")
97 runGitForTest(t, root, "config", "user.email", "reasonix@example.invalid")
98 runGitForTest(t, root, "config", "user.name", "Reasonix Test")
99 writeFileForTest(t, filepath.Join(root, "tracked.txt"), "one\ntwo\n")
100 runGitForTest(t, root, "add", "tracked.txt")
101 runGitForTest(t, root, "commit", "-m", "initial")
102 runGitForTest(t, root, "branch", "-M", "main")
103
104 writeFileForTest(t, filepath.Join(root, "tracked.txt"), "one\nchanged\nthree\n")
105 writeFileForTest(t, filepath.Join(root, "new.txt"), "untracked\n")
106 if err := os.Mkdir(filepath.Join(root, "subdir"), 0o755); err != nil {
107 t.Fatal(err)
108 }
109
110 // This checks Git semantics, not subprocess speed on a shared CI runner.
111 status, err := loadGitStatus(t.Context(), filepath.Join(root, "subdir"))
112 if err != nil {
113 t.Fatalf("loadGitStatus: %v", err)
114 }
115 if status.Repo != filepath.Base(root) || status.Branch != "main" || status.Detached {
116 t.Fatalf("status identity = %+v", status)
117 }
118 if status.Added != 2 || status.Removed != 1 || status.Untracked != 1 {
119 t.Fatalf("status changes = (+%d -%d ?%d), want (+2 -1 ?1)", status.Added, status.Removed, status.Untracked)
120 }
121 if plain := ansi.Strip(status.Render()); !strings.Contains(plain, filepath.Base(root)+"@main") || !strings.Contains(plain, "+2 -1 ?1") {
122 t.Fatalf("rendered status = %q", plain)
123 }
124 }
125
126 func TestLoadGitStatusRejectsCanceledSnapshot(t *testing.T) {
127 for _, cancelAt := range []string{"symbolic-ref", "diff", "status"} {
128 t.Run(cancelAt, func(t *testing.T) {
129 ctx, cancel := context.WithCancel(t.Context())
130 defer cancel()
131 run := func(ctx context.Context, _ string, args ...string) (string, error) {
132 if args[0] == cancelAt {
133 cancel()
134 }
135 if err := ctx.Err(); err != nil {
136 return "", err
137 }
138 switch args[0] {
139 case "rev-parse":
140 return filepath.Join("workspace", "repo"), nil
141 case "symbolic-ref":
142 return "main", nil
143 case "diff":
144 return "2\t1\ttracked.txt\n", nil
145 case "status":
146 return "?? new.txt\n", nil
147 default:
148 t.Fatalf("unexpected git args: %v", args)
149 return "", nil
150 }
151 }
152 status, err := loadGitStatusWithRunner(ctx, "", run)
153 if !errors.Is(err, context.Canceled) || status != (gitStatus{}) {
154 t.Fatalf("canceled query returned status=%+v err=%v", status, err)
155 }
156 })
157 }
158 }
159
160 func TestRunGitDisablesOptionalLocks(t *testing.T) {
161 if runtime.GOOS == "windows" {
162 t.Skip("uses a POSIX shell script fake git")
163 }
164
165 bin := filepath.Join(t.TempDir(), "bin")
166 if err := os.Mkdir(bin, 0o755); err != nil {
167 t.Fatal(err)
168 }
169 fakeGit := filepath.Join(bin, "git")
170 if err := os.WriteFile(fakeGit, []byte("#!/bin/sh\nprintf '%s' \"$GIT_OPTIONAL_LOCKS\"\n"), 0o755); err != nil {
171 t.Fatal(err)
172 }
173 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
174
175 out, err := runGit(context.Background(), "", "status")
176 if err != nil {
177 t.Fatalf("runGit: %v", err)
178 }
179 if out != "0" {
180 t.Fatalf("GIT_OPTIONAL_LOCKS = %q, want 0", out)
181 }
182 }
183
184 func runGitForTest(t *testing.T, cwd string, args ...string) {
185 t.Helper()
186 cmd := exec.Command("git", args...)
187 cmd.Dir = cwd
188 if out, err := cmd.CombinedOutput(); err != nil {
189 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
190 }
191 }
192
193 func writeFileForTest(t *testing.T, path string, body string) {
194 t.Helper()
195 if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
196 t.Fatal(err)
197 }
198 }
199
199 lines GO