-
Notifications
You must be signed in to change notification settings - Fork 123
/
path_utils_test.go
111 lines (97 loc) · 2.48 KB
/
path_utils_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package gitbase
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
fixtures "github.com/src-d/go-git-fixtures"
)
func TestPatternMatches(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
testCases := []struct {
path string
expected []string
}{
{"cmd", []string{
filepath.Join(wd, "cmd"),
}},
{"cmd/*", []string{
filepath.Join(wd, "cmd/gitbase"),
}},
{"cmd/gitbase/*", []string{
filepath.Join(wd, "cmd/gitbase/command"),
filepath.Join(wd, "cmd/gitbase/main.go"),
}},
{"cmd/../cmd/gitbase/*", []string{
filepath.Join(wd, "cmd/gitbase/command"),
filepath.Join(wd, "cmd/gitbase/main.go"),
}},
}
for _, test := range testCases {
t.Run(test.path, func(t *testing.T) {
files, err := PatternMatches(test.path)
require.NoError(t, err)
require.Exactly(t, test.expected, files)
})
}
}
func TestIsGitRepo(t *testing.T) {
var require = require.New(t)
ok, err := IsGitRepo("/do/not/exist")
require.NoError(err)
require.False(ok)
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
ok, err = IsGitRepo(path)
require.NoError(err)
require.True(ok)
}
func TestIsSivaFile(t *testing.T) {
var require = require.New(t)
require.True(IsSivaFile("is.siva"))
require.False(IsSivaFile("not-siva"))
}
func TestStripPrefix(t *testing.T) {
testCases := []struct {
root string
path string
expected string
}{
{
"_testdata/*",
"_testdata/05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
"05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
},
{
"_testdata/*",
"_testdata/foo/05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
"foo/05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
},
}
for _, tt := range testCases {
t.Run(tt.path, func(t *testing.T) {
output, err := StripPrefix(tt.root, tt.path)
require.NoError(t, err)
require.Equal(t, tt.expected, output)
})
}
}
func TestCleanGlob(t *testing.T) {
testCases := []struct {
pattern string
expected string
}{
{"../../../_testdata/?epositories", "../../../_testdata"},
{"../../../_testdata/**/repositories", "../../../_testdata"},
{"../../../_testdata/*/repositories", "../../../_testdata"},
{"../../../_testdata/*", "../../../_testdata"},
{"../../../_testdata/\\*/foo", "../../../_testdata"},
{"../../../_testdata/[a-z]/foo", "../../../_testdata"},
}
for _, tt := range testCases {
t.Run(tt.pattern, func(t *testing.T) {
output := cleanGlob(tt.pattern)
require.Equal(t, tt.expected, output)
})
}
}