Skip to content
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

cmd: refactor list to work with custom indexes #560

Merged
merged 3 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions cmd/krew/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/krew/internal/installation"
"sigs.k8s.io/krew/internal/installation/receipt"
)

func init() {
Expand All @@ -48,8 +49,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, receipt.CanonicalName(r))
}
sort.Strings(names)
fmt.Fprintln(os.Stdout, strings.Join(names, "\n"))
Expand All @@ -58,8 +59,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{receipt.CanonicalName(r), r.Spec.Version})
}
rows = sortByFirstColumn(rows)
return printTable(os.Stdout, []string{"PLUGIN", "VERSION"}, rows)
Expand Down
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)
}
}
11 changes: 11 additions & 0 deletions internal/installation/receipt/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sigs.k8s.io/yaml"

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

Expand All @@ -42,6 +43,16 @@ func Load(path string) (index.Receipt, error) {
return indexscanner.ReadReceiptFromFile(path)
}

// CanonicalName returns the index and plugin name from a receipt.
// The index name is omitted if it is the default index.
func CanonicalName(receipt index.Receipt) string {
ahmetb marked this conversation as resolved.
Show resolved Hide resolved
ahmetb marked this conversation as resolved.
Show resolved Hide resolved
name := receipt.Name
if receipt.Status.Source.Name != constants.DefaultIndexName {
name = receipt.Status.Source.Name + "/" + receipt.Name
}
return name
}

// New returns a new receipt with the given plugin and index name.
func New(plugin index.Plugin, indexName string) index.Receipt {
return index.Receipt{
Expand Down
33 changes: 33 additions & 0 deletions internal/installation/receipt/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sigs.k8s.io/krew/internal/index/indexscanner"
"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

func TestStore(t *testing.T) {
Expand Down Expand Up @@ -73,6 +74,38 @@ func TestLoad_preservesNonExistsError(t *testing.T) {
}
}

func TestCanonicalName(t *testing.T) {
tests := []struct {
name string
receipt index.Receipt
expected string
}{
{
name: "from default index",
receipt: testutil.NewReceipt().WithPlugin(testutil.NewPlugin().WithName("foo").V()).V(),
expected: "foo",
},
{
ahmetb marked this conversation as resolved.
Show resolved Hide resolved
name: "from 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 := CanonicalName(test.receipt)
if diff := cmp.Diff(test.expected, actual); diff != "" {
t.Fatalf("expected name to match: %s", diff)
}
})
}
}

func TestNew(t *testing.T) {
testPlugin := testutil.NewPlugin().WithName("foo").WithPlatforms(testutil.NewPlatform().V()).V()
wantReceipt := testutil.NewReceipt().WithPlugin(testPlugin).V()
Expand Down