Skip to content

Commit

Permalink
test(ort-utils): Reduce indentation in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher authored and sschuberth committed Sep 3, 2024
1 parent 4f870c2 commit b68e3b9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 87 deletions.
138 changes: 68 additions & 70 deletions utils/ort/src/funTest/kotlin/storage/LocalFileStorageFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,115 +37,113 @@ import java.io.InputStream

import org.ossreviewtoolkit.utils.common.safeMkdirs

class LocalFileStorageFunTest : WordSpec() {
private fun storage(block: (LocalFileStorage, File) -> Unit) {
class LocalFileStorageFunTest : WordSpec({
fun storage(block: (LocalFileStorage, File) -> Unit) {
val directory = tempdir()
val storage = LocalFileStorage(directory)
block(storage, directory)
}

init {
"Creating the storage" should {
"succeed if the directory exists" {
shouldNotThrowAny {
LocalFileStorage(tempdir())
}
"Creating the storage" should {
"succeed if the directory exists" {
shouldNotThrowAny {
LocalFileStorage(tempdir())
}
}

"succeed if the directory does not exist and must be created" {
val directory = tempdir()
val storageDirectory = directory.resolve("create/storage")
"succeed if the directory does not exist and must be created" {
val directory = tempdir()
val storageDirectory = directory.resolve("create/storage")

LocalFileStorage(storageDirectory).write("file", InputStream.nullInputStream())
LocalFileStorage(storageDirectory).write("file", InputStream.nullInputStream())

storageDirectory.isDirectory shouldBe true
}
storageDirectory.isDirectory shouldBe true
}

"fail if the directory is a file" {
val storageDirectory = tempfile()
"fail if the directory is a file" {
val storageDirectory = tempfile()

shouldThrow<IOException> {
LocalFileStorage(storageDirectory).write("file", InputStream.nullInputStream())
}
shouldThrow<IOException> {
LocalFileStorage(storageDirectory).write("file", InputStream.nullInputStream())
}
}
}

"Reading a file" should {
"succeed if the file exists" {
storage { storage, directory ->
val file = directory.resolve("existing-file")
file.writeText("content")
"Reading a file" should {
"succeed if the file exists" {
storage { storage, directory ->
val file = directory.resolve("existing-file")
file.writeText("content")

val content = storage.read("existing-file").bufferedReader().use(BufferedReader::readText)
val content = storage.read("existing-file").bufferedReader().use(BufferedReader::readText)

content shouldBe "content"
}
content shouldBe "content"
}
}

"fail if the file does not exist" {
storage { storage, _ ->
shouldThrow<FileNotFoundException> {
storage.read("file-does-not-exist")
}
"fail if the file does not exist" {
storage { storage, _ ->
shouldThrow<FileNotFoundException> {
storage.read("file-does-not-exist")
}
}
}

"fail if the requested path is not inside the storage directory" {
storage { storage, _ ->
shouldThrow<IllegalArgumentException> {
storage.read("../file")
}
"fail if the requested path is not inside the storage directory" {
storage { storage, _ ->
shouldThrow<IllegalArgumentException> {
storage.read("../file")
}
}
}
}

"Writing a file" should {
"succeed if the file does not exist" {
storage { storage, directory ->
storage.write("target/file", "content".byteInputStream())
"Writing a file" should {
"succeed if the file does not exist" {
storage { storage, directory ->
storage.write("target/file", "content".byteInputStream())

val file = directory.resolve("target/file")
val file = directory.resolve("target/file")

file shouldBe aFile()
file.readText() shouldBe "content"
}
file shouldBe aFile()
file.readText() shouldBe "content"
}
}

"succeed if the file does exist" {
storage { storage, directory ->
val file = directory.resolve("file")
file.writeText("old content")
"succeed if the file does exist" {
storage { storage, directory ->
val file = directory.resolve("file")
file.writeText("old content")

storage.write("file", "content".byteInputStream())
storage.write("file", "content".byteInputStream())

file shouldBe aFile()
file.readText() shouldBe "content"
}
file shouldBe aFile()
file.readText() shouldBe "content"
}
}

"fail if the target path is not inside the storage directory" {
storage { storage, directory ->
shouldThrow<IllegalArgumentException> {
storage.write("../file", "content".byteInputStream())
}
"fail if the target path is not inside the storage directory" {
storage { storage, directory ->
shouldThrow<IllegalArgumentException> {
storage.write("../file", "content".byteInputStream())
}

val file = directory.resolve("../file")
val file = directory.resolve("../file")

file shouldNot exist()
}
file shouldNot exist()
}
}

"fail if the target path is a directory" {
storage { storage, directory ->
val dir = directory.resolve("dir").safeMkdirs()

shouldThrow<FileNotFoundException> {
storage.write("dir", "content".byteInputStream())
}
"fail if the target path is a directory" {
storage { storage, directory ->
val dir = directory.resolve("dir").safeMkdirs()

dir.isDirectory shouldBe true
shouldThrow<FileNotFoundException> {
storage.write("dir", "content".byteInputStream())
}

dir.isDirectory shouldBe true
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,31 @@ import io.kotest.matchers.shouldBe
import java.io.BufferedReader
import java.io.File

class XZCompressedLocalFileStorageFunTest : StringSpec() {
private fun storage(block: (XZCompressedLocalFileStorage, File) -> Unit) {
class XZCompressedLocalFileStorageFunTest : StringSpec({
fun storage(block: (XZCompressedLocalFileStorage, File) -> Unit) {
val directory = tempdir()
val storage = XZCompressedLocalFileStorage(directory)
block(storage, directory)
}

init {
"Can read written compressed data" {
storage { storage, _ ->
storage.write("new-file", "content".byteInputStream())
"Can read written compressed data" {
storage { storage, _ ->
storage.write("new-file", "content".byteInputStream())

val content = storage.read("new-file").bufferedReader().use(BufferedReader::readText)
val content = storage.read("new-file").bufferedReader().use(BufferedReader::readText)

content shouldBe "content"
}
content shouldBe "content"
}
}

"Can read existing uncompressed data" {
storage { storage, directory ->
val file = directory.resolve("existing-file")
file.writeText("content")
"Can read existing uncompressed data" {
storage { storage, directory ->
val file = directory.resolve("existing-file")
file.writeText("content")

val content = storage.read("existing-file").bufferedReader().use(BufferedReader::readText)
val content = storage.read("existing-file").bufferedReader().use(BufferedReader::readText)

content shouldBe "content"
}
content shouldBe "content"
}
}
}
})

0 comments on commit b68e3b9

Please sign in to comment.