GitLabgitlab-org
cli
main
1//go:build !integration
3package mrutils
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12)
14func Test_ParseLine(t *testing.T) {
15 tests := []struct {
16 name string
17 input string
18 wantStart int
19 wantEnd int
20 wantErr string
21 }{
22 {name: "empty string", input: "", wantStart: 0, wantEnd: 0},
23 {name: "single line", input: "42", wantStart: 42, wantEnd: 42},
24 {name: "line range", input: "10:15", wantStart: 10, wantEnd: 15},
25 {name: "same start and end", input: "5:5", wantStart: 5, wantEnd: 5},
26 {name: "invalid number", input: "abc", wantErr: `invalid line number "abc"`},
27 {name: "invalid range end", input: "10:abc", wantErr: `invalid line range "10:abc"`},
28 {name: "reversed range", input: "15:10", wantErr: `invalid line range "15:10": end must be >= start`},
29 {name: "zero line", input: "0", wantErr: "line number must be positive, got 0"},
30 {name: "negative line", input: "-1", wantErr: "line number must be positive, got -1"},
31 }
32 for _, tt := range tests {
33 t.Run(tt.name, func(t *testing.T) {
34 start, end, err := parseLine(tt.input)
35 if tt.wantErr != "" {
36 assert.EqualError(t, err, tt.wantErr)
37 } else {
38 require.NoError(t, err)
39 assert.Equal(t, tt.wantStart, start)
40 assert.Equal(t, tt.wantEnd, end)
41 }
42 })
43 }
44}
46func Test_FindFileDiff(t *testing.T) {
47 version := &gitlab.MergeRequestDiffVersion{
48 Diffs: []*gitlab.Diff{
49 {NewPath: "src/main.go", OldPath: "src/main.go"},
50 {NewPath: "src/new.go", OldPath: "src/old.go"},
51 },
52 }
54 t.Run("match by NewPath", func(t *testing.T) {
55 d, err := findFileDiff(version, "src/main.go")
56 require.NoError(t, err)
57 assert.Equal(t, "src/main.go", d.NewPath)
58 })
60 t.Run("match by OldPath", func(t *testing.T) {
61 d, err := findFileDiff(version, "src/old.go")
62 require.NoError(t, err)
63 assert.Equal(t, "src/new.go", d.NewPath)
64 })
66 t.Run("not found", func(t *testing.T) {
67 _, err := findFileDiff(version, "nonexistent.go")
68 assert.EqualError(t, err, `file "nonexistent.go" not found in MR diff`)
69 })
70}
72func Test_BuildDiffPosition(t *testing.T) {
73 version := &gitlab.MergeRequestDiffVersion{
74 BaseCommitSHA: "base123",
75 HeadCommitSHA: "head456",
76 StartCommitSHA: "start789",
77 }
79 // Simple diff: one removed, one added, one context line
80 diffContent := `@@ -1,3 +1,3 @@
81 unchanged line
82-old line
83+new line
84 another unchanged
85`
87 fileDiff := &gitlab.Diff{
88 NewPath: "file.go",
89 OldPath: "file.go",
90 Diff: diffContent,
91 }
93 t.Run("new-side single line on added line", func(t *testing.T) {
94 pos, err := buildDiffPosition(version, fileDiff, 2, 2, 0)
95 require.NoError(t, err)
96 assert.Equal(t, "base123", *pos.BaseSHA)
97 assert.Equal(t, "head456", *pos.HeadSHA)
98 assert.Equal(t, "start789", *pos.StartSHA)
99 assert.Equal(t, "file.go", *pos.NewPath)
100 assert.Equal(t, int64(2), *pos.NewLine)
101 // Added line has no OldLine
102 assert.Nil(t, pos.OldLine)
103 assert.Nil(t, pos.LineRange)
104 })
106 t.Run("new-side single line on unchanged line", func(t *testing.T) {
107 pos, err := buildDiffPosition(version, fileDiff, 1, 1, 0)
108 require.NoError(t, err)
109 assert.Equal(t, int64(1), *pos.NewLine)
110 assert.Equal(t, int64(1), *pos.OldLine) // Unchanged => both sides
111 assert.Nil(t, pos.LineRange)
112 })
114 t.Run("new-side multiline range", func(t *testing.T) {
115 pos, err := buildDiffPosition(version, fileDiff, 1, 3, 0)
116 require.NoError(t, err)
117 // NewLine anchors on the end of the range (line 3)
118 assert.Equal(t, int64(3), *pos.NewLine)
119 // Line 3 is unchanged so OldLine is also set
120 require.NotNil(t, pos.OldLine)
121 assert.Equal(t, int64(3), *pos.OldLine)
122 require.NotNil(t, pos.LineRange)
123 assert.Equal(t, "new", *pos.LineRange.Start.Type)
124 assert.Equal(t, int64(1), *pos.LineRange.Start.NewLine)
125 // Start line 1 is unchanged, so OldLine is set
126 require.NotNil(t, pos.LineRange.Start.OldLine)
127 assert.Equal(t, int64(1), *pos.LineRange.Start.OldLine)
128 assert.Equal(t, "new", *pos.LineRange.End.Type)
129 assert.Equal(t, int64(3), *pos.LineRange.End.NewLine)
130 // End line 3 is unchanged, so OldLine is set
131 require.NotNil(t, pos.LineRange.End.OldLine)
132 assert.Equal(t, int64(3), *pos.LineRange.End.OldLine)
133 })
135 t.Run("old-side line on removed line", func(t *testing.T) {
136 pos, err := buildDiffPosition(version, fileDiff, 0, 0, 2)
137 require.NoError(t, err)
138 assert.Equal(t, int64(2), *pos.OldLine)
139 assert.Nil(t, pos.NewLine) // Removed line => no new side
140 })
142 t.Run("old-side line on unchanged line", func(t *testing.T) {
143 pos, err := buildDiffPosition(version, fileDiff, 0, 0, 1)
144 require.NoError(t, err)
145 assert.Equal(t, int64(1), *pos.OldLine)
146 assert.Equal(t, int64(1), *pos.NewLine) // Unchanged => both sides
147 })
149 t.Run("file-level comment", func(t *testing.T) {
150 pos, err := buildDiffPosition(version, fileDiff, 0, 0, 0)
151 require.NoError(t, err)
152 // Should target first line in diff (unchanged line 1)
153 require.NotNil(t, pos.NewLine)
154 })
156 t.Run("line not in diff", func(t *testing.T) {
157 _, err := buildDiffPosition(version, fileDiff, 999, 999, 0)
158 assert.ErrorContains(t, err, "line 999 not found in diff")
159 })
161 t.Run("old line not in diff", func(t *testing.T) {
162 _, err := buildDiffPosition(version, fileDiff, 0, 0, 999)
163 assert.ErrorContains(t, err, "old line 999 not found in diff")
164 })
166 t.Run("multiline range end not in diff", func(t *testing.T) {
167 _, err := buildDiffPosition(version, fileDiff, 1, 999, 0)
168 assert.ErrorContains(t, err, "line 999 not found in diff")
169 })
171 t.Run("file-level on delete-only diff", func(t *testing.T) {
172 deleteDiff := &gitlab.Diff{
173 NewPath: "deleted.go",
174 OldPath: "deleted.go",
175 Diff: "@@ -1,2 +1 @@\n-removed1\n-removed2\n",
176 }
177 pos, err := buildDiffPosition(version, deleteDiff, 0, 0, 0)
178 require.NoError(t, err)
179 require.NotNil(t, pos.OldLine)
180 assert.Equal(t, int64(1), *pos.OldLine)
181 assert.Nil(t, pos.NewLine)
182 })
184 t.Run("file-level on empty diff", func(t *testing.T) {
185 emptyDiff := &gitlab.Diff{
186 NewPath: "empty.go",
187 OldPath: "empty.go",
188 Diff: "",
189 }
190 _, err := buildDiffPosition(version, emptyDiff, 0, 0, 0)
191 assert.ErrorContains(t, err, "diff for empty.go is empty")
192 })
193}
195func Test_lineCode(t *testing.T) {
196 assert.Equal(t, "a78c15ea253085032b9a8a057d8689b6fd7d0dfa_0_42", lineCode("file.go", 42))
197}
Diagnostics8 git calls · 251 ms
CallStatusSizeTime0251 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200746 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=746 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=746 git.pkt.payload.max=719 git.sideband.band1.bytes=719 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=719 git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200469 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=237 git.response.bytes=469 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=6020159a68b7314d330ee4ac0f7529b651d7accc git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=469 git.pkt.payload.max=442 git.sideband.band1.bytes=442 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=442 git.pack.trailer=97b1fd715810f2e169ed93a75830c9ee2eff1fc0Open pack map
fetch2001.6 KiB237 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1662 git.response.cache=MISS git.response.duration_ms=237 git.fetch.wants=c381588aea4bb8bcfc6a3032ea925eaba15d95dd git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1662 git.pkt.payload.max=1635 git.sideband.band1.bytes=1635 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2c96c6cb24b67
- Request
- 5f0d5e29…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format