Skip to content

Commit

Permalink
fix:kustomize cfg grep with no arguments causes panic (#5707)
Browse files Browse the repository at this point in the history
* fix:kustomize cfg grep with no arguments causes panic

* add test for kustomize cfg grep with no arguments
  • Loading branch information
deszhou committed Jul 10, 2024
1 parent 735ad0b commit 7cbaf78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/config/internal/commands/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions cmd/config/internal/commands/grep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 7cbaf78

Please sign in to comment.