GitLabgitlab-org

cli

main
ply7 git calls37 ms99 KiB6 cachedprotocol v2
195 lines5.7 KiBRaw
1//go:build !integration
3package help
5import (
6 "bytes"
7 "strings"
8 "testing"
10 "charm.land/fang/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/commands/alias"
16 "gitlab.com/gitlab-org/cli/internal/commands/alias/set"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20func TestDedent(t *testing.T) {
21 type c struct {
22 input string
23 expected string
24 }
26 cases := []c{
27 {
28 input: " --help Show help for command.\n --version Show glab version\n",
29 expected: "--help Show help for command.\n--version Show glab version\n",
30 },
31 {
32 input: " --help Show help for command.\n -R, --repo OWNER/REPO Select another repository using the OWNER/REPO format\n",
33 expected: " --help Show help for command.\n-R, --repo OWNER/REPO Select another repository using the OWNER/REPO format\n",
34 },
35 {
36 input: " line 1\n\n line 2\n line 3",
37 expected: " line 1\n\n line 2\nline 3",
38 },
39 {
40 input: " line 1\n line 2\n line 3\n\n",
41 expected: "line 1\nline 2\nline 3\n\n",
42 },
43 {
44 input: "\n\n\n\n\n\n",
45 expected: "\n\n\n\n\n\n",
46 },
47 {
48 input: "",
49 expected: "",
50 },
51 }
53 for _, tt := range cases {
54 got := dedent(tt.input)
55 if got != tt.expected {
56 t.Errorf("expected: %q, got: %q", tt.expected, got)
57 }
58 }
61func TestRootHelpFunc(t *testing.T) {
62 type args struct {
63 command *cobra.Command
64 args []string
65 }
66 tests := []struct {
67 name string
68 args args
69 wantOut string
70 }{
71 {
72 name: "alias",
73 args: args{
74 command: alias.NewCmdAlias(cmdtest.NewTestFactory(nil)),
75 },
76 wantOut: "Aliases are shortcuts for longer glab commands. Use aliases to save\n keystrokes for commands you run often, or to compose shell pipelines\n around glab commands.\n\nUSAGE\n alias [command] [flags]\n\nCORE COMMANDS\n delete: Delete an alias.\n list: List aliases.\n set: Set an alias for a longer command.",
77 },
79 {
80 name: "test nested alias cmd",
81 args: args{
82 command: set.NewCmdSet(cmdtest.NewTestFactory(nil)),
83 args: []string{"set", "-h"},
84 },
85 wantOut: "USAGE\n alias set <alias name> '<command>' [flags]\n\nFLAGS\n -s, --shell ",
86 },
87 }
88 for _, tt := range tests {
89 t.Run(tt.name, func(t *testing.T) {
90 streams, _, stdout, _ := cmdtest.TestIOStreams()
91 cmd := tt.args.command
92 if len(tt.args.args) > 0 {
93 // falsify a parent command
94 alias.NewCmdAlias(cmdtest.NewTestFactory(streams)).AddCommand(cmd)
95 }
96 RootHelpFunc(streams, cmd, tt.args.args)
98 output := stdout.String()
99 // Normalize whitespace for comparison
100 normalizedOutput := strings.Join(strings.Fields(output), " ")
101 normalizedWantOut := strings.Join(strings.Fields(tt.wantOut), " ")
102 assert.Contains(t, normalizedOutput, normalizedWantOut)
103 })
104 }
107func TestRootHelpFuncAnnotationSectionsMatchAnnotationSections(t *testing.T) {
108 streams, _, stdout, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
109 cmd := &cobra.Command{
110 Use: "child",
111 Short: "Child command.",
112 Example: "glab child",
113 Run: func(*cobra.Command, []string) {},
114 Annotations: map[string]string{
115 "help:arguments": "\nA child can be supplied by number.\n",
116 "help:environment": "",
117 },
118 }
120 RootHelpFunc(streams, cmd, nil)
122 out := stdout.String()
123 assert.Contains(t, out, "ARGUMENTS\n A child can be supplied by number.\n")
124 assert.NotContains(t, out, "ENVIRONMENT VARIABLES")
127func TestRootUsageFunc(t *testing.T) {
128 type args struct {
129 command *cobra.Command
130 }
131 tests := []struct {
132 name string
133 args args
134 wantErr bool
135 }{
136 {
137 args: args{
138 command: alias.NewCmdAlias(cmdtest.NewTestFactory(nil)),
139 },
140 wantErr: false,
141 },
142 }
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 var out bytes.Buffer
146 if err := RootUsageFunc(&out, tt.args.command); (err != nil) != tt.wantErr {
147 t.Errorf("RootUsageFunc() error = %v, wantErr %v", err, tt.wantErr)
148 }
149 assert.Contains(t, out.String(), "Usage: alias [command] [flags]")
150 })
151 }
154func TestAppendAnnotationSectionsFollowFangHelp(t *testing.T) {
155 streams, _, streamsOut, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
156 root := &cobra.Command{Use: "glab"}
157 child := &cobra.Command{
158 Use: "child",
159 Short: "Child command.",
160 Run: func(*cobra.Command, []string) {},
161 Annotations: map[string]string{
162 "help:arguments": "A child can be supplied by number.\n",
163 "help:environment": "CHILD_VAR: a variable the child reads.\n",
164 },
165 }
166 grandchild := &cobra.Command{Use: "grandchild", Short: "Grandchild command.", Run: func(*cobra.Command, []string) {}}
167 child.AddCommand(grandchild)
168 root.AddCommand(child)
169 var helpOut bytes.Buffer
170 root.SetOut(&helpOut)
171 AppendAnnotationSections(root, streams)
173 execute := func(args ...string) string {
174 helpOut.Reset()
175 root.SetArgs(args)
176 require.NoError(t, fang.Execute(t.Context(), root, fang.WithoutCompletions(), fang.WithoutManpage(), fang.WithoutVersion()))
177 assert.Empty(t, streamsOut.String(), "help must follow the command's output writer, not IOStreams")
178 return helpOut.String()
179 }
181 out := execute("child", "--help")
182 usage := strings.Index(out, "USAGE")
183 arguments := strings.Index(out, "ARGUMENTS\n\n A child can be supplied by number.\n")
184 environment := strings.Index(out, "ENVIRONMENT VARIABLES\n\n CHILD_VAR: a variable the child reads.\n")
185 require.NotEqual(t, -1, usage, out)
186 require.NotEqual(t, -1, arguments, out)
187 require.NotEqual(t, -1, environment, out)
188 assert.Less(t, usage, arguments)
189 assert.Less(t, arguments, environment)
191 out = execute("child", "grandchild", "--help")
192 assert.Contains(t, out, "USAGE")
193 assert.NotContains(t, out, "ENVIRONMENT VARIABLES")
194 assert.NotContains(t, out, "ARGUMENTS")
Diagnostics7 git calls · 37 ms
CallStatusSizeTime037 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=2
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=44
Open 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 · 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:0200172 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=172
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=9cf14981aa55bb2b8bcfc2f2549219dc92956502
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=172
git.pkt.payload.max=145
git.sideband.band1.bytes=145
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=145
git.pack.trailer=aa21db2332923792c8da6536bfb011ad61d33c6c
Open pack map
fetch2001.9 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=1913
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=a4afae7dbdca2c52217468a3eab05928318bd410
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=1913
git.pkt.payload.max=1886
git.sideband.band1.bytes=1886
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
a5e27f873fc10555
Request
3f038ef0…
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