Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support glob pattern **. to match zero or more packages #1194

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pitest/src/main/java/org/pitest/util/Glob.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Glob implements Predicate<String> {

private final Pattern regex;

private static final String ZERO_OR_MORE_PACKAGES = "(?:.*\\.)*";
private static final String DOUBLE_STAR_PACKAGE_MARKER = "#%#%#";

public Glob(final String glob) {
String rectifiedGlob;
if (glob.startsWith("~")) {
Expand All @@ -50,9 +53,10 @@ public static Collection<Predicate<String>> toGlobPredicates(
}

private static String convertGlobToRegex(final String glob) {
final String preparedGlob = glob.replace("**.", DOUBLE_STAR_PACKAGE_MARKER);
final StringBuilder out = new StringBuilder("^");
for (int i = 0; i < glob.length(); ++i) {
final char c = glob.charAt(i);
for (int i = 0; i < preparedGlob.length(); ++i) {
final char c = preparedGlob.charAt(i);
switch (c) {
case '$':
out.append("\\$");
Expand All @@ -74,7 +78,8 @@ private static String convertGlobToRegex(final String glob) {
}
}
out.append('$');
return out.toString();
return out.toString()
.replace(DOUBLE_STAR_PACKAGE_MARKER, ZERO_OR_MORE_PACKAGES);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions pitest/src/test/java/org/pitest/util/GlobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ public void matchesStringsWithPlusSign() {
assertFalse(glob.matches("foo-Bar-car"));
}

@Test
public void shouldSupportDoubleStarPackageMatcher() {
final Glob glob = new Glob("**.databinding.**.Foo");
assertTrue(glob.matches("databinding.Foo"));
assertTrue(glob.matches("databinding.bar.Foo"));
assertTrue(glob.matches("databinding.bar.car.Foo"));
assertTrue(glob.matches("foo.databinding.Foo"));
assertTrue(glob.matches("foo.car.databinding.bar.Foo"));
assertTrue(glob.matches(".databinding.Foo"));
assertFalse(glob.matches("databindingfoo.Foo"));
assertFalse(glob.matches("foodatabinding.Foo"));
assertFalse(glob.matches("databinding.fooFoo"));
}
}