[minor] Fix file pattern matching test and converting paths to universal separator on calling the walking function

This commit is contained in:
Ilya Chernikov
2019-10-11 12:19:09 +02:00
parent b7f9694f3a
commit d51291b187
2 changed files with 5 additions and 3 deletions
@@ -62,12 +62,14 @@ internal val pathElementPattern = if (File.separatorChar == '/') "[^/]*" else "[
internal val pathSeparatorPattern = if (File.separatorChar == '/') "/" else "[/${File.separatorChar.escape()}]."
internal val specialPatternChars = patternCharsToEscape + pathSeparatorChars
private fun String.toUniversalSeparator(): String = if (File.separatorChar == '/') this else replace(File.separatorChar, '/')
internal fun forAllMatchingFilesInDirectory(baseDir: File, namePattern: String, body: (String, InputStream) -> Unit) {
val patternStart = namePattern.indexOfAny(wildcardChars)
if (patternStart < 0) {
// assuming a single file
baseDir.resolve(namePattern).takeIf { it.exists() && it.isFile }?.let { file ->
body(file.relativeToOrSelf(baseDir).path, file.inputStream())
body(file.relativeToOrSelf(baseDir).path.toUniversalSeparator(), file.inputStream())
}
} else {
val patternDirStart = namePattern.lastIndexOfAny(pathSeparatorChars, patternStart)
@@ -77,7 +79,7 @@ internal fun forAllMatchingFilesInDirectory(baseDir: File, namePattern: String,
root.walkTopDown().filter {
re.matches(it.relativeToOrSelf(root).path)
}.forEach { file ->
body(file.relativeToOrSelf(baseDir).path, file.inputStream())
body(file.relativeToOrSelf(baseDir).path.toUniversalSeparator(), file.inputStream())
}
}
}
@@ -18,7 +18,7 @@ class UtilsTest : TestCase() {
fun testPatternConversionWildcards() {
assertPattern("a${pathSeparatorPattern}b\\.$pathElementPattern", "a/b.*")
assertPattern("a$pathSeparatorPattern$pathElementPattern\\.txt", "a/*.txt")
assertPattern("a$pathSeparatorPattern.*/b", "a/**/b")
assertPattern("a$pathSeparatorPattern.*${pathSeparatorPattern}b", "a/**/b")
assertPattern("a${pathSeparatorPattern}b.\\.txt", "a/b?.txt")
assertPattern("$pathElementPattern/b\\.txt", "*/b.txt")
assertPattern(".*${pathSeparatorPattern}b\\.txt", "**/b.txt")