forked from hmarr/codeowners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_test.go
59 lines (48 loc) · 1.31 KB
/
match_test.go
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
56
57
58
59
package codeowners
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type patternTest struct {
Name string `json:"name"`
Pattern string `json:"pattern"`
Paths map[string]bool `json:"paths"`
Focus bool `json:"focus"`
}
func TestMatch(t *testing.T) {
data, err := os.ReadFile("testdata/patterns.json")
require.NoError(t, err)
var tests []patternTest
err = json.Unmarshal(data, &tests)
require.NoError(t, err)
focus := false
for _, test := range tests {
if test.Focus {
focus = true
}
}
for _, test := range tests {
if test.Focus != focus {
continue
}
t.Run(test.Name, func(t *testing.T) {
for path, shouldMatch := range test.Paths {
pattern, err := newPattern(test.Pattern)
require.NoError(t, err)
// Debugging tips:
// - Print the generated regex: `fmt.Println(pattern.regex.String())`
// - Only run a single case by adding `"focus" : true` to the test in the JSON file
actual, err := pattern.match(path)
require.NoError(t, err)
if shouldMatch {
assert.True(t, actual, "expected pattern %s to match path %s", test.Pattern, path)
} else {
assert.False(t, actual, "expected pattern %s to not match path %s", test.Pattern, path)
}
}
})
}
}