@@ -19,21 +19,20 @@ class Kotlin2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAnd
1919
2020 private var port : Int = - 1
2121
22- private def newProjectUnderTest (index : Option [Int ] = None ): Path = {
23- val dir = Files .createTempDirectory(" kotlin2cpgTestsHttpTest" )
24- val file = dir / " main.kt"
25- file.createWithParentsIfNotExists(createParents = true )
26- val indexStr = index.map(_.toString).getOrElse(" " )
27- val content = s """
28- |package mypkg
29- |fun main(args : Array<String>) {
30- | println( $indexStr)
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(" kotlin2cpgTestsHttpTest" ) { dir =>
24+ val file = dir / " main.kt"
25+ file.createWithParentsIfNotExists(createParents = true )
26+ val indexStr = index.map(_.toString).getOrElse(" " )
27+ val content = s """
28+ |package mypkg
29+ |fun main(args : Array<String>) {
30+ | println( $indexStr)
31+ |}
32+ | """ .stripMargin
33+ Files .writeString(file, content)
34+ f(dir)
35+ }
3736 }
3837
3938 override def beforeAll (): Unit = {
@@ -46,39 +45,41 @@ class Kotlin2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAnd
4645
4746 " Using kotlin2cpg in server mode" should {
4847 " build CPGs correctly (single test)" in {
49- val cpgOutFile = FileUtil .newTemporaryFile(" kotlin2cpg.bin" )
50- FileUtil .deleteOnExit(cpgOutFile)
51- val projectUnderTest = newProjectUnderTest()
52- val input = projectUnderTest.absolutePathAsString
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 (" println()" )
48+ FileUtil .usingTemporaryFile(" kotlin2cpg" , " .bin" ) { cpgOutFile =>
49+ newProjectUnderTest() { projectUnderTest =>
50+ val input = projectUnderTest.absolutePathAsString
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 (" println()" )
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(" kotlin2cpg.bin" )
69- FileUtil .deleteOnExit(cpgOutFile)
70- val projectUnderTest = newProjectUnderTest(Some (index))
71- val input = projectUnderTest.absolutePathAsString
72- val output = cpgOutFile.toString
73- val client = FrontendHTTPClient (port)
74- val req = client.buildRequest(Array (s " input= $input" , s " output= $output" , " no-default-exclude" ))
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(" main" )
81- cpg.call.code.l shouldBe List (s " println( $index) " )
68+ FileUtil .usingTemporaryFile(" kotlin2cpg" , " .bin" ) { cpgOutFile =>
69+ newProjectUnderTest(Some (index)) { projectUnderTest =>
70+ val input = projectUnderTest.absolutePathAsString
71+ val output = cpgOutFile.toString
72+ val client = FrontendHTTPClient (port)
73+ val req = client.buildRequest(Array (s " input= $input" , s " output= $output" , " no-default-exclude" ))
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(" main" )
80+ cpg.call.code.l shouldBe List (s " println( $index) " )
81+ }
82+ }
8283 }
8384 }
8485 }
0 commit comments