Skip to content

Instantly share code, notes, and snippets.

@GeePawHill
Last active February 26, 2023 04:39
Show Gist options
  • Save GeePawHill/5bd712f922fcc048c67d163cec782653 to your computer and use it in GitHub Desktop.
Save GeePawHill/5bd712f922fcc048c67d163cec782653 to your computer and use it in GitHub Desktop.
package org.geepawhill.admin
import org.apache.commons.io.FileUtils
import java.nio.file.Files
import java.nio.file.Path
class ContentFolder(val root: Path) {
operator fun get(first: String, vararg parts: String): String {
val resolvedPath = root.resolve(Path.of(first, *parts))
return this[resolvedPath]
}
operator fun get(path: Path): String {
return Files.readString(path)
}
operator fun set(first: String, vararg parts: String, value: String) {
val resolvedPath = root.resolve(Path.of(first, *parts))
this[resolvedPath] = value
}
operator fun set(path: Path, value: String) {
FileUtils.createParentDirectories(path.toFile())
Files.writeString(path, value)
}
}
package org.geepawhill.admin.content
import org.apache.commons.io.FileUtils
import org.assertj.core.api.Assertions.assertThat
import org.geepawhill.admin.ContentFolder
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.nio.file.Path
class ContentFolderTest {
val testPath = Path.of("testData")
val folder = ContentFolder(testPath)
@AfterEach
fun afterEach() {
FileUtils.deleteDirectory(testPath.toFile())
}
@Test
fun `assignment writes file`() {
folder["index.html"] = "A string"
assertThat(folder["index.html"]).isEqualTo("A string")
}
@Test
fun `works using path`() {
folder[Path.of("myfile")] = "A string"
assertThat(folder[Path.of("myfile")]).isEqualTo("A string")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment