Skip to content

Commit e356450

Browse files
Deprecate FileUtil.deleteOnExit in pysrc2cpg HTTP tests (#5770)
1 parent 2e12422 commit e356450

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

joern-cli/frontends/pysrc2cpg/src/test/scala/io/joern/pysrc2cpg/io/PySrc2CpgHTTPServerTests.scala

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ class PySrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndA
1919

2020
private var port: Int = -1
2121

22-
private def newProjectUnderTest(index: Option[Int] = None): Path = {
23-
val dir = Files.createTempDirectory("pysrc2cpgTestsHttpTest")
24-
val file = dir / "main.py"
25-
file.createWithParentsIfNotExists(createParents = true)
26-
val indexStr = index.map(_.toString).getOrElse("")
27-
val content = s"""
28-
|def main():
29-
| print($indexStr)
30-
|""".stripMargin
31-
Files.writeString(file, content)
32-
FileUtil.deleteOnExit(file)
33-
FileUtil.deleteOnExit(dir)
34-
dir
22+
private def newProjectUnderTest[T](index: Option[Int] = None)(f: Path => T): T = {
23+
FileUtil.usingTemporaryDirectory("pysrc2cpgTestsHttpTest") { dir =>
24+
val file = dir / "main.py"
25+
file.createWithParentsIfNotExists(createParents = true)
26+
val indexStr = index.map(_.toString).getOrElse("")
27+
val content = s"""
28+
|def main():
29+
| print($indexStr)
30+
|""".stripMargin
31+
Files.writeString(file, content)
32+
f(dir)
33+
}
3534
}
3635

3736
override def beforeAll(): Unit = {
@@ -44,39 +43,41 @@ class PySrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndA
4443

4544
"Using pysrc2cpg in server mode" should {
4645
"build CPGs correctly (single test)" in {
47-
val cpgOutFile = FileUtil.newTemporaryFile("pysrc2cpg.bin")
48-
FileUtil.deleteOnExit(cpgOutFile)
49-
val projectUnderTest = newProjectUnderTest()
50-
val input = projectUnderTest.toAbsolutePath.toString
51-
val output = cpgOutFile.toString
52-
val client = FrontendHTTPClient(port)
53-
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
54-
client.sendRequest(req) match {
55-
case Failure(exception) => fail(exception.getMessage)
56-
case Success(out) =>
57-
out shouldBe output
58-
val cpg = CpgLoader.load(output)
59-
cpg.method.name.l should contain("main")
60-
cpg.call.code.l should contain("print()")
46+
FileUtil.usingTemporaryFile("pysrc2cpg", ".bin") { cpgOutFile =>
47+
newProjectUnderTest() { projectUnderTest =>
48+
val input = projectUnderTest.toAbsolutePath.toString
49+
val output = cpgOutFile.toString
50+
val client = FrontendHTTPClient(port)
51+
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
52+
client.sendRequest(req) match {
53+
case Failure(exception) => fail(exception.getMessage)
54+
case Success(out) =>
55+
out shouldBe output
56+
val cpg = CpgLoader.load(output)
57+
cpg.method.name.l should contain("main")
58+
cpg.call.code.l should contain("print()")
59+
}
60+
}
6161
}
6262
}
6363

6464
"build CPGs correctly (multi-threaded test)" in {
6565
(0 until 10).par.foreach { index =>
66-
val cpgOutFile = FileUtil.newTemporaryFile("pysrc2cpg.bin")
67-
FileUtil.deleteOnExit(cpgOutFile)
68-
val projectUnderTest = newProjectUnderTest(Some(index))
69-
val input = projectUnderTest.toAbsolutePath.toString
70-
val output = cpgOutFile.toString
71-
val client = FrontendHTTPClient(port)
72-
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
73-
client.sendRequest(req) match {
74-
case Failure(exception) => fail(exception.getMessage)
75-
case Success(out) =>
76-
out shouldBe output
77-
val cpg = CpgLoader.load(output)
78-
cpg.method.name.l should contain("main")
79-
cpg.call.code.l should contain(s"print($index)")
66+
FileUtil.usingTemporaryFile("pysrc2cpg", ".bin") { cpgOutFile =>
67+
newProjectUnderTest(Some(index)) { projectUnderTest =>
68+
val input = projectUnderTest.toAbsolutePath.toString
69+
val output = cpgOutFile.toString
70+
val client = FrontendHTTPClient(port)
71+
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
72+
client.sendRequest(req) match {
73+
case Failure(exception) => fail(exception.getMessage)
74+
case Success(out) =>
75+
out shouldBe output
76+
val cpg = CpgLoader.load(output)
77+
cpg.method.name.l should contain("main")
78+
cpg.call.code.l should contain(s"print($index)")
79+
}
80+
}
8081
}
8182
}
8283
}

0 commit comments

Comments
 (0)