From 666b9c01aa272ff5957e7e316e7da868d40f2e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Mon, 14 Oct 2024 22:33:59 +0200 Subject: [PATCH] chore: allow whitespace after comma in ignore validations comments (#89) * 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 * fix: white spaces in the `disabled_validation_rules` annotation CSV Signed-off-by: Martin Chodur * fix: assert import Signed-off-by: Martin Chodur --------- Signed-off-by: Martin Chodur Co-authored-by: Martin Chodur --- CHANGELOG.md | 1 + pkg/validate/validate.go | 14 +++++++- pkg/validate/validate_test.go | 62 +++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 pkg/validate/validate_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index bab4fe0..d04c8dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go index cbf0fce..89cee8d 100644 --- a/pkg/validate/validate.go +++ b/pkg/validate/validate.go @@ -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 { @@ -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) +} diff --git a/pkg/validate/validate_test.go b/pkg/validate/validate_test.go new file mode 100644 index 0000000..0393167 --- /dev/null +++ b/pkg/validate/validate_test.go @@ -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) + }) + } +}