GitLabgitlab-org

cli

main
ply8 git calls251 ms99 KiB6 cachedprotocol v2
240 lines7.0 KiBRaw
1//go:build !integration
3package set
5import (
6 "os"
7 "path/filepath"
8 "testing"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/config"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20// persistedAliases returns the aliases.yml contents written by a dir-backed
21// config, for tests that assert what the command persisted.
22func persistedAliases(t *testing.T, dir string) string {
23 t.Helper()
24 data, err := os.ReadFile(filepath.Join(dir, "aliases.yml"))
25 require.NoError(t, err)
26 return string(data)
29// newCmdSetWithFakeHierarchy creates NewCmdSet wrapped in a fake command hierarchy
30// needed for validCommand testing.
31func newCmdSetWithFakeHierarchy(f cmdutils.Factory) *cobra.Command {
32 cmd := NewCmdSet(f)
34 // fake command nesting structure needed for validCommand
35 rootCmd := &cobra.Command{}
36 rootCmd.AddCommand(cmd)
37 mrCmd := &cobra.Command{Use: "mr"}
38 mrCmd.AddCommand(&cobra.Command{Use: "checkout"})
39 mrCmd.AddCommand(&cobra.Command{Use: "rebase"})
40 rootCmd.AddCommand(mrCmd)
41 issueCmd := &cobra.Command{Use: "issue"}
42 issueCmd.AddCommand(&cobra.Command{Use: "list"})
43 rootCmd.AddCommand(issueCmd)
45 return rootCmd
48func TestAliasSet_glab_command(t *testing.T) {
49 cfg := config.NewFromString(``)
51 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
52 _, err := exec("set mr 'mr rebase'")
54 if assert.Error(t, err) {
55 assert.Equal(t, `could not create alias: "mr" is already a glab command`, err.Error())
56 }
59func TestAliasSet_empty_aliases(t *testing.T) {
60 dir := t.TempDir()
61 cfg := config.NewFromStringInDir(heredoc.Doc(`
62 aliases:
63 editor: vim
64 `), dir)
66 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
67 output, err := exec("set co 'mr checkout'")
68 if err != nil {
69 t.Fatalf("unexpected error: %s", err)
70 }
72 assert.Contains(t, output.Stderr(), "Added alias")
73 assert.Empty(t, output.String())
75 expected := `co: mr checkout
77 assert.Equal(t, expected, persistedAliases(t, dir))
80func TestAliasSet_existing_alias(t *testing.T) {
81 cfg := config.NewFromString(heredoc.Doc(`
82 aliases:
83 co: mr checkout
84 `))
86 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
87 output, err := exec("set co 'mr checkout -Rcool/repo'")
88 require.NoError(t, err)
90 assert.Regexp(t, "Changed alias.*co.*from.*mr checkout.*to.*mr checkout -Rcool/repo.", output.Stderr())
93func TestAliasSet_space_args(t *testing.T) {
94 dir := t.TempDir()
95 cfg := config.NewFromStringInDir(``, dir)
97 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
98 output, err := exec(`set il 'issue list -l "cool story"'`)
99 require.NoError(t, err)
101 assert.Regexp(t, `Adding alias for.*il.*issue list -l "cool story".`, output.Stderr())
103 assert.Regexp(t, `il: issue list -l "cool story"`, persistedAliases(t, dir))
106func TestAliasSet_arg_processing(t *testing.T) {
107 cases := []struct {
108 Cmd string
109 ExpectedOutputLine string
110 ExpectedConfigLine string
111 }{
112 {`il "issue list"`, "- Adding alias for.*il.*issue list", "il: issue list"},
114 {`iz 'issue list'`, "- Adding alias for.*iz.*issue list", "iz: issue list"},
116 {
117 `ii 'issue list --author="$1" --label="$2"'`,
118 `- Adding alias for.*ii.*issue list --author="\$1" --label="\$2"`,
119 `ii: issue list --author="\$1" --label="\$2"`,
120 },
122 {
123 `ix "issue list --author='\$1' --label='\$2'"`,
124 `- Adding alias for.*ix.*issue list --author='\$1' --label='\$2'`,
125 `ix: issue list --author='\$1' --label='\$2'`,
126 },
127 }
129 for _, c := range cases {
130 t.Run(c.Cmd, func(t *testing.T) {
131 dir := t.TempDir()
132 cfg := config.NewFromStringInDir(``, dir)
134 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
135 output, err := exec("set " + c.Cmd)
136 if err != nil {
137 t.Fatalf("got unexpected error running %s: %s", c.Cmd, err)
138 }
140 assert.Regexp(t, c.ExpectedOutputLine, output.Stderr())
141 assert.Regexp(t, c.ExpectedConfigLine, persistedAliases(t, dir))
142 })
143 }
146func TestAliasSet_init_alias_cfg(t *testing.T) {
147 dir := t.TempDir()
148 cfg := config.NewFromStringInDir(heredoc.Doc(`
149 editor: vim
150 `), dir)
152 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
153 output, err := exec("set diff 'mr diff'")
154 require.NoError(t, err)
156 expected := `diff: mr diff
159 assert.Regexp(t, "Adding alias for.*diff.*mr diff", output.Stderr())
160 assert.Contains(t, output.Stderr(), "Added alias.")
161 assert.Equal(t, expected, persistedAliases(t, dir))
164func TestAliasSet_existing_aliases(t *testing.T) {
165 dir := t.TempDir()
166 cfg := config.NewFromStringInDir(heredoc.Doc(`
167 aliases:
168 foo: bar
169 `), dir)
171 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
172 output, err := exec("set view 'mr view'")
173 require.NoError(t, err)
175 expected := `foo: bar
176view: mr view
179 assert.Regexp(t, "Adding alias for.*view.*mr view", output.Stderr())
180 assert.Contains(t, output.Stderr(), "Added alias.")
181 assert.Equal(t, expected, persistedAliases(t, dir))
184func TestAliasSet_invalid_command(t *testing.T) {
185 cfg := config.NewFromString(``)
187 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
188 _, err := exec("set co 'pe checkout'")
189 if assert.Error(t, err) {
190 assert.Equal(t, "could not create alias: pe checkout does not correspond to a glab command", err.Error())
191 }
194func TestShellAlias_flag(t *testing.T) {
195 dir := t.TempDir()
196 cfg := config.NewFromStringInDir(``, dir)
198 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
199 output, err := exec("set --shell igrep 'glab issue list | grep'")
200 if err != nil {
201 t.Fatalf("unexpected error: %s", err)
202 }
204 assert.Regexp(t, "Adding alias for.*igrep.", output.Stderr())
206 expected := `igrep: '!glab issue list | grep'
208 assert.Equal(t, expected, persistedAliases(t, dir))
211func TestShellAlias_bang(t *testing.T) {
212 dir := t.TempDir()
213 cfg := config.NewFromStringInDir(``, dir)
215 exec := cmdtest.SetupCmdForTest(t, newCmdSetWithFakeHierarchy, true, cmdtest.WithConfig(cfg))
216 output, err := exec("set igrep '!glab issue list | grep'")
217 require.NoError(t, err)
219 assert.Regexp(t, "Adding alias for.*igrep.", output.Stderr())
221 expected := `igrep: '!glab issue list | grep'
223 assert.Equal(t, expected, persistedAliases(t, dir))
226func TestAliasSetCompletion(t *testing.T) {
227 t.Parallel()
229 ios, _, _, _ := cmdtest.TestIOStreams()
230 cfg := config.NewFromString("aliases:\n co: mr checkout\n il: issue list\n")
231 cmd := NewCmdSet(cmdtest.NewTestFactory(ios, cmdtest.WithConfig(cfg)))
233 got, dir := cmd.ValidArgsFunction(cmd, nil, "")
234 assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, dir)
235 assert.Empty(t, got)
237 got, dir = cmd.ValidArgsFunction(cmd, []string{"co"}, "")
238 assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, dir)
239 assert.Empty(t, got)
Diagnostics8 git calls · 251 ms
CallStatusSizeTime0251 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · 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 · hit3 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=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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 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=5
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 · 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=1857
git.response.cache=HIT
git.response.duration_ms=4
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:0200140 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=233
git.response.bytes=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=b47d2886ac22d7dc6502e1d43cef71c40abf4fcd
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=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=a6def49bfc6de675d5709a7ab0a9f413d249fa57
Open pack map
fetch2001.7 KiB233 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1760
git.response.cache=MISS
git.response.duration_ms=233
git.fetch.wants=96342e6f6377b6dcf7511359c0cc7efbb10b11c6
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=1760
git.pkt.payload.max=1733
git.sideband.band1.bytes=1733
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
a5e27b7d79d0c892
Request
35dd6f32…
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