Skip to content

Commit

Permalink
Fix Windows Paths in Coko Tests (#853)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Wendland <[email protected]>
  • Loading branch information
CodingDepot and fwendland authored May 3, 2024
1 parent 1a0b4d1 commit e8d26b3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ class CokoScriptHostTest {
""".trimIndent()
)

val modelImport = modelDefinitionFile.toAbsoluteInvariant()
assertDoesNotThrow {
CokoExecutor.eval(
"""
@file:Import("${modelDefinitionFile.toAbsolutePath()}")
@file:Import("$modelImport")
class TestImpl: TestConcept {
override fun log(message: String) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.UnreachableEOGPass
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.toPath
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
Expand All @@ -34,7 +34,7 @@ class ConceptTranslationTest {
.getResource("IntegrationTests/CokoCpg/Main.java"),
CokoCpgIntegrationTest::class.java.classLoader
.getResource("IntegrationTests/CokoCpg/SimpleOrder.java")
).map { Path(it.path) }.also { assertEquals(2, it.size) }
).map { it.toURI().toPath() }.also { assertEquals(2, it.size) }

val cpgConfiguration =
CPGConfiguration(
Expand Down Expand Up @@ -62,15 +62,7 @@ class ConceptTranslationTest {
val specFiles = listOfNotNull(
CokoCpgIntegrationTest::class.java.classLoader
.getResource("concept/bsi-tr.concepts"),
).map { Path(it.path) }

val cokoConfiguration =
CokoConfiguration(
goodFindings = true,
pedantic = false,
spec = specFiles,
disabledSpecRules = emptyList(),
)
).map { it.toURI().toPath() }

val backend = CokoCpgBackend(cpgConfiguration)
val specEvaluator = CokoExecutor.compileScriptsIntoSpecEvaluator(backend, specFiles)
Expand Down Expand Up @@ -101,15 +93,7 @@ class ConceptTranslationTest {
val specFiles = listOfNotNull(
CokoCpgIntegrationTest::class.java.classLoader
.getResource("concept/some.concepts"),
).map { Path(it.path) }

val cokoConfiguration =
CokoConfiguration(
goodFindings = true,
pedantic = false,
spec = specFiles,
disabledSpecRules = emptyList(),
)
).map { it.toURI().toPath() }

val backend = CokoCpgBackend(cpgConfiguration)
val specEvaluator = CokoExecutor.compileScriptsIntoSpecEvaluator(backend, specFiles)
Expand Down Expand Up @@ -148,7 +132,7 @@ class ConceptTranslationTest {
.getResource("concept/followedByImplementations.codyze.kts"),
CokoCpgIntegrationTest::class.java.classLoader
.getResource("concept/followedByRule.codyze.kts"),
).map { Path(it.path) }
).map { it.toURI().toPath() }

val cokoConfiguration =
CokoConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ class ScriptAnalysisTest {
""".trimIndent()
)

val modelImport = modelDefinitionFile.toAbsoluteInvariant()
val result =
CokoExecutor.eval(
"""
@file:Import("${modelDefinitionFile.toAbsolutePath()}")
@file:Import("$modelImport")
class TestImpl: TestConcept {
override fun log(message: String) { }
Expand All @@ -123,9 +124,10 @@ class ScriptAnalysisTest {
)

val implementationFile = tempDir.resolve("implementation.codyze.kts")
val modelImport = modelDefinitionFile.toAbsoluteInvariant()
implementationFile.writeText(
"""
@file:Import("${modelDefinitionFile.toAbsolutePath()}")
@file:Import("$modelImport")
class TestImpl: TestConcept {
override fun log(message: String) { }
Expand Down Expand Up @@ -177,10 +179,11 @@ class ScriptAnalysisTest {
""".trimIndent()
)

val modelImport = modelDefinitionFile.toAbsoluteInvariant()
val implementationFile = tempDir.resolve("implementation.codyze.kts")
implementationFile.writeText(
"""
@file:Import("${modelDefinitionFile.toAbsolutePath()}")
@file:Import("$modelImport")
class TestImpl: TestConcept {
override fun log(message: String) = op {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* 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 de.fraunhofer.aisec.codyze.specificationLanguages.coko.dsl

import java.nio.file.Path
import kotlin.io.path.invariantSeparatorsPathString

/**
* Forces any path into an absolute path String that is system invariant.
*
* This is especially important when testing with Windows, as paths with backward slashes are not allowed
* as Coko imports.
*/
fun Path.toAbsoluteInvariant(): String {
return this.toAbsolutePath().invariantSeparatorsPathString
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.UnreachableEOGPass
import io.github.detekt.sarif4k.ResultKind
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.toPath
import kotlin.test.assertEquals

// TODO: should probably in codyze-backends or coko-core
Expand All @@ -31,7 +31,7 @@ class WheneverEvaluatorTest {
private val sourceFiles = listOfNotNull(
CokoCpgIntegrationTest::class.java.classLoader
.getResource("concept/CipherTestFile.java"),
).map { Path(it.path) }.also { assertEquals(1, it.size) }
).map { it.toURI().toPath() }.also { assertEquals(1, it.size) }

val cpgConfiguration =
CPGConfiguration(
Expand Down Expand Up @@ -64,7 +64,7 @@ class WheneverEvaluatorTest {
.getResource("concept/bsi-tr-rules.codyze.kts"),
CokoCpgIntegrationTest::class.java.classLoader
.getResource("concept/jca-cipher.codyze.kts")
).map { Path(it.path) }.also { assertEquals(2, it.size) }
).map { it.toURI().toPath() }.also { assertEquals(2, it.size) }

val cokoConfiguration =
CokoConfiguration(
Expand Down

0 comments on commit e8d26b3

Please sign in to comment.