From 908ac00a3b9eb76b12ba51e0818ce31a7ba5c24a Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 18 Mar 2022 21:31:25 +0100 Subject: [PATCH] avoid emphasis with color red on failed results that do not count --- .../provs/framework/core/Prov.kt | 21 +++++-- .../provs/framework/core/ProvTest.kt | 38 ++++++++++++- .../framework/core/TaskFunctionsKtTest.kt | 56 ++++++++++++++----- 3 files changed, 95 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt index b627a59..daaa2c9 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt @@ -275,6 +275,7 @@ open class Prov protected constructor( level-- // post-handling + // determine result val returnValue = if (mode == ResultMode.LAST) { if (internalResultIsLeaf(resultIndex) || taskName == "cmd" || taskName?.replace(" (requireLast)", "") == "repeatTaskUntilSuccess") { @@ -338,9 +339,9 @@ open class Prov protected constructor( return res } - private val ANSI_RESET = "\u001B[0m" private val ANSI_BRIGHT_RED = "\u001B[91m" + private val ANSI_BRIGHT_YELLOW = "\u001B[93m" private val ANSI_BRIGHT_GREEN = "\u001B[92m" private val ANSI_GRAY = "\u001B[90m" @@ -349,8 +350,16 @@ open class Prov protected constructor( "============================================== SUMMARY " + (if (instanceName != null) "(" + instanceName + ") " else "") + "============================================== " ) + val successPerLevel = arrayListOf() 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) { println("----------------------------------------------------------------------------------------------------- ") @@ -359,12 +368,14 @@ open class Prov protected constructor( println("============================================ SUMMARY END ============================================ " + newline()) } - private fun String.formattedAsResultLine(): String = - this + private fun String.formattedAsResultLine(showFailedInYellow: Boolean = false): String { + 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}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) .take(400) + } private fun initProgress() { diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvTest.kt index 8dd55ee..3ee756e 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvTest.kt @@ -279,7 +279,7 @@ internal class ProvTest { @Test @NonCi - fun runProv_printsCorrectOutput() { + fun prov_prints_correct_output_for_overall_success() { // given setRootLoggingLevel(Level.OFF) @@ -305,7 +305,7 @@ internal class ProvTest { val expectedOutput = "============================================== SUMMARY (test instance with no progress info) ============================================== \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 -- cmd [/bin/bash, -c, echo -Start test-]\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", "")) } + @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 fun check_returnsTrue() { // when diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/TaskFunctionsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/TaskFunctionsKtTest.kt index a75bc0e..33930eb 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/TaskFunctionsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/TaskFunctionsKtTest.kt @@ -6,32 +6,62 @@ import org.junit.jupiter.api.Test internal class TaskFunctionsKtTest { - var count = 1 - fun Prov.altenateSuccessAndFailure() = task { - if (count == 0) { - count = 1 - ProvResult(true, out = "0") + var count1 = 2 + fun Prov.alternatingSuccessAndFailure() = task { + if (count1 == 1) { + count1 = 2 + ProvResult(true, out = "1") } else { - count-- - ProvResult(false, err = "1") + count1-- + ProvResult(false, err = count1.toString()) } } - fun Prov.repeating() = requireLast { - val res = repeatTaskUntilSuccess(4, 1) { - altenateSuccessAndFailure() + fun Prov.secondTimeSuccess() = task { + val res = repeatTaskUntilSuccess(3, 1) { + alternatingSuccessAndFailure() } - - if (res.success && ("0" == res.out?.trim())) { + if (res.success && ("1" == res.out?.trim())) { ProvResult(true) } else { 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 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) } } \ No newline at end of file