GitLabgitlab-org
cli
main
1package delete
3import (
4 "errors"
5 "testing"
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 "go.uber.org/mock/gomock"
11 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
13 "gitlab.com/gitlab-org/cli/internal/api"
14 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
15)
17func Test_DeleteProjectMilestone(t *testing.T) {
18 type testCase struct {
19 Name string
20 ExpectedMsg []string
21 wantErr bool
22 cli string
23 wantStderr string
24 setupMock func(tc *gitlabtesting.TestClient)
25 }
27 testCases := []testCase{
28 {
29 Name: "Delete project milestone",
30 ExpectedMsg: []string{"Deleted project milestone with ID 123.\n"},
31 cli: "123 --project 456",
32 setupMock: func(tc *gitlabtesting.TestClient) {
33 tc.MockMilestones.EXPECT().DeleteMilestone("456", int64(123)).Return(nil, nil)
34 },
35 },
36 {
37 Name: "When milestone is not found returns an error",
38 wantErr: true,
39 wantStderr: "404 Not found",
40 cli: "111 --project 456",
41 setupMock: func(tc *gitlabtesting.TestClient) {
42 tc.MockMilestones.EXPECT().DeleteMilestone("456", int64(111)).Return(nil, errors.New("404 Not found"))
43 },
44 },
45 {
46 Name: "When milestone ID is not set returns an error that it is required",
47 wantErr: true,
48 wantStderr: "accepts 1 arg(s), received 0",
49 cli: "--project 456",
50 setupMock: func(tc *gitlabtesting.TestClient) {},
51 },
52 {
53 Name: "When neither project nor group is set it deletes the milestone from the current project",
54 cli: "123",
55 setupMock: func(tc *gitlabtesting.TestClient) {
56 tc.MockMilestones.EXPECT().DeleteMilestone("OWNER/REPO", int64(123), gomock.Any()).Return(nil, nil)
57 },
58 },
59 }
61 for _, tc := range testCases {
62 t.Run(tc.Name, func(t *testing.T) {
63 // GIVEN
64 testClient := gitlabtesting.NewTestClient(t)
65 tc.setupMock(testClient)
66 exec := cmdtest.SetupCmdForTest(
67 t,
68 NewCmdDelete,
69 false,
70 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
71 )
73 // WHEN
74 out, err := exec(tc.cli)
76 // THEN
77 if tc.wantErr {
78 require.Error(t, err)
79 assert.Contains(t, err.Error(), tc.wantStderr)
80 return
81 }
82 require.NoError(t, err)
83 for _, msg := range tc.ExpectedMsg {
84 assert.Equal(t, msg, out.OutBuf.String())
85 }
86 })
87 }
88}
90func Test_DeleteGroupMilestone(t *testing.T) {
91 type testCase struct {
92 Name string
93 ExpectedMsg []string
94 wantErr bool
95 cli string
96 wantStderr string
97 setupMock func(tc *gitlabtesting.TestClient)
98 }
100 testCases := []testCase{
101 {
102 Name: "Delete group milestone",
103 ExpectedMsg: []string{"Deleted group milestone with ID 123.\n"},
104 cli: "123 --group 456",
105 setupMock: func(tc *gitlabtesting.TestClient) {
106 tc.MockGroupMilestones.EXPECT().DeleteGroupMilestone("456", int64(123)).Return(nil, nil)
107 },
108 },
109 {
110 Name: "When milestone is not found returns an error",
111 wantErr: true,
112 wantStderr: "404 Not found",
113 cli: "111 --group 456",
114 setupMock: func(tc *gitlabtesting.TestClient) {
115 tc.MockGroupMilestones.EXPECT().DeleteGroupMilestone("456", int64(111)).Return(nil, errors.New("404 Not found"))
116 },
117 },
118 {
119 Name: "When milestone ID is not set returns an error that it is required",
120 wantErr: true,
121 wantStderr: "accepts 1 arg(s), received 0",
122 cli: "--group 456",
123 setupMock: func(tc *gitlabtesting.TestClient) {},
124 },
125 }
127 for _, tc := range testCases {
128 t.Run(tc.Name, func(t *testing.T) {
129 // GIVEN
130 testClient := gitlabtesting.NewTestClient(t)
131 tc.setupMock(testClient)
132 exec := cmdtest.SetupCmdForTest(
133 t,
134 NewCmdDelete,
135 false,
136 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
137 )
139 // WHEN
140 out, err := exec(tc.cli)
142 // THEN
143 if tc.wantErr {
144 require.Error(t, err)
145 assert.Contains(t, err.Error(), tc.wantStderr)
146 return
147 }
148 require.NoError(t, err)
149 for _, msg := range tc.ExpectedMsg {
150 assert.Equal(t, msg, out.OutBuf.String())
151 }
152 })
153 }
154}
156// Without an explicit --project or --group, these commands fall back to the
157// base repository. When that lookup fails the error has to surface instead of
158// being dropped, otherwise the nil repository is dereferenced and glab panics.
159func Test_DeleteProjectMilestone_BaseRepoError(t *testing.T) {
160 testClient := gitlabtesting.NewTestClient(t)
161 exec := cmdtest.SetupCmdForTest(
162 t,
163 NewCmdDelete,
164 false,
165 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
166 cmdtest.WithBaseRepoError(errors.New("no base repository")),
167 )
169 _, err := exec("123")
171 require.Error(t, err)
172 assert.Contains(t, err.Error(), "no base repository")
173}
Diagnostics8 git calls · 248 ms
CallStatusSizeTime0248 ms
discover200185 B4 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 · 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
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=3 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=3 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:0200287 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=287 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=4b2a62b649e97f7e66e5dd178207a12ec137d596 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=287 git.pkt.payload.max=260 git.sideband.band1.bytes=260 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=260 git.pack.trailer=6593eafdcd01a12bbd3c8da0f75aaca8bc99568fOpen pack map
fetchtree:0200138 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=232 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=4180831830460e634c31843ecb023d02a238acdd 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=138 git.pkt.payload.max=111 git.sideband.band1.bytes=111 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=111 git.pack.trailer=3c20f7a914815e1a04f8ee7da99f96db8529023dOpen pack map
fetch2001.2 KiB232 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1211 git.response.cache=MISS git.response.duration_ms=232 git.fetch.wants=2145acf2dda51aea627f48efc1309a399726593c 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=1211 git.pkt.payload.max=1184 git.sideband.band1.bytes=1184 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
- a5e2c9245de64b67
- Request
- cf50e779…
- 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