Skip to content

Commit

Permalink
SelectorUtils.matchPath(): inconsistent behaviour on POSIX-like and W…
Browse files Browse the repository at this point in the history
…indows for single Wildcard pattern (#191)

This fixes #191 and closes #192
  • Loading branch information
fishermans authored and michael-o committed May 17, 2022
1 parent 8dec931 commit 407a93c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/main/java/org/codehaus/plexus/util/SelectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,32 @@ public static boolean matchPath( String pattern, String str, String separator, b
{
if ( isRegexPrefixedPattern( pattern ) )
{
pattern =
String localPattern =
pattern.substring( REGEX_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length() );

return str.matches( pattern );
return str.matches( localPattern );
}
else
{
if ( isAntPrefixedPattern( pattern ) )
{
pattern = pattern.substring( ANT_HANDLER_PREFIX.length(),
pattern.length() - PATTERN_HANDLER_SUFFIX.length() );
}
String localPattern = isAntPrefixedPattern( pattern )
? pattern.substring( ANT_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length() )
: pattern;
final String osRelatedPath = toOSRelatedPath( str, separator );
final String osRelatedPattern = toOSRelatedPath( localPattern, separator );
return matchAntPathPattern( osRelatedPattern, osRelatedPath, separator, isCaseSensitive );
}
}

return matchAntPathPattern( pattern, str, separator, isCaseSensitive );
private static String toOSRelatedPath( String pattern, String separator )
{
if ( "/".equals( separator ) )
{
return pattern.replace( "\\", separator );
}
if ( "\\".equals( separator ) ) {
return pattern.replace( "/", separator );
}
return pattern;
}

static boolean isRegexPrefixedPattern( String pattern )
Expand Down
52 changes: 51 additions & 1 deletion src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* <p>SelectorUtilsTest class.</p>
*
* @author herve
* @version $Id: $Id
* @since 3.4.0
*/
public class SelectorUtilsTest
Expand Down Expand Up @@ -92,4 +91,55 @@ public void testMatchPath_WindowsFileSeparator()
// Pattern and target don't start with file separator
assertTrue( SelectorUtils.matchPath( "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false ) );
}

@Test
public void testPatternMatchSingleWildcardPosix()
{
assertFalse(SelectorUtils.matchPath(
"/com/test/*",
"/com/test/test/hallo"));
}


@Test
public void testPatternMatchDoubleWildcardCaseInsensitivePosix()
{
assertTrue(SelectorUtils.matchPath(
"/com/test/**",
"/com/test/test/hallo"));
}


@Test
public void testPatternMatchDoubleWildcardPosix()
{
assertTrue(SelectorUtils.matchPath(
"/com/test/**",
"/com/test/test/hallo"));
}


@Test
public void testPatternMatchSingleWildcardWindows()
{
assertFalse(SelectorUtils.matchPath(
"D:\\com\\test\\*",
"D:\\com\\test\\test\\hallo"));

assertFalse(SelectorUtils.matchPath(
"D:/com/test/*",
"D:/com/test/test/hallo"));
}

@Test
public void testPatternMatchDoubleWildcardWindows()
{
assertTrue(SelectorUtils.matchPath(
"D:\\com\\test\\**",
"D:\\com\\test\\test\\hallo"));

assertTrue(SelectorUtils.matchPath(
"D:\\com\\test\\**",
"D:/com/test/test/hallo"));
}
}

0 comments on commit 407a93c

Please sign in to comment.