Compare commits

..

No commits in common. "e53d96a74b11dcabef42bbea54c3d113eae03cf7" and "ca221f19073e3247524ecc697ff8d5ef655a2895" have entirely different histories.

4 changed files with 13 additions and 97 deletions

View File

@ -1,30 +1,21 @@
package filesystem
import java.nio.file.FileAlreadyExistsException
import java.nio.file.FileSystemException
import java.nio.file.Files
import java.nio.file.Path
class FSCreator {
/**
* Create entry, leaving existing folders' contents, but overwriting existing files.
* @throws CyclicFolderException Cyclic folders cannot be created.
*/
@Throws(CyclicFolderException::class)
@Throws(FileSystemException::class)
fun create(
entryToCreate: FSEntry,
destination: String,
) {
// 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),
),
)
val queue = ArrayDeque<Pair<FSEntry, Path>>()
queue.add(entryToCreate to Path.of(destination))
while (queue.isNotEmpty()) {
val (entry, dest) = queue.removeFirst()
@ -42,6 +33,4 @@ class FSCreator {
}
}
}
}
class CyclicFolderException : Exception("Cyclic FSFolders are not supported")
}

View File

@ -6,22 +6,4 @@ 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) {
/**
* 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 FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name)

View File

@ -1,6 +1,7 @@
package filesystem
import org.junit.jupiter.api.*
import java.nio.file.FileSystemException
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.TimeUnit
@ -80,7 +81,7 @@ class FSCreatorTest {
FSFile("hi", "hi"),
),
),
FSFolder("folder", listOf()),
FSFolder("another-folder", listOf()),
FSFile("1.txt", "One!"),
FSFile("2.txt", "Two!"),
),
@ -94,7 +95,7 @@ class FSCreatorTest {
"folder",
listOf(
FSFolder(
"folder",
"another-folder",
listOf(
FSFolder(
"secrets",
@ -112,36 +113,22 @@ class FSCreatorTest {
)
}
assertEquals("hi", Files.readString(Path.of("_tmp/folder/sub-folder/hi")))
assertEquals("P4ssW0rd", Files.readString(Path.of("_tmp/folder/folder/secrets/secret")))
assertEquals("P4ssW0rd", Files.readString(Path.of("_tmp/folder/another-folder/secrets/secret")))
assertEquals("One is a good number", Files.readString(Path.of("_tmp/folder/1.txt")))
assertEquals("Two!", Files.readString(Path.of("_tmp/folder/2.txt")))
assertEquals("Three!", Files.readString(Path.of("_tmp/folder/3.txt")))
}
@Test
@Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle cyclic folders
fun `create throws on cyclic folder`() {
@Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle recursion
fun `create throws on recursive folder`() {
val files = mutableListOf<FSEntry>()
val folder = FSFolder("folder", files)
files.add(folder)
assertThrows<CyclicFolderException> {
assertThrows<FileSystemException> {
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")
}
}
}
fun deleteRecursive(path: Path) {

View File

@ -1,42 +0,0 @@
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())
}
}