GitLabgitlab-org
cli
main
1//go:build !integration
3package summary
5import (
6 "strings"
7 "testing"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
12 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
13 "gitlab.com/gitlab-org/cli/internal/iostreams"
14 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
15 "gitlab.com/gitlab-org/cli/internal/text"
16)
18// coloredPalette / plainPalette build a palette with color forced on or off
19// via WithColorEnabled, so tests are deterministic and parallel-safe without
20// touching ambient TTY/CI/NO_COLOR env.
21func coloredPalette(t *testing.T) palette {
22 t.Helper()
23 buf := &strings.Builder{}
24 ios := iostreams.New(
25 iostreams.WithStdout(buf, true),
26 iostreams.WithStderr(buf, true),
27 iostreams.WithColorEnabled(true),
28 )
29 require.True(t, ios.ColorEnabled(), "WithColorEnabled(true) must force color on")
30 return palette{c: ios.Color()}
31}
33func plainPalette(t *testing.T) palette {
34 t.Helper()
35 buf := &strings.Builder{}
36 ios := iostreams.New(
37 iostreams.WithStdout(buf, false),
38 iostreams.WithStderr(buf, false),
39 iostreams.WithColorEnabled(false),
40 )
41 require.False(t, ios.ColorEnabled(), "WithColorEnabled(false) must force color off")
42 return palette{c: ios.Color()}
43}
45func mixedEntries() []verdict.Entry {
46 return []verdict.Entry{
47 {Package: "lodash", Version: "4.17.21", Verdict: verdict.Blocked, Reason: "policy violation"},
48 {Package: "left-pad", Version: "1.3.0", Verdict: verdict.Warning, Reason: "Deprecated package"},
49 }
50}
52func TestRenderEmptyPrintsNoActivity(t *testing.T) {
53 t.Parallel()
54 ios, _, out, _ := cmdtest.TestIOStreams()
55 Render(ios, nil)
56 assert.Contains(t, out.String(), "no Dependency Firewall activity recorded")
57}
59func TestRenderIncludesTitleAndTableHeaders(t *testing.T) {
60 t.Parallel()
61 out := renderBody(plainPalette(t), mixedEntries(), 1, 1)
62 assert.Contains(t, out, "GitLab Dependency Firewall")
63 assert.Contains(t, out, "PACKAGE")
64 assert.Contains(t, out, "VERSION")
65 assert.Contains(t, out, "REASON")
66}
68func TestRenderMixedSummaryLine(t *testing.T) {
69 t.Parallel()
70 out := renderBody(plainPalette(t), mixedEntries(), 1, 1)
71 assert.Contains(t, out, "1 blocked, 1 warning")
72 assert.Contains(t, out, "lodash")
73 assert.Contains(t, out, "left-pad")
74}
76func TestRenderSingleBlockedSummaryLine(t *testing.T) {
77 t.Parallel()
78 entries := []verdict.Entry{{Package: "lodash", Version: "4.17.21", Verdict: verdict.Blocked, Reason: "policy violation"}}
79 out := renderBody(plainPalette(t), entries, 1, 0)
80 assert.Contains(t, out, "1 package blocked")
81}
83func TestRenderSingleWarningSummaryLine(t *testing.T) {
84 t.Parallel()
85 entries := []verdict.Entry{{Package: "left-pad", Version: "1.3.0", Verdict: verdict.Warning, Reason: "Deprecated package"}}
86 out := renderBody(plainPalette(t), entries, 0, 1)
87 assert.Contains(t, out, "1 package warning")
88}
90func TestRenderBlockedGoesToStderr(t *testing.T) {
91 t.Parallel()
92 ios, _, out, errOut := cmdtest.TestIOStreams()
93 Render(ios, []verdict.Entry{{Package: "lodash", Version: "4.17.21", Verdict: verdict.Blocked, Reason: "policy violation"}})
94 assert.Contains(t, errOut.String(), "GitLab Dependency Firewall", "blocked summary goes to stderr")
95 assert.NotContains(t, out.String(), "GitLab Dependency Firewall")
96}
98func TestRenderWarningGoesToStdout(t *testing.T) {
99 t.Parallel()
100 ios, _, out, errOut := cmdtest.TestIOStreams()
101 Render(ios, []verdict.Entry{{Package: "left-pad", Version: "1.3.0", Verdict: verdict.Warning, Reason: "Deprecated package"}})
102 assert.Contains(t, out.String(), "GitLab Dependency Firewall", "warning summary goes to stdout")
103 assert.NotContains(t, errOut.String(), "GitLab Dependency Firewall")
104}
106func TestRenderStartsAndEndsWithBlankLine(t *testing.T) {
107 t.Parallel()
108 ios, _, _, errOut := cmdtest.TestIOStreams()
109 Render(ios, []verdict.Entry{{Package: "lodash", Version: "4.17.21", Verdict: verdict.Blocked, Reason: "policy violation"}})
110 got := errOut.String()
111 assert.True(t, strings.HasPrefix(got, "\n"), "expected a leading blank line")
112 assert.True(t, strings.HasSuffix(got, "\n\n"), "expected a trailing blank line")
113}
115func TestRenderLongPackageNameIsNotTruncated(t *testing.T) {
116 t.Parallel()
117 const longName = "@some-really-long-scope/a-package-with-a-long-name"
118 entries := []verdict.Entry{{Package: longName, Version: "1.0.0", Verdict: verdict.Blocked, Reason: "policy violation"}}
119 out := renderBody(plainPalette(t), entries, 1, 0)
120 assert.Contains(t, out, longName, "long package name must render in full")
121 assert.NotContains(t, out, "…")
122 assert.NotContains(t, out, "...")
123}
125func TestRenderDrawsUnicodeBox(t *testing.T) {
126 t.Parallel()
127 out := renderBody(plainPalette(t), mixedEntries(), 1, 1)
128 for _, ch := range []string{"╭", "╮", "╰", "╯", "│", "├", "┤", "─"} {
129 assert.Contains(t, out, ch, "output should draw box char %q", ch)
130 }
131}
133// TestRenderBoxIsAligned strips color and asserts every box line — borders AND
134// content rows — has the same visible width, in both color states. This is the
135// invariant that keeps the right border straight.
136func TestRenderBoxIsAligned(t *testing.T) {
137 t.Parallel()
138 for _, tc := range []struct {
139 name string
140 p palette
141 }{
142 {"plain", plainPalette(t)},
143 {"colored", coloredPalette(t)},
144 } {
145 t.Run(tc.name, func(t *testing.T) {
146 t.Parallel()
147 // A crafted reason with control characters must not widen the box.
148 entries := []verdict.Entry{
149 {Package: "lodash", Version: "4.17.21", Verdict: verdict.Blocked, Reason: "policy\tviolation\nsecond line"},
150 {Package: "left-pad", Version: "1.3.0", Verdict: verdict.Warning, Reason: "Deprecated package"},
151 }
152 out := renderBody(tc.p, entries, 1, 1)
154 var width int
155 seen := false
156 for line := range strings.SplitSeq(strings.Trim(out, "\n"), "\n") {
157 w := text.StringWidth(line)
158 if !seen {
159 width, seen = w, true
160 continue
161 }
162 assert.Equal(t, width, w, "every box line must be the same visible width: %q", text.Strip(line))
163 }
164 assert.True(t, seen, "expected box lines")
165 })
166 }
167}
169func TestRenderColorStates(t *testing.T) {
170 t.Parallel()
172 t.Run("colored emits ANSI", func(t *testing.T) {
173 t.Parallel()
174 out := renderBody(coloredPalette(t), mixedEntries(), 1, 1)
175 assert.Contains(t, out, "\x1b[", "colored palette should emit ANSI escapes")
176 })
178 t.Run("plain emits no ANSI", func(t *testing.T) {
179 t.Parallel()
180 out := renderBody(plainPalette(t), mixedEntries(), 1, 1)
181 assert.NotContains(t, out, "\x1b[", "plain palette must not emit ANSI escapes")
182 })
183}
185func TestRenderUsesStatusIcons(t *testing.T) {
186 t.Parallel()
187 out := renderBody(plainPalette(t), mixedEntries(), 1, 1)
188 assert.Contains(t, out, blockedIcon)
189 assert.Contains(t, out, warningIcon)
190}
192func TestRenderEmptyReasonShowsDash(t *testing.T) {
193 t.Parallel()
194 entries := []verdict.Entry{{Package: "express", Version: "4.18.2", Verdict: verdict.Allowed}}
195 out := renderBody(plainPalette(t), entries, 0, 0)
196 assert.Contains(t, out, emptyReason)
197}
199func TestSanitizeCell(t *testing.T) {
200 t.Parallel()
201 assert.Empty(t, sanitizeCell(""))
202 assert.Equal(t, "a b c", sanitizeCell("a\tb\nc"))
203 assert.Equal(t, "malware", sanitizeCell("\x1b[31mmalware\x1b[0m"), "ANSI escapes must be stripped")
204 assert.NotContains(t, sanitizeCell("x\x1b[2Jy"), "\x1b", "control sequences removed")
205}
207func TestReasonOrDash(t *testing.T) {
208 t.Parallel()
209 assert.Equal(t, emptyReason, reasonOrDash(""))
210 assert.Equal(t, emptyReason, reasonOrDash(" \t "), "whitespace-only reason falls back to dash")
211 assert.Equal(t, "policy violation", reasonOrDash("policy violation"))
212}
214// TestRenderSanitizesPackageAndVersion pins that a crafted package name or
215// version can't inject control characters or tear the box.
216func TestRenderSanitizesPackageAndVersion(t *testing.T) {
217 t.Parallel()
218 entries := []verdict.Entry{
219 {Package: "evil\x1b[2Jpkg", Version: "1.0\n0", Verdict: verdict.Blocked, Reason: "policy violation"},
220 }
221 out := renderBody(plainPalette(t), entries, 1, 0)
222 assert.NotContains(t, out, "\x1b", "package/version escapes must be stripped")
223 assert.NotContains(t, out, "\n1.0\n0", "version newline must be collapsed")
224 assert.Contains(t, out, "evilpkg")
225}
227func TestWorstVerdict(t *testing.T) {
228 t.Parallel()
229 assert.Equal(t, verdict.Blocked, worstVerdict(2, 1))
230 assert.Equal(t, verdict.Blocked, worstVerdict(1, 0))
231 assert.Equal(t, verdict.Warning, worstVerdict(0, 3))
232 assert.Equal(t, verdict.Allowed, worstVerdict(0, 0))
233}
235func TestIconForMatchesPaletteIcon(t *testing.T) {
236 t.Parallel()
237 p := plainPalette(t)
238 for _, v := range []verdict.Verdict{verdict.Blocked, verdict.Warning, verdict.Allowed} {
239 assert.Equal(t, iconFor(v), p.icon(v), "plain palette icon must equal the plain glyph for %q", v)
240 }
241}
243func TestAccentByWorstVerdict(t *testing.T) {
244 t.Parallel()
245 c := coloredPalette(t)
246 blocked := c.accent(verdict.Blocked)("x")
247 warned := c.accent(verdict.Warning)("x")
248 allowed := c.accent(verdict.Allowed)("x")
249 // Each severity produces a distinct colorization.
250 assert.NotEqual(t, blocked, warned)
251 assert.NotEqual(t, warned, allowed)
252 assert.NotEqual(t, blocked, allowed)
253}
255// TestNewPaletteSourcesColorForCILogs pins that newPalette derives its color
256// from ColorForCILogs, not Color: with color forced on but the streams marked
257// non-TTY (as in a CI job log), the summary is still colored. The CI/NO_COLOR
258// decision itself is covered by TestColorEnabledForCILogs in the iostreams
259// package, so this test needs no env mutation.
260func TestNewPaletteSourcesColorForCILogs(t *testing.T) {
261 t.Parallel()
262 buf := &strings.Builder{}
263 ios := iostreams.New(
264 iostreams.WithStdout(buf, false),
265 iostreams.WithStderr(buf, false),
266 iostreams.WithColorEnabled(true),
267 )
268 require.False(t, ios.IsaTTY, "streams must be non-TTY to prove color isn't gated on the TTY")
269 out := renderBody(newPalette(ios), mixedEntries(), 1, 1)
270 assert.Contains(t, out, "\x1b[", "summary must stay colored for CI logs even off a TTY")
271}
Diagnostics7 git calls · 382 ms
CallStatusSizeTime0382 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=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=44Open 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:0200361 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=361 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 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=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200171 B · 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=171 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=49b5e8f39e60dbc0d93ae2ad5e42977c52eb59ee 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=171 git.pkt.payload.max=144 git.sideband.band1.bytes=144 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=144 git.pack.trailer=4cb83a763321d2956623f250a565d976e7941ae0Open pack map
fetch2002.6 KiB369 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=369 git.response.bytes=2704 git.response.cache=MISS git.response.duration_ms=369 git.fetch.wants=7431cc8590a347a76cfb3a7deb13d7dd16f96e10 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=2704 git.pkt.payload.max=2677 git.sideband.band1.bytes=2677 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
- a5e2933d0bcb224d
- Request
- 137154a2…
- 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