Compare commits
2 Commits
ca221f1907
...
18b9dac09d
Author | SHA1 | Date |
---|---|---|
Gleb Koval | 18b9dac09d | |
Gleb Koval | efc11deb6e |
|
@ -2,8 +2,10 @@ package filesystem
|
|||
|
||||
import java.nio.file.FileAlreadyExistsException
|
||||
import java.nio.file.FileSystemException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.createDirectory
|
||||
import kotlin.io.path.createFile
|
||||
import kotlin.io.path.writeText
|
||||
|
||||
class FSCreator {
|
||||
/**
|
||||
|
@ -22,13 +24,13 @@ class FSCreator {
|
|||
val path = dest.resolve(entry.name)
|
||||
try {
|
||||
when (entry) {
|
||||
is FSFile -> Files.createFile(path)
|
||||
is FSFolder -> Files.createDirectory(path)
|
||||
is FSFile -> path.createFile()
|
||||
is FSFolder -> path.createDirectory()
|
||||
}
|
||||
} catch (_: FileAlreadyExistsException) {
|
||||
} // Allow files/folders to already exist.
|
||||
when (entry) {
|
||||
is FSFile -> Files.write(path, entry.content.toByteArray())
|
||||
is FSFile -> path.writeText(entry.content)
|
||||
is FSFolder -> queue.addAll(entry.entries.map { it to path })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,21 @@
|
|||
package filesystem
|
||||
|
||||
import kotlin.io.path.Path
|
||||
|
||||
// Note sealed allows for simpler logic in FSCreator by guaranteeing FSFile and FSFolder are the only possible FSEntries
|
||||
// (as we expect), and it also implicitly makes the class abstract as required.
|
||||
sealed class FSEntry(val name: String)
|
||||
sealed class FSEntry(val name: String) {
|
||||
init {
|
||||
val p = Path(name)
|
||||
// Only allow single filenames (no paths or relative references (e.g. ".."))
|
||||
if (p.toList().size != 1 || p.fileName != p.toFile().canonicalFile.toPath().fileName) {
|
||||
throw InvalidEntryNameException(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
class InvalidEntryNameException(name: String) : Exception("Invalid FSEntry name: '$name'")
|
|
@ -1,10 +1,11 @@
|
|||
package filesystem
|
||||
|
||||
import org.junit.jupiter.api.*
|
||||
import java.io.File
|
||||
import java.nio.file.FileSystemException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.createDirectory
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
@ -14,17 +15,26 @@ class FSCreatorTest {
|
|||
@BeforeEach
|
||||
fun `before each`() {
|
||||
assertDoesNotThrow("should create _tmp directory") {
|
||||
Files.createDirectory(Path.of("_tmp"))
|
||||
Path("_tmp").createDirectory()
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun `after each`() {
|
||||
assertDoesNotThrow("should delete _tmp directory") {
|
||||
deleteRecursive(Path.of("_tmp"))
|
||||
File("_tmp").deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create file`() {
|
||||
val file = FSFile("test.txt", "This is a file")
|
||||
assertDoesNotThrow("should create file") {
|
||||
creator.create(file, "_tmp")
|
||||
}
|
||||
assertEquals(file.content, File("_tmp/", file.name).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create entries`() {
|
||||
val readme = FSFile("README", "Hello World!")
|
||||
|
@ -62,10 +72,10 @@ class FSCreatorTest {
|
|||
}
|
||||
// If objects don't exist, these functions will throw anyway, so don't explicitly check for existence.
|
||||
// Similarly, don't explicitly check if an object is a directory.
|
||||
assertEquals(readme.content, Files.readString(Path.of("_tmp/folder", readme.name)))
|
||||
assertEquals(gomod.content, Files.readString(Path.of("_tmp/folder", gomod.name)))
|
||||
assertEquals(maingo.content, Files.readString(Path.of("_tmp/folder", maingo.name)))
|
||||
assertEquals(helloworldgo.content, Files.readString(Path.of("_tmp/folder/utils", helloworldgo.name)))
|
||||
assertEquals(readme.content, File("_tmp/folder", readme.name).readText())
|
||||
assertEquals(gomod.content, File("_tmp/folder", gomod.name).readText())
|
||||
assertEquals(maingo.content, File("_tmp/folder", maingo.name).readText())
|
||||
assertEquals(helloworldgo.content, File("_tmp/folder/utils", helloworldgo.name).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -112,11 +122,11 @@ class FSCreatorTest {
|
|||
"_tmp",
|
||||
)
|
||||
}
|
||||
assertEquals("hi", Files.readString(Path.of("_tmp/folder/sub-folder/hi")))
|
||||
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")))
|
||||
assertEquals("hi", File("_tmp/folder/sub-folder/hi").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
|
||||
|
@ -129,13 +139,4 @@ class FSCreatorTest {
|
|||
creator.create(folder, "_tmp")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteRecursive(path: Path) {
|
||||
if (Files.isDirectory(path)) {
|
||||
for (child in Files.list(path)) {
|
||||
deleteRecursive(child)
|
||||
}
|
||||
}
|
||||
Files.delete(path)
|
||||
}
|
Reference in New Issue