Skip to content

Commit ed8ce81

Browse files
Merge pull request #2699 from thaJeztah/reduce_table_width
formatter: reduce minimum width for columns in table-view
2 parents 0db7f23 + dace8fd commit ed8ce81

51 files changed

Lines changed: 680 additions & 604 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/command/config/formatter_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/docker/cli/cli/command/formatter"
99
"github.com/docker/docker/api/types/swarm"
1010
"gotest.tools/v3/assert"
11-
is "gotest.tools/v3/assert/cmp"
1211
)
1312

1413
func TestConfigContextFormatWrite(t *testing.T) {
@@ -30,9 +29,9 @@ func TestConfigContextFormatWrite(t *testing.T) {
3029
},
3130
// Table format
3231
{formatter.Context{Format: NewFormat("table", false)},
33-
`ID NAME CREATED UPDATED
34-
1 passwords Less than a second ago Less than a second ago
35-
2 id_rsa Less than a second ago Less than a second ago
32+
`ID NAME CREATED UPDATED
33+
1 passwords Less than a second ago Less than a second ago
34+
2 id_rsa Less than a second ago Less than a second ago
3635
`},
3736
{formatter.Context{Format: NewFormat("table {{.Name}}", true)},
3837
`NAME
@@ -53,13 +52,16 @@ id_rsa
5352
Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()},
5453
Spec: swarm.ConfigSpec{Annotations: swarm.Annotations{Name: "id_rsa"}}},
5554
}
56-
for _, testcase := range cases {
57-
out := bytes.NewBufferString("")
58-
testcase.context.Output = out
59-
if err := FormatWrite(testcase.context, configs); err != nil {
60-
assert.ErrorContains(t, err, testcase.expected)
61-
} else {
62-
assert.Check(t, is.Equal(out.String(), testcase.expected))
63-
}
55+
for _, tc := range cases {
56+
tc := tc
57+
t.Run(string(tc.context.Format), func(t *testing.T) {
58+
var out bytes.Buffer
59+
tc.context.Output = &out
60+
if err := FormatWrite(tc.context, configs); err != nil {
61+
assert.ErrorContains(t, err, tc.expected)
62+
} else {
63+
assert.Equal(t, out.String(), tc.expected)
64+
}
65+
})
6466
}
6567
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ID NAME CREATED UPDATED
2-
ID-1-foo 1-foo 2 hours ago About an hour ago
3-
ID-2-foo 2-foo 2 hours ago About an hour ago
4-
ID-10-foo 10-foo 2 hours ago About an hour ago
1+
ID NAME CREATED UPDATED
2+
ID-1-foo 1-foo 2 hours ago About an hour ago
3+
ID-2-foo 2-foo 2 hours ago About an hour ago
4+
ID-10-foo 10-foo 2 hours ago About an hour ago
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ID NAME CREATED UPDATED
2-
ID-bar bar 2 hours ago About an hour ago
3-
ID-foo foo 2 hours ago About an hour ago
1+
ID NAME CREATED UPDATED
2+
ID-bar bar 2 hours ago About an hour ago
3+
ID-foo foo 2 hours ago About an hour ago

cli/command/container/formatter_diff_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/docker/docker/api/types/container"
99
"github.com/docker/docker/pkg/archive"
1010
"gotest.tools/v3/assert"
11-
is "gotest.tools/v3/assert/cmp"
1211
)
1312

1413
func TestDiffContextFormatWrite(t *testing.T) {
@@ -19,10 +18,10 @@ func TestDiffContextFormatWrite(t *testing.T) {
1918
}{
2019
{
2120
formatter.Context{Format: NewDiffFormat("table")},
22-
`CHANGE TYPE PATH
23-
C /var/log/app.log
24-
A /usr/app/app.js
25-
D /usr/app/old_app.js
21+
`CHANGE TYPE PATH
22+
C /var/log/app.log
23+
A /usr/app/app.js
24+
D /usr/app/old_app.js
2625
`,
2726
},
2827
{
@@ -48,14 +47,17 @@ D: /usr/app/old_app.js
4847
{Kind: archive.ChangeDelete, Path: "/usr/app/old_app.js"},
4948
}
5049

51-
for _, testcase := range cases {
52-
out := bytes.NewBufferString("")
53-
testcase.context.Output = out
54-
err := DiffFormatWrite(testcase.context, diffs)
55-
if err != nil {
56-
assert.Error(t, err, testcase.expected)
57-
} else {
58-
assert.Check(t, is.Equal(testcase.expected, out.String()))
59-
}
50+
for _, tc := range cases {
51+
tc := tc
52+
t.Run(string(tc.context.Format), func(t *testing.T) {
53+
out := bytes.NewBufferString("")
54+
tc.context.Output = out
55+
err := DiffFormatWrite(tc.context, diffs)
56+
if err != nil {
57+
assert.Error(t, err, tc.expected)
58+
} else {
59+
assert.Equal(t, out.String(), tc.expected)
60+
}
61+
})
6062
}
6163
}

cli/command/container/formatter_stats_test.go

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ container2 --
126126
}
127127

128128
func TestContainerStatsContextWriteWindows(t *testing.T) {
129-
tt := []struct {
129+
cases := []struct {
130130
context formatter.Context
131131
expected string
132132
}{
@@ -150,51 +150,54 @@ container2 -- --
150150
`,
151151
},
152152
}
153+
stats := []StatsEntry{
154+
{
155+
Container: "container1",
156+
CPUPercentage: 20,
157+
Memory: 20,
158+
MemoryLimit: 20,
159+
MemoryPercentage: 20,
160+
NetworkRx: 20,
161+
NetworkTx: 20,
162+
BlockRead: 20,
163+
BlockWrite: 20,
164+
PidsCurrent: 2,
165+
IsInvalid: false,
166+
},
167+
{
168+
Container: "container2",
169+
CPUPercentage: 30,
170+
Memory: 30,
171+
MemoryLimit: 30,
172+
MemoryPercentage: 30,
173+
NetworkRx: 30,
174+
NetworkTx: 30,
175+
BlockRead: 30,
176+
BlockWrite: 30,
177+
PidsCurrent: 3,
178+
IsInvalid: true,
179+
},
180+
}
153181

154-
for _, te := range tt {
155-
stats := []StatsEntry{
156-
{
157-
Container: "container1",
158-
CPUPercentage: 20,
159-
Memory: 20,
160-
MemoryLimit: 20,
161-
MemoryPercentage: 20,
162-
NetworkRx: 20,
163-
NetworkTx: 20,
164-
BlockRead: 20,
165-
BlockWrite: 20,
166-
PidsCurrent: 2,
167-
IsInvalid: false,
168-
},
169-
{
170-
Container: "container2",
171-
CPUPercentage: 30,
172-
Memory: 30,
173-
MemoryLimit: 30,
174-
MemoryPercentage: 30,
175-
NetworkRx: 30,
176-
NetworkTx: 30,
177-
BlockRead: 30,
178-
BlockWrite: 30,
179-
PidsCurrent: 3,
180-
IsInvalid: true,
181-
},
182-
}
183-
var out bytes.Buffer
184-
te.context.Output = &out
185-
err := statsFormatWrite(te.context, stats, "windows", false)
186-
if err != nil {
187-
assert.Error(t, err, te.expected)
188-
} else {
189-
assert.Check(t, is.Equal(te.expected, out.String()))
190-
}
182+
for _, tc := range cases {
183+
tc := tc
184+
t.Run(string(tc.context.Format), func(t *testing.T) {
185+
var out bytes.Buffer
186+
tc.context.Output = &out
187+
err := statsFormatWrite(tc.context, stats, "windows", false)
188+
if err != nil {
189+
assert.Error(t, err, tc.expected)
190+
} else {
191+
assert.Equal(t, out.String(), tc.expected)
192+
}
193+
})
191194
}
192195
}
193196

194197
func TestContainerStatsContextWriteWithNoStats(t *testing.T) {
195198
var out bytes.Buffer
196199

197-
contexts := []struct {
200+
cases := []struct {
198201
context formatter.Context
199202
expected string
200203
}{
@@ -217,22 +220,26 @@ func TestContainerStatsContextWriteWithNoStats(t *testing.T) {
217220
Format: "table {{.Container}}\t{{.CPUPerc}}",
218221
Output: &out,
219222
},
220-
"CONTAINER CPU %\n",
223+
"CONTAINER CPU %\n",
221224
},
222225
}
223226

224-
for _, context := range contexts {
225-
statsFormatWrite(context.context, []StatsEntry{}, "linux", false)
226-
assert.Check(t, is.Equal(context.expected, out.String()))
227-
// Clean buffer
228-
out.Reset()
227+
for _, tc := range cases {
228+
tc := tc
229+
t.Run(string(tc.context.Format), func(t *testing.T) {
230+
err := statsFormatWrite(tc.context, []StatsEntry{}, "linux", false)
231+
assert.NilError(t, err)
232+
assert.Equal(t, out.String(), tc.expected)
233+
// Clean buffer
234+
out.Reset()
235+
})
229236
}
230237
}
231238

232239
func TestContainerStatsContextWriteWithNoStatsWindows(t *testing.T) {
233240
var out bytes.Buffer
234241

235-
contexts := []struct {
242+
cases := []struct {
236243
context formatter.Context
237244
expected string
238245
}{
@@ -248,22 +255,25 @@ func TestContainerStatsContextWriteWithNoStatsWindows(t *testing.T) {
248255
Format: "table {{.Container}}\t{{.MemUsage}}",
249256
Output: &out,
250257
},
251-
"CONTAINER PRIV WORKING SET\n",
258+
"CONTAINER PRIV WORKING SET\n",
252259
},
253260
{
254261
formatter.Context{
255262
Format: "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}",
256263
Output: &out,
257264
},
258-
"CONTAINER CPU % PRIV WORKING SET\n",
265+
"CONTAINER CPU % PRIV WORKING SET\n",
259266
},
260267
}
261268

262-
for _, context := range contexts {
263-
statsFormatWrite(context.context, []StatsEntry{}, "windows", false)
264-
assert.Check(t, is.Equal(context.expected, out.String()))
265-
// Clean buffer
266-
out.Reset()
269+
for _, tc := range cases {
270+
tc := tc
271+
t.Run(string(tc.context.Format), func(t *testing.T) {
272+
err := statsFormatWrite(tc.context, []StatsEntry{}, "windows", false)
273+
assert.NilError(t, err)
274+
assert.Equal(t, out.String(), tc.expected)
275+
out.Reset()
276+
})
267277
}
268278
}
269279

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2-
container_id busybox:latest "top" Less than a second ago Up 1 second c1
3-
container_id busybox:latest "top" Less than a second ago Up 1 second c2,foo/bar
1+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2+
container_id busybox:latest "top" Less than a second ago Up 1 second c1
3+
container_id busybox:latest "top" Less than a second ago Up 1 second c2,foo/bar
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2-
container_id busybox:latest "top" Less than a second ago Up 1 second c1
3-
container_id busybox:latest "top" Less than a second ago Up 1 second c2
4-
container_id busybox:latest "top" Less than a second ago Up 1 second 80-82/tcp c3
5-
container_id busybox:latest "top" Less than a second ago Up 1 second 81/udp c4
6-
container_id busybox:latest "top" Less than a second ago Up 1 second 8.8.8.8:82->82/tcp c5
1+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2+
container_id busybox:latest "top" Less than a second ago Up 1 second c1
3+
container_id busybox:latest "top" Less than a second ago Up 1 second c2
4+
container_id busybox:latest "top" Less than a second ago Up 1 second 80-82/tcp c3
5+
container_id busybox:latest "top" Less than a second ago Up 1 second 81/udp c4
6+
container_id busybox:latest "top" Less than a second ago Up 1 second 8.8.8.8:82->82/tcp c5
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
2-
current * description of current https://someswarmserver https://someserver (default) all
3-
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
4-
other description of other https://someswarmserver https://someserver (default) all
5-
unset description of unset https://someswarmserver https://someserver (default)
1+
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
2+
current * description of current https://someswarmserver https://someserver (default) all
3+
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
4+
other description of other https://someswarmserver https://someserver (default) all
5+
unset description of unset https://someswarmserver https://someserver (default)

0 commit comments

Comments
 (0)