GitLabgitlab-org
cli
main
1//go:build !integration
3package list
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "go.uber.org/mock/gomock"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
15 "gitlab.com/gitlab-org/cli/internal/api"
16 "gitlab.com/gitlab-org/cli/internal/glinstance"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
18)
20func Test_ProjectList(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 testProject := &gitlab.Project{
31 ID: 123,
32 Description: "This is a test project",
33 PathWithNamespace: "gitlab-org/incubation-engineering/service-desk/meta",
34 }
36 testUserProject := &gitlab.Project{
37 ID: 123,
38 Description: "This is a test project",
39 PathWithNamespace: "testuser/example",
40 }
42 testGroup := &gitlab.Group{
43 ID: 456,
44 Path: "subgroup",
45 FullPath: "me/group/subgroup",
46 }
48 emptyProjectResponse := &gitlab.Response{
49 TotalItems: 0,
50 CurrentPage: 1,
51 TotalPages: 1,
52 }
54 singleProjectResponse := &gitlab.Response{
55 TotalItems: 1,
56 CurrentPage: 1,
57 TotalPages: 1,
58 }
60 testCases := []testCase{
61 {
62 name: "when no projects are found shows an empty list",
63 cli: "",
64 expectedOut: "Showing 0 of 0 projects (Page 1 of 1).\n\n\n",
65 setupMock: func(tc *gitlabtesting.TestClient) {
66 tc.MockProjects.EXPECT().
67 ListProjects(gomock.Any()).
68 Return([]*gitlab.Project{}, emptyProjectResponse, nil)
69 },
70 },
71 {
72 name: "when no arguments, filters by ownership",
73 cli: "",
74 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
75 setupMock: func(tc *gitlabtesting.TestClient) {
76 tc.MockProjects.EXPECT().
77 ListProjects(gomock.Any()).
78 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
79 },
80 },
81 {
82 name: "when starred is passed as an arg, filters by starred",
83 cli: "--starred",
84 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
85 setupMock: func(tc *gitlabtesting.TestClient) {
86 tc.MockProjects.EXPECT().
87 ListProjects(gomock.Any()).
88 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
89 },
90 },
91 {
92 name: "when member is passed as an arg, filters by member",
93 cli: "--member",
94 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
95 setupMock: func(tc *gitlabtesting.TestClient) {
96 tc.MockProjects.EXPECT().
97 ListProjects(gomock.Any()).
98 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
99 },
100 },
101 {
102 name: "when mine is passed explicitly as an arg, filters by ownership",
103 cli: "--mine",
104 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
105 setupMock: func(tc *gitlabtesting.TestClient) {
106 tc.MockProjects.EXPECT().
107 ListProjects(gomock.Any()).
108 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
109 },
110 },
111 {
112 name: "when mine and starred are passed as args, filters by ownership and starred",
113 cli: "--mine --starred",
114 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
115 setupMock: func(tc *gitlabtesting.TestClient) {
116 tc.MockProjects.EXPECT().
117 ListProjects(gomock.Any()).
118 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
119 },
120 },
121 {
122 name: "when starred and member are passed as args, filters by starred and membership",
123 cli: "--starred --member",
124 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
125 setupMock: func(tc *gitlabtesting.TestClient) {
126 tc.MockProjects.EXPECT().
127 ListProjects(gomock.Any()).
128 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
129 },
130 },
131 {
132 name: "when mine and membership are passed as args, filters by ownership and membership",
133 cli: "--mine --member",
134 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
135 setupMock: func(tc *gitlabtesting.TestClient) {
136 tc.MockProjects.EXPECT().
137 ListProjects(gomock.Any()).
138 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
139 },
140 },
141 {
142 name: "when mine, membership and starred is passed explicitly as arguments, filters by ownership, membership and starred",
143 cli: "--mine --member --starred",
144 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
145 setupMock: func(tc *gitlabtesting.TestClient) {
146 tc.MockProjects.EXPECT().
147 ListProjects(gomock.Any()).
148 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
149 },
150 },
151 {
152 name: "view all projects, no filters",
153 cli: "--all",
154 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
155 setupMock: func(tc *gitlabtesting.TestClient) {
156 tc.MockProjects.EXPECT().
157 ListProjects(gomock.Any()).
158 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
159 },
160 },
161 {
162 name: "view all projects ordered by created_at date sorted descending",
163 cli: "--order created_at --sort desc",
164 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
165 setupMock: func(tc *gitlabtesting.TestClient) {
166 tc.MockProjects.EXPECT().
167 ListProjects(gomock.Any()).
168 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
169 },
170 },
171 {
172 name: "view all projects in a specific group",
173 cli: "--group me/group/subgroup",
174 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
175 setupMock: func(tc *gitlabtesting.TestClient) {
176 tc.MockGroups.EXPECT().
177 GetGroup("me/group/subgroup", gomock.Any()).
178 Return(testGroup, nil, nil)
179 tc.MockGroups.EXPECT().
180 ListGroupProjects(int64(456), gomock.Any()).
181 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
182 },
183 },
184 {
185 name: "view all projects in a specific group including subgroups",
186 cli: "--group me/group/subgroup --include-subgroups",
187 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
188 setupMock: func(tc *gitlabtesting.TestClient) {
189 tc.MockGroups.EXPECT().
190 GetGroup("me/group/subgroup", gomock.Any()).
191 Return(testGroup, nil, nil)
192 tc.MockGroups.EXPECT().
193 ListGroupProjects(int64(456), gomock.Any()).
194 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
195 },
196 },
197 {
198 name: "view all not archived projects in a specific group",
199 cli: "-a --group me/group/subgroup --archived=false",
200 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
201 setupMock: func(tc *gitlabtesting.TestClient) {
202 tc.MockGroups.EXPECT().
203 GetGroup("me/group/subgroup", gomock.Any()).
204 Return(testGroup, nil, nil)
205 tc.MockGroups.EXPECT().
206 ListGroupProjects(int64(456), gomock.Any()).
207 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
208 },
209 },
210 {
211 name: "view all archived projects in a specific group",
212 cli: "-a --group me/group/subgroup --archived=true",
213 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
214 setupMock: func(tc *gitlabtesting.TestClient) {
215 tc.MockGroups.EXPECT().
216 GetGroup("me/group/subgroup", gomock.Any()).
217 Return(testGroup, nil, nil)
218 tc.MockGroups.EXPECT().
219 ListGroupProjects(int64(456), gomock.Any()).
220 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
221 },
222 },
223 {
224 name: "view all archived projects",
225 cli: "-a --archived=true",
226 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
227 setupMock: func(tc *gitlabtesting.TestClient) {
228 tc.MockProjects.EXPECT().
229 ListProjects(gomock.Any()).
230 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
231 },
232 },
233 {
234 name: "view all not archived projects",
235 cli: "-a --archived=false",
236 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ngitlab-org/incubation-engineering/service-desk/meta\t\tThis is a test project\n\n",
237 setupMock: func(tc *gitlabtesting.TestClient) {
238 tc.MockProjects.EXPECT().
239 ListProjects(gomock.Any()).
240 Return([]*gitlab.Project{testProject}, singleProjectResponse, nil)
241 },
242 },
243 {
244 name: "view all projects for a given user",
245 cli: "-u testuser",
246 expectedOut: "Showing 1 of 1 projects (Page 1 of 1).\n\nProject path\tGit URL\tDescription\ntestuser/example\t\tThis is a test project\n\n",
247 setupMock: func(tc *gitlabtesting.TestClient) {
248 tc.MockProjects.EXPECT().
249 ListUserProjects("testuser", gomock.Any()).
250 Return([]*gitlab.Project{testUserProject}, singleProjectResponse, nil)
251 },
252 },
253 }
255 for _, tc := range testCases {
256 t.Run(tc.name, func(t *testing.T) {
257 // GIVEN
258 testClient := gitlabtesting.NewTestClient(t)
259 tc.setupMock(testClient)
260 exec := cmdtest.SetupCmdForTest(
261 t,
262 NewCmdList,
263 false,
264 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", glinstance.DefaultHostname, api.WithGitLabClient(testClient.Client))),
265 )
267 // WHEN
268 out, err := exec(tc.cli)
270 // THEN
271 if tc.wantErr {
272 require.Error(t, err)
273 assert.Equal(t, tc.wantStderr, err.Error())
274 return
275 }
276 require.NoError(t, err)
277 assert.Equal(t, tc.expectedOut, out.String())
278 assert.Empty(t, out.Stderr())
279 })
280 }
281}
Diagnostics8 git calls · 320 ms
CallStatusSizeTime0320 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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=44Open pack map
fetchtree:02001.8 KiB · hit0 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=0 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=0 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:0200614 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=614 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=3a89d0cfc8e81880c982180388e42e561a0b7eee 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=614 git.pkt.payload.max=587 git.sideband.band1.bytes=587 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=587 git.pack.trailer=eb8250f8e8340ff2fafc22c5aba90db34fd906a9Open pack map
fetchtree:0200134 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=134 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=169be413780d9e9d7c54514d002c8f196a648e9a 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=a92e6fbe4b21e2fbd70e08d9caa80951a91ffd51Open pack map
fetch2001.5 KiB310 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=310 git.response.bytes=1519 git.response.cache=MISS git.response.duration_ms=310 git.fetch.wants=cb9e3b55a45fb909807901e77542fedd38c5edc0 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=1519 git.pkt.payload.max=1492 git.sideband.band1.bytes=1492 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
- a5ea56871dc67089
- Request
- 3d863ca3…
- 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