Skip to content

Commit

Permalink
update PrintTable to properly harness tabWriter; add bundle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice committed May 9, 2019
1 parent 2c1314d commit 56882df
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
31 changes: 31 additions & 0 deletions cmd/porter/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,34 @@ func TestValidateUninstallCommand(t *testing.T) {
})
}
}

func TestValidateBundleListCommand(t *testing.T) {
testcases := []struct {
name string
args string
wantError string
}{
{"no args", "bundle list", ""},
{"output json", "bundle list -o json", ""},
{"invalid format", "bundle list -o wingdings", "invalid format: wingdings"},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
p := buildRootCommand()
osargs := strings.Split(tc.args, " ")
cmd, args, err := p.Find(osargs)
require.NoError(t, err)

err = cmd.ParseFlags(args)
require.NoError(t, err)

err = cmd.PreRunE(cmd, cmd.Flags().Args())
if tc.wantError == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.wantError)
}
})
}
}
23 changes: 13 additions & 10 deletions pkg/cnab/provider/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

// CondensedClaim holds a subset of pertinent values to be listed from a claim.Claim
type CondensedClaim struct {
Name string
Created time.Time
Action string
Status string
Name string
Created time.Time
Modified time.Time
Action string
Status string
}

// List lists bundles with the printer.Format provided
Expand All @@ -28,10 +29,11 @@ func (d *Duffle) List(opts printer.PrintOptions) error {
var condensedClaims []CondensedClaim
for _, claim := range claims {
condensedClaim := CondensedClaim{
Name: claim.Name,
Created: claim.Created,
Action: claim.Result.Action,
Status: claim.Result.Status,
Name: claim.Name,
Created: claim.Created,
Modified: claim.Modified,
Action: claim.Result.Action,
Status: claim.Result.Status,
}
condensedClaims = append(condensedClaims, condensedClaim)
}
Expand All @@ -48,9 +50,10 @@ func (d *Duffle) List(opts printer.PrintOptions) error {
if !ok {
return nil
}
return []interface{}{cl.Name, cl.Created, cl.Action, cl.Status}
return []interface{}{cl.Name, cl.Created, cl.Modified, cl.Action, cl.Status}
}
return printer.PrintTable(d.Out, condensedClaims, printClaimRow, "NAME", "INSTALLED", "LAST ACTION", "LAST STATUS")
return printer.PrintTable(d.Out, condensedClaims, printClaimRow,
"NAME", "CREATED", "MODIFIED", "LAST ACTION", "LAST STATUS")
default:
return fmt.Errorf("invalid format: %s", opts.Format)
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/printer/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@ func PrintTable(out io.Writer, v interface{}, getRow func(row interface{}) []int

table := NewTableWriter(out)
if len(headers) > 0 {
fmt.Fprintln(table, headers...)
fmt.Fprintln(table, tabify(headers)...)
}
for i := 0; i < rows.Len(); i++ {
fmt.Fprintln(table, getRow(rows.Index(i).Interface())...)
fmt.Fprintln(table, tabify(getRow(rows.Index(i).Interface()))...)
}
return table.Flush()
}

// tabify is a helper function which takes a slice and injects tab characters
// between each element such that tabwriter can work its magic
func tabify(untabified []interface{}) []interface{} {
var tabified []interface{}
for i := 0; i < len(untabified); i++ {
tabified = append(tabified, untabified[i])
// only append tab character if prior to last element
if i+1 < len(untabified) {
tabified = append(tabified, "\t")
}
}
return tabified
}
22 changes: 22 additions & 0 deletions pkg/printer/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,25 @@ func TestPrintTable_WithoutHeaders(t *testing.T) {
require.Nil(t, err)
require.Equal(t, "foo bar\n", b.String())
}

func TestPrintTable_VerifySpacing(t *testing.T) {
v := []testType{
{A: "Fuddruckers", B: "Peach Shake"},
{A: "Ruby Tuesday", B: "Mozzarella Sticks"},
}

b := &bytes.Buffer{}

err := PrintTable(b, v, func(r interface{}) []interface{} {
row, ok := r.(testType)
require.True(t, ok)
return []interface{}{row.A, row.B}
},
"RESTAURANT", "ORDER")

require.Nil(t, err)
require.Equal(t, `RESTAURANT ORDER
Fuddruckers Peach Shake
Ruby Tuesday Mozzarella Sticks
`, b.String())
}

0 comments on commit 56882df

Please sign in to comment.