-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir_test.go
73 lines (64 loc) · 1.99 KB
/
dir_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.
package gitignore_test
import (
"github.com/teris-io/gitignore"
"testing"
"fmt"
)
type dir struct {
path []string
subdirError bool
}
func (d *dir) Path() []string {
return d.path
}
func (d *dir) ReadFile(name string) ([]byte, error) {
if name != ".gitignore" {
return nil, fmt.Errorf("no such file")
}
if len(d.path) == 0 {
return []byte("vendor/\n"), nil
} else if d.path[len(d.path)-1] == "vendor" {
return []byte("!github.com/\n"), nil
}
return nil, fmt.Errorf("no such file")
}
func (d *dir) Subdirs() ([]gitignore.Dir, error) {
if len(d.path) == 0 {
return []gitignore.Dir{&dir{path: append(d.path, "vendor"), subdirError: d.subdirError}, &dir{path: append(d.path, "another"), subdirError: d.subdirError}}, nil
} else if d.path[len(d.path)-1] == "vendor" {
return []gitignore.Dir{&dir{path: append(d.path, "github.com"), subdirError: d.subdirError}, &dir{path: append(d.path, "gopkg.in"), subdirError: d.subdirError}}, nil
}
if d.subdirError {
return nil, fmt.Errorf("failed to list directories")
}
return nil, nil
}
func TestDir_ReadPatterns(t *testing.T) {
patterns, err := gitignore.ReadPatterns(&dir{})
if err != nil {
t.Errorf("no error expected, found %v", err)
}
if len(patterns) != 2 {
t.Errorf("expected 2 patterns, found %v", len(patterns))
}
matcher := gitignore.NewMatcher(patterns)
if !matcher.Match([]string{"vendor"}, true) {
t.Error("expected a match")
}
if !matcher.Match([]string{"vendor", "gopkg.in"}, true) {
t.Error("expected a match")
}
if matcher.Match([]string{"vendor", "github.com"}, true) {
t.Error("expected no match")
}
}
func TestDir_ReadPatterns_error(t *testing.T) {
_, err := gitignore.ReadPatterns(&dir{subdirError: true})
if err == nil {
t.Errorf("expected an error")
} else if err.Error() != "failed to list directories" {
t.Errorf("expecte different error message, found %v", err)
}
}