From 83170dabab58650fed2e5bdd50dd04cb8ffa29ca Mon Sep 17 00:00:00 2001 From: Robert Haimerl Date: Tue, 17 Sep 2024 10:41:13 +0200 Subject: [PATCH] Fix Windows Path Issues in Codyze Tests (#884) Co-authored-by: Wendland, Florian --- .../aisec/codyze/core/VersionProviderTest.kt | 6 ++--- .../aisec/codyze/plugins/PluginTest.kt | 25 +++++++++++------- .../plugins/compiled/CompiledPluginTest.kt | 26 ++++++++++++------- .../codyze/plugins/source/SourcePluginTest.kt | 9 ++++--- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt index 5af5d0bf0..87e423576 100644 --- a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt +++ b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt @@ -16,7 +16,7 @@ package de.fraunhofer.aisec.codyze.core import org.junit.jupiter.api.Test -import java.io.FileOutputStream +import java.io.File import java.util.Properties import kotlin.test.assertEquals @@ -34,7 +34,7 @@ class VersionProviderTest { // change property s.t. internal check fails val oldValue = properties.setProperty("project.name", "test") as String - FileOutputStream(propFile.file).use { + File(propFile.toURI()).outputStream().use { properties.store(it, null) } @@ -49,7 +49,7 @@ class VersionProviderTest { // restore original properties file properties.setProperty("project.name", oldValue) - FileOutputStream(propFile.file).use { + File(propFile.toURI()).outputStream().use { properties.store(it, null) } } diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt index 3270bc2eb..4813b33a8 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt @@ -21,6 +21,8 @@ import io.github.detekt.sarif4k.Result import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import java.io.File +import kotlin.io.path.deleteExisting +import kotlin.io.path.toPath import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -31,12 +33,19 @@ abstract class PluginTest { open val expectedSuccess: Boolean = true abstract val expectedResults: List + val reportUri by lazy { + // use an existing anchor in the resources directory + PluginTest::class.java.classLoader.getResource("targets")!!.toURI() + .resolve("..") + .resolve("generatedReports") + .resolve(resultFileName) + } + @Test fun testResults() { scanFiles() - val resultURI = PluginTest::class.java.classLoader.getResource("generatedReports/$resultFileName")?.toURI() - assertNotNull(resultURI) - val run = extractLastRun(File(resultURI)) + + val run = extractLastRun(File(reportUri)) assertNotNull(run) var results = run.results @@ -58,9 +67,8 @@ abstract class PluginTest { @Test fun testInvocation() { scanFiles() - val resultURI = PluginTest::class.java.classLoader.getResource("generatedReports/$resultFileName")?.toURI() - assertNotNull(resultURI) - val run = extractLastRun(File(resultURI)) + + val run = extractLastRun(File(reportUri)) assertNotNull(run) if (!run.invocations.isNullOrEmpty()) { @@ -72,9 +80,8 @@ abstract class PluginTest { @AfterEach fun cleanup() { - val resultURI = PluginTest::class.java.classLoader.getResource("generatedReports/$resultFileName")?.toURI() - if (resultURI != null) { - File(resultURI).delete() + reportUri.runCatching { + this.toPath().deleteExisting() } } diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt index 80c92c34e..6b1979360 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt @@ -16,24 +16,32 @@ package de.fraunhofer.aisec.codyze.plugins.compiled import de.fraunhofer.aisec.codyze.plugins.PluginTest -import java.nio.file.Path +import java.io.File +import kotlin.io.path.toPath import kotlin.test.assertNotNull abstract class CompiledPluginTest : PluginTest() { override fun scanFiles() { - val libPath = PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")?.path + // certain that resources are available + val libPath = + PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")!! + .toURI().toPath() val contextPaths = listOf( - PluginTest::class.java.classLoader.getResource("targets/libs/bcpkix-jdk18on-1.75.jar")?.path, - PluginTest::class.java.classLoader.getResource("targets/libs/bcprov-jdk18on-1.75.jar")?.path, - PluginTest::class.java.classLoader.getResource("targets/libs/bctls-jdk18on-1.75.jar")?.path, - PluginTest::class.java.classLoader.getResource("targets/libs/bcutil-jdk18on-1.75.jar")?.path + PluginTest::class.java.classLoader.getResource("targets/libs/bcpkix-jdk18on-1.75.jar")!! + .toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bcprov-jdk18on-1.75.jar")!! + .toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bctls-jdk18on-1.75.jar")!! + .toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bcutil-jdk18on-1.75.jar")!! + .toURI().toPath() ) assertNotNull(libPath) plugin.execute( - listOf(Path.of(libPath)), - contextPaths.map { Path.of(it!!) }, - Path.of(libPath).parent.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() + listOf(libPath), + contextPaths, + File(reportUri) ) } } diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt index 1e0d7a479..bcdbadf32 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt @@ -16,18 +16,19 @@ package de.fraunhofer.aisec.codyze.plugins.source import de.fraunhofer.aisec.codyze.plugins.PluginTest -import java.nio.file.Path +import java.io.File +import kotlin.io.path.toPath import kotlin.test.assertNotNull abstract class SourcePluginTest : PluginTest() { override fun scanFiles() { - val sourcePath = PluginTest::class.java.classLoader.getResource("targets/TlsServer.java")?.path + val sourcePath = PluginTest::class.java.classLoader.getResource("targets/TlsServer.java")!!.toURI().toPath() assertNotNull(sourcePath) plugin.execute( - listOf(Path.of(sourcePath)), + listOf(sourcePath), listOf(), - Path.of(sourcePath).parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() + File(reportUri) ) } }