From 60644f5829dd744b822ac4394c2b1f55c4306ba5 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 14 Feb 2025 12:53:07 +0100 Subject: pkg/subsystem: fix a bug in PathMatcher It used to be the case that, if a subsystem matches several rule conditions, it could be returned several times. This has led to incorrect parent inference results. --- pkg/subsystem/match.go | 8 +++++--- pkg/subsystem/match_test.go | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'pkg/subsystem') diff --git a/pkg/subsystem/match.go b/pkg/subsystem/match.go index ad5d3a257..475222c77 100644 --- a/pkg/subsystem/match.go +++ b/pkg/subsystem/match.go @@ -4,7 +4,9 @@ package subsystem import ( + "maps" "regexp" + "slices" "strings" ) @@ -48,7 +50,7 @@ func (p *PathMatcher) register(item *Subsystem) { } func (p *PathMatcher) Match(path string) []*Subsystem { - ret := []*Subsystem{} + ret := map[*Subsystem]struct{}{} for _, m := range p.matches { if m.exclude != nil && m.exclude.MatchString(path) { continue @@ -56,9 +58,9 @@ func (p *PathMatcher) Match(path string) []*Subsystem { if m.include != nil && !m.include.MatchString(path) { continue } - ret = append(ret, m.object) + ret[m.object] = struct{}{} } - return ret + return slices.Collect(maps.Keys(ret)) } func buildMatch(rule PathRule, item *Subsystem) *match { diff --git a/pkg/subsystem/match_test.go b/pkg/subsystem/match_test.go index 7c96b0bcd..f2a3c885b 100644 --- a/pkg/subsystem/match_test.go +++ b/pkg/subsystem/match_test.go @@ -16,6 +16,10 @@ func TestPathMatcher(t *testing.T) { IncludeRegexp: `^arch/arm/.*$`, ExcludeRegexp: `^arch/arm/boot/dts/.*$`, }, + // Add a somewhat overlapping rule so that we test that no duplicates are returned. + { + IncludeRegexp: `^arch/arm/a/.*$`, + }, {IncludeRegexp: `^drivers/spi/spi-pl022\.c$`}, { // nolint:lll @@ -31,7 +35,7 @@ func TestPathMatcher(t *testing.T) { m := MakePathMatcher([]*Subsystem{arm, docs}) assert.ElementsMatch(t, []*Subsystem{arm, docs}, m.Match(`Documentation/devicetree/bindings/interrupt-controller/arm,vic.yaml`)) - assert.ElementsMatch(t, []*Subsystem{arm}, m.Match(`arch/arm/a.c`)) + assert.ElementsMatch(t, []*Subsystem{arm}, m.Match(`arch/arm/a/a.c`)) assert.ElementsMatch(t, []*Subsystem{docs}, m.Match(`Documentation/a/b/c.md`)) assert.Empty(t, m.Match(`arch/boot/dts/a.c`)) } -- cgit mrf-deployment