diff --git a/komga/src/test/kotlin/org/gotson/komga/domain/service/LibraryLifecycleTest.kt b/komga/src/test/kotlin/org/gotson/komga/domain/service/LibraryLifecycleTest.kt index 7304a510b..a36b50ddb 100644 --- a/komga/src/test/kotlin/org/gotson/komga/domain/service/LibraryLifecycleTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/domain/service/LibraryLifecycleTest.kt @@ -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) diff --git a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/FileSystemControllerTest.kt b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/FileSystemControllerTest.kt index cf7763038..9c3d7a7ce 100644 --- a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/FileSystemControllerTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/FileSystemControllerTest.kt @@ -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) {