diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 37f6fd3..1cfa69f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,4 +1,5 @@ [versions] +burst = "2.10.2" changelog = "2.4.0" clikt = "5.0.3" intellij-platform = "2.10.2" @@ -25,6 +26,7 @@ oshai-logging = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = " okio = { module = "com.squareup.okio:okio", version.ref = "okio" } [plugins] +burst = { id = "app.cash.burst", version.ref = "burst" } changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" } kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } intellij-platform = { id = "org.jetbrains.intellij.platform", version.ref = "intellij-platform" } diff --git a/scripting-host/build.gradle.kts b/scripting-host/build.gradle.kts index 5a088b2..24cdeb7 100644 --- a/scripting-host/build.gradle.kts +++ b/scripting-host/build.gradle.kts @@ -18,6 +18,7 @@ import org.gradle.jvm.component.internal.JvmSoftwareComponentInternal plugins { alias(libs.plugins.kotlin.jvm) + alias(libs.plugins.burst) application } diff --git a/scripting-host/src/test/kotlin/ServerScriptingHostCacheTest.kt b/scripting-host/src/test/kotlin/ServerScriptingHostCacheTest.kt deleted file mode 100644 index e5201fa..0000000 --- a/scripting-host/src/test/kotlin/ServerScriptingHostCacheTest.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2024 Eduard Wolf - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.edwardday.serverscript.scripthost - -import kotlinx.coroutines.test.runTest -import kotlin.test.Test -import kotlin.test.assertEquals - -class ServerScriptingHostCacheTest { - - @Test - fun testCacheWithUnixDomainSockets() = runTest { - val testData = readResource("cache_data") - - val urls = List(5) { testData } - val actualResponses = executeWithUnixDomainSockets { - executeScripts(it, urls) - } - - repeat(5) { index -> - val counter = index + 1 - val expected = testData.body.map { it.replace("{counter}", "$counter") } - - val actual = actualResponses[index] - - assertEquals(expected, actual) - } - } - - @Test - fun testSharedCacheWithUnixDomainSockets() = runTest { - val testData1 = readResource("cache_import_script_1") - val testData2 = readResource("cache_import_script_2") - - val urls = listOf(testData1, testData2) - val actualResponses = executeWithUnixDomainSockets { - executeScripts(it, urls) - } - - assertEquals(testData1.body, actualResponses[0]) - assertEquals(testData2.body, actualResponses[1]) - } -} \ No newline at end of file diff --git a/scripting-host/src/test/kotlin/ServerScriptingHostScriptsTest.kt b/scripting-host/src/test/kotlin/ServerScriptingHostScriptsTest.kt deleted file mode 100644 index ea237e2..0000000 --- a/scripting-host/src/test/kotlin/ServerScriptingHostScriptsTest.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2024 Eduard Wolf - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.edwardday.serverscript.scripthost - -import kotlinx.coroutines.test.runTest -import org.junit.runner.RunWith -import org.junit.runners.Parameterized -import kotlin.test.Test -import kotlin.test.assertEquals - -@RunWith(Parameterized::class) -class ServerScriptingHostScriptsTest(private val testCaseName: String) { - - @Test - fun testScript() = runTest { - val testData = readResource(testCaseName) - - val actual = executeWithUnixDomainSockets { executeScript(it, testData) } - - assertEquals(testData.body, actual) - } - - companion object { - - @JvmStatic - @Parameterized.Parameters(name = "script:{0}") - fun data() = listOf( - "big_output", - "custom_headers", - "dependency", - "dependency_repository", - "empty", - "exception", - "import_function", - "import_invalid", - "import_not_existent", - "import_script", - "invalid", - "known_status", - "multiple_output", - "simple_script", - "unknown_status", - ) - } -} \ No newline at end of file diff --git a/scripting-host/src/test/kotlin/ServerScriptingHostTest.kt b/scripting-host/src/test/kotlin/ServerScriptingHostTest.kt new file mode 100644 index 0000000..cbec716 --- /dev/null +++ b/scripting-host/src/test/kotlin/ServerScriptingHostTest.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2024 Eduard Wolf + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.edwardday.serverscript.scripthost + +import app.cash.burst.Burst +import kotlinx.coroutines.test.runTest +import java.io.File +import kotlin.test.Test +import kotlin.test.assertEquals + + +@Burst +class ServerScriptingHostTest { + + @Test + fun testScript( + testCase: TestCases.Script, + ) = runTest { + val testData = readResource(testCase.fileName) + + val actual = executeWithUnixDomainSockets { executeScript(it, testData) } + + assertEquals(testData.body, actual) + } + + @Test + fun testCacheScript( + testCase: TestCases.CacheScript, + ) = runTest { + val urls = testCase.fileNames.map { readResource(it) } + val expected = when (testCase) { + TestCases.CacheScript.CACHE_DATA -> { + urls.mapIndexed { index, testData -> testData.body.map { it.replace("{counter}", "${index + 1}") } } + } + + TestCases.CacheScript.CACHE_IMPORT_SCRIPT -> { + urls.map(TestData::body) + } + } + + val actualResponses = executeWithUnixDomainSockets { + executeScripts(it, urls) + } + + assertEquals(expected, actualResponses) + } + + @Test + fun checkAllTestcasesCovered() { + val path = Thread.currentThread().contextClassLoader.getResource("imports")!!.path + val expected = File(path).parentFile.listFiles { _, name -> name.endsWith(".server.kts") } + .orEmpty() + .map { it.name.removeSuffix(".server.kts") } + .toSet() + + val actual = + (TestCases.Script.entries + TestCases.CacheScript.entries).flatMapTo(mutableSetOf(), TestCases::fileNames) + + assertEquals(expected, actual) + } +} \ No newline at end of file diff --git a/scripting-host/src/test/kotlin/TestCases.kt b/scripting-host/src/test/kotlin/TestCases.kt new file mode 100644 index 0000000..86ab71f --- /dev/null +++ b/scripting-host/src/test/kotlin/TestCases.kt @@ -0,0 +1,32 @@ +package net.edwardday.serverscript.scripthost + +interface TestCases { + val fileNames: List + + enum class Script(val fileName: String) : TestCases { + BIG_OUTPUT("big_output"), + CUSTOM_HEADERS("custom_headers"), + DEPENDENCY("dependency"), + DEPENDENCY_REPOSITORY("dependency_repository"), + EMPTY("empty"), + EXCEPTION("exception"), + IMPORT_FUNCTION("import_function"), + IMPORT_INVALID("import_invalid"), + IMPORT_NOT_EXISTENT("import_not_existent"), + IMPORT_SCRIPT("import_script"), + INVALID("invalid"), + KNOWN_STATUS("known_status"), + MULTIPLE_OUTPUT("multiple_output"), + SIMPLE_SCRIPT("simple_script"), + UNKNOWN_STATUS("unknown_status"), + ; + + override val fileNames: List get() = listOf(fileName) + } + + enum class CacheScript(override val fileNames: List) : TestCases { + CACHE_DATA(List(5) { "cache_data" }), + CACHE_IMPORT_SCRIPT(listOf("cache_import_script_1", "cache_import_script_2")), + ; + } +} \ No newline at end of file