GitLabgitlab-org
cli
main
1//go:build !integration
3package tableprinter
5import (
6 "bytes"
7 "fmt"
8 "strings"
9 "testing"
10 "time"
12 "github.com/stretchr/testify/require"
14 "gitlab.com/gitlab-org/cli/internal/text"
15)
17func Test_ttyTablePrinter_truncate(t *testing.T) {
18 buf := bytes.Buffer{}
19 tp := NewTablePrinter()
20 tp.SetTTYSeparator(" ")
21 tp.SetTerminalWidth(5)
22 tp.SetIsTTY(true)
24 tp.AddCell("1")
25 tp.AddCell("hello")
26 tp.EndRow()
27 tp.AddCell("2")
28 tp.AddCell("world")
29 tp.EndRow()
31 buf.Write(tp.Bytes())
33 expected := "1 h...\n2 w...\n"
34 if buf.String() != expected {
35 t.Errorf("expected: %q, got: %q", expected, buf.Bytes())
36 }
37}
39func Test_nonTTYTablePrinter_truncate(t *testing.T) {
40 buf := bytes.Buffer{}
41 tp := NewTablePrinter()
42 tp.SetTerminalWidth(5)
43 tp.SetIsTTY(false)
45 tp.AddCell("1")
46 tp.AddCell("hello")
47 tp.EndRow()
48 tp.AddCell("2")
49 tp.AddCell("world")
50 tp.EndRow()
52 buf.Write(tp.Bytes())
54 expected := "1\thello\n2\tworld\n"
55 if buf.String() != expected {
56 t.Errorf("expected: %q, got: %q", expected, buf.Bytes())
57 }
58}
60func TestTablePrinter(t *testing.T) {
61 t.Run("nil pointer cell value", func(t *testing.T) {
62 var createdAt *time.Time
63 var status *string
64 updatedAt := time.Now().UTC()
66 tp := NewTablePrinter()
67 tp.AddRow("id:", 1)
68 tp.AddRow("Status:", status)
69 tp.AddRow("Created:", createdAt)
70 tp.AddRow("Updated:", updatedAt)
72 expected := fmt.Sprintf("id:\t1\nStatus:\t<nil>\nCreated:\t<nil>\nUpdated:\t%v\n", updatedAt)
73 require.Equal(t, expected, tp.String())
74 })
75}
77func Test_nonTTYTablePrinter_pad(t *testing.T) {
78 tp := NewTablePrinter()
79 tp.SetIsTTY(false)
80 tp.SetPad(true)
81 tp.SetTerminalWidth(200)
83 tp.AddRow("PACKAGE", "VERSION", "STATUS", "REASON")
84 tp.AddRow("@typescript-eslint/eslint-plugin", "8.18.0", "Blocked", "policy violation")
85 tp.AddRow("lodash", "4.17.21", "Warning", "deprecated")
87 expected := "" +
88 "PACKAGE VERSION STATUS REASON \n" +
89 "@typescript-eslint/eslint-plugin 8.18.0 Blocked policy violation\n" +
90 "lodash 4.17.21 Warning deprecated \n"
91 require.Equal(t, expected, tp.String())
92}
94func Test_nonTTYTablePrinter_pad_notTruncatedWhenNarrow(t *testing.T) {
95 // a narrow terminal would cap columns on a TTY; on a non-TTY, padding must still
96 // align without truncating, including columns that follow the over-wide one
97 tp := NewTablePrinter()
98 tp.SetIsTTY(false)
99 tp.SetPad(true)
100 tp.SetTerminalWidth(20)
102 tp.AddRow("PACKAGE", "STATUS", "REASON")
103 tp.AddRow("@typescript-eslint/eslint-plugin", "Blocked", "policy violation")
105 expected := "" +
106 "PACKAGE STATUS REASON \n" +
107 "@typescript-eslint/eslint-plugin Blocked policy violation\n"
108 require.Equal(t, expected, tp.String())
109}
111func Test_nonTTYTablePrinter_padDisabled_keepsTSV(t *testing.T) {
112 tp := NewTablePrinter()
113 tp.SetIsTTY(false)
114 tp.SetTerminalWidth(5)
116 tp.AddRow("1", "hello")
117 tp.AddRow("2", "world")
119 expected := "1\thello\n2\tworld\n"
120 require.Equal(t, expected, tp.String())
121}
123func Test_nonTTYTablePrinter_pad_maxColWidth(t *testing.T) {
124 // an explicit MaxColWidth bounds padded output too, so a cell wider than the cap
125 // is truncated and the columns after it stay aligned
126 tp := NewTablePrinter()
127 tp.SetIsTTY(false)
128 tp.SetPad(true)
129 tp.MaxColWidth = 6
131 tp.AddRow("PACKAGE", "VERSION", "STATUS")
132 tp.AddRow("@typescript-eslint/eslint-plugin", "8.18.0", "Blocked")
133 tp.AddRow("lodash", "4.17.21", "Warning")
135 expected := "" +
136 "PAC... VER... STATUS\n" +
137 "@ty... 8.18.0 Blo...\n" +
138 "lodash 4.1... War...\n"
139 require.Equal(t, expected, tp.String())
140}
142func Test_ttyTablePrinter_pad_narrowerThanFirstColumn(t *testing.T) {
143 // the terminal is narrower than the first column plus separators, so every
144 // capped column bottoms out at zero width; padded TTY output must still be
145 // aligned and fit the terminal instead of printing those cells unformatted
146 tp := NewTablePrinter()
147 tp.SetIsTTY(true)
148 tp.SetPad(true)
149 tp.SetTerminalWidth(34)
151 tp.AddRow("PACKAGE", "VERSION", "STATUS", "REASON")
152 tp.AddRow("@typescript-eslint/eslint-plugin", "8.18.0", "Blocked", "policy violation")
153 tp.AddRow("lodash", "4.17.21", "Warning", "deprecated")
155 var width int
156 for line := range strings.SplitSeq(strings.TrimRight(tp.String(), "\n"), "\n") {
157 require.LessOrEqualf(t, text.StringWidth(line), 34, "line %q exceeds terminal width", line)
158 if width == 0 {
159 width = text.StringWidth(line)
160 }
161 require.Equalf(t, width, text.StringWidth(line), "line %q not aligned to %d columns", line, width)
162 }
163}
165func Test_ttyTablePrinter_pad_widthNotExceeded(t *testing.T) {
166 tp := NewTablePrinter()
167 tp.SetIsTTY(true)
168 tp.SetPad(true)
169 tp.SetTerminalWidth(40)
171 tp.AddRow("PACKAGE", "VERSION", "STATUS", "REASON")
172 tp.AddRow("@typescript-eslint/eslint-plugin", "8.18.0", "Blocked", "policy violation")
173 tp.AddRow("lodash", "4.17.21", "Warning", "deprecated")
175 var width int
176 for line := range strings.SplitSeq(strings.TrimRight(tp.String(), "\n"), "\n") {
177 require.LessOrEqualf(t, text.StringWidth(line), 40, "line %q exceeds terminal width", line)
178 if width == 0 {
179 width = text.StringWidth(line)
180 }
181 require.Equalf(t, width, text.StringWidth(line), "line %q not aligned to %d columns", line, width)
182 }
183}
Diagnostics6 git calls · 29 ms
CallStatusSizeTime029 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 KiB5 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=5 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=44Open pack map
fetchtree:0200145 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=145 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=08b25f85c676220137ef481de00d814b6336a715 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=145 git.pkt.payload.max=118 git.sideband.band1.bytes=118 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=118 git.pack.trailer=e7b25ba0d135b7b9f7bbafe37a2bbcffac288e70Open pack map
fetch2001.4 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=1443 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=cf4d5a2d5885846ad31c0aeb5ae853fe9902c05c 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=1443 git.pkt.payload.max=1416 git.sideband.band1.bytes=1416 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
- a5e144542f4d0bf5
- Request
- 302f7707…
- 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