Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cli/command/image/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func TestNewImagesCommandSuccess(t *testing.T) {
},
},
}

prevForceStreamRedirected := testForceStreamRedirected
t.Cleanup(func() {
testForceStreamRedirected = prevForceStreamRedirected
})
testForceStreamRedirected = new(bool)
*testForceStreamRedirected = false

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc})
Expand Down
13 changes: 11 additions & 2 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func getPossibleChips(view treeView) (chips []imageChip) {
}

func printImageTree(outs command.Streams, view treeView) {
if streamRedirected(outs.Out()) {
stdoutRedirected := streamRedirected(outs.Out())
if stdoutRedirected {
_, _ = fmt.Fprintln(outs.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
}

Expand All @@ -245,7 +246,10 @@ func printImageTree(outs command.Streams, view treeView) {

isTerm := out.IsTerminal()

out.Println(generateLegend(out, width))
// Don't print legend when output is redirected
if !stdoutRedirected {
out.Println(generateLegend(out, width))
}

possibleChips := getPossibleChips(view)
columns := []imgColumn{
Expand Down Expand Up @@ -517,7 +521,12 @@ func widestFirstColumnValue(headers []imgColumn, images []topImage) int {
return width
}

var testForceStreamRedirected *bool

func streamRedirected(s *streams.Out) bool {
if testForceStreamRedirected != nil {
return *testForceStreamRedirected
}
fd := s.FD()
if os.Stdout.Fd() != fd {
return true
Expand Down
7 changes: 7 additions & 0 deletions cli/command/image/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ func TestPrintImageTreeAnsiTty(t *testing.T) {
imageSpacing: false,
}

prevForceStreamRedirected := testForceStreamRedirected
t.Cleanup(func() {
testForceStreamRedirected = prevForceStreamRedirected
})
testForceStreamRedirected = new(bool)
*testForceStreamRedirected = false

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(nil)
Expand Down
Loading