aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/gitignore/dir_test.go
blob: 61f3fc05adb992a409efadfdbfed04e76eef9eb0 (plain) (blame)
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
package gitignore

import (
	"os"
	"testing"

	"gopkg.in/src-d/go-billy.v2"
	"gopkg.in/src-d/go-billy.v2/memfs"
)

func setupTestFS(subdirError bool) billy.Filesystem {
	fs := memfs.New()
	f, _ := fs.Create(".gitignore")
	f.Write([]byte("vendor/g*/\n"))
	f.Close()
	fs.MkdirAll("vendor", os.ModePerm)
	f, _ = fs.Create("vendor/.gitignore")
	f.Write([]byte("!github.com/\n"))
	f.Close()
	fs.MkdirAll("another", os.ModePerm)
	fs.MkdirAll("vendor/github.com", os.ModePerm)
	fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
	return fs
}

func TestDir_ReadPatterns(t *testing.T) {
	ps, err := ReadPatterns(setupTestFS(false), nil)
	if err != nil {
		t.Errorf("no error expected, found %v", err)
	}
	if len(ps) != 2 {
		t.Errorf("expected 2 patterns, found %v", len(ps))
	}
	m := NewMatcher(ps)
	if !m.Match([]string{"vendor", "gopkg.in"}, true) {
		t.Error("expected a match")
	}
	if m.Match([]string{"vendor", "github.com"}, true) {
		t.Error("expected no match")
	}
}