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
/**
* 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

View file

@ -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