Skip to content

Commit

Permalink
Fix Windows Path Issues in Codyze Tests (#884)
Browse files Browse the repository at this point in the history
Co-authored-by: Wendland, Florian <[email protected]>
  • Loading branch information
CodingDepot and fwendland authored Sep 17, 2024
1 parent 3868a52 commit 83170da
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,12 +33,19 @@ abstract class PluginTest {
open val expectedSuccess: Boolean = true
abstract val expectedResults: List<Result>

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
Expand All @@ -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()) {
Expand All @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}

0 comments on commit 83170da

Please sign in to comment.