-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathantutils_test.go
156 lines (146 loc) · 9.31 KB
/
antutils_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package utils
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
var separator = string(os.PathSeparator)
var paths = getFileSystemsPathsForTestingAntPattern(separator)
var testAntPathToRegExpDataProvider = []struct {
description string
antPattern string
paths []string
expectedMatchingPaths []string
}{
{"check '?' in file's name", filepath.Join("dev", "a", "b?.txt"), paths, []string{filepath.Join("dev", "a", "bb.txt"), filepath.Join("dev", "a", "bc.txt")}},
{"check '?' in directory's name", filepath.Join("dev", "a?", "b.txt"), paths, []string{filepath.Join("dev", "aa", "b.txt")}},
{"check '*' in file's name", filepath.Join("dev", "a", "b*.txt"), paths, []string{filepath.Join("dev", "a", "b.txt"), filepath.Join("dev", "a", "bb.txt"), filepath.Join("dev", "a", "bc.txt")}},
{"check '*' in directory's name", filepath.Join("dev", "*", "b.txt"), paths, []string{filepath.Join("dev", "a", "b.txt"), filepath.Join("dev", "aa", "b.txt")}},
{"check '*' in directory's name", filepath.Join("dev", "*", "a", "b.txt"), paths, nil},
{"check '**' in directory path", filepath.Join("**", "b.txt"), paths, []string{filepath.Join("dev", "a", "b.txt"), filepath.Join("dev", "aa", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("test", "a", "b.txt"), filepath.Join("test", "aa", "b.txt")}},
{"check '**' in the beginning and the end of path", filepath.Join("**", "a2", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt"), "a2"}},
{"check '**' in the beginning and the end of path", "**a2**", paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt"), "a2"}},
{"check double '**'", filepath.Join("**", "a2", "**", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
{"check '**' in the beginning and the end of file", filepath.Join("**", "b.zip", "**"), paths, []string{filepath.Join("dev", "aa", "b.zip"), filepath.Join("test", "aa", "b.zip"), "b.zip", filepath.Join("test2", "b.zip")}},
{"combine '**' and '*'", filepath.Join("**", "a2", "*"), paths, []string{filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
{"combine '**' and '*'", filepath.Join("**", "a2", "*", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
{"combine all signs", filepath.Join("**", "b?.*"), paths, []string{filepath.Join("dev", "a", "bb.txt"), filepath.Join("dev", "a", "bc.txt"), filepath.Join("dev", "aa", "bb.txt"), filepath.Join("dev", "aa", "bc.txt"), filepath.Join("dev", "aa", "bc.zip"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt"), filepath.Join("test", "a", "bb.txt"), filepath.Join("test", "a", "bc.txt"), filepath.Join("test", "aa", "bb.txt"), filepath.Join("test", "aa", "bc.txt"), filepath.Join("test", "aa", "bc.zip")}},
{"'**' all files", "**", paths, paths},
{"test2/**/b/**", filepath.Join("test2", "**", "b", "**"), paths, []string{filepath.Join("test2", "a", "b", "c.zip")}},
{"*/b.zip", filepath.Join("*", "b.zip"), paths, []string{filepath.Join("test2", "b.zip")}},
{"**/dev/**/a3/*c*", filepath.Join("dev", "**", "a3", "*c*"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "bc.txt")}},
{"**/dev/**/a3/**", filepath.Join("dev", "**", "a3", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "a3", "b.txt")}},
{"exclude 'temp/foo5/a'", filepath.Join("**", "foo", "**"), paths, []string{filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo")}},
{"include dirs", filepath.Join("tmp", "*", "**"), paths, []string{"tmp" + separator, filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo5", "a"), filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5")}},
{"include dirs", filepath.Join("tmp", "**"), paths, []string{"tmp" + separator, filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo5", "a"), filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5")}},
{"double and single wildcard", filepath.Join("**", "tmp*", "**"), paths, []string{"tmp" + separator, filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo5", "a"), filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5"), filepath.Join("Wrapper", "tmp", "boo"), filepath.Join("Wrapper", "tmp12", "boo")}},
{"exclude only sub dir", filepath.Join("**", "loo", "**", "bar", "**"), paths, []string{filepath.Join("kmp", "loo", "bar"), filepath.Join("kmp", "loo", "bar", "b"), filepath.Join("kmp", "loo", "bar", "a")}},
{"**/", "**" + separator, paths, paths},
{"xxx/x*", filepath.Join("tmp", "f*"), paths, []string{filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5")}},
{"xxx/x*x", filepath.Join("tmp", "f*5"), paths, []string{filepath.Join("tmp", "foo5")}},
{"xxx/x*", filepath.Join("dev", "a1", "a2", "b*"), paths, []string{filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
{"xxx/*x*", filepath.Join("dev", "a1", "a2", "*c*"), paths, []string{filepath.Join("dev", "a1", "a2", "bc.txt")}},
{"*", "*", paths, []string{"b.zip", "a2"}},
{"*", "*", []string{"a", "a" + separator, filepath.Join("a", "b")}, []string{"a"}},
}
// In each case, we take an array of paths, simulating a filesystem hierarchy, and an ANT pattern expression and
// check if the conversion to regular expression worked.
func TestAntPathToRegExp(t *testing.T) {
for _, test := range testAntPathToRegExpDataProvider {
t.Run(test.description, func(t *testing.T) {
regExpStr := AntToRegex(cleanPath(test.antPattern))
var matches []string
for _, checkedPath := range test.paths {
match, _ := regexp.MatchString(regExpStr, checkedPath)
if match {
matches = append(matches, checkedPath)
}
}
if !equalSlicesIgnoreOrder(matches, test.expectedMatchingPaths) {
t.Error("Unmatched! : ant pattern `" + test.antPattern + "` matches paths:\n[" + strings.Join(test.expectedMatchingPaths, ",") + "]\nbut got:\n[" + strings.Join(matches, ",") + "]")
}
})
}
}
var testAntToRegexProvider = []struct {
ant string
expectedRegex string
}{
{"a.zip", "^a\\.zip$"},
{"ab", "^ab$"},
{"**", "^(.*)$"},
{"**/", "^(.*/)*(.*)$"},
{"**/*", "^(.*/)*([^/]*)$"},
{"/**", "^(/.*)*$"},
{"*/**", "^([^/]*)(/.*)*$"},
{"/**/ab", "^/(.*/)*ab$"},
{"/**/ab*", "^/(.*/)*ab([^/]*)$"},
{"/**/ab/", "^/(.*/)*ab(/.*)*$"},
{"/**/ab/*", "^/(.*/)*ab/([^/]*)$"},
{"/**/ab*/", "^/(.*/)*ab([^/]*)(/.*)*$"},
{"ab/**/", "^ab/(.*/)*(.*)$"},
{"*ab/**/", "^([^/]*)ab/(.*/)*(.*)$"},
{"/ab/**/", "^/ab/(.*/)*(.*)$"},
{"/ab*/**/", "^/ab([^/]*)/(.*/)*(.*)$"},
{"/**/ab/**/", "^/(.*/)*ab/(.*/)*(.*)$"},
{"/**/a*b/**/", "^/(.*/)*a([^/]*)b/(.*/)*(.*)$"},
{"/**/ab/**/cd/**/ef/", "^/(.*/)*ab/(.*/)*cd/(.*/)*ef(/.*)*$"},
}
func TestAntToRegex(t *testing.T) {
for _, test := range testAntToRegexProvider {
t.Run("'"+test.ant+"'", func(t *testing.T) {
regExpStr := AntToRegex(cleanPath(strings.ReplaceAll(test.ant, "/", separator)))
expectedRegExpStr := strings.ReplaceAll(test.expectedRegex, "/", getFileSeparatorForAntToRegex())
if regExpStr != expectedRegExpStr {
t.Error("Unmatched! : ant pattern `" + test.ant + "` translated to:\n" + regExpStr + "\nbut expect it to be:\n" + expectedRegExpStr + "")
}
})
}
}
func getFileSystemsPathsForTestingAntPattern(separator string) []string {
return []string{
filepath.Join("dev", "a", "b.txt"),
filepath.Join("dev", "a", "bb.txt"),
filepath.Join("dev", "a", "bc.txt"),
filepath.Join("dev", "aa", "b.txt"),
filepath.Join("dev", "aa", "bb.txt"),
filepath.Join("dev", "aa", "bc.txt"),
filepath.Join("dev", "aa", "b.zip"),
filepath.Join("dev", "aa", "bc.zip"),
filepath.Join("dev", "a1", "a2", "a3", "b.txt"),
filepath.Join("dev", "a1", "a2", "b.txt"),
filepath.Join("dev", "a1", "a2", "a3", "bc.txt"),
filepath.Join("dev", "a1", "a2", "bc.txt"),
"a2",
filepath.Join("test", "a", "b.txt"),
filepath.Join("test", "a", "bb.txt"),
filepath.Join("test", "a", "bc.txt"),
filepath.Join("test", "aa", "b.txt"),
filepath.Join("test", "aa", "bb.txt"),
filepath.Join("test", "aa", "bc.txt"),
filepath.Join("test", "aa", "b.zip"),
filepath.Join("test", "aa", "bc.zip"),
filepath.Join("test2", "a", "b", "c.zip"),
filepath.Join("test2", "a", "bb", "c.zip"),
filepath.Join("test2", "b.zip"),
"b.zip",
"tmp" + separator,
filepath.Join("tmp", "foo"),
filepath.Join("Wrapper", "tmp", "boo"),
filepath.Join("Wrapper", "tmp12", "boo"),
filepath.Join("tmp", "foo", "a"),
filepath.Join("tmp", "foo5"),
filepath.Join("tmp", "foo5", "a"),
filepath.Join("kmp", "loo"),
filepath.Join("kmp", "loo", "bar", "a"),
filepath.Join("kmp", "loo", "bar", "b"),
filepath.Join("kmp", "loo", "bar"),
filepath.Join("kmp", "loo", "lar"),
filepath.Join("kmp", "loo", "lar", "a"),
filepath.Join("kmp", "loo", "lar"),
filepath.Join("kmp", "loo", "kar", "a"),
filepath.Join("kmp", "loo", "kar"),
}
}