GitLabgitlab-org

cli

main
ply8 git calls228 ms99 KiB6 cachedprotocol v2
273 lines6.3 KiBRaw
1//go:build !integration
3package list
5import (
6 "testing"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "go.uber.org/mock/gomock"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20func TestLabelList(t *testing.T) {
21 type testCase struct {
22 name string
23 cli string
24 expectedOut string
25 wantErr bool
26 wantStderr string
27 setupMock func(tc *gitlabtesting.TestClient)
28 }
30 testLabels := []*gitlab.Label{
31 {
32 ID: 1,
33 Name: "bug",
34 Description: "",
35 Color: "#6699cc",
36 },
37 {
38 ID: 2,
39 Name: "ux",
40 Description: "User Experience",
41 Color: "#3cb371",
42 },
43 }
45 testCases := []testCase{
46 {
47 name: "List project labels",
48 cli: "",
49 expectedOut: heredoc.Doc(`Showing 2 labels on OWNER/REPO. (Page 1)
51 ID Name Description Color
52 1 bug #6699cc
53 2 ux User Experience #3cb371
55 `),
56 setupMock: func(tc *gitlabtesting.TestClient) {
57 tc.MockLabels.EXPECT().
58 ListLabels("OWNER/REPO", gomock.Any()).
59 Return(testLabels, nil, nil)
60 },
61 },
62 {
63 name: "List project labels shows the total from the API",
64 cli: "",
65 expectedOut: heredoc.Doc(`Showing 2 of 33 labels on OWNER/REPO. (Page 1)
67 ID Name Description Color
68 1 bug #6699cc
69 2 ux User Experience #3cb371
71 `),
72 setupMock: func(tc *gitlabtesting.TestClient) {
73 tc.MockLabels.EXPECT().
74 ListLabels("OWNER/REPO", gomock.Any()).
75 Return(testLabels, &gitlab.Response{CurrentPage: 1, TotalPages: 17, TotalItems: 33}, nil)
76 },
77 },
78 {
79 name: "List project labels without a total from the API",
80 cli: "",
81 expectedOut: heredoc.Doc(`Showing 2 labels on OWNER/REPO. (Page 1)
83 ID Name Description Color
84 1 bug #6699cc
85 2 ux User Experience #3cb371
87 `),
88 setupMock: func(tc *gitlabtesting.TestClient) {
89 tc.MockLabels.EXPECT().
90 ListLabels("OWNER/REPO", gomock.Any()).
91 Return(testLabels, &gitlab.Response{CurrentPage: 1, NextPage: 2}, nil)
92 },
93 },
94 {
95 name: "List project labels when there are none",
96 cli: "",
97 expectedOut: "No labels available on OWNER/REPO.\n\n",
98 setupMock: func(tc *gitlabtesting.TestClient) {
99 tc.MockLabels.EXPECT().
100 ListLabels("OWNER/REPO", gomock.Any()).
101 Return([]*gitlab.Label{}, &gitlab.Response{CurrentPage: 1}, nil)
102 },
103 },
104 }
106 for _, tc := range testCases {
107 t.Run(tc.name, func(t *testing.T) {
108 // GIVEN
109 testClient := gitlabtesting.NewTestClient(t)
110 tc.setupMock(testClient)
111 exec := cmdtest.SetupCmdForTest(
112 t,
113 NewCmdList,
114 true,
115 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
116 )
118 // WHEN
119 out, err := exec(tc.cli)
121 // THEN
122 if tc.wantErr {
123 require.Error(t, err)
124 assert.Contains(t, err.Error(), tc.wantStderr)
125 return
126 }
127 require.NoError(t, err)
128 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
129 assert.Empty(t, out.ErrBuf.String())
130 })
131 }
134func TestLabelListJSON(t *testing.T) {
135 testLabels := []*gitlab.Label{
136 {
137 ID: 29739671,
138 Name: "my label",
139 Color: "#00b140",
140 TextColor: "#FFFFFF",
141 Description: "Simple label",
142 OpenIssuesCount: 0,
143 ClosedIssuesCount: 0,
144 OpenMergeRequestsCount: 0,
145 Subscribed: false,
146 Priority: gitlab.NewNullableWithValue(int64(0)),
147 IsProjectLabel: true,
148 },
149 }
151 expectedBody := `[
152 {
153 "id": 29739671,
154 "name": "my label",
155 "color": "#00b140",
156 "text_color": "#FFFFFF",
157 "description": "Simple label",
158 "open_issues_count": 0,
159 "closed_issues_count": 0,
160 "open_merge_requests_count": 0,
161 "subscribed": false,
162 "priority": 0,
163 "is_project_label": true,
164 "archived": false
165 }
166]`
168 // GIVEN
169 testClient := gitlabtesting.NewTestClient(t)
170 testClient.MockLabels.EXPECT().
171 ListLabels("OWNER/REPO", gomock.Any()).
172 Return(testLabels, nil, nil)
174 exec := cmdtest.SetupCmdForTest(
175 t,
176 NewCmdList,
177 true,
178 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
179 )
181 // WHEN
182 output, err := exec("-F json")
184 // THEN
185 require.NoError(t, err)
186 assert.JSONEq(t, expectedBody, output.OutBuf.String())
187 assert.Empty(t, output.ErrBuf.String())
190func TestGroupLabelList(t *testing.T) {
191 testLabels := []*gitlab.GroupLabel{
192 {
193 ID: 1,
194 Name: "groupbug",
195 Description: "",
196 Color: "#6699cc",
197 },
198 {
199 ID: 2,
200 Name: "groupux",
201 Description: "User Experience",
202 Color: "#3cb371",
203 },
204 }
206 expectedOut := heredoc.Doc(`Showing 2 labels on foo. (Page 1)
208 ID Name Description Color
209 1 groupbug #6699cc
210 2 groupux User Experience #3cb371
212 `)
214 // GIVEN
215 testClient := gitlabtesting.NewTestClient(t)
216 testClient.MockGroupLabels.EXPECT().
217 ListGroupLabels("foo", gomock.Any()).
218 Return(testLabels, nil, nil)
220 exec := cmdtest.SetupCmdForTest(
221 t,
222 NewCmdList,
223 true,
224 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
225 )
227 // WHEN
228 output, err := exec("--group foo")
230 // THEN
231 require.NoError(t, err)
232 assert.Equal(t, expectedOut, output.OutBuf.String())
233 assert.Empty(t, output.ErrBuf.String())
236func TestGroupLabelListShowsTotal(t *testing.T) {
237 testLabels := []*gitlab.GroupLabel{
238 {
239 ID: 1,
240 Name: "groupbug",
241 Description: "",
242 Color: "#6699cc",
243 },
244 }
246 expectedOut := heredoc.Doc(`Showing 1 of 33 labels on foo. (Page 33)
248 ID Name Description Color
249 1 groupbug #6699cc
251 `)
253 // GIVEN
254 testClient := gitlabtesting.NewTestClient(t)
255 testClient.MockGroupLabels.EXPECT().
256 ListGroupLabels("foo", gomock.Any()).
257 Return(testLabels, &gitlab.Response{CurrentPage: 33, TotalPages: 33, TotalItems: 33}, nil)
259 exec := cmdtest.SetupCmdForTest(
260 t,
261 NewCmdList,
262 true,
263 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
264 )
266 // WHEN
267 output, err := exec("--group foo --per-page 1 --page 33")
269 // THEN
270 require.NoError(t, err)
271 assert.Equal(t, expectedOut, output.OutBuf.String())
272 assert.Empty(t, output.ErrBuf.String())
Diagnostics8 git calls · 228 ms
CallStatusSizeTime0228 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
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
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=2
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: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=44
Open pack map
fetchtree:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200283 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=283
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=6452924dd420a333df8d553dd2f7746e42cef3dd
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=283
git.pkt.payload.max=256
git.sideband.band1.bytes=256
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=256
git.pack.trailer=ccc783374d764244fbd131e1e835e875879f31cf
Open pack map
fetchtree:0200140 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=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a567eaeca7927e635db75d869867fc66591cf442
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=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=4d9b848d2075d5422e1c74048f9c602a9372c470
Open pack map
fetch2001.5 KiB214 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=214
git.response.bytes=1585
git.response.cache=MISS
git.response.duration_ms=214
git.fetch.wants=2dd98613698631bad853a408d425e0fc71c80e24
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=1585
git.pkt.payload.max=1558
git.sideband.band1.bytes=1558
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
a5e2e8261ac3ecfa
Request
502f8c27…
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