GitLabgitlab-org

cli

main
ply8 git calls221 ms98 KiB6 cachedprotocol v2
193 lines5.6 KiBRaw
1//go:build !integration
3package list
5import (
6 "testing"
7 "time"
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 TestTodoList(t *testing.T) {
21 t.Parallel()
22 type testCase struct {
23 name string
24 cli string
25 expectedOut string
26 wantErr bool
27 setupMock func(tc *gitlabtesting.TestClient)
28 }
30 createdAt := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
31 testTodo := &gitlab.Todo{
32 ID: 42,
33 ActionName: gitlab.TodoAssigned,
34 TargetType: gitlab.TodoTargetMergeRequest,
35 Target: &gitlab.TodoTarget{
36 Title: "Fix the bug",
37 },
38 Project: &gitlab.BasicProject{
39 PathWithNamespace: "mygroup/myproject",
40 },
41 State: "pending",
42 CreatedAt: &createdAt,
43 }
45 groupTodo := &gitlab.Todo{
46 ID: 7,
47 ActionName: gitlab.TodoMentioned,
48 TargetType: gitlab.TodoTargetType("Epic"),
49 Target: &gitlab.TodoTarget{Title: "Plan the quarter"},
50 Group: &gitlab.ProjectNamespace{FullPath: "gitlab-org/plans"},
51 CreatedAt: &createdAt,
52 }
53 projectTodoInGroup := &gitlab.Todo{
54 ID: 8,
55 ActionName: gitlab.TodoAssigned,
56 TargetType: gitlab.TodoTargetIssue,
57 Target: &gitlab.TodoTarget{Title: "Fix the bug"},
58 Project: &gitlab.BasicProject{PathWithNamespace: "mygroup/myproject"},
59 Group: &gitlab.ProjectNamespace{FullPath: "mygroup"},
60 CreatedAt: &createdAt,
61 }
63 testCases := []testCase{
64 {
65 name: "lists pending todos",
66 cli: "",
67 expectedOut: "ID\tAction\tType\tTitle\tProject\tCreated\n42\tassigned\tMergeRequest\tFix the bug\tmygroup/myproject\t2025-01-01 00:00:00 +0000 UTC\n\n",
68 setupMock: func(tc *gitlabtesting.TestClient) {
69 tc.MockTodos.EXPECT().
70 ListTodos(gomock.Any(), gomock.Any()).
71 Return([]*gitlab.Todo{testTodo}, nil, nil)
72 },
73 },
74 {
75 name: "lists done todos with --state=done",
76 cli: "--state=done",
77 expectedOut: "ID\tAction\tType\tTitle\tProject\tCreated\n42\tassigned\tMergeRequest\tFix the bug\tmygroup/myproject\t2025-01-01 00:00:00 +0000 UTC\n\n",
78 setupMock: func(tc *gitlabtesting.TestClient) {
79 tc.MockTodos.EXPECT().
80 ListTodos(gomock.Any(), gomock.Any()).
81 Return([]*gitlab.Todo{testTodo}, nil, nil)
82 },
83 },
84 {
85 name: "filters by action",
86 cli: "--action=assigned",
87 expectedOut: "ID\tAction\tType\tTitle\tProject\tCreated\n42\tassigned\tMergeRequest\tFix the bug\tmygroup/myproject\t2025-01-01 00:00:00 +0000 UTC\n\n",
88 setupMock: func(tc *gitlabtesting.TestClient) {
89 tc.MockTodos.EXPECT().
90 ListTodos(gomock.Any(), gomock.Any()).
91 Return([]*gitlab.Todo{testTodo}, nil, nil)
92 },
93 },
94 {
95 name: "filters by type",
96 cli: "--type=MergeRequest",
97 expectedOut: "ID\tAction\tType\tTitle\tProject\tCreated\n42\tassigned\tMergeRequest\tFix the bug\tmygroup/myproject\t2025-01-01 00:00:00 +0000 UTC\n\n",
98 setupMock: func(tc *gitlabtesting.TestClient) {
99 tc.MockTodos.EXPECT().
100 ListTodos(gomock.Any(), gomock.Any()).
101 Return([]*gitlab.Todo{testTodo}, nil, nil)
102 },
103 },
104 {
105 name: "shows the group path for group-scoped todos and the project path otherwise",
106 cli: "",
107 expectedOut: "ID\tAction\tType\tTitle\tProject\tCreated\n" +
108 "7\tmentioned\tEpic\tPlan the quarter\tgitlab-org/plans\t2025-01-01 00:00:00 +0000 UTC\n" +
109 "8\tassigned\tIssue\tFix the bug\tmygroup/myproject\t2025-01-01 00:00:00 +0000 UTC\n\n",
110 setupMock: func(tc *gitlabtesting.TestClient) {
111 tc.MockTodos.EXPECT().
112 ListTodos(gomock.Any(), gomock.Any()).
113 Return([]*gitlab.Todo{groupTodo, projectTodoInGroup}, nil, nil)
114 },
115 },
116 {
117 name: "returns empty output when no todos",
118 cli: "",
119 expectedOut: "\n",
120 setupMock: func(tc *gitlabtesting.TestClient) {
121 tc.MockTodos.EXPECT().
122 ListTodos(gomock.Any(), gomock.Any()).
123 Return([]*gitlab.Todo{}, nil, nil)
124 },
125 },
126 }
128 for _, tc := range testCases {
129 t.Run(tc.name, func(t *testing.T) {
130 t.Parallel()
131 // GIVEN
132 testClient := gitlabtesting.NewTestClient(t)
133 tc.setupMock(testClient)
134 exec := cmdtest.SetupCmdForTest(
135 t,
136 NewCmd,
137 false,
138 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
139 )
141 // WHEN
142 out, err := exec(tc.cli)
144 // THEN
145 if tc.wantErr {
146 require.Error(t, err)
147 return
148 }
149 require.NoError(t, err)
150 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
151 assert.Empty(t, out.ErrBuf.String())
152 })
153 }
156func TestTodoList_JSON(t *testing.T) {
157 t.Parallel()
159 createdAt := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
160 testTodo := &gitlab.Todo{
161 ID: 42,
162 ActionName: gitlab.TodoAssigned,
163 TargetType: gitlab.TodoTargetMergeRequest,
164 Target: &gitlab.TodoTarget{
165 Title: "Fix the bug",
166 },
167 Project: &gitlab.BasicProject{
168 PathWithNamespace: "mygroup/myproject",
169 },
170 State: "pending",
171 CreatedAt: &createdAt,
172 }
174 testClient := gitlabtesting.NewTestClient(t)
175 testClient.MockTodos.EXPECT().
176 ListTodos(gomock.Any(), gomock.Any()).
177 Return([]*gitlab.Todo{testTodo}, nil, nil)
179 exec := cmdtest.SetupCmdForTest(
180 t,
181 NewCmd,
182 false,
183 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
184 )
186 out, err := exec("--output json")
187 require.NoError(t, err)
189 assert.Contains(t, out.String(), `"id":42`)
190 assert.Contains(t, out.String(), `"action_name":"assigned"`)
191 assert.Contains(t, out.String(), `"target_type":"MergeRequest"`)
192 assert.Empty(t, out.Stderr())
Diagnostics8 git calls · 221 ms
CallStatusSizeTime0221 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 · 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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=3
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273
git.response.cache=HIT
git.response.duration_ms=4
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 · 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:0200165 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=165
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=c6bf531ba0613331e49d2faa099cd3466613e7b8
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=165
git.pkt.payload.max=138
git.sideband.band1.bytes=138
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=138
git.pack.trailer=a34ae15f8bc51d95314a64e29216fc88a5c857b0
Open pack map
fetchtree:0200133 B · 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=133
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=e13e74e1951904177bfc6400e39c755ab95bb9fb
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=133
git.pkt.payload.max=106
git.sideband.band1.bytes=106
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=106
git.pack.trailer=4a81e579a84fe2ad1ae3eba3e9b979d90df7fa59
Open pack map
fetch2001.4 KiB207 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=207
git.response.bytes=1416
git.response.cache=MISS
git.response.duration_ms=207
git.fetch.wants=5442533b2005ea8995321331911537f10869ad49
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=1416
git.pkt.payload.max=1389
git.sideband.band1.bytes=1389
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
a5e2e2f2cf034af4
Request
be7c9966…
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