add function for printing info text

This commit is contained in:
ansgarz 2022-06-14 18:34:11 +02:00
parent 65669ee185
commit f0636067ef
2 changed files with 66 additions and 7 deletions

View file

@ -60,12 +60,14 @@ open class Prov protected constructor(
} }
} }
private val internalResults = arrayListOf<ResultLine>()
private var level = 0 private var level = 0
private var previousLevel = 0 private var previousLevel = 0
private var exit = false private var exit = false
private var runInContainerWithName: String? = null private var runInContainerWithName: String? = null
private val internalResults = arrayListOf<ResultLine>()
private val infoTexts = arrayListOf<String>()
/** /**
* A task is the base execution unit in provs. In the results overview it is represented by one line resp. result (of either success or failure). * A task is the base execution unit in provs. In the results overview it is represented by one line resp. result (of either success or failure).
* Returns success if no sub-tasks are called or if all subtasks finish with success. * Returns success if no sub-tasks are called or if all subtasks finish with success.
@ -233,6 +235,12 @@ open class Prov protected constructor(
ProvResult(success) ProvResult(success)
} }
fun addInfoText(text: String) {
infoTexts.add(text)
}
// ===================================== private functions ==================================
/** /**
* Provides task evaluation, i.e. computes a ProvResult based on the provided resultMode, * Provides task evaluation, i.e. computes a ProvResult based on the provided resultMode,
* on the returned ProvResult from the task as well as on the results from executed subtasks (if there are). * on the returned ProvResult from the task as well as on the results from executed subtasks (if there are).
@ -365,6 +373,7 @@ open class Prov protected constructor(
println("----------------------------------------------------------------------------------------------------- ") println("----------------------------------------------------------------------------------------------------- ")
println("Overall " + internalResults[0].toString().take(10).formattedAsResultLine()) println("Overall " + internalResults[0].toString().take(10).formattedAsResultLine())
} }
printInfoTexts()
println("============================================ SUMMARY END ============================================ " + newline()) println("============================================ SUMMARY END ============================================ " + newline())
} }
@ -412,6 +421,15 @@ open class Prov protected constructor(
} }
} }
private fun printInfoTexts() {
if (infoTexts.isNotEmpty()) {
println("----------------------------------------------------------------------------------------------------- ")
for (text in infoTexts) {
println(text)
}
}
}
} }

View file

@ -590,5 +590,46 @@ internal class ProvTest {
assertEquals(true, res.success) assertEquals(true, res.success)
} }
@Test
fun infoText_is_printed_correctly() {
// 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))
val prov = Prov.newInstance(name = "test instance with no progress info", progressType = ProgressType.NONE)
// when
prov.task {
addInfoText("Text1")
addInfoText("Text2\nwith newline")
}
// then
System.setOut(originalOut)
System.setErr(originalErr)
println(outContent.toString())
val expectedOutput =
"============================================== SUMMARY (test instance with no progress info) ============================================== \n" +
"> \u001B[92mSuccess\u001B[0m -- infoText_is_printed_correctly \n" +
"----------------------------------------------------------------------------------------------------- \n" +
"Text1\n" +
"Text2\n" +
"with newline\n" +
"============================================ SUMMARY END ============================================ \n" +
"\n"
assertEquals(expectedOutput, outContent.toString().replace("\r", ""))
}
} }