Skip to content

Commit

Permalink
Simplify resource path determination
Browse files Browse the repository at this point in the history
  • Loading branch information
fwendland committed Sep 17, 2024
1 parent 52bd34a commit 06366b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
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,11 +16,13 @@
package de.fraunhofer.aisec.codyze.plugins.compiled

import de.fraunhofer.aisec.codyze.plugins.PluginTest
import java.io.File
import kotlin.io.path.toPath
import kotlin.test.assertNotNull

abstract class CompiledPluginTest : PluginTest() {
override fun scanFiles() {
// certain that resources are available
val libPath =
PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")!!
.toURI().toPath()
Expand All @@ -39,7 +41,7 @@ abstract class CompiledPluginTest : PluginTest() {
plugin.execute(
listOf(libPath),
contextPaths,
libPath.parent.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile()
File(reportUri)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package de.fraunhofer.aisec.codyze.plugins.source

import de.fraunhofer.aisec.codyze.plugins.PluginTest
import java.io.File
import kotlin.io.path.toPath
import kotlin.test.assertNotNull

Expand All @@ -27,7 +28,7 @@ abstract class SourcePluginTest : PluginTest() {
plugin.execute(
listOf(sourcePath),
listOf(),
sourcePath.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile()
File(reportUri)
)
}
}

0 comments on commit 06366b1

Please sign in to comment.