@@ -18,16 +18,15 @@ class CSharp2CpgHTTPServerTests extends CSharpCode2CpgFixture with BeforeAndAfte
1818
1919 private var port : Int = - 1
2020
21- private def newProjectUnderTest (index : Option [Int ] = None ): Path = {
22- val dir = Files .createTempDirectory(" csharp2cpgTestsHttpTest" )
23- val file = dir / " main.cs"
24- file.createWithParentsIfNotExists(createParents = true )
25- val indexStr = index.map(_.toString).getOrElse(" " )
26- val content = basicBoilerplate(s " Console.WriteLine( $indexStr); " )
27- Files .writeString(file, content)
28- FileUtil .deleteOnExit(file)
29- FileUtil .deleteOnExit(dir)
30- dir
21+ private def usingProjectUnderTest [T ](index : Option [Int ] = None )(f : Path => T ): T = {
22+ FileUtil .usingTemporaryDirectory(" csharp2cpgTestsHttpTest" ) { dir =>
23+ val file = dir / " main.cs"
24+ file.createWithParentsIfNotExists(createParents = true )
25+ val indexStr = index.map(_.toString).getOrElse(" " )
26+ val content = basicBoilerplate(s " Console.WriteLine( $indexStr); " )
27+ Files .writeString(file, content)
28+ f(dir)
29+ }
3130 }
3231
3332 override def beforeAll (): Unit = {
@@ -40,39 +39,41 @@ class CSharp2CpgHTTPServerTests extends CSharpCode2CpgFixture with BeforeAndAfte
4039
4140 " Using csharp2cpg in server mode" should {
4241 " build CPGs correctly (single test)" in {
43- val cpgOutFile = FileUtil .newTemporaryFile(" csharp2cpg.bin" )
44- FileUtil .deleteOnExit(cpgOutFile)
45- val projectUnderTest = newProjectUnderTest()
46- val input = projectUnderTest.toAbsolutePath.toString
47- val output = cpgOutFile.toString
48- val client = FrontendHTTPClient (port)
49- val req = client.buildRequest(Array (s " input= $input" , s " output= $output" ))
50- client.sendRequest(req) match {
51- case Failure (exception) => fail(exception.getMessage)
52- case Success (out) =>
53- out shouldBe output
54- val cpg = CpgLoader .load(output)
55- cpg.method.name.l should contain(" Main" )
56- cpg.call.code.l shouldBe List (" Console.WriteLine()" )
42+ FileUtil .usingTemporaryFile(suffix = " csharp2cpg.bin" ) { cpgOutFile =>
43+ usingProjectUnderTest() { projectUnderTest =>
44+ val input = projectUnderTest.toAbsolutePath.toString
45+ val output = cpgOutFile.toString
46+ val client = FrontendHTTPClient (port)
47+ val req = client.buildRequest(Array (s " input= $input" , s " output= $output" ))
48+ client.sendRequest(req) match {
49+ case Failure (exception) => fail(exception.getMessage)
50+ case Success (out) =>
51+ out shouldBe output
52+ val cpg = CpgLoader .load(output)
53+ cpg.method.name.l should contain(" Main" )
54+ cpg.call.code.l shouldBe List (" Console.WriteLine()" )
55+ }
56+ }
5757 }
5858 }
5959
6060 " build CPGs correctly (multi-threaded test)" in {
6161 (0 until 10 ).par.foreach { index =>
62- val cpgOutFile = FileUtil .newTemporaryFile(" csharp2cpg.bin" )
63- FileUtil .deleteOnExit(cpgOutFile)
64- val projectUnderTest = newProjectUnderTest(Some (index))
65- val input = projectUnderTest.toAbsolutePath.toString
66- val output = cpgOutFile.toString
67- val client = FrontendHTTPClient (port)
68- val req = client.buildRequest(Array (s " input= $input" , s " output= $output" ))
69- client.sendRequest(req) match {
70- case Failure (exception) => fail(exception.getMessage)
71- case Success (out) =>
72- out shouldBe output
73- val cpg = CpgLoader .load(output)
74- cpg.method.name.l should contain(" Main" )
75- cpg.call.code.l shouldBe List (s " Console.WriteLine( $index) " )
62+ FileUtil .usingTemporaryFile(suffix = " csharp2cpg.bin" ) { cpgOutFile =>
63+ usingProjectUnderTest(Some (index)) { projectUnderTest =>
64+ val input = projectUnderTest.toAbsolutePath.toString
65+ val output = cpgOutFile.toString
66+ val client = FrontendHTTPClient (port)
67+ val req = client.buildRequest(Array (s " input= $input" , s " output= $output" ))
68+ client.sendRequest(req) match {
69+ case Failure (exception) => fail(exception.getMessage)
70+ case Success (out) =>
71+ out shouldBe output
72+ val cpg = CpgLoader .load(output)
73+ cpg.method.name.l should contain(" Main" )
74+ cpg.call.code.l shouldBe List (s " Console.WriteLine( $index) " )
75+ }
76+ }
7677 }
7778 }
7879 }
0 commit comments