Compare commits

..

3 Commits

Author SHA1 Message Date
Gleb Koval e7dd3fa073
Verify FSEntry name
Test Workflow / Lint and test library (pull_request) Successful in 1m42s Details
2024-01-07 15:04:13 +00:00
Gleb Koval 726980a669
Test create file & cleanup 2024-01-07 15:03:00 +00:00
Gleb Koval 8aa21dff54 Deny cyclic FSFolders (#3)
Test Workflow / Lint and test library (push) Successful in 3m56s Details
Contributes to #2 .

Handle cyclic folders explicitly, instead of relying on the filesystem.

Reviewed-on: #3
2024-01-07 14:56:16 +00:00
4 changed files with 97 additions and 13 deletions

View File

@ -1,7 +1,6 @@
package filesystem
import java.nio.file.FileAlreadyExistsException
import java.nio.file.FileSystemException
import java.nio.file.Path
import kotlin.io.path.createDirectory
import kotlin.io.path.createFile
@ -10,14 +9,24 @@ import kotlin.io.path.writeText
class FSCreator {
/**
* Create entry, leaving existing folders' contents, but overwriting existing files.
* @throws CyclicFolderException Cyclic folders cannot be created.
*/
@Throws(FileSystemException::class)
@Throws(CyclicFolderException::class)
fun create(
entryToCreate: FSEntry,
destination: String,
) {
val queue = ArrayDeque<Pair<FSEntry, Path>>()
queue.add(entryToCreate to Path.of(destination))
// No point in running anything if we know the input is invalid.
if (entryToCreate is FSFolder && entryToCreate.isCyclic()) {
throw CyclicFolderException()
}
val queue =
ArrayDeque(
listOf(
entryToCreate to Path.of(destination),
),
)
while (queue.isNotEmpty()) {
val (entry, dest) = queue.removeFirst()
@ -36,3 +45,5 @@ class FSCreator {
}
}
}
class CyclicFolderException : Exception("Cyclic FSFolders are not supported")

View File

@ -16,6 +16,24 @@ sealed class FSEntry(val name: String) {
class FSFile(name: String, val content: String) : FSEntry(name)
class FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name)
class FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name) {
/**
* Check whether a folder is cyclic.
*/
fun isCyclic(): Boolean {
val seen = listOf(this).toHashSet<FSEntry>()
val queue = ArrayDeque(entries)
while (queue.isNotEmpty()) {
val entry = queue.removeFirst()
if (!seen.add(entry)) {
return true
}
if (entry is FSFolder) {
queue.addAll(entry.entries)
}
}
return false
}
}
class InvalidEntryNameException(name: String) : Exception("Invalid FSEntry name: '$name'")

View File

@ -2,7 +2,6 @@ package filesystem
import org.junit.jupiter.api.*
import java.io.File
import java.nio.file.FileSystemException
import java.util.concurrent.TimeUnit
import kotlin.io.path.Path
import kotlin.io.path.createDirectory
@ -91,7 +90,7 @@ class FSCreatorTest {
FSFile("hi", "hi"),
),
),
FSFolder("another-folder", listOf()),
FSFolder("folder", listOf()),
FSFile("1.txt", "One!"),
FSFile("2.txt", "Two!"),
),
@ -105,7 +104,7 @@ class FSCreatorTest {
"folder",
listOf(
FSFolder(
"another-folder",
"folder",
listOf(
FSFolder(
"secrets",
@ -123,20 +122,34 @@ class FSCreatorTest {
)
}
assertEquals("hi", File("_tmp/folder/sub-folder/hi").readText())
assertEquals("P4ssW0rd", File("_tmp/folder/another-folder/secrets/secret").readText())
assertEquals("P4ssW0rd", File("_tmp/folder/folder/secrets/secret").readText())
assertEquals("One is a good number", File("_tmp/folder/1.txt").readText())
assertEquals("Two!", File("_tmp/folder/2.txt").readText())
assertEquals("Three!", File("_tmp/folder/3.txt").readText())
}
@Test
@Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle recursion
fun `create throws on recursive folder`() {
@Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle cyclic folders
fun `create throws on cyclic folder`() {
val files = mutableListOf<FSEntry>()
val folder = FSFolder("folder", files)
files.add(folder)
assertThrows<FileSystemException> {
assertThrows<CyclicFolderException> {
creator.create(folder, "_tmp")
}
}
@Test
@Timeout(500, unit = TimeUnit.MILLISECONDS)
fun `create throws on long cyclic folder`() {
val files = mutableListOf<FSEntry>()
val folder1 = FSFolder("folder", files)
val folder2 = FSFolder("folder2", listOf(folder1))
val folder3 = FSFolder("folder3", listOf(folder2))
val folder4 = FSFolder("folder4", listOf(folder3))
files.add(folder4)
assertThrows<CyclicFolderException> {
creator.create(folder4, "_tmp")
}
}
}

View File

@ -0,0 +1,42 @@
package filesystem
import org.junit.jupiter.api.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class FSEntryTest {
@Test
fun `non-cyclic folder`() {
val folder =
FSFolder(
"folder",
listOf(
FSFolder("folder", listOf()),
FSFile("text.txt", "Hello!"),
),
)
assertFalse(folder.isCyclic())
}
@Test
fun `cyclic folder`() {
val files = mutableListOf<FSEntry>()
val folder = FSFolder("folder", files)
files.add(folder)
assertTrue(folder.isCyclic())
}
@Test
fun `long cyclic folder`() {
val files = mutableListOf<FSEntry>()
val folder1 = FSFolder("folder", files)
val folder2 = FSFolder("folder2", listOf(folder1))
val folder3 = FSFolder("folder3", listOf(folder2))
val folder4 = FSFolder("folder4", listOf(folder3))
files.add(folder4)
assertTrue(folder1.isCyclic())
assertTrue(folder2.isCyclic())
assertTrue(folder3.isCyclic())
assertTrue(folder4.isCyclic())
}
}