GitLabgitlab-org
cli
main
1//go:build !integration
3package todo
5import (
6 "errors"
7 "net/http"
8 "testing"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
18)
20func TestMrTodo(t *testing.T) {
21 type testCase struct {
22 name string
23 cli string
24 expectedOut string
25 wantErr bool
26 expectedError error
27 setupMock func(tc *gitlabtesting.TestClient)
28 }
30 testMR := &gitlab.MergeRequest{
31 BasicMergeRequest: gitlab.BasicMergeRequest{
32 ID: 123,
33 IID: 123,
34 ProjectID: 3,
35 Title: "test mr title",
36 Description: "test mr description",
37 State: "opened",
38 },
39 }
41 testCases := []testCase{
42 {
43 name: "when an MR is added as a todo using an MR id",
44 cli: "123",
45 expectedOut: "✓ Done!!\n",
46 setupMock: func(tc *gitlabtesting.TestClient) {
47 tc.MockMergeRequests.EXPECT().
48 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any()).
49 Return(testMR, nil, nil)
50 tc.MockMergeRequests.EXPECT().
51 CreateTodo("OWNER/REPO", int64(123)).
52 Return(nil, &gitlab.Response{Response: &http.Response{StatusCode: http.StatusCreated}}, nil)
53 },
54 },
55 {
56 name: "when an MR is added as a todo using a branch name",
57 cli: "foo",
58 expectedOut: "✓ Done!!\n",
59 setupMock: func(tc *gitlabtesting.TestClient) {
60 tc.MockMergeRequests.EXPECT().
61 ListProjectMergeRequests("OWNER/REPO", gomock.Any()).
62 Return([]*gitlab.BasicMergeRequest{{
63 ID: 123,
64 IID: 123,
65 ProjectID: 3,
66 Title: "test mr title",
67 Description: "test mr description",
68 State: "opened",
69 }}, nil, nil)
70 tc.MockMergeRequests.EXPECT().
71 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any()).
72 Return(testMR, nil, nil)
73 tc.MockMergeRequests.EXPECT().
74 CreateTodo("OWNER/REPO", int64(123)).
75 Return(nil, &gitlab.Response{Response: &http.Response{StatusCode: http.StatusCreated}}, nil)
76 },
77 },
78 {
79 name: "when todo already exists",
80 cli: "foo",
81 wantErr: true,
82 expectedError: errTodoExists,
83 setupMock: func(tc *gitlabtesting.TestClient) {
84 tc.MockMergeRequests.EXPECT().
85 ListProjectMergeRequests("OWNER/REPO", gomock.Any()).
86 Return([]*gitlab.BasicMergeRequest{{
87 ID: 123,
88 IID: 123,
89 ProjectID: 3,
90 Title: "test mr title",
91 Description: "test mr description",
92 State: "opened",
93 }}, nil, nil)
94 tc.MockMergeRequests.EXPECT().
95 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any()).
96 Return(testMR, nil, nil)
97 tc.MockMergeRequests.EXPECT().
98 CreateTodo("OWNER/REPO", int64(123)).
99 Return(nil, &gitlab.Response{Response: &http.Response{StatusCode: http.StatusNotModified}}, nil)
100 },
101 },
102 {
103 name: "when the request fails without a response",
104 cli: "123",
105 wantErr: true,
106 expectedError: errors.New("network unavailable"),
107 setupMock: func(tc *gitlabtesting.TestClient) {
108 tc.MockMergeRequests.EXPECT().
109 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any()).
110 Return(testMR, nil, nil)
111 tc.MockMergeRequests.EXPECT().
112 CreateTodo("OWNER/REPO", int64(123)).
113 Return(nil, nil, errors.New("network unavailable"))
114 },
115 },
116 }
118 for _, tc := range testCases {
119 t.Run(tc.name, func(t *testing.T) {
120 // GIVEN
121 testClient := gitlabtesting.NewTestClient(t)
122 tc.setupMock(testClient)
123 exec := cmdtest.SetupCmdForTest(
124 t,
125 NewCmdTodo,
126 false,
127 cmdtest.WithGitLabClient(testClient.Client),
128 )
130 // WHEN
131 out, err := exec(tc.cli)
133 // THEN
134 if tc.wantErr {
135 require.Error(t, err)
136 if tc.expectedError != nil {
137 assert.Equal(t, tc.expectedError, err)
138 }
139 return
140 }
141 require.NoError(t, err)
142 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
143 assert.Empty(t, out.ErrBuf.String())
144 })
145 }
146}
Diagnostics8 git calls · 213 ms
CallStatusSizeTime0213 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=2 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 · 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=746 git.response.cache=HIT git.response.duration_ms=2 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:0200139 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=198 git.response.bytes=139 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=23d7fc5dea60b9caf8b45aa92f875ecc2a25186d 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=139 git.pkt.payload.max=112 git.sideband.band1.bytes=112 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=112 git.pack.trailer=305de9dfcaa3571d64497986b791dd44394d7adfOpen pack map
fetch2001.1 KiB198 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1111 git.response.cache=MISS git.response.duration_ms=198 git.fetch.wants=caaca6e55fdf7a5a6d11809c889c9af4bf9402ea 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=1111 git.pkt.payload.max=1084 git.sideband.band1.bytes=1084 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
- a5e30842ebb515db
- Request
- 09b68c35…
- 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