From c9a7eb41427ee0bdfb73367673c38f645f606f65 Mon Sep 17 00:00:00 2001 From: az Date: Sat, 1 Apr 2023 11:56:36 +0200 Subject: [PATCH] add failure result to output if not yet included --- .../provs/framework/core/Prov.kt | 8 +++++ .../provs/framework/core/ProvTest.kt | 33 ++++++++++++++----- 2 files changed, 33 insertions(+), 8 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 87a88c8..b45b6c3 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt @@ -312,6 +312,14 @@ open class Prov protected constructor( internalResults[resultIndex].provResult = returnValue + // Add failure result to output if not yet included, + // which is the case if the result was not part of another subtask but created and returned by the lambda itself. + // Success results do not need to be added here as they don't change the overall success evaluation, + // whereas the failure results may have a useful error message, which should be in the output. + if (!resultOfTaskLambda.success && (resultOfTaskLambda != internalResults.last().provResult)) { + internalResults.add(ResultLine(level + 1, "<>", resultOfTaskLambda)) + } + if (level == 0) { endProgress() processor.close() 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 220acc1..d78ed51 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvTest.kt @@ -254,12 +254,12 @@ internal class ProvTest { } - // given + // additional methods to be used in the tests below fun Prov.checkPrereq_evaluateToFailure() = requireLast { ProvResult(false, err = "This is a test error.") } - fun Prov.methodThatProvidesSomeOutput() = requireLast { + fun Prov.testMethodForOutputTest_with_mode_requireLast() = requireLast { if (!checkPrereq_evaluateToFailure().success) { sh( @@ -273,6 +273,17 @@ internal class ProvTest { sh("echo -End test-") } + fun Prov.testMethodForOutputTest_nested_with_failure() = taskWithResult { + + taskWithResult(name = "sub1") { + taskWithResult { + ProvResult(true) + } + ProvResult(false, err = "Iamanerrormessage") + } + cmd("echo -End test-") + } + @Test @NonCi fun prov_prints_correct_output_for_overall_success() { @@ -290,7 +301,7 @@ internal class ProvTest { // when Prov.newInstance(name = "test instance with no progress info", progressType = ProgressType.NONE) - .methodThatProvidesSomeOutput() + .testMethodForOutputTest_with_mode_requireLast() // then System.setOut(originalOut) @@ -300,7 +311,7 @@ internal class ProvTest { val expectedOutput = "============================================== SUMMARY (test instance with no progress info) =============================================\n" + - "> \u001B[92mSuccess\u001B[0m -- methodThatProvidesSomeOutput (requireLast) \n" + + "> \u001B[92mSuccess\u001B[0m -- testMethodForOutputTest_with_mode_requireLast (requireLast) \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" + @@ -317,7 +328,7 @@ internal class ProvTest { @Test @NonCi - fun prov_prints_correct_output_for_failure() { + fun prov_prints_correct_output_for_nested_calls_with_failure() { // given setRootLoggingLevel(Level.OFF) @@ -332,7 +343,7 @@ internal class ProvTest { // when Prov.newInstance(name = "test instance with no progress info", progressType = ProgressType.NONE) - .checkPrereq_evaluateToFailure() + .testMethodForOutputTest_nested_with_failure() // then System.setOut(originalOut) @@ -342,7 +353,13 @@ internal class ProvTest { val expectedOutput = "============================================== SUMMARY (test instance with no progress info) =============================================\n" + - "> \u001B[91mFAILED\u001B[0m -- checkPrereq_evaluateToFailure (requireLast) -- Error: This is a test error.\n" + + "> \u001B[91mFAILED\u001B[0m -- testMethodForOutputTest_nested_with_failure \n" + + "---> \u001B[91mFAILED\u001B[0m -- sub1 \n" + + "------> \u001B[92mSuccess\u001B[0m -- testMethodForOutputTest_nested_with_failure \n" + + "------> \u001B[91mFAILED\u001B[0m -- <> -- Error: Iamanerrormessage\n" + + "---> \u001B[92mSuccess\u001B[0m -- cmd [/bin/bash, -c, echo -End test-]\n" + + "----------------------------------------------------------------------------------------------------\n" + + "Overall > \u001B[91mFAILED\u001B[0m \n" + "============================================ SUMMARY END ===========================================\n" + "\n" @@ -603,7 +620,7 @@ internal class ProvTest { val prov = Prov.newInstance(name = "test instance with no progress info", progressType = ProgressType.NONE) - // when + // when prov.task { addInfoText("Text1") addInfoText("Text2\nwith newline")