test: better handling of temp directories

This commit is contained in:
Gauthier Roebroeck 2023-03-15 14:22:18 +08:00
parent c0d00ccede
commit c0d6bf29e2
2 changed files with 38 additions and 22 deletions

View file

@ -8,15 +8,18 @@ import org.gotson.komga.domain.model.Library
import org.gotson.komga.domain.model.PathContainedInPath
import org.gotson.komga.domain.persistence.LibraryRepository
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.io.TempDir
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.io.FileNotFoundException
import java.net.URL
import java.nio.file.Files
import java.nio.file.Path
@ExtendWith(SpringExtension::class)
@SpringBootTest
@ -45,7 +48,14 @@ class LibraryLifecycleTest(
fun `when adding library with non-directory root folder then exception is thrown`() {
// when
val thrown = catchThrowable {
libraryLifecycle.addLibrary(Library("test", Files.createTempFile(null, null).toUri().toURL()))
libraryLifecycle.addLibrary(
Library(
"test",
Files.createTempFile(null, null)
.also { it.toFile().deleteOnExit() }
.toUri().toURL(),
),
)
}
// then
@ -53,13 +63,13 @@ class LibraryLifecycleTest(
}
@Test
fun `given existing library when adding library with same name then exception is thrown`() {
fun `given existing library when adding library with same name then exception is thrown`(@TempDir path1: Path, @TempDir path2: Path) {
// given
libraryLifecycle.addLibrary(Library("test", Files.createTempDirectory(null).toUri().toURL()))
libraryLifecycle.addLibrary(Library("test", path1.toUri().toURL()))
// when
val thrown = catchThrowable {
libraryLifecycle.addLibrary(Library("test", Files.createTempDirectory(null).toUri().toURL()))
libraryLifecycle.addLibrary(Library("test", path2.toUri().toURL()))
}
// then
@ -67,9 +77,8 @@ class LibraryLifecycleTest(
}
@Test
fun `given existing library when adding library with root folder as child of existing library then exception is thrown`() {
fun `given existing library when adding library with root folder as child of existing library then exception is thrown`(@TempDir parent: Path) {
// given
val parent = Files.createTempDirectory(null)
libraryLifecycle.addLibrary(Library("parent", parent.toUri().toURL()))
// when
@ -85,9 +94,8 @@ class LibraryLifecycleTest(
}
@Test
fun `given existing library when adding library with root folder as parent of existing library then exception is thrown`() {
fun `given existing library when adding library with root folder as parent of existing library then exception is thrown`(@TempDir parent: Path) {
// given
val parent = Files.createTempDirectory(null)
val child = Files.createTempDirectory(parent, null)
libraryLifecycle.addLibrary(Library("child", child.toUri().toURL()))
@ -105,8 +113,14 @@ class LibraryLifecycleTest(
@Nested
inner class Update {
private val rootFolder = Files.createTempDirectory(null)
private val library = Library("Existing", rootFolder.toUri().toURL())
private lateinit var rootFolder: Path
private lateinit var library: Library
@BeforeAll
fun setup(@TempDir root: Path) {
rootFolder = root
library = Library("Existing", rootFolder.toUri().toURL())
}
@Test
fun `given existing library when updating with non-existent root folder then exception is thrown`() {
@ -127,7 +141,11 @@ class LibraryLifecycleTest(
val existing = libraryLifecycle.addLibrary(library)
// when
val toUpdate = existing.copy(name = "test", root = Files.createTempFile(null, null).toUri().toURL())
val toUpdate = existing.copy(
name = "test",
root = Files.createTempFile(null, null)
.also { it.toFile().deleteOnExit() }.toUri().toURL(),
)
val thrown = catchThrowable {
libraryLifecycle.updateLibrary(toUpdate)
}
@ -151,13 +169,13 @@ class LibraryLifecycleTest(
}
@Test
fun `given existing library when updating library with same name then exception is thrown`() {
fun `given existing library when updating library with same name then exception is thrown`(@TempDir path1: Path, @TempDir path2: Path) {
// given
libraryLifecycle.addLibrary(Library("test", Files.createTempDirectory(null).toUri().toURL()))
libraryLifecycle.addLibrary(Library("test", path1.toUri().toURL()))
val existing = libraryLifecycle.addLibrary(library)
// when
val toUpdate = existing.copy(name = "test", root = Files.createTempDirectory(null).toUri().toURL())
val toUpdate = existing.copy(name = "test", root = path2.toUri().toURL())
val thrown = catchThrowable {
libraryLifecycle.updateLibrary(toUpdate)
}
@ -183,9 +201,8 @@ class LibraryLifecycleTest(
}
@Test
fun `given existing library when updating library with root folder as child of existing library then exception is thrown`() {
fun `given existing library when updating library with root folder as child of existing library then exception is thrown`(@TempDir parent: Path) {
// given
val parent = Files.createTempDirectory(null)
libraryLifecycle.addLibrary(Library("parent", parent.toUri().toURL()))
val existing = libraryLifecycle.addLibrary(library)
@ -203,9 +220,8 @@ class LibraryLifecycleTest(
}
@Test
fun `given single existing library when updating library with root folder as parent of existing library then no exception is thrown`() {
fun `given single existing library when updating library with root folder as parent of existing library then no exception is thrown`(@TempDir parent: Path) {
// given
val parent = Files.createTempDirectory(null)
val child = Files.createTempDirectory(parent, null)
val existing = libraryLifecycle.addLibrary(Library("child", child.toUri().toURL()))
@ -220,9 +236,8 @@ class LibraryLifecycleTest(
}
@Test
fun `given existing library when updating library with root folder as parent of existing library then exception is thrown`() {
fun `given existing library when updating library with root folder as parent of existing library then exception is thrown`(@TempDir parent: Path) {
// given
val parent = Files.createTempDirectory(null)
val child = Files.createTempDirectory(parent, null)
libraryLifecycle.addLibrary(Library("child", child.toUri().toURL()))
val existing = libraryLifecycle.addLibrary(library)

View file

@ -4,6 +4,7 @@ import org.gotson.komga.domain.model.ROLE_ADMIN
import org.gotson.komga.domain.model.ROLE_USER
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.io.TempDir
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
@ -14,6 +15,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.post
import java.nio.file.Files
import java.nio.file.Path
@ExtendWith(SpringExtension::class)
@SpringBootTest
@ -48,8 +50,7 @@ class FileSystemControllerTest(
@Test
@WithMockUser(roles = [ROLE_USER, ROLE_ADMIN])
fun `given non-existent path param when getDirectoryListing then return bad request`() {
val parent = Files.createTempDirectory(null)
fun `given non-existent path param when getDirectoryListing then return bad request`(@TempDir parent: Path) {
Files.delete(parent)
mockMvc.post(route) {