Skip to content

Commit

Permalink
Merge pull request #24 from inteon/bugfix_linter
Browse files Browse the repository at this point in the history
Fix diff bug in RemoveExtensions caused by multiple prefixes; and fix bug in sorting
  • Loading branch information
jetstack-bot authored Feb 13, 2024
2 parents 464b509 + 8bad93d commit 65cefcd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
15 changes: 12 additions & 3 deletions linter/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package linter

import (
"fmt"
"testing"

"github.com/cert-manager/helm-tool/linter/sets"
Expand Down Expand Up @@ -92,12 +93,20 @@ func TestDiffPaths(t *testing.T) {
wantMissingA: sets.New("app.logLevela", "app.name"),
wantMissingB: sets.New("app.test"),
},
{
a: sets.New("app.test1", "app.test2"),
b: sets.New("app"),
wantMissingA: sets.New[string](),
wantMissingB: sets.New[string](),
},
}

for _, tc := range testcases {
diffA, diffB := DiffPaths(tc.a, tc.b)
t.Run(fmt.Sprintf("%s-%s", tc.a, tc.b), func(t *testing.T) {
diffA, diffB := DiffPaths(tc.a, tc.b)

require.ElementsMatch(t, tc.wantMissingA.UnsortedList(), diffA.UnsortedList())
require.ElementsMatch(t, tc.wantMissingB.UnsortedList(), diffB.UnsortedList())
require.ElementsMatch(t, tc.wantMissingA.UnsortedList(), diffA.UnsortedList())
require.ElementsMatch(t, tc.wantMissingB.UnsortedList(), diffB.UnsortedList())
})
}
}
28 changes: 21 additions & 7 deletions linter/sets/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ func RemovePrefixes(items Set[string], sets ...Set[string]) Set[string] {
nonPrefixes := maps.Clone(items)

values := Union(append(sets, items)...).UnsortedList()
slices.Sort(values)
slices.SortFunc(values, func(a, b string) int {
aSort := strings.ReplaceAll(a, "[", ".[") + "."
bSort := strings.ReplaceAll(b, "[", ".[") + "."
return strings.Compare(aSort, bSort)
})

for i := 0; i < len(values); i++ {
// If the next value is an extension of the current value, skip
// If the next value is an extension of the current value, remove
// the current value.
if i+1 < len(values) && (strings.HasPrefix(values[i+1], values[i]+".") || strings.HasPrefix(values[i+1], values[i]+"[")) {
nonPrefixes.Delete(values[i])
Expand All @@ -54,12 +59,21 @@ func RemoveExtensions(items Set[string], sets ...Set[string]) Set[string] {
nonExtensions := maps.Clone(items)

values := Union(append(sets, items)...).UnsortedList()
slices.Sort(values)
slices.SortFunc(values, func(a, b string) int {
aSort := strings.ReplaceAll(a, "[", ".[") + "."
bSort := strings.ReplaceAll(b, "[", ".[") + "."
return strings.Compare(aSort, bSort)
})

OuterLoop:
for i := 0; i < len(values); i++ {
// If the next value is an extension of the current value, skip
// the current value.
if i+1 < len(values) && (strings.HasPrefix(values[i+1], values[i]+".") || strings.HasPrefix(values[i+1], values[i]+"[")) {
nonExtensions.Delete(values[i+1])
// Remove all following values that are extensions of the current value.
for j := i + 1; j < len(values); j++ {
if !strings.HasPrefix(values[j], values[i]+".") && !strings.HasPrefix(values[j], values[i]+"[") {
continue OuterLoop
}

nonExtensions.Delete(values[j])
}
}

Expand Down

0 comments on commit 65cefcd

Please sign in to comment.