diff --git a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/DefaultContentRootsTests.scala b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/DefaultContentRootsTests.scala index c53f42ab2b96..977013dbac1c 100644 --- a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/DefaultContentRootsTests.scala +++ b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/DefaultContentRootsTests.scala @@ -3,8 +3,15 @@ package io.joern.kotlin2cpg.querying import io.joern.kotlin2cpg.testfixtures.KotlinCode2CpgFixture import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators} import io.shiftleft.semanticcpg.language._ +import io.joern.kotlin2cpg.Config +import io.joern.x2cpg.X2CpgConfig -class DefaultContentRootsTests extends KotlinCode2CpgFixture(withOssDataflow = false, withDefaultJars = true) { +class DefaultContentRootsTests extends KotlinCode2CpgFixture(withOssDataflow = false) { + + override def getOverrideConfig(): Option[X2CpgConfig[?]] = { + val config = Config().withClasspath(getTestResourcesPaths()) + Some(config) + } "CPG for code with a simple function declaration with parameters of stdlib types, but not fully specified" should { val cpg = code(""" diff --git a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/LambdaTests.scala b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/LambdaTests.scala index 978333babf46..bd9644f7c9ff 100644 --- a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/LambdaTests.scala +++ b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/LambdaTests.scala @@ -7,8 +7,16 @@ import io.shiftleft.codepropertygraph.generated.edges.{Capture, Ref} import io.shiftleft.codepropertygraph.generated.nodes.{Binding, Block, ClosureBinding, MethodRef, Return} import io.shiftleft.semanticcpg.language._ import overflowdb.traversal.jIteratortoTraversal +import io.joern.kotlin2cpg.Config +import io.joern.x2cpg.X2CpgConfig + +class LambdaTests extends KotlinCode2CpgFixture(withOssDataflow = false) { + + override def getOverrideConfig(): Option[X2CpgConfig[?]] = { + val config = Config().withClasspath(getTestResourcesPaths()) + Some(config) + } -class LambdaTests extends KotlinCode2CpgFixture(withOssDataflow = false, withDefaultJars = true) { "CPG for code with a simple lambda which captures a method parameter" should { val cpg = code("fun f1(p: String) { 1.let { println(p) } }") diff --git a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeAliasTests.scala b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeAliasTests.scala index d76b92786d78..c9df63f7272c 100644 --- a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeAliasTests.scala +++ b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeAliasTests.scala @@ -1,10 +1,18 @@ package io.joern.kotlin2cpg.querying import io.joern.kotlin2cpg.testfixtures.KotlinCode2CpgFixture +import io.joern.kotlin2cpg.Config import io.joern.x2cpg.Defines import io.shiftleft.semanticcpg.language._ +import io.joern.x2cpg.X2CpgConfig + +class TypeAliasTests extends KotlinCode2CpgFixture(withOssDataflow = false) { + + override def getOverrideConfig(): Option[X2CpgConfig[?]] = { + val config = Config().withClasspath(getTestResourcesPaths()) + Some(config) + } -class TypeAliasTests extends KotlinCode2CpgFixture(withOssDataflow = false, withDefaultJars = true) { "CPG for code with simple typealias to Int" should { val cpg = code(""" |package mypkg diff --git a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/testfixtures/KotlinCodeToCpgFixture.scala b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/testfixtures/KotlinCodeToCpgFixture.scala index 7a422705bb5c..705cbf22b7ca 100644 --- a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/testfixtures/KotlinCodeToCpgFixture.scala +++ b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/testfixtures/KotlinCodeToCpgFixture.scala @@ -14,23 +14,22 @@ import io.shiftleft.utils.ProjectRoot import java.io.File trait KotlinFrontend extends LanguageFrontend { - protected val withTestResourcePaths: Boolean override val fileSuffix: String = ".kt" override def execute(sourceCodeFile: File): Cpg = { - val defaultContentRoot = - BFile(ProjectRoot.relativise("joern-cli/frontends/kotlin2cpg/src/test/resources/jars/")) - implicit val defaultConfig: Config = - Config( - classpath = if (withTestResourcePaths) Set(defaultContentRoot.path.toAbsolutePath.toString) else Set(), - includeJavaSourceFiles = true + implicit val defaultConfig: Config = getConfig() + .map(_.asInstanceOf[Config]) + .getOrElse( + Config( + includeJavaSourceFiles = true + ) ) new Kotlin2Cpg().createCpg(sourceCodeFile.getAbsolutePath).get } } -class KotlinTestCpg(override protected val withTestResourcePaths: Boolean) extends TestCpg with KotlinFrontend { +class KotlinTestCpg() extends TestCpg with KotlinFrontend { private var _withOssDataflow = false def withOssDataflow(value: Boolean = true): this.type = { @@ -49,10 +48,17 @@ class KotlinTestCpg(override protected val withTestResourcePaths: Boolean) exten } } -class KotlinCode2CpgFixture(withOssDataflow: Boolean = false, withDefaultJars: Boolean = false) - extends Code2CpgFixture(() => new KotlinTestCpg(withDefaultJars).withOssDataflow(withOssDataflow)) { +class KotlinCode2CpgFixture(withOssDataflow: Boolean = false) + extends Code2CpgFixture(() => new KotlinTestCpg().withOssDataflow(withOssDataflow)) { implicit val context: EngineContext = EngineContext() protected def flowToResultPairs(path: Path): List[(String, Option[Integer])] = path.resultPairs() + + protected def getTestResourcesPaths(): Set[String] = { + val defaultContentRoot = + BFile(ProjectRoot.relativise("joern-cli/frontends/kotlin2cpg/src/test/resources/jars/")) + + Set(defaultContentRoot.path.toAbsolutePath().toString) + } } diff --git a/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/Code2CpgFixture.scala b/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/Code2CpgFixture.scala index b07d1d0d60f4..8a62cca1f1a7 100644 --- a/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/Code2CpgFixture.scala +++ b/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/Code2CpgFixture.scala @@ -5,6 +5,7 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import scala.collection.mutable +import io.joern.x2cpg.X2CpgConfig // Fixture class from which all tests which require a code to CPG translation step // should either directly or indirectly use. The intended way is to derive from @@ -18,18 +19,25 @@ class Code2CpgFixture[T <: TestCpg](testCpgFactory: () => T) with Matchers with BeforeAndAfterAll with Inside { - private val cpgs = mutable.ArrayBuffer.empty[TestCpg] + private val cpgs = mutable.ArrayBuffer.empty[TestCpg] + private var config: Option[X2CpgConfig[_]] = None + + /** This method can be overridden to specify config that should be used for all tests in a fixture. + */ + def getOverrideConfig(): Option[X2CpgConfig[_]] = { + None + } def code(code: String): T = { val newCpg = testCpgFactory().moreCode(code) cpgs.append(newCpg) - newCpg + newCpg.withConfig(getOverrideConfig()) } def code(code: String, fileName: String): T = { val newCpg = testCpgFactory().moreCode(code, fileName) cpgs.append(newCpg) - newCpg + newCpg.withConfig(getOverrideConfig()) } override def afterAll(): Unit = { diff --git a/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/TestCpg.scala b/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/TestCpg.scala index 1f7cb205d277..b7596eb92c5b 100644 --- a/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/TestCpg.scala +++ b/joern-cli/frontends/x2cpg/src/test/scala/io/joern/x2cpg/testfixtures/TestCpg.scala @@ -40,6 +40,11 @@ abstract class TestCpg extends Cpg() with LanguageFrontend { this } + def withConfig(config: Option[X2CpgConfig[_]]): this.type = { + config.foreach(setConfig) + this + } + private def checkGraphEmpty(): Unit = { if (_graph.isDefined) { throw new RuntimeException("Modifying test data is not allowed after accessing graph.") diff --git a/querydb/src/test/scala/io/joern/suites/AndroidQueryTestSuite.scala b/querydb/src/test/scala/io/joern/suites/AndroidQueryTestSuite.scala index b45488e38874..54e6d51d5b7c 100644 --- a/querydb/src/test/scala/io/joern/suites/AndroidQueryTestSuite.scala +++ b/querydb/src/test/scala/io/joern/suites/AndroidQueryTestSuite.scala @@ -7,9 +7,16 @@ import io.joern.util.QueryUtil import io.shiftleft.codepropertygraph.Cpg import io.shiftleft.codepropertygraph.generated.nodes.ConfigFile import io.shiftleft.semanticcpg.language._ +import io.joern.x2cpg.X2CpgConfig +import io.joern.kotlin2cpg.Config class AndroidQueryTestSuite[QB <: QueryBundle](val queryBundle: QB) - extends KotlinCode2CpgFixture(withOssDataflow = true, withDefaultJars = true) { + extends KotlinCode2CpgFixture(withOssDataflow = true) { + + override def getOverrideConfig(): Option[X2CpgConfig[_]] = { + val config = Config().withClasspath(getTestResourcesPaths()) + Some(config) + } val argumentProvider = new QDBArgumentProvider(3)