avoid emphasis with color red on failed results that do not count

This commit is contained in:
ansgarz 2022-03-18 21:31:25 +01:00
parent d4c7c19126
commit 908ac00a3b
3 changed files with 95 additions and 20 deletions

View file

@ -275,6 +275,7 @@ open class Prov protected constructor(
level-- level--
// post-handling // post-handling
// determine result
val returnValue = val returnValue =
if (mode == ResultMode.LAST) { if (mode == ResultMode.LAST) {
if (internalResultIsLeaf(resultIndex) || taskName == "cmd" || taskName?.replace(" (requireLast)", "") == "repeatTaskUntilSuccess") { if (internalResultIsLeaf(resultIndex) || taskName == "cmd" || taskName?.replace(" (requireLast)", "") == "repeatTaskUntilSuccess") {
@ -338,9 +339,9 @@ open class Prov protected constructor(
return res return res
} }
private val ANSI_RESET = "\u001B[0m" private val ANSI_RESET = "\u001B[0m"
private val ANSI_BRIGHT_RED = "\u001B[91m" private val ANSI_BRIGHT_RED = "\u001B[91m"
private val ANSI_BRIGHT_YELLOW = "\u001B[93m"
private val ANSI_BRIGHT_GREEN = "\u001B[92m" private val ANSI_BRIGHT_GREEN = "\u001B[92m"
private val ANSI_GRAY = "\u001B[90m" private val ANSI_GRAY = "\u001B[90m"
@ -349,8 +350,16 @@ open class Prov protected constructor(
"============================================== SUMMARY " + (if (instanceName != null) "(" + instanceName + ") " else "") + "============================================== SUMMARY " + (if (instanceName != null) "(" + instanceName + ") " else "") +
"============================================== " "============================================== "
) )
val successPerLevel = arrayListOf<Boolean>()
for (result in internalResults) { for (result in internalResults) {
println(result.toString().escapeControlChars().formattedAsResultLine()) val currentLevel = result.level
// store level result
val currentLevelSucces = result.provResult?.success ?: false
if (currentLevel >= successPerLevel.size) successPerLevel.add(currentLevelSucces)
val successOfLevelAbove = if (currentLevel == 0) currentLevelSucces else successPerLevel[currentLevel - 1]
println(result.toString().escapeControlChars().formattedAsResultLine(successOfLevelAbove))
} }
if (internalResults.size > 1) { if (internalResults.size > 1) {
println("----------------------------------------------------------------------------------------------------- ") println("----------------------------------------------------------------------------------------------------- ")
@ -359,12 +368,14 @@ open class Prov protected constructor(
println("============================================ SUMMARY END ============================================ " + newline()) println("============================================ SUMMARY END ============================================ " + newline())
} }
private fun String.formattedAsResultLine(): String = private fun String.formattedAsResultLine(showFailedInYellow: Boolean = false): String {
this val failedColor = if (showFailedInYellow) ANSI_BRIGHT_YELLOW else ANSI_BRIGHT_RED
return this
.replaceFirst("${RESULT_PREFIX}Success", RESULT_PREFIX + ANSI_BRIGHT_GREEN + "Success" + ANSI_RESET) .replaceFirst("${RESULT_PREFIX}Success", RESULT_PREFIX + ANSI_BRIGHT_GREEN + "Success" + ANSI_RESET)
.replaceFirst("${RESULT_PREFIX}FAILED", RESULT_PREFIX + ANSI_BRIGHT_RED + "FAILED" + ANSI_RESET) .replaceFirst("${RESULT_PREFIX}FAILED", RESULT_PREFIX + failedColor + "FAILED" + ANSI_RESET)
.replace("${RESULT_PREFIX}executing...", RESULT_PREFIX + ANSI_GRAY + "executing..." + ANSI_RESET) .replace("${RESULT_PREFIX}executing...", RESULT_PREFIX + ANSI_GRAY + "executing..." + ANSI_RESET)
.take(400) .take(400)
}
private fun initProgress() { private fun initProgress() {

View file

@ -279,7 +279,7 @@ internal class ProvTest {
@Test @Test
@NonCi @NonCi
fun runProv_printsCorrectOutput() { fun prov_prints_correct_output_for_overall_success() {
// given // given
setRootLoggingLevel(Level.OFF) setRootLoggingLevel(Level.OFF)
@ -305,7 +305,7 @@ internal class ProvTest {
val expectedOutput = val expectedOutput =
"============================================== SUMMARY (test instance with no progress info) ============================================== \n" + "============================================== SUMMARY (test instance with no progress info) ============================================== \n" +
"> \u001B[92mSuccess\u001B[0m -- methodThatProvidesSomeOutput (requireLast) \n" + "> \u001B[92mSuccess\u001B[0m -- methodThatProvidesSomeOutput (requireLast) \n" +
"---> \u001B[91mFAILED\u001B[0m -- checkPrereq_evaluateToFailure (requireLast) -- Error: This is a test error.\n" + "---> \u001B[93mFAILED\u001B[0m -- checkPrereq_evaluateToFailure (requireLast) -- Error: This is a test error.\n" +
"---> \u001B[92mSuccess\u001B[0m -- sh \n" + "---> \u001B[92mSuccess\u001B[0m -- sh \n" +
"------> \u001B[92mSuccess\u001B[0m -- cmd [/bin/bash, -c, echo -Start test-]\n" + "------> \u001B[92mSuccess\u001B[0m -- cmd [/bin/bash, -c, echo -Start test-]\n" +
"------> \u001B[92mSuccess\u001B[0m -- cmd [/bin/bash, -c, echo Some output]\n" + "------> \u001B[92mSuccess\u001B[0m -- cmd [/bin/bash, -c, echo Some output]\n" +
@ -319,6 +319,40 @@ internal class ProvTest {
assertEquals(expectedOutput, outContent.toString().replace("\r", "")) assertEquals(expectedOutput, outContent.toString().replace("\r", ""))
} }
@Test
@NonCi
fun prov_prints_correct_output_for_failure() {
// given
setRootLoggingLevel(Level.OFF)
val outContent = ByteArrayOutputStream()
val errContent = ByteArrayOutputStream()
val originalOut = System.out
val originalErr = System.err
System.setOut(PrintStream(outContent))
System.setErr(PrintStream(errContent))
// when
Prov.newInstance(name = "test instance with no progress info", progressType = ProgressType.NONE)
.checkPrereq_evaluateToFailure()
// then
System.setOut(originalOut)
System.setErr(originalErr)
println(outContent.toString())
val expectedOutput =
"============================================== SUMMARY (test instance with no progress info) ============================================== \n" +
"> \u001B[91mFAILED\u001B[0m -- checkPrereq_evaluateToFailure (requireLast) -- Error: This is a test error.\n" +
"============================================ SUMMARY END ============================================ \n" +
"\n"
assertEquals(expectedOutput, outContent.toString().replace("\r", ""))
}
@Test @Test
fun check_returnsTrue() { fun check_returnsTrue() {
// when // when

View file

@ -6,32 +6,62 @@ import org.junit.jupiter.api.Test
internal class TaskFunctionsKtTest { internal class TaskFunctionsKtTest {
var count = 1 var count1 = 2
fun Prov.altenateSuccessAndFailure() = task { fun Prov.alternatingSuccessAndFailure() = task {
if (count == 0) { if (count1 == 1) {
count = 1 count1 = 2
ProvResult(true, out = "0") ProvResult(true, out = "1")
} else { } else {
count-- count1--
ProvResult(false, err = "1") ProvResult(false, err = count1.toString())
} }
} }
fun Prov.repeating() = requireLast { fun Prov.secondTimeSuccess() = task {
val res = repeatTaskUntilSuccess(4, 1) { val res = repeatTaskUntilSuccess(3, 1) {
altenateSuccessAndFailure() alternatingSuccessAndFailure()
} }
if (res.success && ("1" == res.out?.trim())) {
if (res.success && ("0" == res.out?.trim())) {
ProvResult(true) ProvResult(true)
} else { } else {
ProvResult(false) ProvResult(false)
} }
} }
var count2 = 3
fun Prov.thirdTimeSuccess() = task {
if (count2 <= 1) {
count2 = 3
ProvResult(true, out = "1")
} else {
count2--
ProvResult(false, err = count2.toString())
}
}
fun thirdTimeSuccessForNotAProvTaks(): ProvResult {
if (count2 <= 1) {
count2 = 3
return ProvResult(true, out = "1")
} else {
count2--
return ProvResult(false, err = count2.toString())
}
}
@Test @Test
fun repeat_and_requireLast() { fun repeat_and_requireLast() {
assertTrue(testLocal().repeating().success) // when
val res1 = testLocal().secondTimeSuccess()
val res2 = testLocal().repeatTaskUntilSuccess(3, 0) { thirdTimeSuccess() }
val res3 = testLocal().repeatTaskUntilSuccess(2, 0) { thirdTimeSuccess() }
val res4 = testLocal().repeatTaskUntilSuccess(3, 0) { thirdTimeSuccessForNotAProvTaks() }
// then
assertTrue(res1.success)
assertTrue(res2.success)
assertFalse(res3.success)
assertTrue(res4.success)
} }
} }