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

Commit

Permalink
further improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackAnubis7 committed Sep 30, 2022
1 parent 8eef2ad commit ae9fbd9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ object BazelFlag {
@JvmStatic fun repositoryOverride(repositoryName: String, path: String): String =
arg("override_repository", "$repositoryName=$path")

@JvmStatic fun testOutputAll(): String =
arg("test_output", "all")

private fun arg(name: String, value: String) =
String.format("--%s=%s", name, value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BspClientTestNotifier {
val taskStartParams = TaskStartParams(taskId)
taskStartParams.dataKind = TaskDataKind.TEST_START
taskStartParams.data = testStart
if (isSuite) taskStartParams.message = "<S>" else taskStartParams.message = "<T>"
taskStartParams.message = if (isSuite) SUITE_TAG else TEST_TAG
bspClient.onBuildTaskStart(taskStartParams)
}

Expand Down Expand Up @@ -69,7 +69,7 @@ class BspClientTestNotifier {
val taskFinishParams = TaskFinishParams(taskId, StatusCode.OK)
taskFinishParams.dataKind = TaskDataKind.TEST_FINISH
taskFinishParams.data = testFinish
if (isSuite) taskFinishParams.message = "<S>" else taskFinishParams.message = "<T>"
taskFinishParams.message = if (isSuite) SUITE_TAG else TEST_TAG
bspClient.onBuildTaskFinish(taskFinishParams)
}

Expand Down Expand Up @@ -103,4 +103,9 @@ class BspClientTestNotifier {
fun initialize(buildClient: BuildClient) {
bspClient = buildClient
}

companion object {
const val SUITE_TAG = "<S>"
const val TEST_TAG = "<T>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseError
import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode
import org.jetbrains.bsp.bazel.bazelrunner.BazelProcessResult
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag
import org.jetbrains.bsp.bazel.logger.BspClientTestNotifier
import org.jetbrains.bsp.bazel.server.bsp.managers.BazelBspCompilationManager
import org.jetbrains.bsp.bazel.server.sync.BspMappings.toBspId
Expand Down Expand Up @@ -45,7 +46,7 @@ class ExecuteService(
result = bazelRunner.commandBuilder().test()
.withTargets(targetsSpec)
.withArguments(params.arguments)
.withFlags(listOf("--test_output=all"))
.withFlags(listOf(BazelFlag.testOutputAll()))
.executeBazelBesCommand(params.originId)
.waitAndGetResult(true)
JUnitTestParser(bspClientTestNotifier).processTestOutputWithJUnit5(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,62 @@ import io.kotest.matchers.shouldBe
import org.jetbrains.bsp.bazel.bazelrunner.BazelProcessResult
import org.jetbrains.bsp.bazel.bazelrunner.outputs.OutputCollector
import org.jetbrains.bsp.bazel.logger.BspClientTestNotifier
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import java.util.*

class JUnitTestParserTest {
private val outputString = """

@Test
fun `should finish all started tests`() {
bspClient.startedSuiteStack.empty() shouldBe true
}

@Test
fun `should conduct correct number of tests`() {
bspClient.conductedTests.size shouldBeExactly 18
}

@Test
fun `should conduct correct number of suites`() {
bspClient.conductedSuites.size shouldBeExactly 4
}

@Test
fun `should fail one test`() {
val failed = bspClient.conductedTests.filter { it.status == TestStatus.FAILED }
failed.size shouldBeExactly 1
failed.firstOrNull()?.name shouldBe "should return ScalaLanguagePlugin for Scala Language()"
}

@Test
fun `should parse nested tests correctly`() {
bspClient.getParentByName("should really work()", false) shouldBe "An inner inner test"
bspClient.getParentByName("An inner inner test", true) shouldBe "Tests for the method shouldGetPlugin"
bspClient.getParentByName("Tests for the method shouldGetPlugin", true) shouldBe "LanguagePluginServiceTest"
bspClient.getParentByName("LanguagePluginServiceTest", true) shouldBe null
}

@Test
fun `should distinguish similarly-named tests and suites`() {
bspClient.conductedTests.count { it.name == "Tests for the method shouldGetPlugin" } shouldBeExactly 1
bspClient.conductedSuites.count { it.name == "Tests for the method shouldGetPlugin" } shouldBeExactly 1
}

@Test
fun `should parse tests with trimmed name`() {
bspClient.conductedTests.count {
it.name == "should return CppLanguagePlugin for Cpp Languahgfdhgusdhgfihdfhgosihdfgoisdfogih..."
} shouldBeExactly 1
}

@Test
fun `should detect testing duration`() {
bspClient.duration shouldBeExactly 382
}

companion object {
private val outputString = """
|Invoking: /opt/homebrew/bin/bazel build --bes_backend=grpc://localhost:60052 --define=ORIGINID=test-a9896735-e16b-49f0-b013-7d337213422e -- //server/src/test/java/org/jetbrains/bsp/bazel/server/sync/languages:LanguagePluginServiceTest
|Loading:
|Loading: 0 packages loaded
Expand Down Expand Up @@ -86,67 +137,22 @@ class JUnitTestParserTest {
|Executed 0 out of 1 test: 1 test passes.
|Command completed in 236ms (exit code 0)""".trimMargin()

private val bazelProcessResult: BazelProcessResult
private val jUnitTestParser: JUnitTestParser
private val bspClient: MockBspClient

init {
val collector = OutputCollector()
outputString.lines().forEach { collector.onNextLine(it) }
bazelProcessResult = BazelProcessResult(collector, OutputCollector(), 0)
val testNotifier = BspClientTestNotifier()
bspClient = MockBspClient()
testNotifier.initialize(bspClient)
jUnitTestParser = JUnitTestParser(testNotifier)
jUnitTestParser.processTestOutputWithJUnit(bazelProcessResult)
}

@Test
fun `should finish all started tests`() {
bspClient.startedSuiteStack.empty() shouldBe true
}

@Test
fun `should conduct correct number of tests`() {
bspClient.conductedTests.size shouldBeExactly 18
}

@Test
fun `should conduct correct number of suites`() {
bspClient.conductedSuites.size shouldBeExactly 4
}

@Test
fun `should fail one test`() {
val failed = bspClient.conductedTests.filter { it.status == TestStatus.FAILED }
failed.size shouldBeExactly 1
failed.firstOrNull()?.name shouldBe "should return ScalaLanguagePlugin for Scala Language()"
}

@Test
fun `should parse nested tests correctly`() {
bspClient.getParentByName("should really work()", false) shouldBe "An inner inner test"
bspClient.getParentByName("An inner inner test", true) shouldBe "Tests for the method shouldGetPlugin"
bspClient.getParentByName("Tests for the method shouldGetPlugin", true) shouldBe "LanguagePluginServiceTest"
bspClient.getParentByName("LanguagePluginServiceTest", true) shouldBe null
}

@Test
fun `should distinguish similarly-named tests and suites`() {
bspClient.conductedTests.count { it.name == "Tests for the method shouldGetPlugin" } shouldBeExactly 1
bspClient.conductedSuites.count { it.name == "Tests for the method shouldGetPlugin" } shouldBeExactly 1
}

@Test
fun `should parse tests with trimmed name`() {
bspClient.conductedTests.count {
it.name == "should return CppLanguagePlugin for Cpp Languahgfdhgusdhgfihdfhgosihdfgoisdfogih..."
} shouldBeExactly 1
}

@Test
fun `should detect testing duration`() {
bspClient.duration shouldBeExactly 382
private lateinit var bazelProcessResult: BazelProcessResult
private lateinit var jUnitTestParser: JUnitTestParser
private lateinit var bspClient: MockBspClient

@JvmStatic
@BeforeAll
fun init() {
val collector = OutputCollector()
outputString.lines().forEach { collector.onNextLine(it) }
bazelProcessResult = BazelProcessResult(collector, OutputCollector(), 0)
val testNotifier = BspClientTestNotifier()
bspClient = MockBspClient()
testNotifier.initialize(bspClient)
jUnitTestParser = JUnitTestParser(testNotifier)
jUnitTestParser.processTestOutputWithJUnit5(bazelProcessResult)
}
}
}

Expand Down Expand Up @@ -182,7 +188,7 @@ private class MockBspClient : BuildClient {
when (params?.dataKind) {
TaskDataKind.TEST_START -> {
val testStart = params.data as? TestStart
val isSuite = params.message.take(3) == "<S>"
val isSuite = params.message.startsWith(BspClientTestNotifier.SUITE_TAG)
val displayName = testStart?.displayName
if (isSuite) startedSuiteStack.push(displayName)
else startedTest = displayName
Expand All @@ -201,7 +207,7 @@ private class MockBspClient : BuildClient {
when (params?.dataKind) {
TaskDataKind.TEST_FINISH -> {
val testFinish = params.data as TestFinish
val isSuite = params.message.take(3) == "<S>"
val isSuite = params.message.startsWith(BspClientTestNotifier.SUITE_TAG)
val displayName = testFinish.displayName
if (isSuite && displayName == stackPeekOrNull(startedSuiteStack)) {
stackPopOrNull(startedSuiteStack)
Expand Down

0 comments on commit ae9fbd9

Please sign in to comment.