[Native][tests] Truncate large test output to avoid blowing up in logs

This commit is contained in:
Dmitriy Dolovov
2021-12-08 23:28:03 +03:00
parent 51a79c4592
commit eb371fa5e0
4 changed files with 21 additions and 18 deletions
@@ -98,9 +98,7 @@ internal abstract class LoggedData {
appendLine("- Exit code: ${exitCode.code} (${exitCode.name})")
appendDuration(duration)
appendLine()
appendLine("========== BEGIN: RAW COMPILER OUTPUT ==========")
if (compilerOutput.isNotEmpty()) appendLine(compilerOutput.trimEnd())
appendLine("========== END: RAW COMPILER OUTPUT ==========")
appendPotentiallyLargeOutput(compilerOutput, "RAW COMPILER OUTPUT", truncateLargeOutput = false)
appendLine()
appendLine(parameters)
}
@@ -223,16 +221,24 @@ internal abstract class LoggedData {
appendLine("- Exit code: ${runResult.exitCode ?: "<unknown>"}")
appendDuration(runResult.duration)
appendLine()
appendLine("========== BEGIN: STDOUT ==========")
val stdOut = runResult.processOutput.stdOut.filteredOutput
if (stdOut.isNotEmpty()) appendLine(stdOut.trimEnd())
appendLine("========== END: STDOUT ==========")
appendPotentiallyLargeOutput(runResult.processOutput.stdOut.filteredOutput, "STDOUT", truncateLargeOutput = true)
appendLine()
appendLine("========== BEGIN: STDERR ==========")
val stdErr = runResult.processOutput.stdErr
if (stdErr.isNotEmpty()) appendLine(stdErr.trimEnd())
appendLine("========== END: STDERR ==========")
appendPotentiallyLargeOutput(runResult.processOutput.stdErr, "STDERR", truncateLargeOutput = true)
return this
}
private fun StringBuilder.appendPotentiallyLargeOutput(output: String, subject: String, truncateLargeOutput: Boolean) {
appendLine("========== BEGIN: $subject ==========")
if (output.length > MAX_PRINTED_OUTPUT_LENGTH && truncateLargeOutput) {
append(output.substring(0, MAX_PRINTED_OUTPUT_LENGTH).trimEnd()).appendLine("...")
appendLine()
appendLine("********** The output is too large (${output.length} characters in total), it has been truncated to avoid excessive logs **********")
} else if (output.isNotEmpty()) {
appendLine(output.trimEnd())
}
appendLine("========== END: $subject ==========")
}
private const val MAX_PRINTED_OUTPUT_LENGTH = 8 * 1024
}
}
@@ -75,7 +75,7 @@ internal abstract class AbstractLocalProcessRunner<R>(private val executionTimeo
abstract inner class ResultHandler(runResult: RunResult.Completed) : AbstractRunner<R>.ResultHandler(runResult) {
override fun handle(): R {
verifyExpectation(0, runResult.exitCode) { "$visibleProcessName exited with non-zero code." }
verifyExpectation(runResult.exitCode == 0) { "$visibleProcessName exited with non-zero code." }
return doHandle()
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.konan.blackboxtest.support.runner
import org.jetbrains.kotlin.konan.blackboxtest.support.LoggedData
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
import org.jetbrains.kotlin.test.services.JUnit5Assertions.fail
import org.opentest4j.TestAbortedException
@@ -47,10 +46,6 @@ internal abstract class AbstractRunner<R> {
abstract fun getLoggedRun(): LoggedData
abstract fun handle(): R
protected inline fun <T> verifyExpectation(expected: T, actual: T, crossinline errorMessage: () -> String) {
assertEquals(expected, actual) { getLoggedRun().withErrorMessage(errorMessage()) }
}
protected inline fun verifyExpectation(shouldBeTrue: Boolean, crossinline errorMessage: () -> String) {
assertTrue(shouldBeTrue) { getLoggedRun().withErrorMessage(errorMessage()) }
}
@@ -91,7 +91,9 @@ internal class LocalTestRunner(
private fun verifyNonTestOutput(nonTestOutput: String) {
testRun.runParameters.get<TestRunParameter.WithExpectedOutputData> {
verifyExpectation(convertLineSeparators(expectedOutputDataFile.readText()), convertLineSeparators(nonTestOutput)) {
// Don't use verifyExpectation(expected, actual) to avoid exposing potentially large test output in exception message
// and blowing up test logs.
verifyExpectation(convertLineSeparators(expectedOutputDataFile.readText()) == convertLineSeparators(nonTestOutput)) {
"Tested process output mismatch. See \"TEST STDOUT\" and \"EXPECTED OUTPUT DATA FILE\" below."
}
}