Skip to content

Commit

Permalink
fail if TopN is negative
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryFissionGames committed Oct 18, 2023
1 parent d2a506c commit 89cad5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/stanza/fileconsumer/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func New(c Criteria) (*Matcher, error) {
return nil, fmt.Errorf("'regex' must be specified when 'sort_by' is specified")
}

if c.OrderingCriteria.TopN < 0 {
return nil, fmt.Errorf("`top_n` must be a positive integer")
}

if c.OrderingCriteria.TopN == 0 {
c.OrderingCriteria.TopN = defaultOrderingCriteriaTopN
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/stanza/fileconsumer/matcher/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ func TestNew(t *testing.T) {
},
expectedErr: "compile regex: error parsing regexp: missing closing ]: `[a-z`",
},
{
name: "TopN is negative",
criteria: Criteria{
Include: []string{"*.log"},
OrderingCriteria: OrderingCriteria{
Regex: "[a-z]",
TopN: -1,
SortBy: []Sort{
{
SortType: "numeric",
RegexKey: "key",
},
},
},
},
expectedErr: "`top_n` must be a positive integer",
},
{
name: "SortTypeEmpty",
criteria: Criteria{
Expand Down

0 comments on commit 89cad5c

Please sign in to comment.