Skip to content

Commit

Permalink
cmd: refactor list to work with custom indexes (#560)
Browse files Browse the repository at this point in the history
* Update list to show plugins from custom indexes

* Add godoc comment

* Move displayName to cmd package
  • Loading branch information
chriskim06 authored Mar 29, 2020
1 parent eb1a452 commit 0571889
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
21 changes: 17 additions & 4 deletions cmd/krew/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/krew/internal/installation"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

func init() {
Expand All @@ -48,8 +50,8 @@ Remarks:
// return sorted list of plugin names when piped to other commands or file
if !isTerminal(os.Stdout) {
var names []string
for _, receipt := range receipts {
names = append(names, receipt.Name)
for _, r := range receipts {
names = append(names, displayName(r))
}
sort.Strings(names)
fmt.Fprintln(os.Stdout, strings.Join(names, "\n"))
Expand All @@ -58,8 +60,8 @@ Remarks:

// print table
var rows [][]string
for _, receipt := range receipts {
rows = append(rows, []string{receipt.Name, receipt.Spec.Version})
for _, r := range receipts {
rows = append(rows, []string{displayName(r), r.Spec.Version})
}
rows = sortByFirstColumn(rows)
return printTable(os.Stdout, []string{"PLUGIN", "VERSION"}, rows)
Expand Down Expand Up @@ -87,3 +89,14 @@ func sortByFirstColumn(rows [][]string) [][]string {
})
return rows
}

// displayName returns the index and plugin name from a receipt.
// The index name is omitted if it is the default index.
func displayName(receipt index.Receipt) string {
name := receipt.Name
indexName := receipt.Status.Source.Name
if indexName != "" && indexName != constants.DefaultIndexName {
name = receipt.Status.Source.Name + "/" + receipt.Name
}
return name
}
61 changes: 61 additions & 0 deletions cmd/krew/cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2019 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"

"github.com/google/go-cmp/cmp"

"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/index"
)

func Test_displayName(t *testing.T) {
tests := []struct {
name string
receipt index.Receipt
expected string
}{
{
name: "explicit default index",
receipt: testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("foo").V()).V(),
expected: "foo",
},
{
name: "no index",
receipt: testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("foo").V()).WithStatus(index.ReceiptStatus{}).V(),
expected: "foo",
},
{
name: "custom index",
receipt: testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("bar").V()).WithStatus(index.ReceiptStatus{
Source: index.SourceIndex{
Name: "foo",
},
}).V(),
expected: "foo/bar",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := displayName(test.receipt)
if diff := cmp.Diff(test.expected, actual); diff != "" {
t.Fatalf("expected name to match: %s", diff)
}
})
}
}
15 changes: 13 additions & 2 deletions integration_test/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

"sigs.k8s.io/krew/pkg/constants"
)

func TestKrewList(t *testing.T) {
Expand All @@ -26,7 +28,8 @@ func TestKrewList(t *testing.T) {
test, cleanup := NewTest(t)
defer cleanup()

initialList := test.WithIndex().Krew("list").RunOrFailOutput()
test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex()
initialList := test.Krew("list").RunOrFailOutput()
initialOut := []byte{'\n'}

if diff := cmp.Diff(initialList, initialOut); diff != "" {
Expand All @@ -40,5 +43,13 @@ func TestKrewList(t *testing.T) {
if diff := cmp.Diff(eventualList, expected); diff != "" {
t.Fatalf("'list' output doesn't match:\n%s", diff)
}
// TODO(ahmetb): install multiple plugins and see if the output is sorted

test.Krew("index", "add", "foo", test.TempDir().Path("index/"+constants.DefaultIndexName)).RunOrFail()
test.Krew("install", "foo/"+validPlugin2).RunOrFail()

want := []string{validPlugin, "foo/" + validPlugin2}
actual := lines(test.Krew("list").RunOrFailOutput())
if diff := cmp.Diff(actual, want); diff != "" {
t.Fatalf("'list' output doesn't match:\n%s", diff)
}
}

0 comments on commit 0571889

Please sign in to comment.