From cac055bb08d585291e0b6ac5c3be6dd708e44a0a Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 19 May 2022 15:01:16 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=AE=8C=E5=96=84=20AppPaths=20?= =?UTF-8?q?=E4=B8=AD=E5=AF=B9=20BOT=5FDATA=5FPATH=20=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=9A=84=E6=B5=8B=E8=AF=95=E6=B5=81=E7=A8=8B?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过使用 System-Lambda 库, 补充 AppPaths 中对环境变量使用的测试. --- scalabot-app/build.gradle.kts | 1 + scalabot-app/src/test/kotlin/AppConfigTest.kt | 21 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/scalabot-app/build.gradle.kts b/scalabot-app/build.gradle.kts index c265f5c..18a5858 100644 --- a/scalabot-app/build.gradle.kts +++ b/scalabot-app/build.gradle.kts @@ -36,6 +36,7 @@ dependencies { testImplementation(kotlin("test")) testImplementation("io.mockk:mockk:1.12.3") + testImplementation("com.github.stefanbirkner:system-lambda:1.2.1") } tasks.test { diff --git a/scalabot-app/src/test/kotlin/AppConfigTest.kt b/scalabot-app/src/test/kotlin/AppConfigTest.kt index 503bcbf..94654a7 100644 --- a/scalabot-app/src/test/kotlin/AppConfigTest.kt +++ b/scalabot-app/src/test/kotlin/AppConfigTest.kt @@ -1,5 +1,6 @@ package net.lamgc.scalabot +import com.github.stefanbirkner.systemlambda.SystemLambda import com.google.gson.Gson import io.mockk.every import io.mockk.mockk @@ -40,23 +41,27 @@ internal class AppPathsTest { @Test 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") - if (System.getenv("BOT_DATA_PATH") != null) { + + val expectEnvValue = "fromEnvironmentVariable" + SystemLambda.withEnvironmentVariable("BOT_DATA_PATH", expectEnvValue).execute { assertEquals( - System.getenv("BOT_DATA_PATH"), AppPaths.DATA_ROOT.file.path, - "`DATA_ROOT`没有返回 env 的值." + expectEnvValue, AppPaths.DATA_ROOT.file.path, + "`DATA_ROOT`没有优先返回 env 的值." ) - } else { + } + + SystemLambda.withEnvironmentVariable("BOT_DATA_PATH", null).execute { assertEquals( 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") 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) assertNotNull(System.getProperty("user.dir"), "环境还原失败!") }