GitLabgitlab-org
cli
main
1//go:build !integration
3package delete
5import (
6 "bytes"
7 "errors"
8 "fmt"
9 "testing"
11 "github.com/google/shlex"
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/glrepo"
21 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22)
24func Test_NewCmdDelete(t *testing.T) {
25 tests := []struct {
26 name string
27 cli string
28 wants options
29 stdinTTY bool
30 wantsErr bool
31 }{
32 {
33 name: "delete var",
34 cli: "cool_secret",
35 wantsErr: false,
36 },
37 {
38 name: "delete scoped var",
39 cli: "cool_secret --scope prod",
40 wantsErr: false,
41 },
42 {
43 name: "delete group var",
44 cli: "cool_secret -g mygroup",
45 wantsErr: false,
46 },
47 {
48 name: "delete scoped group var",
49 cli: "cool_secret -g mygroup --scope prod",
50 wantsErr: false,
51 },
52 {
53 name: "no name",
54 cli: "",
55 wantsErr: true,
56 },
57 {
58 name: "invalid characters in name",
59 cli: "BAD-SECRET",
60 wantsErr: true,
61 },
62 }
64 for _, tt := range tests {
65 t.Run(tt.name, func(t *testing.T) {
66 io, _, _, _ := cmdtest.TestIOStreams()
67 f := cmdtest.NewTestFactory(io,
68 func(f *cmdtest.Factory) {
69 f.ApiClientStub = func(repoHost string) (*api.Client, error) {
70 tc := gitlabtesting.NewTestClient(t)
71 tc.MockProjectVariables.EXPECT().RemoveVariable(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
72 tc.MockGroupVariables.EXPECT().RemoveVariable(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
73 return cmdtest.NewTestApiClient(t, nil, "", repoHost, api.WithGitLabClient(tc.Client)), nil
74 }
75 },
76 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
77 )
79 io.IsInTTY = tt.stdinTTY
81 argv, err := shlex.Split(tt.cli)
82 require.NoError(t, err)
84 cmd := NewCmdDelete(f)
85 cmd.SetArgs(argv)
86 cmd.SetIn(&bytes.Buffer{})
87 cmd.SetOut(&bytes.Buffer{})
88 cmd.SetErr(&bytes.Buffer{})
90 _, err = cmd.ExecuteC()
91 if tt.wantsErr {
92 assert.Error(t, err)
93 return
94 }
95 assert.NoError(t, err)
96 })
97 }
98}
100func Test_deleteRun(t *testing.T) {
101 tests := []struct {
102 name string
103 key string
104 scope string
105 group string
106 wantsErr bool
107 wantsOutput string
108 setupMock func(tc *gitlabtesting.TestClient)
109 }{
110 {
111 name: "delete project variable no scope",
112 key: "TEST_VAR",
113 scope: "*",
114 wantsErr: false,
115 wantsOutput: "✓ Deleted variable TEST_VAR with scope * for owner/repo.\n",
116 setupMock: func(tc *gitlabtesting.TestClient) {
117 tc.MockProjectVariables.EXPECT().
118 RemoveVariable("owner/repo", "TEST_VAR", gomock.Any(), gomock.Any()).
119 Return(nil, nil)
120 },
121 },
122 {
123 name: "delete project variable with stage scope",
124 key: "TEST_VAR",
125 scope: "stage",
126 wantsErr: false,
127 wantsOutput: "✓ Deleted variable TEST_VAR with scope stage for owner/repo.\n",
128 setupMock: func(tc *gitlabtesting.TestClient) {
129 tc.MockProjectVariables.EXPECT().
130 RemoveVariable("owner/repo", "TEST_VAR", gomock.Any(), gomock.Any()).
131 Return(nil, nil)
132 },
133 },
134 {
135 name: "delete group variable",
136 key: "TEST_VAR",
137 scope: "*",
138 group: "testGroup",
139 wantsErr: false,
140 wantsOutput: "✓ Deleted variable TEST_VAR for group testGroup with scope *.\n",
141 setupMock: func(tc *gitlabtesting.TestClient) {
142 tc.MockGroupVariables.EXPECT().
143 RemoveVariable("testGroup", "TEST_VAR", gomock.Any(), gomock.Any()).
144 Return(nil, nil)
145 },
146 },
147 }
149 for _, tt := range tests {
150 t.Run(tt.name, func(t *testing.T) {
151 // GIVEN
152 testClient := gitlabtesting.NewTestClient(t)
153 tt.setupMock(testClient)
155 io, _, stdout, _ := cmdtest.TestIOStreams()
157 opts := &options{
158 apiClient: func(repoHost string) (*api.Client, error) {
159 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
160 },
161 baseRepo: func() (glrepo.Interface, error) {
162 return glrepo.New("owner", "repo", "gitlab.com"), nil
163 },
164 io: io,
165 key: tt.key,
166 scope: tt.scope,
167 group: tt.group,
168 }
170 // WHEN
171 err := opts.run(t.Context())
173 // THEN
174 if tt.wantsErr {
175 require.Error(t, err)
176 return
177 }
178 require.NoError(t, err)
179 assert.Equal(t, tt.wantsOutput, stdout.String())
180 })
181 }
182}
184func Test_deleteGroupScope(t *testing.T) {
185 t.Parallel()
187 apiErr := errors.New("group variable delete failed")
188 for _, tt := range []struct {
189 name string
190 cli string
191 scope string
192 apiErr error
193 }{
194 {name: "default scope", cli: "TEST_VARIABLE -g mygroup", scope: "*"},
195 {name: "explicit wildcard", cli: "TEST_VARIABLE -g mygroup -s '*'", scope: "*"},
196 {name: "stage scope", cli: "TEST_VARIABLE --group mygroup --scope stage", scope: "stage"},
197 {name: "wildcard environment", cli: "TEST_VARIABLE -g mygroup -s 'review/*'", scope: "review/*"},
198 {name: "API error", cli: "TEST_VARIABLE -g mygroup -s stage", scope: "stage", apiErr: apiErr},
199 } {
200 t.Run(tt.name, func(t *testing.T) {
201 t.Parallel()
202 tc := gitlabtesting.NewTestClient(t)
203 tc.MockGroupVariables.EXPECT().RemoveVariable("mygroup", "TEST_VARIABLE", &gitlab.RemoveGroupVariableOptions{
204 Filter: &gitlab.VariableFilter{EnvironmentScope: tt.scope},
205 }, gomock.Any()).Return(nil, tt.apiErr)
206 exec := cmdtest.SetupCmdForTest(t, NewCmdDelete, false,
207 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(tc.Client))))
209 out, err := exec(tt.cli)
210 if tt.apiErr != nil {
211 require.ErrorIs(t, err, tt.apiErr)
212 assert.Empty(t, out.String())
213 return
214 }
215 require.NoError(t, err)
216 assert.Equal(t, fmt.Sprintf("✓ Deleted variable TEST_VARIABLE for group mygroup with scope %s.\n", tt.scope), out.String())
217 assert.Empty(t, out.Stderr())
218 })
219 }
220}
Diagnostics8 git calls · 281 ms
CallStatusSizeTime0281 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 KiB3 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=UPDATING 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=3 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 · 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=32926 git.response.cache=HIT git.response.duration_ms=3 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 · 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:0200375 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=375 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=210e8ccdc9766a94adae919c6922ea88d77a8fb1 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=375 git.pkt.payload.max=348 git.sideband.band1.bytes=348 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=348 git.pack.trailer=8c3972a32424b06b4c572b8aa869b28eca201985Open pack map
fetchtree:0200138 B · 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=138 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=1b4d6f228f2c5058b660f697273ef8f1a222e6b1 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=d27ffc4439b38f5880fdd4131e2542a84d358fccOpen pack map
fetch2001.7 KiB264 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=264 git.response.bytes=1695 git.response.cache=MISS git.response.duration_ms=264 git.fetch.wants=c082da6be03fff80ccc0f12c657296d975d3f3bd 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=1695 git.pkt.payload.max=1668 git.sideband.band1.bytes=1668 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
- a5e1ac35980f2c38
- Request
- 8d60d25f…
- 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