Compare commits
2 Commits
v0.1.1
...
18b9dac09d
Author | SHA1 | Date | |
---|---|---|---|
18b9dac09d
|
|||
efc11deb6e
|
@@ -1,7 +1,8 @@
|
||||
package filesystem
|
||||
|
||||
import java.nio.file.FileAlreadyExistsException
|
||||
import kotlin.io.path.Path
|
||||
import java.nio.file.FileSystemException
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.createDirectory
|
||||
import kotlin.io.path.createFile
|
||||
import kotlin.io.path.writeText
|
||||
@@ -9,30 +10,14 @@ 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 DuplicateEntryNameException A folder or sub-folder contains entries with duplicate names.
|
||||
*/
|
||||
@Throws(CyclicFolderException::class, DuplicateEntryNameException::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) {
|
||||
if (entryToCreate.isCyclic()) {
|
||||
throw CyclicFolderException()
|
||||
}
|
||||
if (entryToCreate.deepHasDuplicateNames()) {
|
||||
throw DuplicateEntryNameException()
|
||||
}
|
||||
}
|
||||
|
||||
val queue =
|
||||
ArrayDeque(
|
||||
listOf(
|
||||
entryToCreate to Path(destination),
|
||||
),
|
||||
)
|
||||
val queue = ArrayDeque<Pair<FSEntry, Path>>()
|
||||
queue.add(entryToCreate to Path.of(destination))
|
||||
|
||||
while (queue.isNotEmpty()) {
|
||||
val (entry, dest) = queue.removeFirst()
|
||||
@@ -50,8 +35,4 @@ class FSCreator {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CyclicFolderException : Exception("Cyclic FSFolders are not supported")
|
||||
|
||||
class DuplicateEntryNameException : Exception("Folder contains entries with duplicate names")
|
||||
}
|
@@ -16,44 +16,6 @@ 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
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a folder contains multiple entries with the same name.
|
||||
*/
|
||||
fun hasDuplicateNames(): Boolean {
|
||||
val seen = HashSet<String>()
|
||||
return entries.any { !seen.add(it.name) }
|
||||
}
|
||||
|
||||
internal fun deepHasDuplicateNames(): Boolean {
|
||||
val queue = ArrayDeque(listOf(this))
|
||||
while (queue.isNotEmpty()) {
|
||||
val entry = queue.removeFirst()
|
||||
if (entry.hasDuplicateNames()) {
|
||||
return true
|
||||
}
|
||||
queue.addAll(entry.entries.mapNotNull { it as? FSFolder })
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
class FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name)
|
||||
|
||||
class InvalidEntryNameException(name: String) : Exception("Invalid FSEntry name: '$name'")
|
@@ -2,6 +2,7 @@ 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
|
||||
@@ -90,7 +91,7 @@ class FSCreatorTest {
|
||||
FSFile("hi", "hi"),
|
||||
),
|
||||
),
|
||||
FSFolder("folder", listOf()),
|
||||
FSFolder("another-folder", listOf()),
|
||||
FSFile("1.txt", "One!"),
|
||||
FSFile("2.txt", "Two!"),
|
||||
),
|
||||
@@ -104,8 +105,7 @@ class FSCreatorTest {
|
||||
"folder",
|
||||
listOf(
|
||||
FSFolder(
|
||||
// Repeated name should be fine here (not throw)
|
||||
"folder",
|
||||
"another-folder",
|
||||
listOf(
|
||||
FSFolder(
|
||||
"secrets",
|
||||
@@ -123,63 +123,19 @@ class FSCreatorTest {
|
||||
)
|
||||
}
|
||||
assertEquals("hi", File("_tmp/folder/sub-folder/hi").readText())
|
||||
assertEquals("P4ssW0rd", File("_tmp/folder/folder/secrets/secret").readText())
|
||||
assertEquals("P4ssW0rd", File("_tmp/folder/another-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 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> {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create throws on folder with duplicate names`() {
|
||||
val folder =
|
||||
FSFolder(
|
||||
"folder",
|
||||
listOf(
|
||||
FSFile("README.md", "# Test File"),
|
||||
FSFile("hello-world.txt", "Hello World!"),
|
||||
FSFolder(
|
||||
"src",
|
||||
listOf(
|
||||
FSFile("README.md", "# Source files"),
|
||||
FSFolder(
|
||||
"solution",
|
||||
listOf(
|
||||
FSFile("solution.py", "print('1 + 1 = 1')"),
|
||||
FSFile("tmp", "A temporary file"),
|
||||
FSFolder("tmp", listOf()),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
FSFolder("tmp", listOf()),
|
||||
),
|
||||
)
|
||||
assertThrows<DuplicateEntryNameException> {
|
||||
assertThrows<FileSystemException> {
|
||||
creator.create(folder, "_tmp")
|
||||
}
|
||||
}
|
||||
|
@@ -1,136 +0,0 @@
|
||||
package filesystem
|
||||
|
||||
import org.junit.jupiter.api.*
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class FSEntryTest {
|
||||
@Test
|
||||
fun `valid name entries`() {
|
||||
assertDoesNotThrow("should construct FSFile and FSFolder without throwing") {
|
||||
FSFile("A file with a name.tar.xz", "Contents")
|
||||
FSFolder(".a folder with a name", listOf())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `invalid name entries`() {
|
||||
assertThrows<InvalidEntryNameException> {
|
||||
FSFile("File/here", "Contents")
|
||||
}
|
||||
assertThrows<InvalidEntryNameException> {
|
||||
FSFolder("Folder/here", listOf())
|
||||
}
|
||||
assertThrows<InvalidEntryNameException> {
|
||||
FSFolder(".", listOf())
|
||||
}
|
||||
assertThrows<InvalidEntryNameException> {
|
||||
FSFolder("/", listOf())
|
||||
}
|
||||
}
|
||||
|
||||
@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())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no duplicate names folder`() {
|
||||
val folder =
|
||||
FSFolder(
|
||||
"folder",
|
||||
listOf(
|
||||
FSFile("README.md", "# Test File"),
|
||||
FSFile("hello-world.txt", "Hello World!"),
|
||||
FSFolder(
|
||||
"src",
|
||||
listOf(
|
||||
FSFile("README.md", "# Source files"),
|
||||
FSFile("solution-1.py", "print('1 + 1 = 1')"),
|
||||
FSFile("solution-2.py", "print('1 + 1 = 1')"),
|
||||
),
|
||||
),
|
||||
FSFolder("tmp", listOf()),
|
||||
),
|
||||
)
|
||||
assertFalse(folder.hasDuplicateNames())
|
||||
assertFalse(folder.deepHasDuplicateNames())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `shallow duplicate names folder`() {
|
||||
val folder =
|
||||
FSFolder(
|
||||
"folder",
|
||||
listOf(
|
||||
FSFile("README.md", "# Test File"),
|
||||
FSFile("hello-world.txt", "Hello World!"),
|
||||
FSFolder(
|
||||
"src",
|
||||
listOf(
|
||||
FSFile("README.md", "# Source files"),
|
||||
FSFile("solution-1.py", "print('1 + 1 = 1')"),
|
||||
FSFile("solution-2.py", "print('1 + 1 = 1')"),
|
||||
),
|
||||
),
|
||||
FSFolder("tmp", listOf()),
|
||||
FSFile("tmp", "A temporary file"),
|
||||
),
|
||||
)
|
||||
assertTrue(folder.hasDuplicateNames())
|
||||
assertTrue(folder.deepHasDuplicateNames())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deep duplicate names folder`() {
|
||||
val folder =
|
||||
FSFolder(
|
||||
"folder",
|
||||
listOf(
|
||||
FSFile("README.md", "# Test File"),
|
||||
FSFile("hello-world.txt", "Hello World!"),
|
||||
FSFolder(
|
||||
"src",
|
||||
listOf(
|
||||
FSFile("README.md", "# Source files"),
|
||||
FSFile("solution-1.py", "print('1 + 1 = 1')"),
|
||||
FSFile("solution-1.py", "print('1 + 1 = 1')"),
|
||||
),
|
||||
),
|
||||
FSFolder("tmp", listOf()),
|
||||
),
|
||||
)
|
||||
assertFalse(folder.hasDuplicateNames())
|
||||
assertTrue(folder.deepHasDuplicateNames())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user