Skip to content

Commit 6f54186

Browse files
Deprecate FileUtil.deleteOnExit in gosrc2cpg HTTP tests (#5773)
1 parent f319129 commit 6f54186

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/io/GoSrc2CpgHTTPServerTests.scala

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ class GoSrc2CpgHTTPServerTests 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("gosrc2cpgTestsHttpTest")
24-
val file = dir / "main.go"
25-
file.createWithParentsIfNotExists(createParents = true)
26-
val indexStr = index.map(_.toString).getOrElse("")
27-
val content = s"""
28-
|package main
29-
|func main$indexStr() {
30-
| print("Hello World!")
31-
|}
32-
|""".stripMargin
33-
Files.writeString(file, content)
34-
FileUtil.deleteOnExit(file)
35-
FileUtil.deleteOnExit(dir)
36-
dir
22+
private def newProjectUnderTest[T](index: Option[Int] = None)(f: Path => T): T = {
23+
FileUtil.usingTemporaryDirectory("gosrc2cpgTestsHttpTest") { dir =>
24+
val file = dir / "main.go"
25+
file.createWithParentsIfNotExists(createParents = true)
26+
val indexStr = index.map(_.toString).getOrElse("")
27+
val content = s"""
28+
|package main
29+
|func main$indexStr() {
30+
| print("Hello World!")
31+
|}
32+
|""".stripMargin
33+
Files.writeString(file, content)
34+
f(dir)
35+
}
3736
}
3837

3938
override def beforeAll(): Unit = {
@@ -46,39 +45,41 @@ class GoSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndA
4645

4746
"Using gosrc2cpg in server mode" should {
4847
"build CPGs correctly (single test)" in {
49-
val cpgOutFile = FileUtil.newTemporaryFile("gosrc2cpg.bin")
50-
FileUtil.deleteOnExit(cpgOutFile)
51-
val projectUnderTest = newProjectUnderTest()
52-
val input = projectUnderTest.toAbsolutePath.toString
53-
val output = cpgOutFile.toString
54-
val client = FrontendHTTPClient(port)
55-
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
56-
client.sendRequest(req) match {
57-
case Failure(exception) => fail(exception.getMessage)
58-
case Success(out) =>
59-
out shouldBe output
60-
val cpg = CpgLoader.load(output)
61-
cpg.method.name.l should contain("main")
62-
cpg.call.code.l shouldBe List("""print("Hello World!")""")
48+
FileUtil.usingTemporaryFile("gosrc2cpg", ".bin") { cpgOutFile =>
49+
newProjectUnderTest() { projectUnderTest =>
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 shouldBe List("""print("Hello World!")""")
61+
}
62+
}
6363
}
6464
}
6565

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

0 commit comments

Comments
 (0)