Skip to content

Commit

Permalink
NioFS: fix relative paths when listing a dir
Browse files Browse the repository at this point in the history
which was not detected before because relative tests uses DOT
  • Loading branch information
oldergod committed Jul 26, 2023
1 parent 9b8c853 commit cc310e2
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNull
Expand Down Expand Up @@ -280,6 +279,108 @@ abstract class AbstractFileSystemTest(
assertTrue(entries.toString()) { entries.isNotEmpty() && entries.all { it.isRelative } }
}

@Test
fun listOnRelativePathWhichIsNotDotReturnsRelativePaths() {
// Make sure there's always at least one file so our assertion is useful. We copy the first 2
// entries of the real working directory of the JVM to validate the results on all environment.
if (
fileSystem is FakeFileSystem ||
fileSystem is ForwardingFileSystem && fileSystem.delegate is FakeFileSystem
) {
val workingDirectory = "/directory".toPath()
fileSystem.createDirectory(workingDirectory)
(fileSystem as? FakeFileSystem)?.workingDirectory = workingDirectory
((fileSystem as? ForwardingFileSystem)?.delegate as? FakeFileSystem)?.workingDirectory =
workingDirectory
val apiDir = "api".toPath()
fileSystem.createDirectory(apiDir)
fileSystem.write(apiDir / "okio.api".toPath()) {
writeUtf8("hello, world!")
}
} else if (isWrappingJimFileSystem) {
val apiDir = "api".toPath()
fileSystem.createDirectory(apiDir)
fileSystem.write(apiDir / "okio.api".toPath()) {
writeUtf8("hello, world!")
}
}

try {
assertEquals(
listOf("api".toPath() / "okio.api".toPath()),
fileSystem.list("api".toPath()),
// List some entries to help debugging.
fileSystem.listRecursively(".".toPath()).take(5).toList().joinToString(),
)
} catch (e: Throwable) {
if (e !is AssertionError && e !is FileNotFoundException) { throw e }

// Non JVM environments.
val firstChild = fileSystem.list("Library".toPath()).first()
assertTrue(
// List some entries to help debugging.
fileSystem.listRecursively(".".toPath()).take(5).toList().joinToString(),
) {
// To avoid relying too much on the environment we check that the path contains its parent
// once and that it's relative.
firstChild.isRelative &&
firstChild.toString().startsWith("Library") &&
firstChild.toString().split("Library").size == 2
}
}
}

@Test
fun listOrNullOnRelativePathWhichIsNotDotReturnsRelativePaths() {
// Make sure there's always at least one file so our assertion is useful. We copy the first 2
// entries of the real working directory of the JVM to validate the results on all environment.
if (
fileSystem is FakeFileSystem ||
fileSystem is ForwardingFileSystem && fileSystem.delegate is FakeFileSystem
) {
val workingDirectory = "/directory".toPath()
fileSystem.createDirectory(workingDirectory)
(fileSystem as? FakeFileSystem)?.workingDirectory = workingDirectory
((fileSystem as? ForwardingFileSystem)?.delegate as? FakeFileSystem)?.workingDirectory =
workingDirectory
val apiDir = "api".toPath()
fileSystem.createDirectory(apiDir)
fileSystem.write(apiDir / "okio.api".toPath()) {
writeUtf8("hello, world!")
}
} else if (isWrappingJimFileSystem) {
val apiDir = "api".toPath()
fileSystem.createDirectory(apiDir)
fileSystem.write(apiDir / "okio.api".toPath()) {
writeUtf8("hello, world!")
}
}

try {
assertEquals(
listOf("api".toPath() / "okio.api".toPath()),
fileSystem.listOrNull("api".toPath()),
// List some entries to help debugging.
fileSystem.listRecursively(".".toPath()).take(5).toList().joinToString(),
)
} catch (e: Throwable) {
if (e !is AssertionError && e !is FileNotFoundException) { throw e }

// Non JVM environments.
val firstChild = fileSystem.list("Library".toPath()).first()
assertTrue(
// List some entries to help debugging.
fileSystem.listRecursively(".".toPath()).take(5).toList().joinToString(),
) {
// To avoid relying too much on the environment we check that the path contains its parent
// once and that it's relative.
firstChild.isRelative &&
firstChild.toString().startsWith("Library") &&
firstChild.toString().split("Library").size == 2
}
}
}

@Test
fun listResultsAreSorted() {
val fileA = base / "a"
Expand Down Expand Up @@ -2438,19 +2539,6 @@ abstract class AbstractFileSystemTest(
}
}

private fun assertClosedFailure(block: () -> Unit) {
val exception = assertFails {
block()
}
val exceptionType = exception::class.simpleName
assertTrue(
exceptionType == "IOException" ||
exceptionType == "IllegalStateException" ||
exceptionType == "ClosedChannelException",
"unexpected exception: $exception",
)
}

protected fun supportsSymlink(): Boolean {
if (fileSystem is FakeFileSystem) return fileSystem.allowSymlinks
if (windowsLimitations) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class NioFileSystemWrappingFileSystem(private val nioFileSystem: NioFil
return null
}
}
val result = entries.mapTo(mutableListOf()) { entry -> dir / entry.toString() }
val result = entries.mapTo(mutableListOf()) { entry -> entry.toOkioPath() }
result.sort()
return result
}
Expand Down

0 comments on commit cc310e2

Please sign in to comment.