Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ class GoSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndA

private var port: Int = -1

private def newProjectUnderTest(index: Option[Int] = None): Path = {
val dir = Files.createTempDirectory("gosrc2cpgTestsHttpTest")
val file = dir / "main.go"
file.createWithParentsIfNotExists(createParents = true)
val indexStr = index.map(_.toString).getOrElse("")
val content = s"""
|package main
|func main$indexStr() {
| print("Hello World!")
|}
|""".stripMargin
Files.writeString(file, content)
FileUtil.deleteOnExit(file)
FileUtil.deleteOnExit(dir)
dir
private def newProjectUnderTest[T](index: Option[Int] = None)(f: Path => T): T = {
FileUtil.usingTemporaryDirectory("gosrc2cpgTestsHttpTest") { dir =>
val file = dir / "main.go"
file.createWithParentsIfNotExists(createParents = true)
val indexStr = index.map(_.toString).getOrElse("")
val content = s"""
|package main
|func main$indexStr() {
| print("Hello World!")
|}
|""".stripMargin
Files.writeString(file, content)
f(dir)
}
}

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

"Using gosrc2cpg in server mode" should {
"build CPGs correctly (single test)" in {
val cpgOutFile = FileUtil.newTemporaryFile("gosrc2cpg.bin")
FileUtil.deleteOnExit(cpgOutFile)
val projectUnderTest = newProjectUnderTest()
val input = projectUnderTest.toAbsolutePath.toString
val output = cpgOutFile.toString
val client = FrontendHTTPClient(port)
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
client.sendRequest(req) match {
case Failure(exception) => fail(exception.getMessage)
case Success(out) =>
out shouldBe output
val cpg = CpgLoader.load(output)
cpg.method.name.l should contain("main")
cpg.call.code.l shouldBe List("""print("Hello World!")""")
FileUtil.usingTemporaryFile("gosrc2cpg", ".bin") { cpgOutFile =>
newProjectUnderTest() { projectUnderTest =>
val input = projectUnderTest.toAbsolutePath.toString
val output = cpgOutFile.toString
val client = FrontendHTTPClient(port)
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
client.sendRequest(req) match {
case Failure(exception) => fail(exception.getMessage)
case Success(out) =>
out shouldBe output
val cpg = CpgLoader.load(output)
cpg.method.name.l should contain("main")
cpg.call.code.l shouldBe List("""print("Hello World!")""")
}
}
}
}

"build CPGs correctly (multi-threaded test)" in {
(0 until 10).par.foreach { index =>
val cpgOutFile = FileUtil.newTemporaryFile("gosrc2cpg.bin")
FileUtil.deleteOnExit(cpgOutFile)
val projectUnderTest = newProjectUnderTest(Some(index))
val input = projectUnderTest.toAbsolutePath.toString
val output = cpgOutFile.toString
val client = FrontendHTTPClient(port)
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
client.sendRequest(req) match {
case Failure(exception) => fail(exception.getMessage)
case Success(out) =>
out shouldBe output
val cpg = CpgLoader.load(output)
cpg.method.name.l should contain(s"main$index")
cpg.call.code.l shouldBe List("""print("Hello World!")""")
FileUtil.usingTemporaryFile("gosrc2cpg", ".bin") { cpgOutFile =>
newProjectUnderTest(Some(index)) { projectUnderTest =>
val input = projectUnderTest.toAbsolutePath.toString
val output = cpgOutFile.toString
val client = FrontendHTTPClient(port)
val req = client.buildRequest(Array(s"input=$input", s"output=$output"))
client.sendRequest(req) match {
case Failure(exception) => fail(exception.getMessage)
case Success(out) =>
out shouldBe output
val cpg = CpgLoader.load(output)
cpg.method.name.l should contain(s"main$index")
cpg.call.code.l shouldBe List("""print("Hello World!")""")
}
}
}
}
}
Expand Down
Loading