Skip to content

Commit

Permalink
chore: allow whitespace after comma in ignore validations comments (#89)
Browse files Browse the repository at this point in the history
* chore: allow whitespace after comma in ignore validations comments

* chore: remove empty elements caused by trailing comma

* move exclude rules generation into a func and add tests

* simplifed generateExcludedRules and updated test case per request

* add deduplication of ignored rule names

* remove unused field

* avoid using continue in a loop

* update unit tests to account for deduplication

* fix: lint

Signed-off-by: Martin Chodur <[email protected]>

* fix: white spaces in the `disabled_validation_rules` annotation CSV

Signed-off-by: Martin Chodur <[email protected]>

* fix: assert import

Signed-off-by: Martin Chodur <[email protected]>

---------

Signed-off-by: Martin Chodur <[email protected]>
Co-authored-by: Martin Chodur <[email protected]>
  • Loading branch information
jmichalek132 and FUSAKLA authored Oct 14, 2024
1 parent e5eaf73 commit 666b9c0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Fixed: Ignore white spaces around rule names in the `disabled_validation_rules` annotation CSV format (Thanks @jmichalek13 !)

## [3.3.0] - 2024-10-03
- Changed: Upgrade to go 1.23
Expand Down
14 changes: 13 additions & 1 deletion pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Files(fileNames []string, validationRules []*validationrule.ValidationRule,
var excludedRules []string
excludedRulesText, ok := originalRule.Annotations[excludeAnnotationName]
if ok {
excludedRules = strings.Split(excludedRulesText, ",")
excludedRules = generateExcludedRules(excludedRulesText)
}
disabledValidators := ruleNode.DisabledValidators(disableValidationsComment)
if err := validator.KnownValidators(config.AllScope, disabledValidators); err != nil {
Expand Down Expand Up @@ -166,3 +166,15 @@ func Files(fileNames []string, validationRules []*validationrule.ValidationRule,
validationReport.Duration = time.Since(start)
return validationReport
}

func generateExcludedRules(excludedRulesText string) []string {
var excludedRules []string
for _, r := range strings.Split(excludedRulesText, ",") {
rule := strings.TrimSpace(r)
if rule != "" {
excludedRules = append(excludedRules, rule)
}
}
slices.Sort(excludedRules)
return slices.Compact(excludedRules)
}
62 changes: 62 additions & 0 deletions pkg/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package validate

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerateExcludedRules(t *testing.T) {
type testCase struct {
input string
expected []string
}
testCases := []testCase{
{
input: "check-test-label,check-testy-label",
expected: []string{"check-test-label", "check-testy-label"},
},
{
input: "check-test-label,check-test-label",
expected: []string{"check-test-label"},
},
{
input: "check-test-label, check-testy-label",
expected: []string{"check-test-label", "check-testy-label"},
},
{
input: "check-test-label, check-testy-label,",
expected: []string{"check-test-label", "check-testy-label"},
},
{
input: "check-test-label ,check-test-label",
expected: []string{"check-test-label"},
},
{
input: "check-test-label,check-test-label, ",
expected: []string{"check-test-label"},
},
{
input: "check-test-label , check testy label , ",
expected: []string{"check testy label", "check-test-label"},
},
{
input: "check-test-label , check-testy-label ",
expected: []string{"check-test-label", "check-testy-label"},
},
{
input: "check-test-label , check-testy-label, , ",
expected: []string{"check-test-label", "check-testy-label"},
},
{
input: " check-test-label , check-testy-label , , ",
expected: []string{"check-test-label", "check-testy-label"},
},
}
for i, tc := range testCases {
t.Run(string(rune(i)), func(t *testing.T) {
result := generateExcludedRules(tc.input)
assert.Equal(t, tc.expected, result)
})
}
}

0 comments on commit 666b9c0

Please sign in to comment.