Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add removing stack traces for simple report #1931

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,15 +38,6 @@ private fun TestExecutionData.reduceTestCases() = copy(
}
)

internal fun List<TestExecutionData>.removeStackTraces(): List<TestExecutionData> =
map(TestExecutionData::removeStackTraces)

private fun TestExecutionData.removeStackTraces() = copy(
testCases = testCases.onEach {
if (it.flaky) it.stackTraces = emptyList()
}
)

// For primary step return stepId instead of primaryStepId
private val Step.primaryStepId get() = multiStep?.primaryStepId ?: stepId

Expand Down
15 changes: 15 additions & 0 deletions test_runner/src/main/kotlin/ftl/domain/junit/StackTraceRemover.kt
@@ -0,0 +1,15 @@
package ftl.domain.junit

import ftl.api.JUnitTest

fun JUnitTest.Result.removeStackTraces() = copy(testsuites = testsuites?.removeStackTraces())

private fun MutableCollection<JUnitTest.Suite>.removeStackTraces() = map { testSuite ->
testSuite.copy(testcases = testSuite.testcases?.withRemovedStackTraces())
}.toMutableList()

private fun MutableCollection<JUnitTest.Case>.withRemovedStackTraces() =
map { testCase -> testCase.removeFlakyStackTrace() }.toMutableList()

private fun JUnitTest.Case.removeFlakyStackTrace(): JUnitTest.Case =
if (flaky == true) copy(errors = null, failures = null) else this
Expand Up @@ -17,6 +17,7 @@ import ftl.client.google.GcStorage
import ftl.config.FtlConstants
import ftl.domain.junit.merge
import ftl.domain.junit.mergeTestTimes
import ftl.domain.junit.removeStackTraces
import ftl.json.MatrixMap
import ftl.json.isAllSuccessful
import ftl.reports.CostReport
Expand Down Expand Up @@ -129,7 +130,7 @@ object ReportManager {
// ios supports only legacy parsing
args is IosArgs -> processXmlFromFile(matrices, args, parseJUnitTestResultFromFile)
args.useLegacyJUnitResult -> processXmlFromFile(matrices, args, parseJUnitLegacyTestResultFromFile)
else -> generateJUnitTestResultFromApi((args to matrices).toApiIdentity())
else -> generateJUnitTestResultFromApi((args to matrices).toApiIdentity()).removeStackTraces()
}

@VisibleForTesting
Expand Down
@@ -0,0 +1,68 @@
package ftl.domain.junit

import com.google.common.truth.Truth.assertThat
import ftl.api.JUnitTest
import org.junit.Test

class StackTraceRemoverTest {

val testcases = mutableListOf(
JUnitTest.Case("name", "class", "time", failures = listOf("fail"), errors = listOf("error")),
JUnitTest.Case("name2", "class2", "time2", failures = listOf("fail2"), errors = listOf("error2")),
)

@Test
fun `Should remove stack traces for flaky tests`() {
// given
val testcases = mutableListOf(
JUnitTest.Case("name", "class", "time", failures = listOf("fail"), errors = listOf("error")),
JUnitTest.Case("name2", "class2", "time2", failures = listOf("fail2"), errors = listOf("error2")),
)
val junitTestResult = testResultsFor(testcases, flaky = true)

// when
val actual = junitTestResult.removeStackTraces()

// then
actual.testsuites?.forEach { suite ->
assertThat(suite.testcases?.all { testCase -> testCase.failures == null }).isTrue()
assertThat(suite.testcases?.all { testCase -> testCase.errors == null }).isTrue()
}
}

@Test
fun `Should not remove stack traces for not flaky test`() {
// given
val junitTestResult = testResultsFor(testcases, flaky = false)

// when
val actual = junitTestResult.removeStackTraces()

// then
actual.testsuites?.forEach { suite ->
assertThat(suite.testcases?.all { testCase -> testCase.failures == null }).isFalse()
assertThat(suite.testcases?.all { testCase -> testCase.errors == null }).isFalse()
}
}

private fun testResultsFor(testCases: MutableList<JUnitTest.Case>, flaky: Boolean) = JUnitTest.Result(
testsuites = mutableListOf(
JUnitTest.Suite(
"",
"-1",
"-1",
-1,
"-1",
"-1",
"-1",
"-1",
"-1",
"-1",
testCases.onEach { it.apply { this.flaky = flaky } },
null,
null,
null
)
)
)
}
Expand Up @@ -9,7 +9,6 @@ import com.google.testing.model.TestExecution
import ftl.client.junit.TestExecutionData
import ftl.client.junit.flaky
import ftl.client.junit.prepareForJUnitResult
import ftl.client.junit.removeStackTraces
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand Down Expand Up @@ -133,42 +132,6 @@ class PrepareForJUnitResultKtTest {
assertEquals(3, preparedTestCase.first().testCases.first().stackTraces.count())
}

@Test
fun `should return single prepared test case without stack trace`() {
val testCases = listOf(
TestCase().apply {
startTime = Timestamp().apply {
seconds = 1
}
},
TestCase().apply {
startTime = Timestamp().apply {
seconds = 2
}
stackTraces = listOf(
StackTrace().apply {
exception = "exception"
}
)
}
)
val primaryStep = Step().apply {
stepId = "1"
}
val testExecutionDataList = listOf(
TestExecutionData(
testExecution = TestExecution(),
timestamp = Timestamp(),
testCases = testCases,
step = primaryStep
)
)
val preparedTestCase = testExecutionDataList.prepareForJUnitResult().removeStackTraces()
assertEquals(preparedTestCase.count(), 1)
assertEquals(preparedTestCase.first().testCases.count(), 1)
assertTrue(preparedTestCase.first().testCases.first().stackTraces.isNullOrEmpty())
}

@Test
fun `should not throws when stack traces is null`() {
val testCases = listOf(
Expand Down