Repository: joernio/joern
Pull request #1801 (2022-10-04 21:42:09)

Remove `semanticcpg/testing` deprecated class use
doing this in smaller chunks than the previous attempt https://github.com/joernio/joern/pull/1778/files


Patch:
diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/NewTagNodePairTraversal.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/NewTagNodePairTraversal.scala
index 280bdece1..300778da8 100644
--- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/NewTagNodePairTraversal.scala
+++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/NewTagNodePairTraversal.scala
@@ -3,9 +3,11 @@ package io.shiftleft.semanticcpg.language
 import io.shiftleft.codepropertygraph.generated.EdgeTypes
 import io.shiftleft.codepropertygraph.generated.nodes.{NewNode, NewTagNodePair, StoredNode}
 import io.shiftleft.passes.DiffGraph
+import overflowdb.BatchedUpdate.DiffGraphBuilder
 import overflowdb.traversal._
 
 class NewTagNodePairTraversal(traversal: Traversal[NewTagNodePair]) extends HasStoreMethod {
+  val original = traversal
 
   override def store()(implicit diffGraph: DiffGraph.Builder): Unit = {
     traversal.foreach { tagNodePair =>
@@ -19,7 +21,6 @@ class NewTagNodePairTraversal(traversal: Traversal[NewTagNodePair]) extends HasS
           diffGraph.addEdge(tagValue, tag.asInstanceOf[NewNode], EdgeTypes.TAGGED_BY, Nil)
       }
     }
-
   }
 
 }


Test patch:
diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/testing/package.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/testing/package.scala
index d1ecc590c..5754cdd2b 100644
--- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/testing/package.scala
+++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/testing/package.scala
@@ -3,8 +3,10 @@ package io.shiftleft.semanticcpg
 import io.shiftleft.codepropertygraph.Cpg
 import io.shiftleft.codepropertygraph.generated.nodes._
 import io.shiftleft.codepropertygraph.generated.{EdgeTypes, Languages, ModifierTypes}
-import io.shiftleft.passes.{CpgPass, DiffGraph}
-import io.shiftleft.semanticcpg.language._
+import io.shiftleft.passes.{SimpleCpgPass}
+import io.shiftleft.semanticcpg.language.{NewTagNodePairTraversal, _}
+import overflowdb.BatchedUpdate
+import overflowdb.BatchedUpdate.DiffGraphBuilder
 
 package object testing {
 
@@ -12,12 +14,26 @@ package object testing {
 
     def apply(): MockCpg = new MockCpg
 
-    def apply(f: (DiffGraph.Builder, Cpg) => Unit): MockCpg = new MockCpg().withCustom(f)
-
+    def apply(f: (DiffGraphBuilder, Cpg) => Unit): MockCpg = new MockCpg().withCustom(f)
   }
 
   case class MockCpg(cpg: Cpg = Cpg.emptyCpg) {
 
+    // TODO: remove this fn as soon as an appropriate `store` method has been defined on the traversal class itself
+    private def store(traversal: NewTagNodePairTraversal, diffGraph: DiffGraphBuilder): Unit = {
+      traversal.original.foreach { tagNodePair =>
+        val tag      = tagNodePair.tag
+        val tagValue = tagNodePair.node
+        diffGraph.addNode(tag.asInstanceOf[NewNode])
+        tagValue match {
+          case tagValue: StoredNode =>
+            diffGraph.addEdge(tagValue, tag.asInstanceOf[NewNode], EdgeTypes.TAGGED_BY)
+          case tagValue: NewNode =>
+            diffGraph.addEdge(tagValue, tag.asInstanceOf[NewNode], EdgeTypes.TAGGED_BY, Nil)
+        }
+      }
+    }
+
     def withMetaData(language: String = Languages.C): MockCpg = withMetaData(language, Nil)
 
     def withMetaData(language: String, overlays: List[String]): MockCpg = {
@@ -125,12 +141,12 @@ package object testing {
       paramTags: List[(String, String)] = List()
     ): MockCpg =
       withCustom { (graph, cpg) =>
-        implicit val diffGraph: DiffGraph.Builder = graph
+        implicit val diffGraph: DiffGraphBuilder = graph
         methodTags.foreach { case (k, v) =>
-          cpg.method.name(methodName).newTagNodePair(k, v).store()
+          store(cpg.method.name(methodName).newTagNodePair(k, v), diffGraph)
         }
         paramTags.foreach { case (k, v) =>
-          cpg.method.name(methodName).parameter.newTagNodePair(k, v).store()
+          store(cpg.method.name(methodName).parameter.newTagNodePair(k, v), diffGraph)
         }
       }
 
@@ -198,12 +214,12 @@ package object testing {
       graph.addNode(newNode)
     }
 
-    def withCustom(f: (DiffGraph.Builder, Cpg) => Unit): MockCpg = {
-      val diffGraph = new DiffGraph.Builder
+    def withCustom(f: (DiffGraphBuilder, Cpg) => Unit): MockCpg = {
+      val diffGraph = new DiffGraphBuilder
       f(diffGraph, cpg)
-      class MyPass extends CpgPass(cpg) {
-        override def run(): Iterator[DiffGraph] = {
-          Iterator(diffGraph.build())
+      class MyPass extends SimpleCpgPass(cpg) {
+        override def run(builder: BatchedUpdate.DiffGraphBuilder): Unit = {
+          builder.absorb(diffGraph)
         }
       }
       new MyPass().createAndApply()
diff --git a/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/types/structure/MethodTests.scala b/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/types/structure/MethodTests.scala
index a42454717..f542b467b 100644
--- a/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/types/structure/MethodTests.scala
+++ b/semanticcpg/src/test/scala/io/shiftleft/semanticcpg/language/types/structure/MethodTests.scala
@@ -56,7 +56,7 @@ class MethodTests extends AnyWordSpec with Matchers {
           .withCustom { case (graph, cpg) =>
             val namespaceBlock = cpg.namespaceBlock("namespace").head
             val method         = cpg.method("foo").head
-            graph.addNodeProperty(method, Method.PropertyNames.AstParentType, NamespaceBlock.Label)
+            graph.setNodeProperty(method, Method.PropertyNames.AstParentType, NamespaceBlock.Label)
             graph.addEdge(namespaceBlock, method, EdgeTypes.AST)
           }
           .cpg