diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2025-02-14 12:53:07 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2025-02-14 12:15:28 +0000 |
| commit | 60644f5829dd744b822ac4394c2b1f55c4306ba5 (patch) | |
| tree | 38a95d0f2c738e136d9eef7bd427a2150c9a9774 /pkg/subsystem | |
| parent | 51ca21064fc15b860fcd59e386b4a67009f68fc0 (diff) | |
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.
Diffstat (limited to 'pkg/subsystem')
| -rw-r--r-- | pkg/subsystem/match.go | 8 | ||||
| -rw-r--r-- | pkg/subsystem/match_test.go | 6 |
2 files changed, 10 insertions, 4 deletions
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`)) } |
