GitLabgitlab-org
cli
main
1//go:build !integration
3package iostreams
5import (
6 "bytes"
7 "fmt"
8 "os"
9 "strings"
10 "testing"
12 "github.com/charmbracelet/colorprofile"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15)
17func Test_HelperFunctions(t *testing.T) {
18 // Base ios object that is modified as required
19 ios := &IOStreams{
20 In: os.Stdin,
21 StdOut: NewColorable(os.Stdout),
22 StdErr: NewColorable(os.Stderr),
24 IsaTTY: IsTerminal(os.Stdout),
25 IsErrTTY: IsTerminal(os.Stderr),
26 IsInTTY: IsTerminal(os.Stdin),
27 promptDisabled: false,
29 pagerCommand: os.Getenv("PAGER"),
30 }
32 t.Run("InitIOStream()", func(t *testing.T) {
33 t.Run("PAGER=", func(t *testing.T) {
34 t.Setenv("PAGER", "")
36 got := New(
37 WithStdin(os.Stdin, IsTerminal(os.Stdin)),
38 WithStdout(NewColorable(os.Stdout), IsTerminal(os.Stdout)),
39 WithStderr(NewColorable(os.Stderr), IsTerminal(os.Stderr)),
40 WithPagerCommand(PagerCommandFromEnv()),
41 )
43 assert.Equal(t, ios.In, got.In)
44 assert.Equal(t, ios.IsaTTY, got.IsaTTY)
45 assert.Equal(t, ios.IsErrTTY, got.IsErrTTY)
46 assert.Equal(t, ios.IsInTTY, got.IsInTTY)
47 assert.Equal(t, ios.promptDisabled, got.promptDisabled)
48 assert.Empty(t, got.pagerCommand)
49 })
50 t.Run("GLAB_PAGER=", func(t *testing.T) {
51 t.Setenv("GLAB_PAGER", "more")
53 got := New(
54 WithStdin(os.Stdin, IsTerminal(os.Stdin)),
55 WithStdout(NewColorable(os.Stdout), IsTerminal(os.Stdout)),
56 WithStderr(NewColorable(os.Stderr), IsTerminal(os.Stderr)),
57 WithPagerCommand(PagerCommandFromEnv()),
58 )
60 assert.Equal(t, ios.In, got.In)
61 assert.Equal(t, ios.IsaTTY, got.IsaTTY)
62 assert.Equal(t, ios.IsErrTTY, got.IsErrTTY)
63 assert.Equal(t, ios.IsInTTY, got.IsInTTY)
64 assert.Equal(t, ios.promptDisabled, got.promptDisabled)
65 assert.Equal(t, "more", got.pagerCommand)
66 })
67 })
69 t.Run("IsOutputTTY()", func(t *testing.T) {
70 t.Run("true", func(t *testing.T) {
71 ios := *ios
73 ios.IsaTTY = true
74 ios.IsErrTTY = true
76 got := ios.IsOutputTTY()
77 assert.True(t, got)
78 })
79 t.Run("false", func(t *testing.T) {
80 t.Run("IsaTTY=false", func(t *testing.T) {
81 ios := *ios
83 ios.IsaTTY = false
84 ios.IsErrTTY = true
86 got := ios.IsOutputTTY()
87 assert.False(t, got)
88 })
89 t.Run("IsErrTTY=false", func(t *testing.T) {
90 ios := *ios
92 ios.IsaTTY = true
93 ios.IsErrTTY = false
95 got := ios.IsOutputTTY()
96 assert.False(t, got)
97 })
98 })
99 })
101 t.Run("SetPager()", func(t *testing.T) {
102 t.Run("more", func(t *testing.T) {
103 ios := *ios
104 ios.SetPager("more")
105 assert.Equal(t, "more", ios.pagerCommand)
106 })
107 })
109 t.Run("PromptEnabled()", func(t *testing.T) {
110 t.Run("true", func(t *testing.T) {
111 var got bool
112 ios := *ios
114 ios.promptDisabled = false
115 ios.IsaTTY = true
116 ios.IsErrTTY = true
118 got = ios.PromptEnabled()
119 assert.True(t, got)
120 })
122 t.Run("false", func(t *testing.T) {
123 t.Run("promptDisabled=true", func(t *testing.T) {
124 var got bool
125 ios := *ios
127 ios.promptDisabled = true
128 got = ios.PromptEnabled()
129 assert.False(t, got)
130 })
132 t.Run("IsaTTY=false", func(t *testing.T) {
133 var got bool
134 ios := *ios
136 ios.IsaTTY = false
137 got = ios.PromptEnabled()
138 assert.False(t, got)
139 })
141 t.Run("IsErrTTY=true", func(t *testing.T) {
142 var got bool
143 ios := *ios
145 ios.IsErrTTY = false
146 got = ios.PromptEnabled()
147 assert.False(t, got)
148 })
149 })
150 })
152 t.Run("SetPrompt()", func(t *testing.T) {
153 t.Run("disabled", func(t *testing.T) {
154 t.Run("true", func(t *testing.T) {
155 ios := *ios
156 ios.SetPrompt("true")
157 assert.True(t, ios.promptDisabled)
158 })
159 t.Run("1", func(t *testing.T) {
160 ios := *ios
161 ios.SetPrompt("1")
162 assert.True(t, ios.promptDisabled)
163 })
164 })
165 t.Run("enabled", func(t *testing.T) {
166 t.Run("false", func(t *testing.T) {
167 ios := *ios
168 ios.SetPrompt("false")
169 assert.False(t, ios.promptDisabled)
170 })
171 t.Run("0", func(t *testing.T) {
172 ios := *ios
173 ios.SetPrompt("0")
174 assert.False(t, ios.promptDisabled)
175 })
176 })
177 })
178}
180func Test_stripControlCharacters(t *testing.T) {
181 type args struct {
182 badString string
183 }
184 tests := []struct {
185 name string
186 args args
187 want string
188 }{
189 {
190 name: "With moving 2 lines up",
191 args: args{
192 badString: "echo evil!" + //
193 "exit 0" + //
194 "[2Aecho Hello World!",
195 },
196 want: "echo evil!exit 0^[[2Aecho Hello World!",
197 },
198 {
199 name: "With obfuscating characters",
200 args: args{
201 badString: "echo evil!" + //
202 "exit 0" + //
203 "[2;0;0;5;3;2;Aecho Hello World!",
204 },
205 want: "echo evil!exit 0^[[2;0;0;5;3;2;Aecho Hello World!",
206 },
207 {
208 name: "With clearing the screen",
209 args: args{
210 badString: "echo evil!" + //
211 "exit 0" + //
212 "[2Lecho Hello World!",
213 },
214 want: "echo evil!exit 0^[[2Lecho Hello World!",
215 },
216 {
217 name: "control character with empty parameters",
218 args: args{
219 badString: "[2;;;Aecho Hello World!",
220 },
221 want: "^[[2;;;Aecho Hello World!",
222 },
223 {
224 name: "With colors",
225 args: args{
226 badString: "\033[0;30mSome text",
227 },
228 want: "\x1b[0;30mSome text",
229 },
230 }
232 for _, tt := range tests {
233 t.Run(tt.name, func(t *testing.T) {
234 got := stripControlCharacters(tt.args.badString)
235 assert.Equal(t, tt.want, got)
236 })
237 }
238}
240// A PAGER holding only whitespace is not a runnable command. shlex.Split
241// returns no arguments for it, so building the command indexed into an empty
242// slice and panicked. Treat it the same as an unset pager.
243func TestStartPager_BlankCommand(t *testing.T) {
244 for _, pager := range []string{" ", "\t", "\n", " \t\n "} {
245 t.Run(fmt.Sprintf("%q", pager), func(t *testing.T) {
246 ios := New(WithPagerCommand(pager))
247 ios.IsaTTY = true
248 originalStdOut := ios.StdOut
250 err := ios.StartPager()
252 require.NoError(t, err)
253 assert.Equal(t, originalStdOut, ios.StdOut, "no pager should have been started")
254 })
255 }
256}
258func TestWritePagerOutput_LongLine(t *testing.T) {
259 longLine := strings.Repeat("x", 70*1024)
260 var output bytes.Buffer
262 err := writePagerOutput(&output, strings.NewReader(longLine))
264 require.NoError(t, err)
265 assert.Equal(t, longLine+"\n", output.String())
266}
268func TestNew_ColorMode(t *testing.T) {
269 unsetColorEnv(t)
271 tests := []struct {
272 name string
273 mode ColorMode
274 wantColor bool
275 }{
276 {name: "unset defaults to auto detection", mode: "", wantColor: false},
277 {name: "auto behaves like unset", mode: ColorModeAuto, wantColor: false},
278 {name: "never forces color off", mode: ColorModeNever, wantColor: false},
279 {name: "always forces color on regardless of TTY", mode: ColorModeAlways, wantColor: true},
280 }
282 for _, tt := range tests {
283 t.Run(tt.name, func(t *testing.T) {
284 t.Setenv("TERM", "dumb")
285 t.Setenv("COLORTERM", "")
287 opts := []IOStreamsOption{}
288 if tt.mode != "" {
289 opts = append(opts, WithColorMode(tt.mode))
290 }
291 ios := New(opts...)
292 // New() without WithStdout/WithStderr leaves IsaTTY/IsErrTTY false,
293 // so "auto"/unset resolve to no color here; only "always" must
294 // override that to prove it forces color on.
296 assert.Equal(t, tt.wantColor, ios.ColorEnabled())
297 })
298 }
299}
301// ColorProfile is the single value every output path derives from: whether
302// color is on, and which tier it uses, are both read off it.
303func TestIOStreams_ColorProfile(t *testing.T) {
304 unsetColorEnv(t)
305 t.Setenv("TERM", "xterm-256color")
306 t.Setenv("COLORTERM", "")
308 t.Run("a non-TTY defaults to no color", func(t *testing.T) {
309 ios := New()
310 assert.Equal(t, colorprofile.NoTTY, ios.ColorProfile())
311 assert.False(t, ios.ColorEnabled())
312 })
314 t.Run("always colors a pipe at the terminal tier", func(t *testing.T) {
315 ios := New(WithColorMode(ColorModeAlways))
316 assert.Equal(t, colorprofile.ANSI256, ios.ColorProfile())
317 assert.True(t, ios.ColorEnabled())
318 })
320 t.Run("never wins on a TTY", func(t *testing.T) {
321 ios := New(WithColorMode(ColorModeNever))
322 ios.IsaTTY, ios.IsErrTTY = true, true
323 ios.SetColorMode(ColorModeNever)
324 assert.Equal(t, colorprofile.NoTTY, ios.ColorProfile())
325 assert.False(t, ios.ColorEnabled())
326 })
328 t.Run("SetColorMode re-resolves the profile", func(t *testing.T) {
329 ios := New()
330 require.Equal(t, colorprofile.NoTTY, ios.ColorProfile())
332 ios.SetColorMode(ColorModeAlways)
333 assert.Equal(t, colorprofile.ANSI256, ios.ColorProfile())
335 ios.SetColorMode(ColorModeNever)
336 assert.Equal(t, colorprofile.NoTTY, ios.ColorProfile())
337 })
339 t.Run("WithColorEnabled pins the profile for tests", func(t *testing.T) {
340 assert.Equal(t, colorprofile.ANSI256, New(WithColorEnabled(true)).ColorProfile())
341 assert.Equal(t, colorprofile.NoTTY, New(WithColorEnabled(false), WithColorMode(ColorModeAlways)).ColorProfile())
342 })
343}
345func TestIOStreams_Is256ColorSupported_IsATerminalCapability(t *testing.T) {
346 unsetColorEnv(t)
348 t.Setenv("TERM", "xterm-256color")
349 t.Setenv("COLORTERM", "")
350 assert.True(t, New().Is256ColorSupported(), "a 256-color terminal reports support even when color is off")
352 t.Setenv("TERM", "xterm-16color")
353 assert.False(t, New().Is256ColorSupported())
354}
356// Color-for-CI-logs colors a non-TTY, so it needs the terminal tier even
357// though the stdout profile is NoTTY there.
358func TestIOStreams_ColorForCILogs_KeepsTheTierOffATTY(t *testing.T) {
359 unsetColorEnv(t)
360 t.Setenv("TERM", "xterm-256color")
361 t.Setenv("COLORTERM", "")
362 t.Setenv("GITLAB_CI", "true")
364 ios := New()
365 require.Equal(t, colorprofile.NoTTY, ios.ColorProfile())
367 assert.Equal(t, "\x1b[38;5;242mtext\x1b[m", ios.ColorForCILogs().Gray("text"))
368}
370func TestIOStreams_SetColorMode(t *testing.T) {
371 unsetColorEnv(t)
372 t.Setenv("TERM", "dumb")
373 t.Setenv("COLORTERM", "")
375 ios := New()
376 require.False(t, ios.ColorEnabled(), "precondition: no TTY, no explicit mode")
378 ios.SetColorMode(ColorModeAlways)
379 assert.True(t, ios.ColorEnabled())
380 assert.Equal(t, ColorModeAlways, ios.ColorMode())
382 ios.SetColorMode(ColorModeNever)
383 assert.False(t, ios.ColorEnabled())
384 assert.Equal(t, ColorModeNever, ios.ColorMode())
385}
387func TestIOStreams_ColorMode_DefaultsToAuto(t *testing.T) {
388 ios := New()
389 assert.Equal(t, ColorModeAuto, ios.ColorMode())
390}
392func unsetColorEnv(t *testing.T) {
393 t.Helper()
394 for _, key := range []string{"NO_COLOR", "COLOR_ENABLED"} {
395 if val, ok := os.LookupEnv(key); ok {
396 os.Unsetenv(key)
397 t.Cleanup(func() { t.Setenv(key, val) })
398 }
399 }
400}
402func TestResolveBackgroundColor(t *testing.T) {
403 t.Run("explicit non-auto style is returned as-is", func(t *testing.T) {
404 t.Setenv("GLAB_GLAMOUR_STYLE", "")
405 t.Setenv("GLAMOUR_STYLE", "")
406 ios := New(WithColorMode(ColorModeAlways))
408 got := ios.ResolveBackgroundColor("notty")
410 assert.Equal(t, "notty", got)
411 assert.Equal(t, "notty", ios.BackgroundColor())
412 })
414 t.Run("GLAB_GLAMOUR_STYLE overrides a non-auto config style", func(t *testing.T) {
415 t.Setenv("GLAB_GLAMOUR_STYLE", "light")
416 t.Setenv("GLAMOUR_STYLE", "")
417 ios := New(WithColorMode(ColorModeAlways))
419 got := ios.ResolveBackgroundColor("dark")
421 assert.Equal(t, "light", got)
422 })
424 t.Run("never beats an explicit style", func(t *testing.T) {
425 t.Setenv("GLAB_GLAMOUR_STYLE", "")
426 t.Setenv("GLAMOUR_STYLE", "")
427 ios := New(WithColorMode(ColorModeNever))
429 assert.Equal(t, "none", ios.ResolveBackgroundColor("dark"))
430 assert.Equal(t, "none", ios.BackgroundColor())
431 })
433 t.Run("never beats a style from the environment", func(t *testing.T) {
434 t.Setenv("GLAB_GLAMOUR_STYLE", "dark")
435 t.Setenv("GLAMOUR_STYLE", "")
436 ios := New(WithColorMode(ColorModeNever))
438 assert.Equal(t, "none", ios.ResolveBackgroundColor("light"))
439 })
441 t.Run("auto with color disabled resolves to none", func(t *testing.T) {
442 t.Setenv("GLAB_GLAMOUR_STYLE", "")
443 t.Setenv("GLAMOUR_STYLE", "")
444 ios := New(WithColorMode(ColorModeNever))
446 got := ios.ResolveBackgroundColor("auto")
448 assert.Equal(t, "none", got)
449 })
451 t.Run("auto with color enabled reaches real background detection", func(t *testing.T) {
452 t.Setenv("GLAB_GLAMOUR_STYLE", "")
453 t.Setenv("GLAMOUR_STYLE", "")
454 ios := New(WithColorMode(ColorModeAlways))
456 got := ios.ResolveBackgroundColor("auto")
458 // Background detection depends on the environment, so only check that
459 // "auto" resolves to a real value.
460 assert.Contains(t, []string{"dark", "light"}, got)
461 })
462}
Diagnostics6 git calls · 60 ms
CallStatusSizeTime060 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 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=3 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
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: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=2 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=44Open pack map
fetchtree:0200432 B · 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=432 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=a79e8e89fbedf0d57724da455f6326f6e79de3b5 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=432 git.pkt.payload.max=405 git.sideband.band1.bytes=405 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=405 git.pack.trailer=6b5f558fa7a37603ce66aafb676f53aedcb02370Open pack map
fetch2002.9 KiB · hit53 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=53 git.response.bytes=2960 git.response.cache=HIT git.response.duration_ms=53 git.fetch.wants=47cb571640ffa09c5d171f77e40647956825cec3 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=2960 git.pkt.payload.max=2933 git.sideband.band1.bytes=2933 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
- a5e2c3a28840aedd
- Request
- 59aba32a…
- 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