Skip to content

Commit

Permalink
Add SingleFileReportWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
platan committed Nov 1, 2023
1 parent 9898387 commit f326431
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@ abstract class GanttDiagramReporter<T : ReportConfig.Format> {
}

abstract fun report(report: TestExecutionScheduleReport, baseDir: File, taskName: String)
protected fun save(
report: String,
taskName: String,
baseDir: File,
location: String,
extension: String
): File {
val reportsDir = prepareReportsDir(baseDir, location)
val reportFile = File(reportsDir, "$taskName.$extension")
reportFile.createNewFile()
reportFile.writeText(report)
return reportFile
}

fun prepareReportsDir(baseDir: File, location: String): File {
val reportsDir = File(baseDir, location)
reportsDir.mkdirs()
return reportsDir
}

abstract fun getConfigType(): Class<out ReportConfig.Format>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.platan.tests_execution_chart.reporters

import java.io.File

class SingleFileReportWriter {

fun save(
report: String,
taskName: String,
baseDir: File,
location: String,
extension: String
): File {
val reportsDir = prepareReportsDir(baseDir, location)
val reportFile = File(reportsDir, "$taskName.$extension")
reportFile.createNewFile()
reportFile.writeText(report)
return reportFile
}

private fun prepareReportsDir(baseDir: File, location: String): File {
val reportsDir = File(baseDir, location)
reportsDir.mkdirs()
return reportsDir
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.platan.tests_execution_chart.reporters.html

import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.mermaid.TestExecutionMermaidDiagramFormatter
import java.net.URL

private const val TEMPLATE_HTML_FILE = "template.html"
private const val GRAPH_PLACEHOLDER = "@GRAPH_PLACEHOLDER@"
private const val MAX_TEXT_SIZE_PLACEHOLDER = "@MAX_TEXT_SIZE@"
private const val MERMAID_SRC_PLACEHOLDER = "@MERMAID_SRC@"
private const val TABLE_SRC_PLACEHOLDER = "@TABLE@"

class HtmlGanttDiagramFormatter {

fun formatHtml(
report: TestExecutionScheduleReport,
scriptSrc: String,
maxTextSize: Int
): String {
val template: String = loadTemplate(TEMPLATE_HTML_FILE)
return prepareHtmlReport(report, template, scriptSrc, maxTextSize)
}

private fun loadTemplate(templatePath: String): String {
val resource: URL? = this::class.java.classLoader.getResource(templatePath)
val template: String
if (resource == null) {
throw RuntimeException("$templatePath not found")
} else {
template = resource.readText(Charsets.UTF_8)
}
return template
}


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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@ package io.github.platan.tests_execution_chart.reporters.html
import io.github.platan.tests_execution_chart.report.ReportConfig
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.GanttDiagramReporter
import io.github.platan.tests_execution_chart.reporters.mermaid.TestExecutionMermaidDiagramFormatter
import io.github.platan.tests_execution_chart.reporters.SingleFileReportWriter
import java.io.File
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths

private const val TEMPLATE_HTML_FILE = "template.html"
private const val GRAPH_PLACEHOLDER = "@GRAPH_PLACEHOLDER@"
private const val MAX_TEXT_SIZE_PLACEHOLDER = "@MAX_TEXT_SIZE@"
private const val MERMAID_JS_FILE_NAME = "mermaid.min.js"
private const val MERMAID_SRC_PLACEHOLDER = "@MERMAID_SRC@"
private const val TABLE_SRC_PLACEHOLDER = "@TABLE@"
const val MERMAID_JS_FILE_NAME = "mermaid.min.js"


class HtmlGanttDiagramReporter :
GanttDiagramReporter<HtmlConfig>() {



override fun report(report: TestExecutionScheduleReport, baseDir: File, taskName: String) {
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 scriptSrc = config.script.src
if (config.script.embed) {
val reportsDir = prepareReportsDir(baseDir, config.outputLocation)
Expand All @@ -37,33 +25,21 @@ class HtmlGanttDiagramReporter :
scriptSrc = scriptFileName
}
val maxTextSize = config.script.options.maxTextSize
val htmlReport = prepareHtmlReport(report, template, scriptSrc, maxTextSize)
val reportFile = save(htmlReport, taskName, baseDir, config.outputLocation, "html")

val htmlReport = HtmlGanttDiagramFormatter().formatHtml(report, scriptSrc, maxTextSize)
val reportFile = SingleFileReportWriter().save(htmlReport, taskName, baseDir, config.outputLocation, "html")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}

override fun getConfigType(): Class<out ReportConfig.Format> = HtmlConfig::class.java

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)
private fun prepareReportsDir(baseDir: File, location: String): File {
val reportsDir = File(baseDir, location)
reportsDir.mkdirs()
return reportsDir
}


private fun downloadFile(url: URL, path: String) {
url.openStream().use { Files.copy(it, Paths.get(path)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.platan.tests_execution_chart.reporters.json
import io.github.platan.tests_execution_chart.report.ReportConfig
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.GanttDiagramReporter
import io.github.platan.tests_execution_chart.reporters.SingleFileReportWriter
import java.io.File

class JsonReporter : GanttDiagramReporter<JsonConfig>() {
Expand All @@ -13,7 +14,7 @@ class JsonReporter : GanttDiagramReporter<JsonConfig>() {
taskName: String
) {
val jsonReport = JsonTestExecutionReportFormatter().format(report)
val reportFile = save(jsonReport, taskName, baseDir, config.outputLocation, "json")
val reportFile = SingleFileReportWriter().save(jsonReport, taskName, baseDir, config.outputLocation, "json")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.platan.tests_execution_chart.reporters.mermaid
import io.github.platan.tests_execution_chart.report.ReportConfig
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.GanttDiagramReporter
import io.github.platan.tests_execution_chart.reporters.SingleFileReportWriter
import java.io.File

class MermaidTestsReporter :
Expand All @@ -11,7 +12,7 @@ class MermaidTestsReporter :

override fun report(report: TestExecutionScheduleReport, baseDir: File, taskName: String) {
val mermaidReport = TestExecutionMermaidDiagramFormatter().format(report)
val reportFile = save(mermaidReport, taskName, baseDir, config.outputLocation, "txt")
val reportFile = SingleFileReportWriter().save(mermaidReport, taskName, baseDir, config.outputLocation, "txt")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}

Expand Down

0 comments on commit f326431

Please sign in to comment.