/
WildcardTest.java
214 lines (197 loc) · 9.69 KB
/
WildcardTest.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package git.path;
import com.google.common.io.ByteStreams;
import org.eclipse.jgit.errors.InvalidPatternException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
/**
* Test wildcard parsing.
*
* @author Artem V. Navrotskiy <bozaro@users.noreply.github.com>
*/
public class WildcardTest {
@DataProvider
public static Object[][] splitPatternData() {
return new Object[][]{
new Object[]{"foo", new String[]{"foo"}},
new Object[]{"foo/", new String[]{"foo/"}},
new Object[]{"/bar", new String[]{"/", "bar"}},
new Object[]{"/foo/bar/**", new String[]{"/", "foo/", "bar/", "**"}},
};
}
@Test(dataProvider = "splitPatternData")
public static void splitPatternTest(@NotNull String pattern, @NotNull String[] expected) {
final List<String> actual = WildcardHelper.splitPattern(pattern);
Assert.assertEquals(actual.toArray(new String[actual.size()]), expected, pattern);
}
@DataProvider
public static Object[][] normalizePatternData() {
return new Object[][]{
// Simple mask
new Object[]{"/", new String[0]},
new Object[]{"*/", new String[]{"*/", "**/"}},
new Object[]{"*", new String[]{"**/", "*"}},
new Object[]{"**", new String[]{"**/", "*"}},
new Object[]{"**/", new String[]{"**/"}},
new Object[]{"foo", new String[]{"**/", "foo"}},
new Object[]{"foo/", new String[]{"**/", "foo/"}},
new Object[]{"/foo", new String[]{"foo"}},
// Convert path file mask
new Object[]{"foo/**.bar", new String[]{"foo/", "**/", "*.bar"}},
new Object[]{"foo/***.bar", new String[]{"foo/", "**/", "*.bar"}},
// Collapse and reorder adjacent masks
new Object[]{"foo/*/bar", new String[]{"foo/", "*/", "bar"}},
new Object[]{"foo/**/bar", new String[]{"foo/", "**/", "bar"}},
new Object[]{"foo/*/*/bar", new String[]{"foo/", "*/", "*/", "bar"}},
new Object[]{"foo/**/*/bar", new String[]{"foo/", "*/", "**/", "bar"}},
new Object[]{"foo/*/**/bar", new String[]{"foo/", "*/", "**/", "bar"}},
new Object[]{"foo/*/**.bar", new String[]{"foo/", "*/", "**/", "*.bar"}},
new Object[]{"foo/**/**/bar", new String[]{"foo/", "**/", "bar"}},
new Object[]{"foo/**/**.bar", new String[]{"foo/", "**/", "*.bar"}},
new Object[]{"foo/**/*/**/*/bar", new String[]{"foo/", "*/", "*/", "**/", "bar"}},
new Object[]{"foo/**/*/**/*/**.bar", new String[]{"foo/", "*/", "*/", "**/", "*.bar"}},
// Collapse trailing masks
new Object[]{"foo/**", new String[]{"foo/", "**/", "*"}},
new Object[]{"foo/**/*", new String[]{"foo/", "**/", "*"}},
new Object[]{"foo/**/*/*", new String[]{"foo/", "*/", "**/", "*"}},
new Object[]{"foo/**/", new String[]{"foo/", "**/"}},
new Object[]{"foo/**/*/", new String[]{"foo/", "*/", "**/"}},
new Object[]{"foo/**/*/*/", new String[]{"foo/", "*/", "*/", "**/"}},
};
}
@Test(dataProvider = "normalizePatternData")
public static void normalizePatternTest(@NotNull String pattern, @NotNull String[] expected) {
final List<String> actual = WildcardHelper.normalizePattern(WildcardHelper.splitPattern(pattern));
Assert.assertTrue(actual.size() > 0);
Assert.assertEquals(actual.remove(0), "/");
Assert.assertEquals(actual.toArray(new String[actual.size()]), expected, pattern);
}
@DataProvider
public static Object[][] pathMatcherData() {
return new Object[][]{
// Simple pattern
new Object[]{"/", "foo/bar", true, null},
new Object[]{"*", "foo/bar", true, true},
new Object[]{"*/", "foo/bar", true, null},
new Object[]{"/", "foo/bar/", true, null},
new Object[]{"*", "foo/bar/", true, true},
new Object[]{"*/", "foo/bar/", true, true},
new Object[]{"**/", "foo/bar/", true, true},
new Object[]{"foo/**/", "foo/bar/", true, true},
new Object[]{"foo/**/", "foo/bar/xxx", true, null},
new Object[]{"foo/**/", "foo/bar/xxx/", true, true},
new Object[]{"f*o", "foo/bar", true, null},
new Object[]{"/f*o", "foo/bar", true, null},
new Object[]{"f*o/", "foo/bar", true, null},
new Object[]{"foo/", "foo/bar", true, null},
new Object[]{"/foo/", "foo/bar", true, null},
new Object[]{"/foo", "foo/", true, true},
new Object[]{"foo", "foo/", true, true},
new Object[]{"foo/", "foo/", true, true},
new Object[]{"foo/", "foo", null, null},
new Object[]{"bar", "foo/bar", true, true},
new Object[]{"b*r", "foo/bar", true, true},
new Object[]{"/bar", "foo/bar", null, null},
new Object[]{"bar/", "foo/bar", null, null},
new Object[]{"b*r/", "foo/bar", null, null},
new Object[]{"bar/", "foo/bar/", true, true},
new Object[]{"b*r/", "foo/bar/", true, true},
new Object[]{"b[a-z]r", "foo/bar", true, true},
new Object[]{"b[a-z]r", "foo/b0r", null, null},
new Object[]{"b[a-z]r", "foo/b0r/", false, false},
new Object[]{"/t*e*t", "test", true, true},
// More complex pattern
new Object[]{"foo/*/bar/", "foo/bar/", false, false},
new Object[]{"foo/*/bar/", "bar/", null, null},
new Object[]{"foo/*/bar/", "foo/a/bar/", true, true},
new Object[]{"foo/*/bar/", "foo/a/b/bar/", null, null},
new Object[]{"foo/*/*/bar/", "foo/a/b/bar/", true, true},
new Object[]{"foo/**/bar/a/", "foo/bar/b/bar/a/", true, true},
new Object[]{"foo/**/bar/a/", "foo/bar/bar/bar/a/", true, true},
new Object[]{"foo/**/bar/a/", "foo/bar/bar/b/a/", false, false},
new Object[]{"foo/**/bar/", "foo/bar/", true, true},
new Object[]{"foo/**/bar/", "bar/", null, null},
new Object[]{"foo/**/bar/", "foo/a/bar/", true, true},
new Object[]{"foo/**/bar/", "foo/a/b/bar/", true, true},
new Object[]{"foo/*/**/*/bar/", "foo/a/bar/", false, false},
new Object[]{"foo/*/**/*/bar/", "foo/a/b/bar/", true, true},
new Object[]{"foo/*/**/*/bar/", "foo/a/b/c/bar/", true, true},
new Object[]{"foo/**/xxx/**/bar/", "foo/xxx/bar/", true, true},
new Object[]{"foo/**/xxx/**/bar/", "foo/xxx/b/c/bar/", true, true},
new Object[]{"foo/**/xxx/**/bar/", "foo/a/xxx/c/bar/", true, true},
new Object[]{"foo/**/xxx/**/bar/", "foo/a/c/xxx/bar/", true, true},
new Object[]{"foo/**/xxx/**/bar/", "foo/bar/xxx/", false, false},
new Object[]{"foo/**/xxx/**/bar/", "foo/bar/xxx/bar/", true, true},
new Object[]{"foo/**/xxx/**/bar/", "foo/bar/xxx/xxx/bar/", true, true},
};
}
@Test(dataProvider = "pathMatcherData")
public static void nativeMatcherExactTest(@NotNull String pattern, @NotNull String path, @Nullable Boolean ignored, @Nullable Boolean expectedMatch) throws InvalidPatternException, IOException, InterruptedException {
Path temp = Files.createTempDirectory("git-matcher");
try {
if (new ProcessBuilder()
.directory(temp.toFile())
.command("git", "init", ".")
.start()
.waitFor() != 0) {
throw new SkipException("Can't find git");
}
Files.write(temp.resolve(".gitattributes"), (pattern + " test\n").getBytes(StandardCharsets.UTF_8));
byte[] output = ByteStreams.toByteArray(
new ProcessBuilder()
.directory(temp.toFile())
.command("git", "check-attr", "-a", "--", path)
.start()
.getInputStream()
);
Assert.assertEquals(output.length > 0, expectedMatch == Boolean.TRUE);
} finally {
Files.walkFileTree(temp, new DeleteTreeVisitor());
}
}
@Test(dataProvider = "pathMatcherData")
public static void pathMatcherPrefixTest(@NotNull String pattern, @NotNull String path, @Nullable Boolean expectedMatch, @Nullable Boolean ignored) throws InvalidPatternException {
pathMatcherCheck(pattern, path, false, expectedMatch);
}
@Test(dataProvider = "pathMatcherData")
public static void pathMatcherExactTest(@NotNull String pattern, @NotNull String path, @Nullable Boolean ignored, @Nullable Boolean expectedMatch) throws InvalidPatternException {
pathMatcherCheck(pattern, path, true, expectedMatch);
}
private static void pathMatcherCheck(@NotNull String pattern, @NotNull String path, boolean exact, @Nullable Boolean expectedMatch) throws InvalidPatternException {
PathMatcher matcher = WildcardHelper.createMatcher(pattern, exact);
for (String name : WildcardHelper.splitPattern(path)) {
if (matcher == null) break;
boolean isDir = name.endsWith("/");
matcher = matcher.createChild(isDir ? name.substring(0, name.length() - 1) : name, isDir);
}
if (expectedMatch == null) {
Assert.assertNull(matcher);
} else {
Assert.assertNotNull(matcher);
Assert.assertEquals(matcher.isMatch(), expectedMatch.booleanValue());
}
}
@DataProvider
public static Object[][] tryRemoveBackslashesData() {
return new Object[][]{
new Object[]{"test", "test"},
new Object[]{"test\\n", "test\\n"},
new Object[]{"space\\ ", "space "},
new Object[]{"foo\\!bar\\ ", "foo!bar "},
new Object[]{"\\#some", "#some"},
new Object[]{"foo\\[bar", "foo\\[bar"},
};
}
@Test(dataProvider = "tryRemoveBackslashesData")
public static void tryRemoveBackslashesTest(@NotNull String pattern, @NotNull String expected) {
Assert.assertEquals(WildcardHelper.tryRemoveBackslashes(pattern), expected);
}
}