GitLabgitlab-org
cli
main
1//go:build !integration
3package create
5import (
6 "fmt"
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 TestSnippetCreate(t *testing.T) {
21 type testCase struct {
22 name string
23 command string
24 wantErr string
25 wantStderr []string
26 wantStdout []string
27 setupMock func(tc *gitlabtesting.TestClient)
28 }
30 testCases := []testCase{
31 {
32 name: "Create personal snippet",
33 command: "testdata/snippet.txt --personal -d 'Hello World snippet' -f 'snippet.txt' -t 'This is a snippet'",
34 wantStderr: []string{"- Creating snippet in personal space"},
35 wantStdout: []string{"https://gitlab.example.com/-/snippets/1"},
36 setupMock: func(tc *gitlabtesting.TestClient) {
37 tc.MockSnippets.EXPECT().
38 CreateSnippet(gomock.Any()).
39 Return(&gitlab.Snippet{
40 ID: 1,
41 Title: "This is a snippet",
42 Description: "Hello World snippet",
43 WebURL: "https://gitlab.example.com/-/snippets/1",
44 FileName: "snippet.txt",
45 Files: []gitlab.SnippetFile{
46 {
47 Path: "snippet.txt",
48 RawURL: "https://gitlab.example.com/-/snippets/1/raw/main/snippet.txt",
49 },
50 },
51 }, nil, nil)
52 },
53 },
54 {
55 name: "Create project snippet",
56 command: "testdata/snippet.txt -d 'Hello World snippet' -f 'snippet.txt' -t 'This is a snippet'",
57 wantStderr: []string{"- Creating snippet in OWNER/REPO"},
58 wantStdout: []string{"https://gitlab.example.com/OWNER/REPO/-/snippets/1"},
59 setupMock: func(tc *gitlabtesting.TestClient) {
60 tc.MockProjectSnippets.EXPECT().
61 CreateSnippet("OWNER/REPO", gomock.Any()).
62 Return(&gitlab.Snippet{
63 ID: 1,
64 Title: "This is a snippet",
65 Description: "Hello World snippet",
66 WebURL: "https://gitlab.example.com/OWNER/REPO/-/snippets/1",
67 FileName: "snippet.txt",
68 Files: []gitlab.SnippetFile{
69 {
70 Path: "snippet.txt",
71 RawURL: "https://gitlab.example.com/-/OWNER/REPO/snippets/1/raw/main/snippet.txt",
72 },
73 },
74 }, nil, nil)
75 },
76 },
78 {
79 name: "Create project snippet using a path",
80 command: "testdata/snippet.txt -d 'Hello World snippet' -t 'This is a snippet'",
81 wantStderr: []string{"- Creating snippet in OWNER/REPO"},
82 wantStdout: []string{"https://gitlab.example.com/OWNER/REPO/-/snippets/1"},
83 setupMock: func(tc *gitlabtesting.TestClient) {
84 tc.MockProjectSnippets.EXPECT().
85 CreateSnippet("OWNER/REPO", gomock.Any()).
86 Return(&gitlab.Snippet{
87 ID: 1,
88 Title: "This is a snippet",
89 Description: "Hello World snippet",
90 WebURL: "https://gitlab.example.com/OWNER/REPO/-/snippets/1",
91 FileName: "snippet.txt",
92 Files: []gitlab.SnippetFile{
93 {
94 Path: "snippet.txt",
95 RawURL: "https://gitlab.example.com/-/OWNER/REPO/snippets/1/raw/main/snippet.txt",
96 },
97 },
98 }, nil, nil)
99 },
100 },
102 {
103 name: "Create project snippet from multiple files",
104 command: "-d 'Hello World snippet' -t 'This is a snippet' testdata/file1.md testdata/file2.md",
105 wantStderr: []string{"- Creating snippet in OWNER/REPO"},
106 wantStdout: []string{"https://gitlab.example.com/OWNER/REPO/-/snippets/1"},
107 setupMock: func(tc *gitlabtesting.TestClient) {
108 tc.MockProjectSnippets.EXPECT().
109 CreateSnippet("OWNER/REPO", gomock.Any()).
110 Return(&gitlab.Snippet{
111 ID: 1,
112 Title: "This is a snippet",
113 Description: "Hello World snippet",
114 WebURL: "https://gitlab.example.com/OWNER/REPO/-/snippets/1",
115 FileName: "snippet.txt",
116 Files: []gitlab.SnippetFile{
117 {
118 Path: "snippet.txt",
119 RawURL: "https://gitlab.example.com/-/OWNER/REPO/snippets/1/raw/main/snippet.txt",
120 },
121 },
122 }, nil, nil)
123 },
124 },
125 {
126 name: "Create snippet 403 failure",
127 command: "testdata/snippet.txt -d 'Hello World snippet' -f 'snippet.txt' -t 'This is a snippet'",
128 wantErr: "failed to create snippet: 403 Forbidden",
129 setupMock: func(tc *gitlabtesting.TestClient) {
130 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
131 tc.MockProjectSnippets.EXPECT().
132 CreateSnippet("OWNER/REPO", gomock.Any()).
133 Return(nil, forbiddenResponse, fmt.Errorf("403 Forbidden"))
134 },
135 },
136 {
137 name: "Create personal snippet 403 failure",
138 command: "testdata/snippet.txt --personal -d 'Hello World snippet' -f 'snippet.txt' -t 'This is a personal snippet'",
139 wantErr: "failed to create snippet: 403 Forbidden",
140 setupMock: func(tc *gitlabtesting.TestClient) {
141 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
142 tc.MockSnippets.EXPECT().
143 CreateSnippet(gomock.Any()).
144 Return(nil, forbiddenResponse, fmt.Errorf("403 Forbidden"))
145 },
146 },
147 {
148 name: "Create snippet no stdin failure",
149 command: "-d 'Hello World snippet' -f 'snippet.txt' -t 'This is a personal snippet'",
150 wantErr: "stdin required if no 'path' is provided",
151 setupMock: func(tc *gitlabtesting.TestClient) {
152 // No mock needed - fails before API call
153 },
154 },
155 {
156 name: "Create snippet no path failure",
157 command: "-d 'Hello World snippet' -t 'This is a personal snippet'",
158 wantErr: "if 'path' is not provided, 'filename' and stdin are required",
159 setupMock: func(tc *gitlabtesting.TestClient) {
160 // No mock needed - fails before API call
161 },
162 },
163 }
165 for _, tc := range testCases {
166 t.Run(tc.name, func(t *testing.T) {
167 // GIVEN
168 testClient := gitlabtesting.NewTestClient(t)
169 tc.setupMock(testClient)
171 exec := cmdtest.SetupCmdForTest(
172 t,
173 NewCmdCreate,
174 false,
175 cmdtest.WithGitLabClient(testClient.Client),
176 )
178 // WHEN
179 out, err := exec(tc.command)
181 // THEN
182 if tc.wantErr != "" {
183 require.Error(t, err)
184 assert.Contains(t, err.Error(), tc.wantErr)
185 return
186 }
187 require.NoError(t, err)
189 for _, msg := range tc.wantStdout {
190 assert.Contains(t, out.String(), msg)
191 }
193 for _, msg := range tc.wantStderr {
194 assert.Contains(t, out.Stderr(), msg)
195 }
196 })
197 }
198}
Diagnostics8 git calls · 490 ms
CallStatusSizeTime0490 ms
discover200185 B3 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 KiB2 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=UPDATING 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 · 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:0200169 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=169 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=a80c002fcd583cc74c217cd1e1ee16aea4e7e70b 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=169 git.pkt.payload.max=142 git.sideband.band1.bytes=142 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=142 git.pack.trailer=8ba52b73a78950e6664cb65379b8cd65fc810d6eOpen pack map
fetchtree:0200173 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=475 git.response.bytes=173 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=28c64b6cda6e3263690e25cda1aa7c9912d186c9 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=173 git.pkt.payload.max=146 git.sideband.band1.bytes=146 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=146 git.pack.trailer=c5e9e7bf1570c18966f9c942c9de39e737a3f60dOpen pack map
fetch2001.2 KiB475 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1267 git.response.cache=MISS git.response.duration_ms=475 git.fetch.wants=0233990766cd907fa1c4cc8dfa2d8a6ef4f849aa 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=1267 git.pkt.payload.max=1240 git.sideband.band1.bytes=1240 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
- a5e2c8cdff2d4b67
- Request
- c1fa6c77…
- 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