Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
small naming improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackAnubis7 committed Sep 30, 2022
1 parent 4814b6d commit 8eef2ad
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ch.epfl.scala.bsp4j.TestStatus
import ch.epfl.scala.bsp4j.TestTask

class BspClientTestNotifier {
private var bspClient: BuildClient? = null
private lateinit var bspClient: BuildClient
private var originId: String? = null

fun withOriginId(originId: String?): BspClientTestNotifier {
Expand All @@ -37,7 +37,20 @@ class BspClientTestNotifier {
taskStartParams.dataKind = TaskDataKind.TEST_START
taskStartParams.data = testStart
if (isSuite) taskStartParams.message = "<S>" else taskStartParams.message = "<T>"
bspClient!!.onBuildTaskStart(taskStartParams)
bspClient.onBuildTaskStart(taskStartParams)
}

/**
* Notifies the client about finishing a test suite. Synonymous to:
* ```
* finishTest(true, displayName, taskId, TestStatus.PASSED, "")
* ```
*
* @param displayName display name of the finished test suite
* @param taskId TaskId if the finished test suite
*/
fun finishTestSuite(displayName: String?, taskId: TaskId?) {
finishTest(true, displayName, taskId, TestStatus.PASSED, "")
}

/**
Expand All @@ -57,7 +70,7 @@ class BspClientTestNotifier {
taskFinishParams.dataKind = TaskDataKind.TEST_FINISH
taskFinishParams.data = testFinish
if (isSuite) taskFinishParams.message = "<S>" else taskFinishParams.message = "<T>"
bspClient!!.onBuildTaskFinish(taskFinishParams)
bspClient.onBuildTaskFinish(taskFinishParams)
}

/**
Expand All @@ -71,7 +84,7 @@ class BspClientTestNotifier {
val taskStartParams = TaskStartParams(taskId)
taskStartParams.dataKind = TaskDataKind.TEST_TASK
taskStartParams.data = testingBegin
bspClient!!.onBuildTaskStart(taskStartParams)
bspClient.onBuildTaskStart(taskStartParams)
}

/**
Expand All @@ -84,23 +97,10 @@ class BspClientTestNotifier {
val taskFinishParams = TaskFinishParams(taskId, StatusCode.OK)
taskFinishParams.dataKind = TaskDataKind.TEST_REPORT
taskFinishParams.data = testReport
bspClient!!.onBuildTaskFinish(taskFinishParams)
}

/**
* Notifies the client about finishing a test suite. Synonymous to:
* ```
* finishTest(true, displayName, taskId, TestStatus.PASSED, "")
* ```
*
* @param displayName display name of the finished test suite
* @param taskId TaskId if the finished test suite
*/
fun finishTestSuite(displayName: String?, taskId: TaskId?) {
finishTest(true, displayName, taskId, TestStatus.PASSED, "")
bspClient.onBuildTaskFinish(taskFinishParams)
}

fun initialize(buildClient: BuildClient?) {
fun initialize(buildClient: BuildClient) {
bspClient = buildClient
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ExecuteService(
.withFlags(listOf("--test_output=all"))
.executeBazelBesCommand(params.originId)
.waitAndGetResult(true)
JUnitTestParser(bspClientTestNotifier).processTestOutputWithJUnit(result)
JUnitTestParser(bspClientTestNotifier).processTestOutputWithJUnit5(result)
return TestResult(result.statusCode).apply {
originId = originId
data = result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,31 @@ private data class TestOutputLine(
val taskId: TaskId?
)

private data class StartedTestingTarget(
private data class StartedBuildTarget(
val uri: String,
val taskId: TaskId
)

class JUnitTestParser(
private val bspClientTestNotifier: BspClientTestNotifier
) {
fun processTestOutputWithJUnit(testResult: BazelProcessResult) {
fun processTestOutputWithJUnit5(testResult: BazelProcessResult) {
val startedSuites: Stack<TestOutputLine> = Stack()
var currentTestTarget: StartedTestingTarget? = null
var startedBuildTarget: StartedBuildTarget? = null
var previousOutputLine: TestOutputLine? = null

testResult.stdoutLines.forEach {
if (currentTestTarget == null) {
currentTestTarget = beginTesting(it)
if (startedBuildTarget == null) {
startedBuildTarget = checkLineForTestingBeginning(it)
} else {
val currentLineMatcher = testLinePattern.matcher(it)
val testingEndedMatcher = testingEndedPattern.matcher(it)
val currentOutputLine = getCurrentOutputLine(currentLineMatcher)
val currentOutputLine = getCurrentOutputLine(it)
if (currentOutputLine != null) {
processPreviousOutputLine(previousOutputLine, currentOutputLine, startedSuites)
previousOutputLine = currentOutputLine
} else if (testingEndedMatcher.find()) {
processTestingEndingLine(startedSuites, previousOutputLine, testingEndedMatcher, currentTestTarget)
currentTestTarget = null
processTestingEndingLine(startedSuites, previousOutputLine, testingEndedMatcher, startedBuildTarget)
startedBuildTarget = null
}
}
}
Expand All @@ -54,14 +53,14 @@ class JUnitTestParser(
startedSuites: Stack<TestOutputLine>,
previousOutputLine: TestOutputLine?,
testingEndedMatcher: Matcher,
currentTestTarget: StartedTestingTarget?
startedBuildTarget: StartedBuildTarget?
) {
startAndFinishTest(startedSuites, previousOutputLine!!)
while (startedSuites.isNotEmpty()) {
finishTopmostSuite(startedSuites)
}
val time = testingEndedMatcher.group("time").toLongOrNull() ?: 0
endTesting(currentTestTarget!!, time)
endTesting(startedBuildTarget!!, time)
}

private fun processPreviousOutputLine(
Expand All @@ -74,36 +73,49 @@ class JUnitTestParser(
startSuite(startedSuites, previousOutputLine)
} else {
startAndFinishTest(startedSuites, previousOutputLine)
while (startedSuites.isNotEmpty() && startedSuites.peek().indent >= currentOutputLine.indent) {
finishTopmostSuite(startedSuites)
}
removeAllFinishedSuites(startedSuites, currentOutputLine)
}
}
}

private fun getCurrentOutputLine(currentLineMatcher: Matcher) = if (currentLineMatcher.find()) {
TestOutputLine(
currentLineMatcher.group("name"),
currentLineMatcher.group("result") == "",
currentLineMatcher.group("message"),
currentLineMatcher.start("name"),
private fun removeAllFinishedSuites(
startedSuites: Stack<TestOutputLine>,
currentOutputLine: TestOutputLine
) {
while (startedSuites.isNotEmpty() && startedSuites.peek().indent >= currentOutputLine.indent) {
finishTopmostSuite(startedSuites)
}
}

private fun getCurrentOutputLine(line: String): TestOutputLine? {
val currentLineMatcher = testLinePattern.matcher(line)
return if (currentLineMatcher.find()) {
TestOutputLine(
currentLineMatcher.group("name"),
currentLineMatcher.group("result") == "",
currentLineMatcher.group("message"),
currentLineMatcher.start("name"),
null
)
} else {
null
)
} else {
null
}
}

private fun beginTesting(line: String): StartedTestingTarget? {
private fun checkLineForTestingBeginning(line: String): StartedBuildTarget? {
val testingStartMatcher = testingStartPattern.matcher(line)
if (testingStartMatcher.find()) {
val currentTestTarget = StartedTestingTarget(testingStartMatcher.group("target"), TaskId(testUUID()))
bspClientTestNotifier.beginTestTarget(BuildTargetIdentifier(currentTestTarget.uri), currentTestTarget.taskId)
return currentTestTarget
}
return null
return if (testingStartMatcher.find())
StartedBuildTarget(testingStartMatcher.group("target"), TaskId(testUUID())).also {
beginTesting(it)
}
else null
}

private fun beginTesting(startedBuildTarget: StartedBuildTarget) {
bspClientTestNotifier.beginTestTarget(BuildTargetIdentifier(startedBuildTarget.uri), startedBuildTarget.taskId)
}

private fun endTesting(testTarget: StartedTestingTarget, millis: Long) {
private fun endTesting(testTarget: StartedBuildTarget, millis: Long) {
val report = TestReport(BuildTargetIdentifier(testTarget.uri), 0, 0, 0, 0, 0)
report.time = millis
bspClientTestNotifier.endTestTarget(report, testTarget.taskId)
Expand Down

0 comments on commit 8eef2ad

Please sign in to comment.