fix:kustomize cfg grep with no arguments causes panic (#5707)

* fix:kustomize cfg grep with no arguments causes panic

* add test for kustomize cfg grep with no arguments
This commit is contained in:
Dennis Zhou
2024-07-10 15:46:40 +08:00
committed by GitHub
parent 735ad0beef
commit 7cbaf78b1a
2 changed files with 17 additions and 0 deletions

View File

@@ -54,6 +54,9 @@ type GrepRunner struct {
}
func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing required argument: QUERY")
}
r.GrepFilter.Compare = func(a, b string) (int, error) {
qa, err := resource.ParseQuantity(a)
if err != nil {

View File

@@ -421,3 +421,17 @@ spec:
})
}
}
// TestGrepCmd_noQuery verifies the grep command errors when QUERY argument is missing
func TestGrepCmd_noQuery(t *testing.T) {
b := &bytes.Buffer{}
r := commands.GetGrepRunner("")
// No QUERY argument
r.Command.SetArgs([]string{})
r.Command.SetOut(b)
err := r.Command.Execute()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "missing required argument: QUERY")
}
}