@@ -19,19 +19,18 @@ class SwiftSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeA
1919
2020 private var port : Int = - 1
2121
22- private def newProjectUnderTest (index : Option [Int ] = None ): Path = {
23- val dir = Files .createTempDirectory(" swiftsrc2cpgTestsHttpTest" )
24- val file = dir / " main.swift"
25- file.createWithParentsIfNotExists(createParents = true )
26- val indexStr = index.map(_.toString).getOrElse(" " )
27- val content = s """
28- |func main() {
29- | println( $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(" swiftsrc2cpgTestsHttpTest" ) { dir =>
24+ val file = dir / " main.swift"
25+ file.createWithParentsIfNotExists(createParents = true )
26+ val indexStr = index.map(_.toString).getOrElse(" " )
27+ val content = s """
28+ |func main() {
29+ | println( $indexStr)
30+ |} """ .stripMargin
31+ Files .writeString(file, content)
32+ f(dir)
33+ }
3534 }
3635
3736 override def beforeAll (): Unit = {
@@ -44,39 +43,41 @@ class SwiftSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeA
4443
4544 " Using swiftsrc2cpg in server mode" should {
4645 " build CPGs correctly (single test)" in {
47- val cpgOutFile = FileUtil .newTemporaryFile(" swiftsrc2cpg.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(" println()" )
46+ FileUtil .usingTemporaryFile(" swiftsrc2cpg" , " .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(" println()" )
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(" swiftsrc2cpg.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 " println( $index) " )
66+ FileUtil .usingTemporaryFile(" swiftsrc2cpg" , " .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 " println( $index) " )
79+ }
80+ }
8081 }
8182 }
8283 }
0 commit comments