Skip to content

Commit 65cefcd

Browse files
authored
Merge pull request #24 from inteon/bugfix_linter
Fix diff bug in RemoveExtensions caused by multiple prefixes; and fix bug in sorting
2 parents 464b509 + 8bad93d commit 65cefcd

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

linter/diff_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package linter
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/cert-manager/helm-tool/linter/sets"
@@ -92,12 +93,20 @@ func TestDiffPaths(t *testing.T) {
9293
wantMissingA: sets.New("app.logLevela", "app.name"),
9394
wantMissingB: sets.New("app.test"),
9495
},
96+
{
97+
a: sets.New("app.test1", "app.test2"),
98+
b: sets.New("app"),
99+
wantMissingA: sets.New[string](),
100+
wantMissingB: sets.New[string](),
101+
},
95102
}
96103

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

100-
require.ElementsMatch(t, tc.wantMissingA.UnsortedList(), diffA.UnsortedList())
101-
require.ElementsMatch(t, tc.wantMissingB.UnsortedList(), diffB.UnsortedList())
108+
require.ElementsMatch(t, tc.wantMissingA.UnsortedList(), diffA.UnsortedList())
109+
require.ElementsMatch(t, tc.wantMissingB.UnsortedList(), diffB.UnsortedList())
110+
})
102111
}
103112
}

linter/sets/prefix.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ func RemovePrefixes(items Set[string], sets ...Set[string]) Set[string] {
3232
nonPrefixes := maps.Clone(items)
3333

3434
values := Union(append(sets, items)...).UnsortedList()
35-
slices.Sort(values)
35+
slices.SortFunc(values, func(a, b string) int {
36+
aSort := strings.ReplaceAll(a, "[", ".[") + "."
37+
bSort := strings.ReplaceAll(b, "[", ".[") + "."
38+
return strings.Compare(aSort, bSort)
39+
})
40+
3641
for i := 0; i < len(values); i++ {
37-
// If the next value is an extension of the current value, skip
42+
// If the next value is an extension of the current value, remove
3843
// the current value.
3944
if i+1 < len(values) && (strings.HasPrefix(values[i+1], values[i]+".") || strings.HasPrefix(values[i+1], values[i]+"[")) {
4045
nonPrefixes.Delete(values[i])
@@ -54,12 +59,21 @@ func RemoveExtensions(items Set[string], sets ...Set[string]) Set[string] {
5459
nonExtensions := maps.Clone(items)
5560

5661
values := Union(append(sets, items)...).UnsortedList()
57-
slices.Sort(values)
62+
slices.SortFunc(values, func(a, b string) int {
63+
aSort := strings.ReplaceAll(a, "[", ".[") + "."
64+
bSort := strings.ReplaceAll(b, "[", ".[") + "."
65+
return strings.Compare(aSort, bSort)
66+
})
67+
68+
OuterLoop:
5869
for i := 0; i < len(values); i++ {
59-
// If the next value is an extension of the current value, skip
60-
// the current value.
61-
if i+1 < len(values) && (strings.HasPrefix(values[i+1], values[i]+".") || strings.HasPrefix(values[i+1], values[i]+"[")) {
62-
nonExtensions.Delete(values[i+1])
70+
// Remove all following values that are extensions of the current value.
71+
for j := i + 1; j < len(values); j++ {
72+
if !strings.HasPrefix(values[j], values[i]+".") && !strings.HasPrefix(values[j], values[i]+"[") {
73+
continue OuterLoop
74+
}
75+
76+
nonExtensions.Delete(values[j])
6377
}
6478
}
6579

0 commit comments

Comments
 (0)