From 08e060ff00cd342a5b212751e6013a9e9a99aa08 Mon Sep 17 00:00:00 2001 From: az Date: Fri, 8 Oct 2021 17:11:46 +0200 Subject: [PATCH] add task with custom name to class Prov --- .../provs/core/Prov.kt | 16 ++++++-- .../provs/core/ProvTest.kt | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Prov.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Prov.kt index db9458b..6d8372e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Prov.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Prov.kt @@ -65,6 +65,14 @@ open class Prov protected constructor( private var runInContainerWithName: String? = null + /** + * Defines a task with a custom name instead of the name of the calling function. + * Returns success if all subtasks finished with success (same as requireAll). + */ + fun task(name: String? = null, a: Prov.() -> ProvResult): ProvResult { + return handle(ResultMode.ALL, name) { a() } + } + /** * defines a task with default success behavior, i.e. returns success if all subtasks finished with success. * Same as requireAll. @@ -243,7 +251,7 @@ open class Prov protected constructor( /** * Provides result handling, e.g. gather results for result summary */ - private fun handle(mode: ResultMode, a: Prov.() -> ProvResult): ProvResult { + private fun handle(mode: ResultMode, name: String? = null, a: Prov.() -> ProvResult): ProvResult { // init if (level == 0) { @@ -255,8 +263,8 @@ open class Prov protected constructor( // pre-handling val resultIndex = internalResults.size - val method = getCallingMethodName() - val internalResult = ResultLine(level, method, null) + val taskName = name ?: getCallingMethodName() + val internalResult = ResultLine(level, taskName, null) internalResults.add(internalResult) previousLevel = level @@ -277,7 +285,7 @@ open class Prov protected constructor( // post-handling val returnValue = if (mode == ResultMode.LAST) { - if (internalResultIsLeaf(resultIndex) || method == "cmd") + if (internalResultIsLeaf(resultIndex) || taskName == "cmd") res.copy() else ProvResult(res.success) } else if (mode == ResultMode.ALL) { // leaf diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/core/ProvTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/core/ProvTest.kt index 273ea89..b3a602f 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/core/ProvTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/core/ProvTest.kt @@ -341,6 +341,44 @@ internal class ProvTest { assertEquals("123", res?.plain()?.trim()) } + + @Test + fun custom_task_name_appears_in_results() { + // given + fun Prov.taskA() = task("TaskB") { + task("taskC") { + ProvResult(true) + } + } + + 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", progressType = ProgressType.NONE).taskA() + + // then + System.setOut(originalOut) + System.setErr(originalErr) + + println(outContent.toString()) + + val expectedOutput = + "============================================== SUMMARY (test instance) ============================================== \n" + + "> \u001B[92mSuccess\u001B[0m -- TaskB \n" + + "---> \u001B[92mSuccess\u001B[0m -- taskC \n" + + "----------------------------------------------------------------------------------------------------- \n" + + "Overall > \u001B[92mSuccess\u001B[0m\n" + + "============================================ SUMMARY END ============================================ \n" + + "\n" + + assertEquals(expectedOutput, outContent.toString().replace("\r", "")) + } + @Test fun addResultToEval_success() { // given