Do not mutate headers when initialising tableprinter
This commit is contained in:
parent
f2d6a8ec5c
commit
cb357785dd
2 changed files with 26 additions and 3 deletions
|
|
@ -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
|
||||
|
|
@ -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),
|
||||
)
|
||||
|
|
|
|||
22
internal/tableprinter/table_printer_test.go
Normal file
22
internal/tableprinter/table_printer_test.go
Normal file
|
|
@ -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…
Add table
Add a link
Reference in a new issue