Test create file & cleanup
This commit is contained in:
parent
8aa21dff54
commit
726980a669
|
@ -1,9 +1,10 @@
|
||||||
package filesystem
|
package filesystem
|
||||||
|
|
||||||
import org.junit.jupiter.api.*
|
import org.junit.jupiter.api.*
|
||||||
import java.nio.file.Files
|
import java.io.File
|
||||||
import java.nio.file.Path
|
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
import kotlin.io.path.createDirectory
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
@ -13,17 +14,26 @@ class FSCreatorTest {
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun `before each`() {
|
fun `before each`() {
|
||||||
assertDoesNotThrow("should create _tmp directory") {
|
assertDoesNotThrow("should create _tmp directory") {
|
||||||
Files.createDirectory(Path.of("_tmp"))
|
Path("_tmp").createDirectory()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
fun `after each`() {
|
fun `after each`() {
|
||||||
assertDoesNotThrow("should delete _tmp directory") {
|
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
|
@Test
|
||||||
fun `create entries`() {
|
fun `create entries`() {
|
||||||
val readme = FSFile("README", "Hello World!")
|
val readme = FSFile("README", "Hello World!")
|
||||||
|
@ -61,10 +71,10 @@ class FSCreatorTest {
|
||||||
}
|
}
|
||||||
// If objects don't exist, these functions will throw anyway, so don't explicitly check for existence.
|
// 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.
|
// Similarly, don't explicitly check if an object is a directory.
|
||||||
assertEquals(readme.content, Files.readString(Path.of("_tmp/folder", readme.name)))
|
assertEquals(readme.content, File("_tmp/folder", readme.name).readText())
|
||||||
assertEquals(gomod.content, Files.readString(Path.of("_tmp/folder", gomod.name)))
|
assertEquals(gomod.content, File("_tmp/folder", gomod.name).readText())
|
||||||
assertEquals(maingo.content, Files.readString(Path.of("_tmp/folder", maingo.name)))
|
assertEquals(maingo.content, File("_tmp/folder", maingo.name).readText())
|
||||||
assertEquals(helloworldgo.content, Files.readString(Path.of("_tmp/folder/utils", helloworldgo.name)))
|
assertEquals(helloworldgo.content, File("_tmp/folder/utils", helloworldgo.name).readText())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -111,11 +121,11 @@ class FSCreatorTest {
|
||||||
"_tmp",
|
"_tmp",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
assertEquals("hi", Files.readString(Path.of("_tmp/folder/sub-folder/hi")))
|
assertEquals("hi", File("_tmp/folder/sub-folder/hi").readText())
|
||||||
assertEquals("P4ssW0rd", Files.readString(Path.of("_tmp/folder/folder/secrets/secret")))
|
assertEquals("P4ssW0rd", File("_tmp/folder/folder/secrets/secret").readText())
|
||||||
assertEquals("One is a good number", Files.readString(Path.of("_tmp/folder/1.txt")))
|
assertEquals("One is a good number", File("_tmp/folder/1.txt").readText())
|
||||||
assertEquals("Two!", Files.readString(Path.of("_tmp/folder/2.txt")))
|
assertEquals("Two!", File("_tmp/folder/2.txt").readText())
|
||||||
assertEquals("Three!", Files.readString(Path.of("_tmp/folder/3.txt")))
|
assertEquals("Three!", File("_tmp/folder/3.txt").readText())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -143,12 +153,3 @@ class FSCreatorTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteRecursive(path: Path) {
|
|
||||||
if (Files.isDirectory(path)) {
|
|
||||||
for (child in Files.list(path)) {
|
|
||||||
deleteRecursive(child)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Files.delete(path)
|
|
||||||
}
|
|
Reference in New Issue