GitLabgitlab-org

cli

main
ply6 git calls176 ms98 KiB4 cachedprotocol v2
218 lines8.3 KiBRaw
1//go:build !integration
3package iostreams
5import (
6 "image/color"
7 "os"
8 "testing"
10 "charm.land/lipgloss/v2"
11 "github.com/charmbracelet/colorprofile"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
16func Test_isColorEnabled(t *testing.T) {
17 t.Run("default", func(t *testing.T) {
18 for _, key := range []string{"NO_COLOR", "COLOR_ENABLED"} {
19 if val, ok := os.LookupEnv(key); ok {
20 os.Unsetenv(key)
21 t.Cleanup(func() { t.Setenv(key, val) })
22 }
23 }
25 got := detectIsColorEnabled()
26 assert.True(t, got)
27 })
29 t.Run("NO_COLOR", func(t *testing.T) {
30 t.Setenv("NO_COLOR", "")
32 got := detectIsColorEnabled()
33 assert.False(t, got)
34 })
36 t.Run("COLOR_ENABLED == 1", func(t *testing.T) {
37 t.Setenv("NO_COLOR", "")
38 t.Setenv("COLOR_ENABLED", "1")
40 got := detectIsColorEnabled()
41 assert.True(t, got)
42 })
44 t.Run("COLOR_ENABLED == true", func(t *testing.T) {
45 t.Setenv("NO_COLOR", "")
46 t.Setenv("COLOR_ENABLED", "true")
48 got := detectIsColorEnabled()
49 assert.True(t, got)
50 })
53func TestColorEnabledForCILogs(t *testing.T) {
54 tests := []struct {
55 name string
56 colorField bool // whether the stdout profile has color (TTY-based)
57 mode ColorMode
58 inCI bool
59 setNoColor bool
60 colorEnable string // COLOR_ENABLED value; "" means unset
61 want bool
62 }{
63 {name: "TTY color already enabled", colorField: true, want: true},
64 {name: "not a TTY and not CI", colorField: false, want: false},
65 {name: "not a TTY but in GitLab CI", colorField: false, inCI: true, want: true},
66 {name: "in CI but NO_COLOR set", colorField: false, inCI: true, setNoColor: true, want: false},
67 {name: "in CI, NO_COLOR set, COLOR_ENABLED override", colorField: false, inCI: true, setNoColor: true, colorEnable: "1", want: true},
68 {name: "in CI but color is never", colorField: false, mode: ColorModeNever, inCI: true, want: false},
69 {name: "never wins over COLOR_ENABLED in CI", colorField: false, mode: ColorModeNever, inCI: true, setNoColor: true, colorEnable: "1", want: false},
70 {name: "in CI with color always", colorField: true, mode: ColorModeAlways, inCI: true, want: true},
71 }
73 for _, tt := range tests {
74 t.Run(tt.name, func(t *testing.T) {
75 // Start from a clean slate: unset the vars this decision reads so
76 // a case that doesn't set one isn't affected by ambient values.
77 // The cases below set what they need via t.Setenv, which already
78 // registers its own restore cleanup.
79 unsetEnvForTest(t, "NO_COLOR", "COLOR_ENABLED", "GITLAB_CI")
80 if tt.inCI {
81 t.Setenv("GITLAB_CI", "true")
82 }
83 if tt.setNoColor {
84 t.Setenv("NO_COLOR", "")
85 }
86 if tt.colorEnable != "" {
87 t.Setenv("COLOR_ENABLED", tt.colorEnable)
88 }
90 s := &IOStreams{colorMode: tt.mode, colorProfile: profileIf(tt.colorField)}
91 assert.Equal(t, tt.want, s.ColorEnabledForCILogs())
92 })
93 }
96// unsetEnvForTest unsets env vars for the duration of the test and restores
97// their original values on cleanup. The testing package has no t.Unsetenv, and
98// t.Setenv can only set (not remove) a var, so the restore uses os.Setenv.
99func unsetEnvForTest(t *testing.T, keys ...string) {
100 t.Helper()
101 for _, key := range keys {
102 if val, ok := os.LookupEnv(key); ok {
103 os.Unsetenv(key)
104 t.Cleanup(func() { os.Setenv(key, val) }) //nolint:usetesting // restoring an unset var; t.Setenv can't remove/restore-absent
105 }
106 }
109func Test_makeColorFunc(t *testing.T) {
110 tests := []struct {
111 name string
112 ansiColorName string
113 brandColor color.Color
114 profile colorprofile.Profile
115 want string
116 }{
117 {name: "gray 16 colors", ansiColorName: "black+h", profile: colorprofile.ANSI, want: "\x1b[0;90mtext\x1b[0m"},
118 {name: "gray 256 colors", ansiColorName: "black+h", profile: colorprofile.ANSI256, want: "\x1b[38;5;242mtext\x1b[m"},
119 {name: "gray under truecolor uses the 256-color gray", ansiColorName: "black+h", profile: colorprofile.TrueColor, want: "\x1b[38;5;242mtext\x1b[m"},
120 {name: "no colors", ansiColorName: "black+h", profile: colorprofile.NoTTY, want: "text"},
121 {name: "ascii profile has no colors", ansiColorName: "green", brandColor: lipgloss.Color("#34D058"), profile: colorprofile.ASCII, want: "text"},
122 {name: "green when truecolor provided", ansiColorName: "green", brandColor: lipgloss.Color("#34D058"), profile: colorprofile.TrueColor, want: "\x1b[38;2;52;208;88mtext\x1b[m"},
123 {name: "green when truecolor provided, but no terminal support", ansiColorName: "green", brandColor: lipgloss.Color("#34D058"), profile: colorprofile.ANSI256, want: "\x1b[0;32mtext\x1b[0m"},
124 {name: "green when no truecolor in palette", ansiColorName: "green", profile: colorprofile.TrueColor, want: "\x1b[0;32mtext\x1b[0m"},
125 {name: "magenta 16 colors", ansiColorName: "magenta", profile: colorprofile.ANSI, want: "\x1b[0;35mtext\x1b[0m"},
126 {name: "cyan 16 colors", ansiColorName: "cyan", profile: colorprofile.ANSI, want: "\x1b[0;36mtext\x1b[0m"},
127 {name: "red 16 colors", ansiColorName: "red", profile: colorprofile.ANSI, want: "\x1b[0;31mtext\x1b[0m"},
128 {name: "yellow 16 colors", ansiColorName: "yellow", profile: colorprofile.ANSI, want: "\x1b[0;33mtext\x1b[0m"},
129 {name: "blue 16 colors", ansiColorName: "blue", profile: colorprofile.ANSI, want: "\x1b[0;34mtext\x1b[0m"},
130 {name: "default+b 16 colors", ansiColorName: "default+b", profile: colorprofile.ANSI, want: "\x1b[0;1;39mtext\x1b[0m"},
131 {name: "unknown name is left uncolored", ansiColorName: "nope", profile: colorprofile.ANSI, want: "text"},
132 }
134 for _, tt := range tests {
135 t.Run(tt.name, func(t *testing.T) {
136 require.Equal(t, tt.want, makeColorFunc(tt.profile, tt.brandColor, tt.ansiColorName)("text"))
137 })
138 }
141func Test_colorTier(t *testing.T) {
142 tests := []struct {
143 name string
144 term string
145 colorterm string
146 want colorprofile.Profile
147 }{
148 {name: "24bit TERM", term: "xterm-24bit", want: colorprofile.TrueColor},
149 {name: "truecolor TERM", term: "xterm-truecolor", want: colorprofile.TrueColor},
150 {name: "truecolor COLORTERM", term: "xterm", colorterm: "truecolor", want: colorprofile.TrueColor},
151 {name: "24bit COLORTERM", term: "xterm", colorterm: "24bit", want: colorprofile.TrueColor},
152 {name: "256color TERM", term: "xterm-256color", want: colorprofile.ANSI256},
153 {name: "256 COLORTERM", term: "xterm", colorterm: "256", want: colorprofile.ANSI256},
154 {name: "16 colors", term: "xterm-16color", want: colorprofile.ANSI},
155 {name: "dumb terminal still has a tier for forced color", term: "dumb", want: colorprofile.ANSI},
156 }
158 for _, tt := range tests {
159 t.Run(tt.name, func(t *testing.T) {
160 t.Setenv("TERM", tt.term)
161 t.Setenv("COLORTERM", tt.colorterm)
163 require.Equal(t, tt.want, colorTier())
164 })
165 }
168func Test_resolveColorProfile(t *testing.T) {
169 tests := []struct {
170 name string
171 mode ColorMode
172 isTTY bool
173 noColor string
174 colorEnv string
175 want colorprofile.Profile
176 }{
177 {name: "auto on a TTY takes the terminal tier", mode: ColorModeAuto, isTTY: true, want: colorprofile.ANSI256},
178 {name: "unset mode behaves like auto", mode: "", isTTY: true, want: colorprofile.ANSI256},
179 {name: "auto off a TTY is NoTTY", mode: ColorModeAuto, isTTY: false, want: colorprofile.NoTTY},
180 {name: "auto honors NO_COLOR", mode: ColorModeAuto, isTTY: true, noColor: "1", want: colorprofile.NoTTY},
181 {name: "COLOR_ENABLED overrides NO_COLOR under auto", mode: ColorModeAuto, isTTY: true, noColor: "1", colorEnv: "true", want: colorprofile.ANSI256},
182 {name: "never is NoTTY even on a TTY", mode: ColorModeNever, isTTY: true, want: colorprofile.NoTTY},
183 {name: "always colors a non-TTY", mode: ColorModeAlways, isTTY: false, want: colorprofile.ANSI256},
184 {name: "always beats NO_COLOR", mode: ColorModeAlways, isTTY: false, noColor: "1", want: colorprofile.ANSI256},
185 }
187 for _, tt := range tests {
188 t.Run(tt.name, func(t *testing.T) {
189 t.Setenv("TERM", "xterm-256color")
190 t.Setenv("COLORTERM", "")
191 unsetColorEnv(t)
192 if tt.noColor != "" {
193 t.Setenv("NO_COLOR", tt.noColor)
194 }
195 if tt.colorEnv != "" {
196 t.Setenv("COLOR_ENABLED", tt.colorEnv)
197 }
199 require.Equal(t, tt.want, resolveColorProfile(tt.mode, tt.isTTY))
200 })
201 }
204func TestColorMode_AllColorModes(t *testing.T) {
205 got := AllColorModes()
206 require.Equal(t, []ColorMode{
207 ColorModeAuto,
208 ColorModeNever,
209 ColorModeAlways,
210 }, got)
213func Test_makeColorFunc_emptyText(t *testing.T) {
214 t.Setenv("COLORTERM", "")
215 t.Setenv("TERM", "xterm-16color")
217 require.Empty(t, makeColorFunc(colorprofile.ANSI, nil, "green")(""))
Diagnostics6 git calls · 176 ms
CallStatusSizeTime0176 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=44
Open pack map
fetchtree:0200432 B · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=432
git.response.cache=HIT
git.response.duration_ms=1
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=6b5f558fa7a37603ce66aafb676f53aedcb02370
Open pack map
fetch2002.2 KiB170 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=170
git.response.bytes=2236
git.response.cache=MISS
git.response.duration_ms=170
git.fetch.wants=b9f07dfe14d52d812e696ff89a239cddce366330
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=2236
git.pkt.payload.max=2209
git.sideband.band1.bytes=2209
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
a5e2c303bd11aedd
Request
b3f86a01…
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