GitLabgitlab-org

cli

main
ply8 git calls266 ms99 KiB6 cachedprotocol v2
279 lines7.3 KiBRaw
1//go:build !integration
3package expand
5import (
6 "errors"
7 "reflect"
8 "testing"
10 "github.com/MakeNowJust/heredoc/v2"
12 "gitlab.com/gitlab-org/cli/internal/config"
15func TestExpandAlias(t *testing.T) {
16 findShFunc := func() (string, error) {
17 return "/usr/bin/sh", nil
18 }
20 cfg := config.NewFromString(heredoc.Doc(`
21 aliases:
22 co: mr checkout
23 il: issue list --author="$1" --label="$2"
24 ia: issue list --author="$1" --assignee="$1"
25 ic: issue create --title "$1"
26 is: issue list --search=$1
27 iv: issue view $1 --comments
28 ten: issue list --label="$1" --search="$10"
29 il2: issue list --label="$2" --author="$1"
30 ip: issue list --search="price$"
31 i0: issue list --search="$0"
32 `))
34 type args struct {
35 config config.Config
36 argv []string
37 }
38 tests := []struct {
39 name string
40 args args
41 wantExpanded []string
42 wantIsShell bool
43 wantErr error
44 }{
45 {
46 name: "no arguments",
47 args: args{
48 config: cfg,
49 argv: []string{},
50 },
51 wantExpanded: []string(nil),
52 wantIsShell: false,
53 wantErr: nil,
54 },
55 {
56 name: "too few arguments",
57 args: args{
58 config: cfg,
59 argv: []string{"glab"},
60 },
61 wantExpanded: []string(nil),
62 wantIsShell: false,
63 wantErr: nil,
64 },
65 {
66 name: "no expansion",
67 args: args{
68 config: cfg,
69 argv: []string{"glab", "mr", "status"},
70 },
71 wantExpanded: []string{"mr", "status"},
72 wantIsShell: false,
73 wantErr: nil,
74 },
75 {
76 name: "simple expansion",
77 args: args{
78 config: cfg,
79 argv: []string{"glab", "co"},
80 },
81 wantExpanded: []string{"mr", "checkout"},
82 wantIsShell: false,
83 wantErr: nil,
84 },
85 {
86 name: "adding arguments after expansion",
87 args: args{
88 config: cfg,
89 argv: []string{"glab", "co", "123"},
90 },
91 wantExpanded: []string{"mr", "checkout", "123"},
92 wantIsShell: false,
93 wantErr: nil,
94 },
95 {
96 name: "not enough arguments for expansion",
97 args: args{
98 config: cfg,
99 argv: []string{"glab", "il"},
100 },
101 wantExpanded: []string{},
102 wantIsShell: false,
103 wantErr: errors.New(`not enough arguments for alias: issue list --author="$1" --label="$2"`),
104 },
105 {
106 name: "not enough arguments for expansion 2",
107 args: args{
108 config: cfg,
109 argv: []string{"glab", "il", "vilmibm"},
110 },
111 wantExpanded: []string{},
112 wantIsShell: false,
113 wantErr: errors.New(`not enough arguments for alias: issue list --author="vilmibm" --label="$2"`),
114 },
115 {
116 name: "satisfy expansion arguments",
117 args: args{
118 config: cfg,
119 argv: []string{"glab", "il", "vilmibm", "help wanted"},
120 },
121 wantExpanded: []string{"issue", "list", "--author=vilmibm", "--label=help wanted"},
122 wantIsShell: false,
123 wantErr: nil,
124 },
125 {
126 name: "mixed positional and non-positional arguments",
127 args: args{
128 config: cfg,
129 argv: []string{"glab", "il", "vilmibm", "epic", "-R", "monalisa/testing"},
130 },
131 wantExpanded: []string{"issue", "list", "--author=vilmibm", "--label=epic", "-R", "monalisa/testing"},
132 wantIsShell: false,
133 wantErr: nil,
134 },
135 {
136 name: "dollar in expansion",
137 args: args{
138 config: cfg,
139 argv: []string{"glab", "ia", "$coolmoney$"},
140 },
141 wantExpanded: []string{"issue", "list", "--author=$coolmoney$", "--assignee=$coolmoney$"},
142 wantIsShell: false,
143 wantErr: nil,
144 },
145 {
146 name: "dollar in argument keeps the extra arguments",
147 args: args{
148 config: cfg,
149 argv: []string{"glab", "ia", "$coolmoney$", "-R", "monalisa/testing"},
150 },
151 wantExpanded: []string{"issue", "list", "--author=$coolmoney$", "--assignee=$coolmoney$", "-R", "monalisa/testing"},
152 wantIsShell: false,
153 wantErr: nil,
154 },
155 {
156 name: "placeholder text in argument is not a placeholder",
157 args: args{
158 config: cfg,
159 argv: []string{"glab", "ic", "Costs $5 now", "--description", "x"},
160 },
161 wantExpanded: []string{"issue", "create", "--title", "Costs $5 now", "--description", "x"},
162 wantIsShell: false,
163 wantErr: nil,
164 },
165 {
166 name: "placeholder text in argument is not filled by another argument",
167 args: args{
168 config: cfg,
169 argv: []string{"glab", "il", "$2", "bug"},
170 },
171 wantExpanded: []string{"issue", "list", "--author=$2", "--label=bug"},
172 wantIsShell: false,
173 wantErr: nil,
174 },
175 {
176 name: "quotes in argument are kept",
177 args: args{
178 config: cfg,
179 argv: []string{"glab", "ic", `Fix "login" page`},
180 },
181 wantExpanded: []string{"issue", "create", "--title", `Fix "login" page`},
182 wantIsShell: false,
183 wantErr: nil,
184 },
185 {
186 name: "argument with spaces in an unquoted placeholder stays one argument",
187 args: args{
188 config: cfg,
189 argv: []string{"glab", "is", "login page", "-R", "monalisa/testing"},
190 },
191 wantExpanded: []string{"issue", "list", "--search=login page", "-R", "monalisa/testing"},
192 wantIsShell: false,
193 wantErr: nil,
194 },
195 {
196 name: "argument starting with a hash in an unquoted placeholder is not a comment",
197 args: args{
198 config: cfg,
199 argv: []string{"glab", "iv", "#123"},
200 },
201 wantExpanded: []string{"issue", "view", "#123", "--comments"},
202 wantIsShell: false,
203 wantErr: nil,
204 },
205 {
206 name: "placeholders out of order keep the extra arguments",
207 args: args{
208 config: cfg,
209 argv: []string{"glab", "il2", "vilmibm", "epic", "-R", "monalisa/testing"},
210 },
211 wantExpanded: []string{"issue", "list", "--label=epic", "--author=vilmibm", "-R", "monalisa/testing"},
212 wantIsShell: false,
213 wantErr: nil,
214 },
215 {
216 name: "dollar without placeholder keeps the extra arguments",
217 args: args{
218 config: cfg,
219 argv: []string{"glab", "ip", "-R", "monalisa/testing"},
220 },
221 wantExpanded: []string{"issue", "list", "--search=price$", "-R", "monalisa/testing"},
222 wantIsShell: false,
223 wantErr: nil,
224 },
225 {
226 name: "dollar zero is not a placeholder",
227 args: args{
228 config: cfg,
229 argv: []string{"glab", "i0"},
230 },
231 wantExpanded: []string{"issue", "list", "--search=$0"},
232 wantIsShell: false,
233 wantErr: nil,
234 },
235 {
236 name: "two-digit placeholder",
237 args: args{
238 config: cfg,
239 argv: []string{"glab", "ten", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"},
240 },
241 wantExpanded: []string{"issue", "list", "--label=one", "--search=ten"},
242 wantIsShell: false,
243 wantErr: nil,
244 },
245 {
246 name: "not enough arguments for a two-digit placeholder",
247 args: args{
248 config: cfg,
249 argv: []string{"glab", "ten", "one"},
250 },
251 wantExpanded: []string{},
252 wantIsShell: false,
253 wantErr: errors.New(`not enough arguments for alias: issue list --label="one" --search="$10"`),
254 },
255 }
256 for _, tt := range tests {
257 t.Run(tt.name, func(t *testing.T) {
258 gotExpanded, gotIsShell, err := ExpandAlias(tt.args.config, tt.args.argv, findShFunc)
259 if tt.wantErr != nil {
260 if err == nil {
261 t.Fatal("expected error")
262 }
263 if tt.wantErr.Error() != err.Error() {
264 t.Fatalf("expected error %q, got %q", tt.wantErr, err)
265 }
266 return
267 }
268 if err != nil {
269 t.Fatalf("got error: %v", err)
270 }
271 if !reflect.DeepEqual(gotExpanded, tt.wantExpanded) {
272 t.Errorf("ExpandAlias() gotExpanded = %v, want %v", gotExpanded, tt.wantExpanded)
273 }
274 if gotIsShell != tt.wantIsShell {
275 t.Errorf("ExpandAlias() gotIsShell = %v, want %v", gotIsShell, tt.wantIsShell)
276 }
277 })
278 }
Diagnostics8 git calls · 266 ms
CallStatusSizeTime0266 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 KiB · hit2 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=HIT
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=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=44
Open pack map
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: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=4
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200256 B · hit4 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=256
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=9b63d874f7ad9ca8ff5a684fa9f649afab76eddc
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=256
git.pkt.payload.max=229
git.sideband.band1.bytes=229
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=229
git.pack.trailer=21020800d0a123456c692912cb576372452d51ba
Open pack map
fetchtree:0200144 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=251
git.response.bytes=144
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=53a7f35a9eb83c12a89d7c45b3e70a8b0196e587
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=144
git.pkt.payload.max=117
git.sideband.band1.bytes=117
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=117
git.pack.trailer=677c523b92315f5070acf708f5e16db1df8368f8
Open pack map
fetch2001.5 KiB251 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1493
git.response.cache=MISS
git.response.duration_ms=251
git.fetch.wants=3013a9bf18a9bd341cb64bcc5803a26c477bb392
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=1493
git.pkt.payload.max=1466
git.sideband.band1.bytes=1466
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
a5e2e1f43c274af4
Request
cc42ab0b…
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