MCPcopy Create free account
hub / github.com/alibaba/open-code-review / TestGitFailure

Function TestGitFailure

internal/diff/git_error_test.go:21–95  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestGitFailure(t *testing.T) {
22 baseErr := errors.New("exit status 129")
23
24 t.Run("includes git's own message", func(t *testing.T) {
25 err := gitFailure("git show", "error: unknown option `diff-merges=first-parent'\n", baseErr)
26 got := err.Error()
27 if !strings.Contains(got, "unknown option") {
28 t.Errorf("error %q drops git's message", got)
29 }
30 if !strings.Contains(got, "git show failed") {
31 t.Errorf("error %q lost the operation name", got)
32 }
33 if !strings.Contains(got, "exit status 129") {
34 t.Errorf("error %q lost the exit status", got)
35 }
36 })
37
38 t.Run("wrapped error stays unwrappable", func(t *testing.T) {
39 // Callers up the stack match on the underlying error, so quoting git's
40 // output must not cost them errors.Is.
41 err := gitFailure("git show", "fatal: bad object", baseErr)
42 if !errors.Is(err, baseErr) {
43 t.Error("gitFailure broke the error chain")
44 }
45 })
46
47 t.Run("empty output leaves no dangling separator", func(t *testing.T) {
48 err := gitFailure("git show", " \n\t ", baseErr)
49 if got := err.Error(); got != "git show failed: exit status 129" {
50 t.Errorf("error = %q, want the bare form with no trailing colon", got)
51 }
52 })
53
54 t.Run("long output keeps the tail", func(t *testing.T) {
55 // runGit returns stdout and stderr combined, so a command that failed
56 // partway through carries real diff ahead of git's diagnosis. The
57 // diagnosis is last, which is the half worth keeping.
58 noise := strings.Repeat("+padding line\n", 400)
59 err := gitFailure("git diff", noise+"fatal: the real problem", baseErr)
60 got := err.Error()
61 if !strings.Contains(got, "fatal: the real problem") {
62 t.Error("truncation dropped the tail, which is where git's diagnosis is")
63 }
64 if !strings.Contains(got, "...") {
65 t.Error("truncated output is not marked as truncated")
66 }
67 if len(got) > gitDiagLimit+200 {
68 t.Errorf("error is %d bytes; truncation is not bounding it", len(got))
69 }
70 })
71
72 t.Run("short output is not truncated", func(t *testing.T) {
73 if got := gitFailure("git show", "fatal: bad object", baseErr).Error(); strings.Contains(got, "...") {
74 t.Errorf("error %q was truncated despite fitting the limit", got)
75 }
76 })
77
78 // Git speaks the user's locale. #972 came from a Japanese-language Windows

Callers

nothing calls this directly

Calls 3

gitFailureFunction · 0.85
ErrorMethod · 0.80
RunMethod · 0.45

Tested by

no test coverage detected