Skip to content
Open
Show file tree
Hide file tree
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,20 +19,19 @@ class JsSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndA

private var port: Int = -1

private def newProjectUnderTest(index: Option[Int] = None): Path = {
val dir = Files.createTempDirectory("jssrc2cpgTestsHttpTest")
val file = dir / "main.js"
file.createWithParentsIfNotExists(createParents = true)
val indexStr = index.map(_.toString).getOrElse("")
val content = s"""
|function main$indexStr() {
| console.log("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("jssrc2cpgTestsHttpTest") { dir =>
val file = dir / "main.js"
file.createWithParentsIfNotExists(createParents = true)
val indexStr = index.map(_.toString).getOrElse("")
val content = s"""
|function main$indexStr() {
| console.log("Hello World!");
|}
|""".stripMargin
Files.writeString(file, content)
f(dir)
}
}

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

"Using jssrc2cpg in server mode" should {
"build CPGs correctly (single test)" in {
val cpgOutFile = FileUtil.newTemporaryFile("jssrc2cpg.bin")
FileUtil.deleteOnExit(cpgOutFile)
val projectUnderTest = newProjectUnderTest()
val input = projectUnderTest.absolutePathAsString
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 should contain("""console.log("Hello World!")""")
FileUtil.usingTemporaryFile("jssrc2cpg", ".bin") { cpgOutFile =>
newProjectUnderTest() { projectUnderTest =>
val input = projectUnderTest.absolutePathAsString
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 should contain("""console.log("Hello World!")""")
}
}
}
}

"build CPGs correctly (multi-threaded test)" in {
(0 until 10).par.foreach { index =>
val cpgOutFile = FileUtil.newTemporaryFile("jssrc2cpg.bin")
FileUtil.deleteOnExit(cpgOutFile)
val projectUnderTest = newProjectUnderTest(Some(index))
val input = projectUnderTest.absolutePathAsString
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 should contain("""console.log("Hello World!")""")
FileUtil.usingTemporaryFile("jssrc2cpg", ".bin") { cpgOutFile =>
newProjectUnderTest(Some(index)) { projectUnderTest =>
val input = projectUnderTest.absolutePathAsString
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 should contain("""console.log("Hello World!")""")
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ trait JsSrc2CpgFrontend extends LanguageFrontend {
override type ConfigType = Config

def execute(sourceCodePath: java.io.File): Cpg = {
val cpgOutFile = FileUtil.newTemporaryFile(suffix = "cpg.bin")
FileUtil.deleteOnExit(cpgOutFile)
val jssrc2cpg = new JsSrc2Cpg()
val config = getConfig()
.fold(Config(tsTypes = false))(_.asInstanceOf[Config])
.withInputPath(sourceCodePath.getAbsolutePath)
.withOutputPath(cpgOutFile.toString)
val res = jssrc2cpg.createCpg(config).get
new PostFrontendValidator(res, false).run()
res

}
}
Loading