From 580328ece7c23cc428a4943f1c9ef316c124b00b Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Mon, 6 Dec 2021 21:55:26 +0300 Subject: [PATCH] [Native][tests] Fix: Avoid IO exceptions on Windows while expanding globs --- .../konan/blackboxtest/support/util/Globs.kt | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Globs.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Globs.kt index 28a648968e4..cb5032c5b18 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Globs.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Globs.kt @@ -5,31 +5,32 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.util +import org.junit.jupiter.api.Assertions.assertTrue import java.io.File import java.nio.file.* import java.nio.file.attribute.BasicFileAttributes -import kotlin.io.path.name /** - * Naive sub-optimal implementation of glob expansion. + * Naive suboptimal implementation of glob expansion. */ -internal fun expandGlobTo(unexpendedPath: File, output: MutableCollection) { - val normalizedUnexpandedPath = Paths.get(unexpendedPath.path).toAbsolutePath().normalize() +internal fun expandGlobTo(unexpandedPath: File, output: MutableCollection) { + assertTrue(unexpandedPath.isAbsolute) { "Path must be absolute: $unexpandedPath" } - val paths = generateSequence(normalizedUnexpandedPath) { it.parent }.toMutableList().apply { reverse() } + val paths: List = generateSequence(unexpandedPath) { it.parentFile }.toMutableList().apply { reverse() } for (index in 1 until paths.size) { - val path = paths[index] + val path: File = paths[index] val isGlob = '*' in path.name if (isGlob) { - val basePath = paths[index - 1] + val basePath: File = paths[index - 1] + val basePathAsPath: Path = basePath.toPath() - val pattern = basePath.relativize(normalizedUnexpandedPath).toString() - val matcher = FileSystems.getDefault().getPathMatcher("glob:$pattern") + val pattern: String = unexpandedPath.relativeTo(basePath).path + val matcher: PathMatcher = FileSystems.getDefault().getPathMatcher("glob:$pattern") - Files.walkFileTree(basePath, object : SimpleFileVisitor() { + Files.walkFileTree(basePathAsPath, object : SimpleFileVisitor() { override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { - if (matcher.matches(basePath.relativize(file))) output += file.toFile() + if (matcher.matches(basePathAsPath.relativize(file))) output += file.toFile() return FileVisitResult.CONTINUE } }) @@ -39,7 +40,7 @@ internal fun expandGlobTo(unexpendedPath: File, output: MutableCollection) } // No globs to expand. - output += normalizedUnexpandedPath.toFile() + output += unexpandedPath } internal fun expandGlob(unexpendedPath: File): Collection = buildList { expandGlobTo(unexpendedPath, this) }