Skip to content

Commit

Permalink
Replace "\" in filepath with "/" before comparing the path with a glo…
Browse files Browse the repository at this point in the history
…b in which always uses "/" as separator

Closes pinterest#1599
  • Loading branch information
paul-dingemans committed Aug 27, 2022
1 parent e843b80 commit 86bc598
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 86bc598

Please sign in to comment.