Repository: kubernetes-sigs/krew
Pull request #863 (2024-09-30 19:01:36)

Fixing search results
Fixes https://github.com/kubernetes-sigs/krew/issues/862
<!-- For proposed features, make sure there's an issue it's discussed first -->
I'm not really sure what might could be added to the tests to account for this.

k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/krew/pull/863#" title="Author self-approved">wallentx</a>*
**Once this PR has been reviewed and has the lgtm label**, please assign [soltysh](https://github.com/soltysh) for approval. For more information see [the Kubernetes Code Review Process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process).

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fkrew).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/krew/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["soltysh"]} -->
k8s-ci-robot: Welcome @wallentx! <br><br>It looks like this is your first PR to <a href='https://github.com/kubernetes-sigs/krew'>kubernetes-sigs/krew</a> 🎉. Please refer to our [pull request process documentation](https://git.k8s.io/community/contributors/guide/pull-requests.md) to help your PR have a smooth ride to approval. <br><br>You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. [Here is the bot commands documentation](https://go.k8s.io/bot-commands). <br><br>You can also check if kubernetes-sigs/krew has [its own contribution guidelines](https://github.com/kubernetes-sigs/krew/tree/master/CONTRIBUTING.md). <br><br>You may want to refer to our [testing guide](https://git.k8s.io/community/contributors/devel/sig-testing/testing.md) if you run into trouble with your tests not passing. <br><br>If you are having difficulty getting your pull request seen, please follow the [recommended escalation practices](https://github.com/kubernetes/community/blob/master/contributors/guide/pull-requests.md#why-is-my-pull-request-not-getting-reviewed). Also, for tips and tricks in the contribution process you may want to read the [Kubernetes contributor cheat sheet](https://git.k8s.io/community/contributors/guide/contributor-cheatsheet/README.md). We want to make sure your contribution gets all the attention it needs! <br><br>Thank you, and welcome to Kubernetes. :smiley:
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/krew/pull/863#pullrequestreview-2338520358" title="Approved">ahmetb</a>*, *<a href="https://github.com/kubernetes-sigs/krew/pull/863#" title="Author self-approved">wallentx</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fkrew).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubernetes-sigs/krew/blob/master/OWNERS)~~ [ahmetb]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
wallentx: > we should probably add a test case here https://github.com/kubernetes-sigs/krew/blob/master/cmd/krew/cmd/search_test.go#L31-L62 with an empty keyword expecting the full set of names. this bug has probably been in krew for some time

I've made the requested changes to the search_test 👍
chriskim06: actually looking into it a bit more, we have this integration test https://github.com/kubernetes-sigs/krew/blob/master/integration_test/search_test.go#L24-L34 but it didnt catch this because it just checks that there are at least 10 lines in the output. wonder if we should update this to an exact number (1 + number of plugins in the cloned index; the 1 being the header line in the search output). 
wallentx: @chriskim06 I've got `Allow edits and access to secrets by maintainers` checked, so feel free to do whatever you deem is most appropriate.

Patch:
diff --git a/cmd/krew/cmd/search.go b/cmd/krew/cmd/search.go
index d4b10a9..12a4dfb 100644
--- a/cmd/krew/cmd/search.go
+++ b/cmd/krew/cmd/search.go
@@ -38,7 +38,7 @@ type searchItem struct {
 type searchCorpus []searchItem
 
 func (s searchCorpus) descriptions() []string {
-	var res = make([]string, len(s))
+	res := make([]string, 0, len(s))
 	for _, corpus := range s {
 		res = append(res, corpus.description)
 	}
@@ -46,7 +46,7 @@ func (s searchCorpus) descriptions() []string {
 }
 
 func (s searchCorpus) names() []string {
-	var res = make([]string, len(s))
+	res := make([]string, 0, len(s))
 	for _, corpus := range s {
 		res = append(res, corpus.name)
 	}


Test patch:
diff --git a/cmd/krew/cmd/search_test.go b/cmd/krew/cmd/search_test.go
index 00797c9..bb4a3c7 100644
--- a/cmd/krew/cmd/search_test.go
+++ b/cmd/krew/cmd/search_test.go
@@ -59,6 +59,16 @@ func Test_searchByNameAndDesc(t *testing.T) {
 			},
 			expected: []string{"baz"},
 		},
+		{
+			keyword: "",
+			names:   []string{"plugin1", "plugin2", "plugin3"}, // empty keyword, only names match
+			descs: []string{
+				"Description for plugin1",
+				"Description for plugin2",
+				"Description for plugin3",
+			},
+			expected: []string{"plugin1", "plugin2", "plugin3"},
+		},
 	}
 
 	for _, tp := range testPlugins {