-
Notifications
You must be signed in to change notification settings - Fork 4
/
gitglob_test.go
90 lines (71 loc) · 2.56 KB
/
gitglob_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func tempLoc() string {
return filepath.Join(os.TempDir(), "snag/test/stuff")
}
func createFiles(t *testing.T, names []string) {
for _, name := range names {
f, err := os.Create(filepath.Join(tempLoc(), name))
require.NoError(t, err, "Error creating temp file %s", err)
f.Close()
}
}
func deleteFiles(t *testing.T, names []string) {
for _, name := range names {
err := os.Remove(filepath.Join(tempLoc(), name))
require.NoError(t, err, "Error removing temp file %s", err)
}
}
func TestGlobMatch(t *testing.T) {
v := tempLoc()
require.NoError(t, os.MkdirAll(v, 0777))
defer os.RemoveAll(os.TempDir() + "/snag")
// blank line
assert.False(t, globMatch("", v))
// a comment
assert.False(t, globMatch("#a comment", v))
// regular match no slash
assert.True(t, globMatch("gitglob.go", "gitglob.go"))
// negation no slash
assert.False(t, globMatch("!gitglob.go", "gitglob.go"))
// match with slash
tmpFiles := []string{"foo.txt"}
createFiles(t, tmpFiles)
assert.True(t, globMatch(tempLoc()+"/foo.txt", v+"/foo.txt"))
deleteFiles(t, tmpFiles)
// negate match with slash
tmpFiles = []string{"foo.txt"}
createFiles(t, tmpFiles)
assert.False(t, globMatch("!"+tempLoc()+"/foo.txt", v+"/foo.txt"))
deleteFiles(t, tmpFiles)
// directory
assert.True(t, globMatch(tempLoc(), v))
// directory with trailing slash
assert.True(t, globMatch(tempLoc()+"/", v))
// star matching
tmpFiles = []string{"foo.txt"}
createFiles(t, tmpFiles)
assert.True(t, globMatch(tempLoc()+"/*.txt", v+"/foo.txt"))
assert.False(t, globMatch(tempLoc()+"/*.txt", v+"/somedir/foo.txt"))
deleteFiles(t, tmpFiles)
// double star prefix
assert.True(t, globMatch("**/foo.txt", v+"/hello/foo.txt"))
assert.True(t, globMatch("**/foo.txt", v+"/some/dirs/foo.txt"))
// double star suffix
assert.True(t, globMatch(tempLoc()+"/hello/**", v+"/hello/foo.txt"))
assert.False(t, globMatch(tempLoc()+"/hello/**", v+"/some/dirs/foo.txt"))
// double star in path
assert.True(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/hello/world.txt"))
assert.True(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/hello/stuff/world.txt"))
assert.False(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/some/dirs/foo.txt"))
// negate doubl start patterns
assert.False(t, globMatch("!**/foo.txt", v+"/hello/foo.txt"))
assert.False(t, globMatch("!"+tempLoc()+"/hello/**", v+"/hello/foo.txt"))
assert.False(t, globMatch("!"+tempLoc()+"/hello/**/world.txt", v+"/hello/world.txt"))
}