GitLabgitlab-org
cli
main
1//go:build !integration
3package add
5import (
6 "errors"
7 "fmt"
8 "net/url"
9 "strings"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "go.uber.org/mock/gomock"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
19 "gitlab.com/gitlab-org/cli/internal/api"
20 "gitlab.com/gitlab-org/cli/internal/git"
21 "gitlab.com/gitlab-org/cli/internal/glinstance"
22 "gitlab.com/gitlab-org/cli/internal/glrepo"
23 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
24 "gitlab.com/gitlab-org/cli/test"
25)
27var testProject = &gitlab.Project{
28 ID: 1,
29 Name: "my-project",
30 PathWithNamespace: "alice/my-project",
31 HTTPURLToRepo: "https://gitlab.com/alice/my-project.git",
32 SSHURLToRepo: "git@gitlab.com:alice/my-project.git",
33}
35func remoteNamed(name string) glrepo.Remotes {
36 remote := &git.Remote{
37 Name: name,
38 FetchURL: &url.URL{
39 Scheme: "https",
40 Host: "gitlab.com",
41 Path: "/alice/my-project.git",
42 },
43 PushURL: &url.URL{
44 Scheme: "https",
45 Host: "gitlab.com",
46 Path: "/alice/my-project.git",
47 },
48 }
49 repo := glrepo.New("alice", "my-project", glinstance.DefaultHostname)
50 return glrepo.Remotes{&glrepo.Remote{Remote: remote, Repo: repo}}
51}
53func TestDefaultRemoteName(t *testing.T) {
54 tests := []struct {
55 projectID string
56 want string
57 }{
58 {"alice/repo", "alice"},
59 {"group/subgroup/project", "group"},
60 {"a/b/c/d", "a"},
61 }
62 for _, tt := range tests {
63 assert.Equal(t, tt.want, defaultRemoteName(tt.projectID))
64 }
65}
67func TestNewCmdRemoteAdd(t *testing.T) {
68 tests := []struct {
69 name string
70 args string
71 remotes glrepo.Remotes
72 remotesErr error
73 mockSetup func(tc *gitlabtesting.TestClient)
74 expectedShellouts []string
75 wantOut string
76 wantErr string
77 }{
78 {
79 name: "adds remote with default name (namespace) using ssh",
80 args: "alice/my-project",
81 remotes: glrepo.Remotes{},
82 mockSetup: func(tc *gitlabtesting.TestClient) {
83 tc.MockProjects.EXPECT().
84 GetProject("alice/my-project", gomock.Any(), gomock.Any()).
85 Return(testProject, nil, nil)
86 },
87 expectedShellouts: []string{"git remote add -f alice -- git@gitlab.com:alice/my-project.git"},
88 wantOut: "✓ Remote \"alice\" added using ssh protocol.\n",
89 },
90 {
91 name: "adds remote with custom --name flag",
92 args: "alice/my-project --name upstream",
93 remotes: glrepo.Remotes{},
94 mockSetup: func(tc *gitlabtesting.TestClient) {
95 tc.MockProjects.EXPECT().
96 GetProject("alice/my-project", gomock.Any(), gomock.Any()).
97 Return(testProject, nil, nil)
98 },
99 expectedShellouts: []string{"git remote add -f upstream -- git@gitlab.com:alice/my-project.git"},
100 wantOut: "✓ Remote \"upstream\" added using ssh protocol.\n",
101 },
102 {
103 name: "subgroup path uses first component as remote name",
104 args: "group/subgroup/project",
105 remotes: glrepo.Remotes{},
106 mockSetup: func(tc *gitlabtesting.TestClient) {
107 tc.MockProjects.EXPECT().
108 GetProject("group/subgroup/project", gomock.Any(), gomock.Any()).
109 Return(&gitlab.Project{
110 ID: 2,
111 PathWithNamespace: "group/subgroup/project",
112 HTTPURLToRepo: "https://gitlab.com/group/subgroup/project.git",
113 SSHURLToRepo: "git@gitlab.com:group/subgroup/project.git",
114 }, nil, nil)
115 },
116 expectedShellouts: []string{"git remote add -f group -- git@gitlab.com:group/subgroup/project.git"},
117 wantOut: "✓ Remote \"group\" added using ssh protocol.\n",
118 },
119 {
120 name: "adds remote using --protocol https flag",
121 args: "alice/my-project --protocol https",
122 remotes: glrepo.Remotes{},
123 mockSetup: func(tc *gitlabtesting.TestClient) {
124 tc.MockProjects.EXPECT().
125 GetProject("alice/my-project", gomock.Any(), gomock.Any()).
126 Return(testProject, nil, nil)
127 },
128 expectedShellouts: []string{"git remote add -f alice -- https://gitlab.com/alice/my-project.git"},
129 wantOut: "✓ Remote \"alice\" added using https protocol.\n",
130 },
131 {
132 name: "adds remote using --protocol ssh flag",
133 args: "alice/my-project --protocol ssh",
134 remotes: glrepo.Remotes{},
135 mockSetup: func(tc *gitlabtesting.TestClient) {
136 tc.MockProjects.EXPECT().
137 GetProject("alice/my-project", gomock.Any(), gomock.Any()).
138 Return(testProject, nil, nil)
139 },
140 expectedShellouts: []string{"git remote add -f alice -- git@gitlab.com:alice/my-project.git"},
141 wantOut: "✓ Remote \"alice\" added using ssh protocol.\n",
142 },
143 {
144 name: "fails with invalid protocol value",
145 args: "alice/my-project --protocol ftp",
146 mockSetup: func(tc *gitlabtesting.TestClient) {},
147 wantErr: "invalid protocol",
148 },
149 {
150 name: "fails without slash in project reference",
151 args: "my-project",
152 mockSetup: func(tc *gitlabtesting.TestClient) {},
153 wantErr: `invalid project reference`,
154 },
155 {
156 name: "fails when remote name already exists",
157 args: "alice/my-project --name origin",
158 remotes: remoteNamed("origin"),
159 mockSetup: func(tc *gitlabtesting.TestClient) {},
160 wantErr: `remote "origin" already exists`,
161 },
162 {
163 name: "fails when remotes cannot be listed",
164 args: "alice/my-project",
165 remotesErr: errors.New("git remote failed"),
166 mockSetup: func(tc *gitlabtesting.TestClient) {},
167 wantErr: "failed to list remotes: git remote failed",
168 },
169 {
170 name: "fails when project not found",
171 args: "alice/missing-project",
172 remotes: glrepo.Remotes{},
173 mockSetup: func(tc *gitlabtesting.TestClient) {
174 tc.MockProjects.EXPECT().
175 GetProject("alice/missing-project", gomock.Any(), gomock.Any()).
176 Return(nil, nil, fmt.Errorf("404 Not Found"))
177 },
178 wantErr: `failed to find project "alice/missing-project": 404 Not Found`,
179 },
180 {
181 name: "fails when the API returns an option-like clone URL",
182 args: "alice/my-project",
183 remotes: glrepo.Remotes{},
184 mockSetup: func(tc *gitlabtesting.TestClient) {
185 tc.MockProjects.EXPECT().
186 GetProject("alice/my-project", gomock.Any(), gomock.Any()).
187 Return(&gitlab.Project{
188 ID: 1,
189 PathWithNamespace: "alice/my-project",
190 SSHURLToRepo: "--upload-pack=touch x",
191 }, nil, nil)
192 },
193 wantErr: "invalid git remote URL",
194 },
195 {
196 name: "works in empty repo with no remotes",
197 args: "alice/my-project",
198 remotes: nil,
199 mockSetup: func(tc *gitlabtesting.TestClient) {
200 tc.MockProjects.EXPECT().
201 GetProject("alice/my-project", gomock.Any(), gomock.Any()).
202 Return(testProject, nil, nil)
203 },
204 expectedShellouts: []string{"git remote add -f alice -- git@gitlab.com:alice/my-project.git"},
205 wantOut: "✓ Remote \"alice\" added using ssh protocol.\n",
206 },
207 }
209 for _, tt := range tests {
210 t.Run(tt.name, func(t *testing.T) {
211 cs, csTeardown := test.InitCmdStubber()
212 defer csTeardown()
214 for _, stub := range tt.expectedShellouts {
215 cs.Stub(stub)
216 }
218 tc := gitlabtesting.NewTestClient(t)
219 tt.mockSetup(tc)
221 exec := cmdtest.SetupCmdForTest(t, NewCmdRemoteAdd, false,
222 cmdtest.WithApiClient(
223 cmdtest.NewTestApiClient(t, nil, "", glinstance.DefaultHostname, api.WithGitLabClient(tc.Client)),
224 ),
225 func(f *cmdtest.Factory) {
226 f.RemotesStub = func() (glrepo.Remotes, error) {
227 return tt.remotes, tt.remotesErr
228 }
229 },
230 )
232 out, err := exec(tt.args)
234 if tt.wantErr != "" {
235 require.Error(t, err)
236 assert.Contains(t, err.Error(), tt.wantErr)
237 assert.Zero(t, cs.Count)
238 return
239 }
241 require.NoError(t, err)
242 assert.Equal(t, tt.wantOut, out.OutBuf.String())
244 assert.Equal(t, len(tt.expectedShellouts), cs.Count)
245 for i, expected := range tt.expectedShellouts {
246 assert.Equal(t, expected, strings.Join(cs.Calls[i].Args, " "))
247 }
248 })
249 }
250}
Diagnostics9 git calls · 293 ms
CallStatusSizeTime0293 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
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
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=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:0200139 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=139 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=86b4a9f4a0b09eded2143443ad74d7f917671c23 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=6a9499ce505b6df1dd25fe321c1d78aa9bbaf947Open pack map
fetchtree:0200135 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=135 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=b692c18e7078bb6aa5483ed627c7ca0f9bba8494 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=135 git.pkt.payload.max=108 git.sideband.band1.bytes=108 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=108 git.pack.trailer=e8840bdb8fc726bffb54b68317e2352e4cfb344eOpen pack map
fetch2001.8 KiB278 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=278 git.response.bytes=1863 git.response.cache=MISS git.response.duration_ms=278 git.fetch.wants=f75ac1cde33560188c753953ac7937f9fc8eb7a4 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=1863 git.pkt.payload.max=1836 git.sideband.band1.bytes=1836 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
- a5ea62154fbdecd4
- Request
- 57a1ac90…
- 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