GitLabgitlab-org

cli

main
ply6 git calls30 ms99 KiB4 cachedprotocol v2
192 lines7.2 KiBRaw
1//go:build !integration
3package commands
5import (
6 "reflect"
7 "strings"
8 "testing"
10 "github.com/spf13/cobra"
11 "github.com/spf13/pflag"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "gitlab.com/gitlab-org/cli/internal/api"
16 "gitlab.com/gitlab-org/cli/internal/cmdutils"
17 "gitlab.com/gitlab-org/cli/internal/commands/help"
18 "gitlab.com/gitlab-org/cli/internal/config"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22func TestMain(m *testing.M) {
23 cmdtest.InitTest(m, "")
26func TestRootVersion(t *testing.T) {
27 ios, _, stdout, _ := cmdtest.TestIOStreams()
28 rootCmd := NewCmdRoot(cmdutils.NewFactory(ios, false, config.NewBlankConfig(), api.BuildInfo{Version: "v1.0.0", Commit: "abcdefgh"}))
29 rootCmd.SetOut(stdout)
30 assert.NoError(t, rootCmd.Flag("version").Value.Set("true"))
31 require.NoError(t, rootCmd.Execute())
33 assert.Equal(t, "glab 1.0.0 (abcdefgh)\n", stdout.String())
36func TestRootNoArg(t *testing.T) {
37 ios, _, stdout, _ := cmdtest.TestIOStreams()
38 rootCmd := NewCmdRoot(cmdutils.NewFactory(ios, false, config.NewBlankConfig(), api.BuildInfo{Version: "v1.0.0", Commit: "abcdefgh"}))
39 require.NoError(t, rootCmd.Execute())
41 assert.Contains(t, stdout.String(), "GLab is an open source GitLab CLI tool that brings GitLab to your command line.\n")
42 assert.Contains(t, stdout.String(), `USAGE
43 glab <command> <subcommand> [flags]
45CORE COMMANDS`)
48// Regression test for gitlab-org/cli#8371: pflag/cobra deprecation warnings
49// must not land on stdout. We deliberately do NOT call rootCmd.SetOut in the
50// test — cobra's c.Print falls back to os.Stderr in production, and we want
51// the test to fail if anyone re-wires rootCmd.Out to f.IO().StdOut (in which
52// case the deprecation would land in the captured stdout buffer below).
53// Positive "appears on stderr" coverage is left to manual spot-checks since
54// we cannot capture os.Stderr from cmdtest without heavier plumbing.
55func TestDeprecationWarningStaysOffStdOut(t *testing.T) {
56 ios, _, stdout, _ := cmdtest.TestIOStreams()
57 rootCmd := NewCmdRoot(cmdutils.NewFactory(ios, false, config.NewBlankConfig(), api.BuildInfo{Version: "v1.0.0", Commit: "abcdefgh"}))
59 child := &cobra.Command{
60 Use: "deprecation-test-child",
61 RunE: func(cmd *cobra.Command, args []string) error { return nil },
62 }
63 child.Flags().Bool("old", false, "old flag")
64 require.NoError(t, child.Flags().MarkDeprecated("old", "use --new instead"))
65 rootCmd.AddCommand(child)
67 rootCmd.SetArgs([]string{"deprecation-test-child", "--old"})
68 require.NoError(t, rootCmd.Execute())
70 assert.NotContains(t, stdout.String(), "deprecated", "deprecation warning leaked to stdout")
73// NewCmdRoot must not wire cobra's Out to f.IO().StdOut. Doing so routes
74// every diagnostic message cobra prints (deprecation warnings, "unknown help
75// topic", usage-on-error) onto the stdout data channel — see
76// gitlab-org/cli#8371 and spf13/cobra#1708.
77func TestRootDoesNotWireCobraOutToStdOut(t *testing.T) {
78 ios, _, stdout, _ := cmdtest.TestIOStreams()
79 rootCmd := NewCmdRoot(cmdutils.NewFactory(ios, false, config.NewBlankConfig(), api.BuildInfo{Version: "v1.0.0", Commit: "abcdefgh"}))
81 // OutOrStderr returns cmd.Out if set, otherwise os.Stderr.
82 // We assert it is NOT the StdOut buffer the factory was built with.
83 got := rootCmd.OutOrStderr()
84 assert.NotSame(t, stdout, got, "rootCmd.Out must not be wired to StdOut")
87// TestAllDeprecatedFlagsRouteToStderr walks the entire registered command
88// tree, finds every deprecated flag, and exercises each one through
89// ParseFlags. It asserts no deprecation message leaks to the stdout channel.
90//
91// This is the auto-scaling regression net for gitlab-org/cli#8371: any
92// deprecated flag added in the future is exercised automatically. If anyone
93// re-wires rootCmd.SetOut to f.IO().StdOut, the deprecation warning would
94// land in the captured stdout buffer below and this test fails.
95//
96// We deliberately do NOT override rootCmd.SetOut in the test. In production
97// cobra's c.Print falls back to os.Stderr when Out is unset, and that is the
98// behavior we want to verify. Capturing os.Stderr to assert positive
99// presence would require global state hackery; manual spot-checks cover
100// that side.
101func TestAllDeprecatedFlagsRouteToStderr(t *testing.T) {
102 ios, _, stdout, _ := cmdtest.TestIOStreams()
103 rootCmd := NewCmdRoot(cmdutils.NewFactory(ios, false, config.NewBlankConfig(), api.BuildInfo{Version: "v1.0.0", Commit: "abcdefgh"}))
105 type target struct {
106 cmd *cobra.Command
107 flag *pflag.Flag
108 }
109 var targets []target
111 var walk func(*cobra.Command)
112 walk = func(c *cobra.Command) {
113 c.LocalFlags().VisitAll(func(f *pflag.Flag) {
114 if f.Deprecated != "" {
115 targets = append(targets, target{c, f})
116 }
117 })
118 for _, sub := range c.Commands() {
119 walk(sub)
120 }
121 }
122 walk(rootCmd)
124 require.NotEmpty(t, targets, "expected to find deprecated flags in the command tree — did the walk break?")
126 for _, tg := range targets {
127 name := tg.cmd.CommandPath() + " --" + tg.flag.Name
128 t.Run(name, func(t *testing.T) {
129 stdout.Reset()
131 // Build a parseable arg. Bool flags don't need a value;
132 // non-bool flags accept their stringified DefValue.
133 arg := "--" + tg.flag.Name
134 if tg.flag.Value.Type() != "bool" {
135 arg = arg + "=" + tg.flag.DefValue
136 }
138 // ParseFlags triggers cobra's deprecation flush via c.Print().
139 // Ignore the error — required-positional-arg failures still let
140 // the deprecation message flush first.
141 _ = tg.cmd.ParseFlags([]string{arg})
143 assert.NotContains(t, stdout.String(), "deprecated", "deprecation warning leaked to stdout for %s", name)
144 })
145 }
148// AppendAnnotationSections replaces the help func of every direct child of
149// root, so one set there before main.go calls it would be lost silently.
150func TestTopLevelCommandsInheritRootHelpFunc(t *testing.T) {
151 ios, _, _, _ := cmdtest.TestIOStreams()
152 rootCmd := NewCmdRoot(cmdtest.NewTestFactory(ios))
153 rootHelp := reflect.ValueOf(rootCmd.HelpFunc()).Pointer()
155 for _, c := range rootCmd.Commands() {
156 assert.Equal(t, rootHelp, reflect.ValueOf(c.HelpFunc()).Pointer(),
157 "%s sets its own help func, which help.AppendAnnotationSections would replace. Set it on a subcommand instead, or make AppendAnnotationSections keep it.", c.CommandPath())
158 }
161// A command that sets its own help func must chain to its parent's, or its
162// help loses the sections that AppendAnnotationSections adds.
163func TestAnnotatedCommandHelpShowsAnnotationSections(t *testing.T) {
164 ios, _, stdout, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
165 rootCmd := NewCmdRoot(cmdtest.NewTestFactory(ios))
166 rootCmd.SetHelpFunc(func(*cobra.Command, []string) {})
167 rootCmd.SetOut(stdout)
168 help.AppendAnnotationSections(rootCmd, ios)
170 var annotated []*cobra.Command
171 var walk func(*cobra.Command)
172 walk = func(c *cobra.Command) {
173 for _, sub := range c.Commands() {
174 if len(help.AnnotationSections(sub)) > 0 {
175 annotated = append(annotated, sub)
176 }
177 walk(sub)
178 }
179 }
180 walk(rootCmd)
181 require.NotEmpty(t, annotated, "expected annotated commands in the command tree — did the walk break?")
183 for _, c := range annotated {
184 t.Run(c.CommandPath(), func(t *testing.T) {
185 stdout.Reset()
186 c.HelpFunc()(c, nil)
187 for _, s := range help.AnnotationSections(c) {
188 assert.Contains(t, stdout.String(), strings.ToUpper(s.Title))
189 }
190 })
191 }
Diagnostics6 git calls · 30 ms
CallStatusSizeTime030 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=4
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 · 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:0 · deepen 1020032 KiB · 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=32926
git.response.cache=HIT
git.response.duration_ms=4
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
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=21
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
fetch2002.5 KiB · hit21 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2573
git.response.cache=HIT
git.response.duration_ms=21
git.fetch.wants=48ea6295c63267f8ee249651d077bd4c273f4647
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=2573
git.pkt.payload.max=2546
git.sideband.band1.bytes=2546
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
a5e231431851a594
Request
bad39645…
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