Skip to content

Commit 7f1bd09

Browse files
committed
introduce TempFileCopy and use in test with flatgraphCpg
fixes PR build, since flatgraph writes to the test file on '.close' which leads to changes in the repo, which is not permitted for PR builds
1 parent 0e136d4 commit 7f1bd09

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.shiftleft.utils
2+
3+
import java.nio.file.Path
4+
import java.nio.file.Files
5+
import java.nio.file.StandardCopyOption
6+
7+
class TempFileCopy(sourcePath: Path, prefix: String = "resource", suffix: String = ".tmp") extends AutoCloseable {
8+
val path: Path = {
9+
val temp = Files.createTempFile(prefix, suffix)
10+
Files.copy(sourcePath, temp, StandardCopyOption.REPLACE_EXISTING)
11+
temp
12+
}
13+
14+
override def close() = Files.deleteIfExists(path)
15+
}

codepropertygraph/src/test/scala/io/shiftleft/codepropertygraph/cpgloading/CpgLoaderTests.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.shiftleft.codepropertygraph.cpgloading
33
import flatgraph.{Accessors, DiffGraphApplier}
44
import io.shiftleft.codepropertygraph.generated.Cpg
55
import io.shiftleft.codepropertygraph.generated.nodes.NewMethod
6-
import io.shiftleft.utils.ProjectRoot
6+
import io.shiftleft.utils.{ProjectRoot, TempFileCopy}
77
import io.shiftleft.codepropertygraph.generated.nodes.Type
88
import org.scalatest.BeforeAndAfterAll
99
import 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

Comments
 (0)