(t *testing.T)
| 90 | } |
| 91 | |
| 92 | func TestGitLabFetchBranchDiff(t *testing.T) { |
| 93 | t.Parallel() |
| 94 | |
| 95 | t.Run("TrailingNewlineAppended", func(t *testing.T) { |
| 96 | t.Parallel() |
| 97 | |
| 98 | // When a file diff does not end with a newline, FetchBranchDiff |
| 99 | // should append one so the unified diff is well-formed. |
| 100 | mux := http.NewServeMux() |
| 101 | mux.HandleFunc("/api/v4/projects/owner%2Frepo/repository/compare", func(w http.ResponseWriter, _ *http.Request) { |
| 102 | w.Header().Set("Content-Type", "application/json") |
| 103 | // diff field intentionally lacks a trailing newline. |
| 104 | _, _ = w.Write([]byte(`{"diffs":[{"old_path":"a.txt","new_path":"a.txt","diff":"@@ -1 +1 @@\n-old\n+new"}]}`)) |
| 105 | }) |
| 106 | mux.HandleFunc("/api/v4/projects/owner%2Frepo", func(w http.ResponseWriter, _ *http.Request) { |
| 107 | w.Header().Set("Content-Type", "application/json") |
| 108 | _, _ = w.Write([]byte(`{"default_branch":"main"}`)) |
| 109 | }) |
| 110 | |
| 111 | srv := httptest.NewServer(mux) |
| 112 | defer srv.Close() |
| 113 | |
| 114 | gp, err := gitprovider.New("gitlab", srv.URL, srv.Client()) |
| 115 | require.NoError(t, err) |
| 116 | |
| 117 | diff, err := gp.FetchBranchDiff( |
| 118 | t.Context(), |
| 119 | "token", |
| 120 | gitprovider.BranchRef{Owner: "owner", Repo: "repo", Branch: "feat"}, |
| 121 | ) |
| 122 | require.NoError(t, err) |
| 123 | // Must end with newline even though the API response did not. |
| 124 | assert.True(t, len(diff) > 0 && diff[len(diff)-1] == '\n') |
| 125 | assert.Equal(t, "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-old\n+new\n", diff) |
| 126 | }) |
| 127 | |
| 128 | t.Run("EmptyDefaultBranch", func(t *testing.T) { |
| 129 | t.Parallel() |
| 130 | |
| 131 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 132 | w.Header().Set("Content-Type", "application/json") |
| 133 | _, _ = w.Write([]byte(`{"default_branch":""}`)) |
| 134 | })) |
| 135 | defer srv.Close() |
| 136 | |
| 137 | gp, err := gitprovider.New("gitlab", srv.URL, srv.Client()) |
| 138 | require.NoError(t, err) |
| 139 | |
| 140 | _, err = gp.FetchBranchDiff( |
| 141 | t.Context(), |
| 142 | "test-token", |
| 143 | gitprovider.BranchRef{Owner: "owner", Repo: "repo", Branch: "feat"}, |
| 144 | ) |
| 145 | require.Error(t, err) |
| 146 | assert.Contains(t, err.Error(), "default branch is empty") |
| 147 | }) |
| 148 | |
| 149 | t.Run("CompareTimeout", func(t *testing.T) { |
nothing calls this directly
no test coverage detected