-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: make krew search work with multiple indexes
Mostly just refactoring. Added test to make sure search output is alphabetically sorted (even in case of multiple indexes). Signed-off-by: Ahmet Alp Balkan <[email protected]>
- Loading branch information
Showing
4 changed files
with
274 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 ( | ||
"sigs.k8s.io/krew/pkg/constants" | ||
"sigs.k8s.io/krew/pkg/index" | ||
) | ||
|
||
// indexOf returns the index name of a receipt. | ||
func indexOf(r index.Receipt) string { | ||
if r.Status.Source.Name == "" { | ||
return constants.DefaultIndexName | ||
} | ||
return r.Status.Source.Name | ||
} | ||
|
||
// displayName returns the display name of a Plugin. | ||
// The index name is omitted if it is the default index. | ||
func displayName(p index.Plugin, indexName string) string { | ||
if isDefaultIndex(indexName) { | ||
return p.Name | ||
} | ||
return indexName + "/" + p.Name | ||
} | ||
|
||
func isDefaultIndex(name string) bool { | ||
return name == "" || name == constants.DefaultIndexName | ||
} | ||
|
||
// canonicalName returns INDEX/NAME value for a plugin, even if | ||
// it is in the default index. | ||
func canonicalName(p index.Plugin, indexName string) string { | ||
if isDefaultIndex(indexName) { | ||
indexName = constants.DefaultIndexName | ||
} | ||
return indexName + "/" + p.Name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// 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/constants" | ||
"sigs.k8s.io/krew/pkg/index" | ||
) | ||
|
||
func Test_isDefaultIndex(t *testing.T) { | ||
if !isDefaultIndex("") { | ||
t.Error("empty string must indicate default index") | ||
} | ||
if !isDefaultIndex("default") { // nb: intentionally not using the const to ensure compatibility | ||
t.Error("default index must indicate default index") | ||
} | ||
if isDefaultIndex("foo") { // nb: intentionally not using the const to ensure compatibility | ||
t.Error("name=foo must not indicate default index") | ||
} | ||
} | ||
|
||
func TestIndexOf(t *testing.T) { | ||
noIndex := testutil.NewReceipt().WithPlugin(testutil.NewPlugin().V()).WithStatus(index.ReceiptStatus{}).V() | ||
if got := indexOf(noIndex); got != constants.DefaultIndexName { | ||
t.Errorf("expected default index for no index in status; got=%q", got) | ||
} | ||
defaultIndex := testutil.NewReceipt().WithPlugin(testutil.NewPlugin().V()).V() | ||
if got := indexOf(defaultIndex); got != constants.DefaultIndexName { | ||
t.Errorf("expected 'default' for default index; got=%q", got) | ||
} | ||
customIndex := testutil.NewReceipt().WithPlugin(testutil.NewPlugin().V()).WithStatus( | ||
index.ReceiptStatus{Source: index.SourceIndex{Name: "foo"}}).V() | ||
if got := indexOf(customIndex); got != "foo" { | ||
t.Errorf("expected custom index name; got=%q", got) | ||
} | ||
} | ||
|
||
func Test_displayName(t *testing.T) { | ||
type args struct { | ||
p index.Plugin | ||
index string | ||
} | ||
tests := []struct { | ||
name string | ||
in args | ||
expected string | ||
}{ | ||
{ | ||
name: "explicit default index", | ||
in: args{ | ||
p: testutil.NewPlugin().WithName("foo").V(), | ||
index: constants.DefaultIndexName, | ||
}, | ||
expected: "foo", | ||
}, | ||
{ | ||
name: "no index", | ||
in: args{ | ||
p: testutil.NewPlugin().WithName("foo").V(), | ||
index: "", | ||
}, | ||
expected: "foo", | ||
}, | ||
{ | ||
name: "custom index", | ||
in: args{ | ||
p: testutil.NewPlugin().WithName("bar").V(), | ||
index: "foo", | ||
}, | ||
expected: "foo/bar", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := displayName(tt.in.p, tt.in.index) | ||
if diff := cmp.Diff(tt.expected, actual); diff != "" { | ||
t.Fatalf("expected name to match: %s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_canonicalName(t *testing.T) { | ||
p1 := testutil.NewPlugin().WithName("foo").V() | ||
if expected, got := "default/foo", canonicalName(p1, ""); got != expected { | ||
t.Errorf("expected=%q; got=%q", expected, got) | ||
} | ||
p2 := testutil.NewPlugin().WithName("bar").V() | ||
if expected, got := "default/bar", canonicalName(p2, "default"); got != expected { | ||
t.Errorf("expected=%q; got=%q", expected, got) | ||
} | ||
p3 := testutil.NewPlugin().WithName("quux").V() | ||
if expected, got := "custom/quux", canonicalName(p3, "custom"); got != expected { | ||
t.Errorf("expected=%q; got=%q", expected, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters