mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
test(config): 补充一部分 AppPaths 的单元测试项目.
补充了针对 AppPaths.defaultInitializer() 和 AppPaths.DATA_ROOT 的单元测试项. 其他的有待补充.
This commit is contained in:
parent
7b985ce325
commit
27f54c3c36
@ -35,6 +35,7 @@ dependencies {
|
||||
implementation("io.prometheus:simpleclient_httpserver:0.15.0")
|
||||
|
||||
testImplementation(kotlin("test"))
|
||||
testImplementation("io.mockk:mockk:1.12.3")
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
|
@ -1,12 +1,19 @@
|
||||
package net.lamgc.scalabot
|
||||
|
||||
import com.google.gson.Gson
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.math.abs
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class BotAccountTest {
|
||||
internal class BotAccountTest {
|
||||
|
||||
@Test
|
||||
fun deserializerTest() {
|
||||
@ -29,3 +36,90 @@ class BotAccountTest {
|
||||
|
||||
}
|
||||
|
||||
internal class AppPathsTest {
|
||||
|
||||
@Test
|
||||
fun `Data root path priority`() {
|
||||
System.setProperty("bot.path.data", "A")
|
||||
|
||||
assertEquals("A", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有优先返回 Property 的值.")
|
||||
System.getProperties().remove("bot.path.data")
|
||||
if (System.getenv("BOT_DATA_PATH") != null) {
|
||||
assertEquals(
|
||||
System.getenv("BOT_DATA_PATH"), AppPaths.DATA_ROOT.file.path,
|
||||
"`DATA_ROOT`没有返回 env 的值."
|
||||
)
|
||||
} else {
|
||||
assertEquals(
|
||||
System.getProperty("user.dir"), AppPaths.DATA_ROOT.file.path,
|
||||
"`DATA_ROOT`没有返回 `user.dir` 的值."
|
||||
)
|
||||
val userDir = System.getProperty("user.dir")
|
||||
System.getProperties().remove("user.dir")
|
||||
assertEquals(".", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有返回 `.`(当前目录).")
|
||||
System.setProperty("user.dir", userDir)
|
||||
assertNotNull(System.getProperty("user.dir"), "环境还原失败!")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `default initializer`(@TempDir testDir: File) {
|
||||
val dirPath = "${testDir.canonicalPath}/directory/"
|
||||
val dirFile = File(dirPath)
|
||||
mockk<AppPaths> {
|
||||
every { file }.returns(File(dirPath))
|
||||
every { path }.returns(dirPath)
|
||||
every { initial() }.answers {
|
||||
Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
||||
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
||||
.apply { isAccessible = true }
|
||||
.invoke(null, this@mockk)
|
||||
}
|
||||
}.initial()
|
||||
assertTrue(dirFile.exists() && dirFile.isDirectory, "默认初始器未正常初始化【文件夹】.")
|
||||
|
||||
File(testDir, "test.txt").apply {
|
||||
mockk<AppPaths> {
|
||||
every { file }.returns(this@apply)
|
||||
every { path }.returns(this@apply.canonicalPath)
|
||||
every { initial() }.answers {
|
||||
Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
||||
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
||||
.apply { isAccessible = true }
|
||||
.invoke(null, this@mockk)
|
||||
}
|
||||
}.initial()
|
||||
assertTrue(this@apply.exists() && this@apply.isFile, "默认初始器未正常初始化【文件】.")
|
||||
}
|
||||
|
||||
val alreadyExistsFile = File("${testDir.canonicalPath}/alreadyExists.txt").apply {
|
||||
if (!exists()) {
|
||||
createNewFile()
|
||||
}
|
||||
}
|
||||
assertTrue(alreadyExistsFile.exists(), "文件状态与预期不符.")
|
||||
mockk<File> {
|
||||
every { exists() }.returns(true)
|
||||
every { canonicalPath }.answers { alreadyExistsFile.canonicalPath }
|
||||
every { createNewFile() }.answers { alreadyExistsFile.createNewFile() }
|
||||
every { mkdirs() }.answers { alreadyExistsFile.mkdirs() }
|
||||
every { mkdir() }.answers { alreadyExistsFile.mkdir() }
|
||||
}.apply {
|
||||
mockk<AppPaths> {
|
||||
every { file }.returns(this@apply)
|
||||
every { path }.returns(this@apply.canonicalPath)
|
||||
every { initial() }.answers {
|
||||
Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
||||
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
||||
.apply { isAccessible = true }
|
||||
.invoke(null, this@mockk)
|
||||
}
|
||||
}.initial()
|
||||
verify(exactly = 0) { createNewFile() }
|
||||
verify(exactly = 0) { mkdir() }
|
||||
verify(exactly = 0) { mkdirs() }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user