From 86bc5984532bf16a57f95ef0941059a53c52f327 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sat, 27 Aug 2022 21:26:12 +0200 Subject: [PATCH] Replace "\" in filepath with "/" before comparing the path with a glob in which always uses "/" as separator Closes #1599 --- .../pinterest/ktlint/internal/FileUtils.kt | 11 +++- .../ktlint/internal/FileUtilsTest.kt | 60 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt index f692875b71..959870b088 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt @@ -15,6 +15,7 @@ import java.nio.file.Path import java.nio.file.Paths import java.nio.file.SimpleFileVisitor import java.nio.file.attribute.BasicFileAttributes +import kotlin.io.path.absolutePathString import kotlin.io.path.isDirectory import kotlin.system.exitProcess import kotlin.system.measureTimeMillis @@ -95,8 +96,14 @@ internal fun FileSystem.fileSequence( filePath: Path, fileAttrs: BasicFileAttributes, ): FileVisitResult { - if (negatedPathMatchers.none { it.matches(filePath) } && - pathMatchers.any { it.matches(filePath) } + val path = + if (onWindowsOS) { + Paths.get(filePath.absolutePathString().replace(File.separatorChar, '/')) + } else { + filePath + } + if (negatedPathMatchers.none { it.matches(path) } && + pathMatchers.any { it.matches(path) } ) { logger.trace { "- File: $filePath: Include" } result.add(filePath) diff --git a/ktlint/src/test/kotlin/com/pinterest/ktlint/internal/FileUtilsTest.kt b/ktlint/src/test/kotlin/com/pinterest/ktlint/internal/FileUtilsTest.kt index 89a94c7e31..96d2638ee7 100644 --- a/ktlint/src/test/kotlin/com/pinterest/ktlint/internal/FileUtilsTest.kt +++ b/ktlint/src/test/kotlin/com/pinterest/ktlint/internal/FileUtilsTest.kt @@ -7,15 +7,20 @@ import java.io.File import java.nio.file.FileSystem import java.nio.file.Files import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.absolutePathString +import kotlin.io.path.isDirectory import mu.KotlinLogging import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.DisabledOnOs import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.OS import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource import org.junit.jupiter.params.provider.ValueSource private val logger = KotlinLogging.logger {}.initKtLintKLogger() @@ -256,6 +261,61 @@ internal class FileUtilsTest { .doesNotContain(ktFile2InProjectSubDirectory) } + @EnabledOnOs(OS.WINDOWS) + @Nested + inner class ExploreFileSystem { + val rootPath = Paths.get(rootDir) + + @Test + fun `Is absolute path`() { + assertThat(rootPath.isAbsolute).isTrue + } + + @Test + fun `Is directory`() { + assertThat(rootPath.isDirectory()).isTrue + } + + @Test + fun `Has root dir name`() { + assertThat(rootPath.absolutePathString()).isEqualTo("C:\\") + } + + @ParameterizedTest(name = "Path: {0}, expected result: {1}") + @CsvSource( + value = [ + ", C:\\", + "project1, C:\\project1", + "project1/src, C:\\project1\\src", + "project1/src/main, C:\\project1\\src\\main", + "project1/src/main/kotlin, C:\\project1\\src\\main\\kotlin", + "project1/src/main/kotlin.One.kt, C:\\project1\\src\\main\\kotlin\\One.kt", + ], + ) + fun `Resolve`(path: String?, expected: String) { + val actual = rootPath.resolve(Paths.get(path ?: "")) + + assertThat(actual).isEqualTo(expected) + } + + @ParameterizedTest(name = "Path: {0}, expected result: {1}") + @CsvSource( + value = [ + ", C:\\", + "project1, C:\\project1", + "project1/src, C:\\project1\\src", + "project1/src/main, C:\\project1\\src\\main", + "project1/src/main/kotlin, C:\\project1\\src\\main\\kotlin", + "project1/src/main/kotlin.One.kt, C:\\project1\\src\\main\\kotlin\\One.kt", + ], + ) + fun `Resolve normalized`(path: String?, expected: String) { + val actual = rootPath.resolve(Paths.get(path?.normalizePath() ?: "")) + + assertThat(actual).isEqualTo(expected) + } + } + private fun String.normalizePath() = replace('/', File.separatorChar) private fun FileSystem.createFile(it: String) {