GitLabgitlab-org
cli
main
1//go:build !integration
3package cmdutils
5import (
6 "errors"
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
13 "github.com/spf13/cobra"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18)
20// newDescriptionCmd builds a command with the same flag pair the real commands
21// register: a bound --description plus --description-file.
22func newDescriptionCmd(description *string) *cobra.Command {
23 cmd := &cobra.Command{Use: "test", RunE: func(*cobra.Command, []string) error { return nil }}
24 cmd.Flags().StringVarP(description, "description", "d", "", "Description.")
25 AddDescriptionFileFlag(cmd, "issue")
26 return cmd
27}
29func TestAddDescriptionFileFlag(t *testing.T) {
30 t.Parallel()
32 var description string
33 cmd := newDescriptionCmd(&description)
35 flag := cmd.Flags().Lookup("description-file")
36 require.NotNil(t, flag)
37 assert.Empty(t, flag.Shorthand, "--description-file must not take a short flag")
38 assert.Equal(t, `Read the issue description from a file. Use "-" to read from standard input.`, flag.Usage)
39}
41func TestAddDescriptionFileFlag_MutuallyExclusiveWithDescription(t *testing.T) {
42 t.Parallel()
44 var description string
45 cmd := newDescriptionCmd(&description)
46 cmd.SetArgs([]string{"--description", "inline", "--description-file", "some.md"})
47 cmd.SetOut(io.Discard)
48 cmd.SetErr(io.Discard)
50 err := cmd.Execute()
52 require.Error(t, err)
53 assert.Contains(t, err.Error(), "[description description-file]")
54}
56func TestResolveDescriptionFile(t *testing.T) {
57 t.Parallel()
59 const body = "# Heading\n\nA body with \"quotes\", $VARS and `backticks`.\n"
61 tests := []struct {
62 name string
63 fileContent string
64 stdin string
65 useStdin bool
66 want string
67 }{
68 {
69 name: "reads a multi-line file verbatim",
70 fileContent: body,
71 want: body,
72 },
73 {
74 name: "reads an empty file",
75 fileContent: "",
76 want: "",
77 },
78 {
79 name: "reads from standard input",
80 stdin: body,
81 useStdin: true,
82 want: body,
83 },
84 }
86 for _, tc := range tests {
87 t.Run(tc.name, func(t *testing.T) {
88 t.Parallel()
90 ios := testIOStreamsWithStdin(tc.stdin)
92 var description string
93 cmd := newDescriptionCmd(&description)
95 path := "-"
96 if !tc.useStdin {
97 path = filepath.Join(t.TempDir(), "description.md")
98 require.NoError(t, os.WriteFile(path, []byte(tc.fileContent), 0o600))
99 }
100 require.NoError(t, cmd.ParseFlags([]string{"--description-file", path}))
102 require.NoError(t, ResolveDescriptionFile(ios, cmd))
104 // The file content lands on --description, so commands only read one flag.
105 assert.Equal(t, tc.want, description)
106 got, err := cmd.Flags().GetString("description")
107 require.NoError(t, err)
108 assert.Equal(t, tc.want, got)
110 // Callers gate on Changed("description"), so it must report the file too.
111 assert.True(t, cmd.Flags().Changed("description"))
112 })
113 }
114}
116func TestResolveDescriptionFile_NoFlagIsNoOp(t *testing.T) {
117 t.Parallel()
119 ios := testIOStreamsWithStdin("")
121 var description string
122 cmd := newDescriptionCmd(&description)
123 require.NoError(t, cmd.ParseFlags([]string{"--description", "inline"}))
125 require.NoError(t, ResolveDescriptionFile(ios, cmd))
127 assert.Equal(t, "inline", description, "an inline description must not be overwritten")
128}
130func TestResolveDescriptionFile_MissingFile(t *testing.T) {
131 t.Parallel()
133 ios := testIOStreamsWithStdin("")
135 var description string
136 cmd := newDescriptionCmd(&description)
137 missing := filepath.Join(t.TempDir(), "absent.md")
138 require.NoError(t, cmd.ParseFlags([]string{"--description-file", missing}))
140 err := ResolveDescriptionFile(ios, cmd)
142 require.Error(t, err)
143 assert.Contains(t, err.Error(), "failed to read description file")
144 assert.Empty(t, description)
145}
147func TestResolveDescriptionFile_RejectsDashOnlyContent(t *testing.T) {
148 t.Parallel()
150 ios := testIOStreamsWithStdin("")
152 var description string
153 cmd := newDescriptionCmd(&description)
154 path := filepath.Join(t.TempDir(), "dash.md")
155 require.NoError(t, os.WriteFile(path, []byte("-"), 0o600))
156 require.NoError(t, cmd.ParseFlags([]string{"--description-file", path}))
158 err := ResolveDescriptionFile(ios, cmd)
160 require.Error(t, err)
161 assert.Contains(t, err.Error(), "which --description uses to open an editor")
162 assert.Empty(t, description, `a file of just "-" must not reach --description and open an editor`)
163}
165// failingCloser reads normally but fails to close, so the deferred cleanup error
166// has something to join.
167type failingCloser struct{ io.Reader }
169func (failingCloser) Close() error { return errors.New("close failed") }
171func TestResolveDescriptionFile_JoinsStdinCloseError(t *testing.T) {
172 t.Parallel()
174 ios := iostreams.New(
175 iostreams.WithStdin(failingCloser{strings.NewReader("a description")}, false),
176 iostreams.WithStdout(&strings.Builder{}, false),
177 iostreams.WithStderr(&strings.Builder{}, false),
178 )
180 var description string
181 cmd := newDescriptionCmd(&description)
182 require.NoError(t, cmd.ParseFlags([]string{"--description-file", "-"}))
184 err := ResolveDescriptionFile(ios, cmd)
186 require.Error(t, err, "a failed stdin close must not be swallowed")
187 assert.Contains(t, err.Error(), "close failed")
188}
190// testIOStreamsWithStdin is testIOStreams with readable stdin content, for the
191// --description-file "-" cases.
192func testIOStreamsWithStdin(stdin string) *iostreams.IOStreams {
193 var out, errOut strings.Builder
194 return iostreams.New(
195 iostreams.WithStdin(io.NopCloser(strings.NewReader(stdin)), false),
196 iostreams.WithStdout(&out, false),
197 iostreams.WithStderr(&errOut, false),
198 )
199}
Diagnostics6 git calls · 30 ms
CallStatusSizeTime030 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 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=2 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
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=4 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=44Open 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=5 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.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=2 git.response.bytes=1238 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17 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=1238 git.pkt.payload.max=1211 git.sideband.band1.bytes=1211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1211 git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetch2001.7 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=1741 git.response.cache=HIT git.response.duration_ms=20 git.fetch.wants=8da07952418ea145bbca42d7bd0a8833aed860d6 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=1741 git.pkt.payload.max=1714 git.sideband.band1.bytes=1714 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
- a5e1b1b7f974f54b
- Request
- 7e84366a…
- 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