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 import kotlin.test.Test import kotlin.test.assertEquals class FSCreatorTest { private val creator = FSCreator() @BeforeEach fun `before each`() { assertDoesNotThrow("should create _tmp directory") { Files.createDirectory(Path.of("_tmp")) } } @AfterEach fun `after each`() { assertDoesNotThrow("should delete _tmp directory") { deleteRecursive(Path.of("_tmp")) } } @Test fun `create entries`() { val readme = FSFile("README", "Hello World!") val gomod = FSFile("go.mod", "module example.com/hello-world\n\ngo1.21.5") val maingo = FSFile("main.go", "package main\n\nimport \"example.com/hello-world/utils\"\n\n" + "func main() {\n\tprintln(\"Hello World!\")\n}") val helloworldgo = FSFile("hello-world.go", "package utils\n\nfunc PrintHelloWorld() {\n" + "\tprintln(\"Hello World!\")\n}") assertDoesNotThrow("should create entries") { creator.create(FSFolder("folder", listOf( readme, gomod, maingo, FSFolder("utils", listOf( helloworldgo )) )), "_tmp") } // 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))) } @Test fun `create existing entries`() { assertDoesNotThrow("creation one") { creator.create(FSFolder("folder", listOf( FSFolder("sub-folder", listOf( FSFile("hi", "hi") )), FSFolder("another-folder", listOf()), FSFile("1.txt", "One!"), FSFile("2.txt", "Two!") )), "_tmp") } assertDoesNotThrow("creation two") { creator.create(FSFolder("folder", listOf( FSFolder("another-folder", listOf( FSFolder("secrets", listOf( FSFile("secret", "P4ssW0rd") )) )), FSFile("1.txt", "One is a good number"), FSFile("3.txt", "Three!") )), "_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"))) } @Test @Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle recursion fun `create throws on recursive folder`() { val files = mutableListOf() val folder = FSFolder("folder", files) files.add(folder) assertThrows { creator.create(folder, "_tmp") } } } fun deleteRecursive(path: Path) { if (Files.isDirectory(path)) { for (child in Files.list(path)) { deleteRecursive(child) } } Files.delete(path) }