11package dartzee.core.util
22
3+ import dartzee.core.util.FileUtil.renameToWithRetries
34import dartzee.helper.AbstractTest
45import dartzee.logging.CODE_FILE_ERROR
56import dartzee.logging.Severity
67import io.kotest.matchers.collections.shouldContainExactly
8+ import io.kotest.matchers.file.shouldExist
9+ import io.kotest.matchers.file.shouldNotExist
710import io.kotest.matchers.shouldBe
811import io.kotest.matchers.types.shouldBeInstanceOf
12+ import io.mockk.every
13+ import io.mockk.mockk
914import java.awt.Dimension
1015import java.io.File
1116import java.nio.file.DirectoryNotEmptyException
@@ -14,7 +19,7 @@ import org.junit.jupiter.api.AfterEach
1419import org.junit.jupiter.api.BeforeEach
1520import org.junit.jupiter.api.Test
1621
17- class TestFileUtil : AbstractTest() {
22+ class FileUtilTest : AbstractTest() {
1823 private val testDirectory = File("Test/")
1924
2025 @BeforeEach
@@ -67,7 +72,7 @@ class TestFileUtil : AbstractTest() {
6772 new.createNewFile()
6873 new.writeText("New")
6974
70- FileUtil.swapInFile(current.path, new.path)
75+ FileUtil.swapInFile(current.path, new.path) shouldBe null
7176
7277 new.exists() shouldBe false
7378 current.exists() shouldBe true
@@ -87,13 +92,105 @@ class TestFileUtil : AbstractTest() {
8792 new.createNewFile()
8893 new.writeText("New")
8994
90- FileUtil.swapInFile("Test/Current", "Test/New")
95+ FileUtil.swapInFile("Test/Current", "Test/New") shouldBe null
9196
9297 new.exists() shouldBe false
9398 current.exists() shouldBe true
9499 current.readLines().shouldContainExactly("New")
95100 }
96101
102+ @Test
103+ fun `Should pre-clean for when a move has previously failed`() {
104+ val zzDir = File("Test/zzCurrent")
105+ zzDir.mkdir()
106+
107+ val inTheWay = File("Test/zzCurrent/Bad.txt")
108+ inTheWay.createNewFile()
109+ inTheWay.writeText("muahaha")
110+
111+ val currentDir = File("Test/Current")
112+ currentDir.mkdir()
113+ val newDir = File("Test/New")
114+ newDir.mkdir()
115+
116+ val current = File("Test/Current/File.txt")
117+ current.createNewFile()
118+ current.writeText("Current")
119+
120+ val new = File("Test/New/File.txt")
121+ new.createNewFile()
122+ new.writeText("New")
123+
124+ FileUtil.swapInFile("Test/Current", "Test/New") shouldBe null
125+
126+ zzDir.shouldNotExist()
127+ inTheWay.shouldNotExist()
128+ newDir.shouldNotExist()
129+ new.shouldNotExist()
130+
131+ currentDir.shouldExist()
132+ current.shouldExist()
133+ current.readLines().shouldContainExactly("New")
134+ }
135+
136+ @Test
137+ fun `Rename should fail for non-existent file`() {
138+ val f = File("Test/Test.txt")
139+
140+ f.renameToWithRetries(File("Test/New.txt")) shouldBe false
141+
142+ val error = verifyLog(CODE_FILE_ERROR, Severity.ERROR)
143+ error.message shouldBe
144+ "Trying to rename [Test/Test.txt] to [Test/New.txt] but it does not exist"
145+ }
146+
147+ @Test
148+ fun `Rename should fail if destination already exists`() {
149+ val f = File("Test/Test.txt")
150+ f.createNewFile()
151+ val new = File("Test/New.txt")
152+ new.createNewFile()
153+
154+ f.renameToWithRetries(new) shouldBe false
155+
156+ val error = verifyLog(CODE_FILE_ERROR, Severity.ERROR)
157+ error.message shouldBe
158+ "Trying to rename [Test/Test.txt] to [Test/New.txt] but [Test/New.txt] already exists"
159+ }
160+
161+ @Test
162+ fun `Rename should succeed on retry`() {
163+ val f = mockk<File>()
164+ every { f.exists() } returns true
165+
166+ var counter = 0
167+ every { f.renameTo(any()) } answers
168+ {
169+ if (counter < 2) {
170+ counter++
171+ false
172+ } else {
173+ true
174+ }
175+ }
176+
177+ val new = File("Test/New.txt")
178+ f.renameToWithRetries(new) shouldBe true
179+
180+ verifyLog(CODE_FILE_ERROR, Severity.WARN)
181+ }
182+
183+ @Test
184+ fun `Rename should give up on persistent failure`() {
185+ val f = mockk<File>()
186+ every { f.exists() } returns true
187+ every { f.renameTo(any()) } returns false
188+
189+ val new = File("Test/New.txt")
190+
191+ f.renameToWithRetries(new) shouldBe false
192+ }
193+
97194 @Test
98195 fun `Should read image dimensions correctly`() {
99196 val bean = javaClass.getResource("/Bean.png")!!.toURI()
0 commit comments