Skip to content

Commit

Permalink
Support backtick character in test name in HTML report
Browse files Browse the repository at this point in the history
  • Loading branch information
platan committed Mar 7, 2023
1 parent 7397201 commit c4e82e4
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,44 @@ private const val TABLE_SRC_PLACEHOLDER = "@TABLE@"

internal class HtmlGanttDiagramReporter(private val config: Html, private val logger: Logger) : GanttDiagramReporter() {
override fun report(report: TestExecutionScheduleReport, task: Test) {
val mermaid = TestExecutionMermaidDiagramFormatter().format(report)
val table = HtmlTableFormatter().format(report)
val resource: URL? = this::class.java.classLoader.getResource(TEMPLATE_HTML_FILE)
val template: String
if (resource == null) {
throw RuntimeException("$TEMPLATE_HTML_FILE not found")
} else {
template = resource.readText(Charsets.UTF_8)
}
var src = config.getScript().src.get()
var scriptSrc = config.getScript().src.get()
if (config.getScript().embed.get()) {
val reportsDir = prepareReportsDir(task, config.outputLocation.get())
val scriptFileName = MERMAID_JS_FILE_NAME
downloadFile(URL(src), "${reportsDir.absolutePath}/$scriptFileName")
src = scriptFileName
downloadFile(URL(scriptSrc), "${reportsDir.absolutePath}/$scriptFileName")
scriptSrc = scriptFileName
}
val maxTextSize = config.getScript().getConfig().maxTextSize.get()
val htmlReport = template
.replace(GRAPH_PLACEHOLDER, mermaid)
val htmlReport = prepareHtmlReport(report, template, scriptSrc, maxTextSize)
val reportFile = save(task, htmlReport, config.outputLocation.get(), "html")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}

private fun prepareHtmlReport(
report: TestExecutionScheduleReport,
template: String,
src: String,
maxTextSize: Int
): String {
val mermaid = TestExecutionMermaidDiagramFormatter().format(report)
val escapedMermaid = mermaid
.replace("\\", "\\\\")
// Mermaid data is put in javascript code and specified using backtick character (`).
// We have to escape ` character.
.replace("`", "\\`")
val table = HtmlTableFormatter().format(report)
return template
.replace(GRAPH_PLACEHOLDER, escapedMermaid)
.replace(MERMAID_SRC_PLACEHOLDER, src)
.replace(MAX_TEXT_SIZE_PLACEHOLDER, maxTextSize.toString())
.replace(TABLE_SRC_PLACEHOLDER, table)

val reportFile = save(task, htmlReport, config.outputLocation.get(), "html")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}

private fun downloadFile(url: URL, path: String) {
Expand Down

0 comments on commit c4e82e4

Please sign in to comment.