@@ -19,20 +19,19 @@ class JsSrc2CpgHTTPServerTests 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(" jssrc2cpgTestsHttpTest" )
24- val file = dir / " main.js"
25- file.createWithParentsIfNotExists(createParents = true )
26- val indexStr = index.map(_.toString).getOrElse(" " )
27- val content = s """
28- |function main $indexStr() {
29- | console.log("Hello World!");
30- |}
31- | """ .stripMargin
32- Files .writeString(file, content)
33- FileUtil .deleteOnExit(file)
34- FileUtil .deleteOnExit(dir)
35- dir
22+ private def newProjectUnderTest [T ](index : Option [Int ] = None )(f : Path => T ): T = {
23+ FileUtil .usingTemporaryDirectory(" jssrc2cpgTestsHttpTest" ) { dir =>
24+ val file = dir / " main.js"
25+ file.createWithParentsIfNotExists(createParents = true )
26+ val indexStr = index.map(_.toString).getOrElse(" " )
27+ val content = s """
28+ |function main $indexStr() {
29+ | console.log("Hello World!");
30+ |}
31+ | """ .stripMargin
32+ Files .writeString(file, content)
33+ f(dir)
34+ }
3635 }
3736
3837 override def beforeAll (): Unit = {
@@ -45,39 +44,41 @@ class JsSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndA
4544
4645 " Using jssrc2cpg in server mode" should {
4746 " build CPGs correctly (single test)" in {
48- val cpgOutFile = FileUtil .newTemporaryFile(" jssrc2cpg.bin" )
49- FileUtil .deleteOnExit(cpgOutFile)
50- val projectUnderTest = newProjectUnderTest()
51- val input = projectUnderTest.absolutePathAsString
52- val output = cpgOutFile.toString
53- val client = FrontendHTTPClient (port)
54- val req = client.buildRequest(Array (s " input= $input" , s " output= $output" ))
55- client.sendRequest(req) match {
56- case Failure (exception) => fail(exception.getMessage)
57- case Success (out) =>
58- out shouldBe output
59- val cpg = CpgLoader .load(output)
60- cpg.method.name.l should contain(" main" )
61- cpg.call.code.l should contain(""" console.log("Hello World!")""" )
47+ FileUtil .usingTemporaryFile(" jssrc2cpg" , " .bin" ) { cpgOutFile =>
48+ newProjectUnderTest() { projectUnderTest =>
49+ val input = projectUnderTest.absolutePathAsString
50+ val output = cpgOutFile.toString
51+ val client = FrontendHTTPClient (port)
52+ val req = client.buildRequest(Array (s " input= $input" , s " output= $output" ))
53+ client.sendRequest(req) match {
54+ case Failure (exception) => fail(exception.getMessage)
55+ case Success (out) =>
56+ out shouldBe output
57+ val cpg = CpgLoader .load(output)
58+ cpg.method.name.l should contain(" main" )
59+ cpg.call.code.l should contain(""" console.log("Hello World!")""" )
60+ }
61+ }
6262 }
6363 }
6464
6565 " build CPGs correctly (multi-threaded test)" in {
6666 (0 until 10 ).par.foreach { index =>
67- val cpgOutFile = FileUtil .newTemporaryFile(" jssrc2cpg.bin" )
68- FileUtil .deleteOnExit(cpgOutFile)
69- val projectUnderTest = newProjectUnderTest(Some (index))
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" ))
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 should contain(""" console.log("Hello World!")""" )
67+ FileUtil .usingTemporaryFile(" jssrc2cpg" , " .bin" ) { cpgOutFile =>
68+ newProjectUnderTest(Some (index)) { projectUnderTest =>
69+ val input = projectUnderTest.absolutePathAsString
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(s " main $index" )
79+ cpg.call.code.l should contain(""" console.log("Hello World!")""" )
80+ }
81+ }
8182 }
8283 }
8384 }
0 commit comments