add task with custom name to class Prov

This commit is contained in:
az 2021-10-08 17:11:46 +02:00
parent 3653620369
commit 08e060ff00
2 changed files with 50 additions and 4 deletions

View file

@ -65,6 +65,14 @@ open class Prov protected constructor(
private var runInContainerWithName: String? = null 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. * defines a task with default success behavior, i.e. returns success if all subtasks finished with success.
* Same as requireAll. * Same as requireAll.
@ -243,7 +251,7 @@ open class Prov protected constructor(
/** /**
* Provides result handling, e.g. gather results for result summary * 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 // init
if (level == 0) { if (level == 0) {
@ -255,8 +263,8 @@ open class Prov protected constructor(
// pre-handling // pre-handling
val resultIndex = internalResults.size val resultIndex = internalResults.size
val method = getCallingMethodName() val taskName = name ?: getCallingMethodName()
val internalResult = ResultLine(level, method, null) val internalResult = ResultLine(level, taskName, null)
internalResults.add(internalResult) internalResults.add(internalResult)
previousLevel = level previousLevel = level
@ -277,7 +285,7 @@ open class Prov protected constructor(
// post-handling // post-handling
val returnValue = val returnValue =
if (mode == ResultMode.LAST) { if (mode == ResultMode.LAST) {
if (internalResultIsLeaf(resultIndex) || method == "cmd") if (internalResultIsLeaf(resultIndex) || taskName == "cmd")
res.copy() else ProvResult(res.success) res.copy() else ProvResult(res.success)
} else if (mode == ResultMode.ALL) { } else if (mode == ResultMode.ALL) {
// leaf // leaf

View file

@ -341,6 +341,44 @@ internal class ProvTest {
assertEquals("123", res?.plain()?.trim()) 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 @Test
fun addResultToEval_success() { fun addResultToEval_success() {
// given // given