Skip to content

Commit d5d2ed5

Browse files
committed
image/tree: Add test for checking ansi escape output
Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 1a261e3 commit d5d2ed5

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

cli/command/image/tree_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package image
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/docker/cli/internal/test"
8+
"gotest.tools/v3/assert"
9+
)
10+
11+
func TestPrintImageTreeAnsiTty(t *testing.T) {
12+
testCases := []struct {
13+
name string
14+
stdinTty bool
15+
stdoutTty bool
16+
stderrTty bool
17+
expectedAnsi bool
18+
}{
19+
{
20+
name: "non-terminal",
21+
stdinTty: false,
22+
stdoutTty: false,
23+
stderrTty: false,
24+
25+
expectedAnsi: false,
26+
},
27+
{
28+
name: "terminal",
29+
stdinTty: true,
30+
stdoutTty: true,
31+
stderrTty: true,
32+
33+
expectedAnsi: true,
34+
},
35+
{
36+
name: "stdout-tty-only",
37+
stdinTty: false,
38+
stdoutTty: true,
39+
stderrTty: false,
40+
41+
expectedAnsi: true,
42+
},
43+
{
44+
name: "stdin-stderr-tty-only",
45+
stdinTty: true,
46+
stdoutTty: false,
47+
stderrTty: true,
48+
49+
expectedAnsi: false,
50+
},
51+
{
52+
name: "stdout-stdin-tty",
53+
stdinTty: true,
54+
stdoutTty: true,
55+
stderrTty: false,
56+
57+
expectedAnsi: true,
58+
},
59+
{
60+
name: "stdout-stderr-tty",
61+
stdinTty: false,
62+
stdoutTty: true,
63+
stderrTty: true,
64+
65+
expectedAnsi: true,
66+
},
67+
{
68+
name: "stdin-tty-only",
69+
stdinTty: true,
70+
stdoutTty: false,
71+
stderrTty: false,
72+
73+
expectedAnsi: false,
74+
},
75+
{
76+
name: "stderr-tty-only",
77+
stdinTty: false,
78+
stdoutTty: false,
79+
stderrTty: true,
80+
81+
expectedAnsi: false,
82+
},
83+
}
84+
85+
mockView := treeView{
86+
images: []topImage{
87+
{
88+
Names: []string{"test-image:latest"},
89+
Details: imageDetails{
90+
ID: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
91+
DiskUsage: "10.5 MB",
92+
InUse: true,
93+
ContentSize: "5.2 MB",
94+
},
95+
Children: []subImage{
96+
{
97+
Platform: "linux/amd64",
98+
Available: true,
99+
Details: imageDetails{
100+
ID: "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
101+
DiskUsage: "5.1 MB",
102+
InUse: false,
103+
ContentSize: "2.5 MB",
104+
},
105+
},
106+
},
107+
},
108+
},
109+
imageSpacing: false,
110+
}
111+
112+
for _, tc := range testCases {
113+
t.Run(tc.name, func(t *testing.T) {
114+
cli := test.NewFakeCli(nil)
115+
cli.In().SetIsTerminal(tc.stdinTty)
116+
cli.Out().SetIsTerminal(tc.stdoutTty)
117+
cli.Err().SetIsTerminal(tc.stderrTty)
118+
119+
printImageTree(cli, mockView)
120+
121+
out := cli.OutBuffer().String()
122+
assert.Check(t, len(out) > 0, "Output should not be empty")
123+
124+
hasAnsi := strings.Contains(out, "\x1b[")
125+
if tc.expectedAnsi {
126+
assert.Check(t, hasAnsi, "Output should contain ANSI escape codes")
127+
} else {
128+
assert.Check(t, !hasAnsi, "Output should not contain ANSI escape codes")
129+
}
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)