8000 Do not mutate headers when initialising tableprinter by williammartin · Pull Request #9033 · cli/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Do not mutate headers when initialising tableprinter #9033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024
Merged
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
7 changes: 4 additions & 3 deletions internal/tableprinter/table_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func NewWithWriter(w io.Writer, isTTY bool, maxWidth int, cs *iostreams.ColorSch
}

if isTTY && len(headers.columns) > 0 {
// Make sure all headers are uppercase.
// Make sure all headers are uppercase, taking a copy of the headers to avoid modifying the original slice.
upperCasedHeaders := make([]string, len(headers.columns))
for i := range headers.columns {
headers.columns[i] = strings.ToUpper(headers.columns[i])
upperCasedHeaders[i] = strings.ToUpper(headers.columns[i])
}

// Make sure all header columns are padded - even the last one. Previously, the last header column
Expand All @@ -77,7 +78,7 @@ func NewWithWriter(w io.Writer, isTTY bool, maxWidth int, cs *iostreams.ColorSch
}

tp.AddHeader(
headers.columns,
upperCasedHeaders,
WithPadding(paddingFunc),
WithColor(cs.LightGrayUnderline),
)
Expand Down
22 changes: 22 additions & 0 deletions internal/tableprinter/table_printer_test.go
8E5E
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tableprinter_test

import (
"testing"

"github.com/cli/cli/v2/internal/tableprinter"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/stretchr/testify/require"
)

func TestHeadersAreNotMutated(t *testing.T) {
// Given a TTY environment so that headers are included in the table
ios, _, _, _ := iostreams.Test()
ios.SetStdoutTTY(true)

// When creating a new table printer
headers := []string{"one", "two", "three"}
_ = tableprinter.New(ios, tableprinter.WithHeader(headers...))

// The provided headers should not be mutated
require.Equal(t, []string{"one", "two", "three"}, headers)
}
Loading
0