fork of https://github.com/sourcegraph/zoekt
1package ignore
2
3import (
4 "bytes"
5 "reflect"
6 "strings"
7 "testing"
8
9 "github.com/gobwas/glob"
10)
11
12func TestParseIgnoreFile(t *testing.T) {
13 tests := []struct {
14 ignoreFile []byte
15 wantIgnoreList []glob.Glob
16 }{
17 {
18 ignoreFile: []byte("# ignore this \n \n foo\n bar/"),
19 wantIgnoreList: []glob.Glob{
20 glob.MustCompile("foo**", '/'),
21 glob.MustCompile("bar/**", '/'),
22 },
23 },
24 {
25 ignoreFile: []byte("/foo/bar \n /qux \n *.go\nfoo.go"),
26 wantIgnoreList: []glob.Glob{
27 glob.MustCompile("foo/bar**", '/'),
28 glob.MustCompile("qux**", '/'),
29 glob.MustCompile("*.go", '/'),
30 glob.MustCompile("foo.go", '/'),
31 },
32 },
33 }
34
35 for _, tt := range tests {
36 m, err := ParseIgnoreFile(bytes.NewReader(tt.ignoreFile))
37 if err != nil {
38 t.Error(err)
39 }
40 if !reflect.DeepEqual(m.ignoreList, tt.wantIgnoreList) {
41 t.Errorf("got %v, expected %v", m.ignoreList, tt.wantIgnoreList)
42 }
43 }
44}
45
46func TestIgnoreMatcher(t *testing.T) {
47 ignoreFile := `
48dir1/
49*.go
50**/data.*
51`
52 ig, err := ParseIgnoreFile(strings.NewReader(ignoreFile))
53 if err != nil {
54 t.Errorf("error in ignoreFile")
55 }
56 tests := []struct {
57 path string
58 wantMatch bool
59 }{
60 {
61 path: "dir1/readme.md",
62 wantMatch: true,
63 },
64 {
65 path: "dir1/dir2/readme.md",
66 wantMatch: true,
67 },
68
69 {
70 path: "foo.go",
71 wantMatch: true,
72 },
73 {
74 path: "dir2/foo.go",
75 wantMatch: false,
76 },
77 {
78 path: "dir3/data.xyz",
79 wantMatch: true,
80 },
81 }
82 for _, tt := range tests {
83 t.Run(tt.path, func(t *testing.T) {
84 if got := ig.Match(tt.path); got != tt.wantMatch {
85 t.Errorf("got %t, expected %t", got, tt.wantMatch)
86 }
87 })
88 }
89}