aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/subsystem/match_test.go
blob: f2a3c885bacd96ae8046fe30425846e79843595b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package subsystem

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestPathMatcher(t *testing.T) {
	arm := &Subsystem{
		PathRules: []PathRule{
			{
				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
				IncludeRegexp: `^drivers/irqchip/irq-vic\.c$|^Documentation/devicetree/bindings/interrupt-controller/arm,vic\.yaml$`,
			},
		},
	}
	docs := &Subsystem{
		PathRules: []PathRule{
			{IncludeRegexp: `^Documentation/.*$`},
		},
	}
	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/a.c`))
	assert.ElementsMatch(t, []*Subsystem{docs}, m.Match(`Documentation/a/b/c.md`))
	assert.Empty(t, m.Match(`arch/boot/dts/a.c`))
}

func TestPathMatchOrder(t *testing.T) {
	s := &Subsystem{
		PathRules: []PathRule{
			{
				IncludeRegexp: `^a/b/.*$`,
				ExcludeRegexp: `^a/.*$`,
			},
		},
	}
	m := MakePathMatcher([]*Subsystem{s})
	// If we first exclude a/, then a/b/c never matches.
	assert.Empty(t, m.Match("a/b/c"))
}