Cleanup rendering and tests

This commit is contained in:
bagtoad 2024-09-29 11:46:37 -06:00
parent 1aa2a824ba
commit 2f608e3772
2 changed files with 4 additions and 74 deletions

View file

@ -12,12 +12,6 @@ import (
"github.com/spf13/cobra"
)
type LicenseRenderer interface {
Render([]api.License, ListOptions) error
}
type TableLicenseRenderer struct{}
type ListOptions struct {
IO *iostreams.IOStreams
HTTPClient func() (*http.Client, error)
@ -68,11 +62,10 @@ func listRun(opts *ListOptions) error {
return cmdutil.NewNoResultsError("no licenses found")
}
r := &TableLicenseRenderer{}
return r.Render(licenses, opts)
return renderLicenseTemplatesTable(licenses, opts)
}
func (r *TableLicenseRenderer) Render(licenses []api.License, opts *ListOptions) error {
func renderLicenseTemplatesTable(licenses []api.License, opts *ListOptions) error {
t := tableprinter.New(opts.IO, tableprinter.WithHeader("KEY", "NAME"))
for _, l := range licenses {
t.AddField(l.Key)

View file

@ -6,7 +6,6 @@ import (
"testing"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/pkg/cmdutil"
@ -24,14 +23,14 @@ func TestNewCmdList(t *testing.T) {
tty bool
}{
{
name: "no arguments",
name: "happy path no arguments",
args: []string{},
wantErr: false,
tty: false,
},
{
name: "too many arguments",
args: []string{"foo", "bar"},
args: []string{"foo"},
wantErr: true,
},
}
@ -63,68 +62,6 @@ func TestNewCmdList(t *testing.T) {
}
}
func TestTableLicenseRenderer(t *testing.T) {
tests := []struct {
name string
opts ListOptions
isTTY bool
wantStdout string
wantErr bool
licenses []api.License
}{
{
name: "licenses + tty",
opts: ListOptions{},
isTTY: true,
wantStdout: heredoc.Doc(`
KEY NAME
mit MIT License
lgpl-3.0 GNU Lesser General Public License v3.0
`),
wantErr: false,
licenses: []api.License{
{
Key: "mit",
Name: "MIT License",
},
{
Key: "lgpl-3.0",
Name: "GNU Lesser General Public License v3.0",
},
},
},
{
name: "no licenses + tty",
opts: ListOptions{},
isTTY: true,
wantStdout: heredoc.Doc(`
KEY NAME
`),
wantErr: false,
licenses: []api.License{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(tt.isTTY)
ios.SetStdinTTY(tt.isTTY)
ios.SetStderrTTY(tt.isTTY)
tt.opts.IO = ios
r := &TableLicenseRenderer{}
err := r.Render(tt.licenses, &tt.opts)
if !tt.wantErr {
assert.NoError(t, err, "Expected no error while rendering table")
}
assert.Equal(t, tt.wantStdout, stdout.String(), "Rendered table differs from expected")
})
}
}
func TestListRun(t *testing.T) {
tests := []struct {
name string