@@ -3,7 +3,7 @@ package io.shiftleft.codepropertygraph.cpgloading
33import flatgraph .{Accessors , DiffGraphApplier }
44import io .shiftleft .codepropertygraph .generated .Cpg
55import io .shiftleft .codepropertygraph .generated .nodes .NewMethod
6- import io .shiftleft .utils .ProjectRoot
6+ import io .shiftleft .utils .{ ProjectRoot , TempFileCopy }
77import io .shiftleft .codepropertygraph .generated .nodes .Type
88import org .scalatest .BeforeAndAfterAll
99import org .scalatest .matchers .should .Matchers
@@ -44,16 +44,15 @@ class CpgLoaderTests extends AnyWordSpec with Matchers with BeforeAndAfterAll {
4444 }
4545
4646 " allow loading of CPG in flatgraph format" in {
47- val flatgraphCpg = ProjectRoot .relativise( " codepropertygraph/src/test/resources/cpg.fg " )
48- Using .resource( CpgLoader .load(flatgraphCpg)) { cpg =>
49- // the test graph was created by c2cpg for https://github.com/joernio/joern/blob/master/tests/code/c/test.c
47+ Using . Manager { use =>
48+ val flatgraphCpg = use(temporaryFlatgraphCpg()).path
49+ val cpg = use( CpgLoader .load(flatgraphCpg))
5050 cpg.graph.nodes(" METHOD" ).size shouldBe 4
51- }
51+ }.get
5252 }
5353
5454 " allow loading of CPG in flatgraph format and persist changes to separate file" in {
55- val flatgraphCpg = Paths .get(ProjectRoot .relativise(" codepropertygraph/src/test/resources/cpg.fg" ))
56- val persistTo = Files .createTempFile(getClass.getSimpleName, " persistToTest" )
55+ val persistTo = Files .createTempFile(getClass.getSimpleName, " persistToTest" )
5756
5857 Using .resource(CpgLoader .load(flatgraphCpg, persistTo)) { cpg =>
5958 DiffGraphApplier .applyDiff(cpg.graph, Cpg .newDiffGraphBuilder.addNode(NewMethod ()))
@@ -63,14 +62,22 @@ class CpgLoaderTests extends AnyWordSpec with Matchers with BeforeAndAfterAll {
6362 cpg.graph.nodes(" METHOD" ).size shouldBe 5
6463 }
6564
66- Using .resource(CpgLoader .load(flatgraphCpg)) { cpg =>
65+ Using .Manager { use =>
66+ val flatgraphCpg = use(temporaryFlatgraphCpg()).path
67+ val cpg = use(CpgLoader .load(flatgraphCpg))
6768 // original cpg should be unchanged
6869 cpg.graph.nodes(" METHOD" ).size shouldBe 4
69- }
70+ }.get
7071 }
7172
7273 " throw an appropriate exception if the provided filename that refers to a non-existing file" in {
7374 a[FileNotFoundException ] should be thrownBy CpgLoader .load(" invalid/path/cpg.bin.zip" )
7475 }
7576
77+ /** this test graph was created by c2cpg for https://github.com/joernio/joern/blob/master/tests/code/c/test.c */
78+ val flatgraphCpg = Paths .get(ProjectRoot .relativise(" codepropertygraph/src/test/resources/cpg.fg" ))
79+
80+ /** flatgraph writes to this file on 'close', so we'll create a temporary copy */
81+ private def temporaryFlatgraphCpg (): TempFileCopy =
82+ TempFileCopy (flatgraphCpg)
7683}
0 commit comments