mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-30 06:37:29 +00:00
test: 完善 AppPaths 中对 BOT_DATA_PATH 环境变量的测试流程.
通过使用 System-Lambda 库, 补充 AppPaths 中对环境变量使用的测试.
This commit is contained in:
parent
efbb57f1f7
commit
cac055bb08
@ -36,6 +36,7 @@ dependencies {
|
|||||||
|
|
||||||
testImplementation(kotlin("test"))
|
testImplementation(kotlin("test"))
|
||||||
testImplementation("io.mockk:mockk:1.12.3")
|
testImplementation("io.mockk:mockk:1.12.3")
|
||||||
|
testImplementation("com.github.stefanbirkner:system-lambda:1.2.1")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.lamgc.scalabot
|
package net.lamgc.scalabot
|
||||||
|
|
||||||
|
import com.github.stefanbirkner.systemlambda.SystemLambda
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
@ -40,23 +41,27 @@ internal class AppPathsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Data root path priority`() {
|
fun `Data root path priority`() {
|
||||||
System.setProperty("bot.path.data", "A")
|
System.setProperty("bot.path.data", "fromSystemProperties")
|
||||||
|
|
||||||
assertEquals("A", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有优先返回 Property 的值.")
|
assertEquals("fromSystemProperties", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有优先返回 Property 的值.")
|
||||||
System.getProperties().remove("bot.path.data")
|
System.getProperties().remove("bot.path.data")
|
||||||
if (System.getenv("BOT_DATA_PATH") != null) {
|
|
||||||
|
val expectEnvValue = "fromEnvironmentVariable"
|
||||||
|
SystemLambda.withEnvironmentVariable("BOT_DATA_PATH", expectEnvValue).execute {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
System.getenv("BOT_DATA_PATH"), AppPaths.DATA_ROOT.file.path,
|
expectEnvValue, AppPaths.DATA_ROOT.file.path,
|
||||||
"`DATA_ROOT`没有返回 env 的值."
|
"`DATA_ROOT`没有优先返回 env 的值."
|
||||||
)
|
)
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
SystemLambda.withEnvironmentVariable("BOT_DATA_PATH", null).execute {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
System.getProperty("user.dir"), AppPaths.DATA_ROOT.file.path,
|
System.getProperty("user.dir"), AppPaths.DATA_ROOT.file.path,
|
||||||
"`DATA_ROOT`没有返回 `user.dir` 的值."
|
"`DATA_ROOT`没有返回 System.properties `user.dir` 的值."
|
||||||
)
|
)
|
||||||
val userDir = System.getProperty("user.dir")
|
val userDir = System.getProperty("user.dir")
|
||||||
System.getProperties().remove("user.dir")
|
System.getProperties().remove("user.dir")
|
||||||
assertEquals(".", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有返回 `.`(当前目录).")
|
assertEquals(".", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有返回替补值 `.`(当前目录).")
|
||||||
System.setProperty("user.dir", userDir)
|
System.setProperty("user.dir", userDir)
|
||||||
assertNotNull(System.getProperty("user.dir"), "环境还原失败!")
|
assertNotNull(System.getProperty("user.dir"), "环境还原失败!")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user