Skip to content

Commit

Permalink
Add Composite reporter that generates JUnit and HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
fr0l committed Aug 14, 2024
1 parent 9cd07ae commit 0d45e6a
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ class CloudInteractor(
val reportOutputSink = reportFormat.fileExtension
?.let { extension ->
(reportOutput ?: File("report$extension"))
.sink()
.buffer()
}

if (reportOutputSink != null) {
Expand All @@ -352,7 +350,7 @@ class CloudInteractor(
reportFormat: ReportFormat,
passed: Boolean,
suiteResult: TestExecutionSummary.SuiteResult,
reportOutputSink: BufferedSink,
reportOutput: File,
testSuiteName: String?
) {
ReporterFactory.buildReporter(reportFormat, testSuiteName)
Expand All @@ -361,7 +359,7 @@ class CloudInteractor(
passed = passed,
suites = listOf(suiteResult)
),
reportOutputSink,
reportOutput,
)
}

Expand Down
5 changes: 2 additions & 3 deletions maestro-cli/src/main/java/maestro/cli/command/TestCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,10 @@ class TestCommand : Callable<Int> {

format.fileExtension?.let { extension ->
(output ?: File("report$extension"))
.sink()
}?.also { sink ->
}?.also { file ->
reporter.report(
this,
sink,
file,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package maestro.cli.report

import maestro.cli.model.TestExecutionSummary
import java.io.File

class CompositeTestSuiteReporter(val reporters: Set<TestSuiteReporter>, override val fileExtension: String?) : TestSuiteReporter {
override fun report(
summary: TestExecutionSummary,
out: File
) {
val baseReportDirectory = getBaseReportDirectory(out)

reporters.forEach { reporter ->
val reportFolder = File(baseReportDirectory, simpleReportFolderName(reporter))
reportFolder.mkdirs()

val reportFile = File(reportFolder, "report${reporter.fileExtension}")

if (reportFile.exists()) {
reportFile.delete()
}

reporter.report(summary, reportFile)
}
}

private fun getBaseReportDirectory(out: File): File {
return if (out.absoluteFile.isDirectory) {
out.absoluteFile
} else {
out.absoluteFile.parentFile
}
}

private fun simpleReportFolderName(reporter: TestSuiteReporter): String {
return reporter.javaClass.simpleName.replace(interfaceName, "")
}

companion object {
private val interfaceName = TestSuiteReporter::class.simpleName!!
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package maestro.cli.report

import maestro.cli.model.TestExecutionSummary
import okio.Sink
import okio.buffer
import kotlinx.html.*
import kotlinx.html.stream.appendHTML
import okio.sink
import java.io.File

class HtmlTestSuiteReporter : TestSuiteReporter {
override fun report(summary: TestExecutionSummary, out: Sink) {
val bufferedOut = out.buffer()
class HtmlTestSuiteReporter(override val fileExtension: String?) : TestSuiteReporter {
override fun report(summary: TestExecutionSummary, out: File) {
val bufferedOut = out.sink().buffer()
val htmlContent = buildHtmlReport(summary)
bufferedOut.writeUtf8(htmlContent)
bufferedOut.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
import com.fasterxml.jackson.module.kotlin.KotlinModule
import maestro.cli.model.FlowStatus
import maestro.cli.model.TestExecutionSummary
import okio.Sink
import okio.buffer
import okio.sink
import java.io.File

class JUnitTestSuiteReporter(
private val mapper: ObjectMapper,
private val testSuiteName: String?
private val testSuiteName: String?,
override val fileExtension: String?
) : TestSuiteReporter {

override fun report(
summary: TestExecutionSummary,
out: Sink
out: File
) {
mapper
.writerWithDefaultPrettyPrinter()
.writeValue(
out.buffer().outputStream(),
out.sink().buffer().outputStream(),
TestSuites(
suites = summary
.suites
Expand Down Expand Up @@ -93,13 +95,14 @@ class JUnitTestSuiteReporter(

companion object {

fun xml(testSuiteName: String? = null) = JUnitTestSuiteReporter(
fun xml(testSuiteName: String?, fileExtension: String?) = JUnitTestSuiteReporter(
mapper = XmlMapper().apply {
registerModule(KotlinModule.Builder().build())
setSerializationInclusion(JsonInclude.Include.NON_NULL)
configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true)
},
testSuiteName = testSuiteName
testSuiteName = testSuiteName,
fileExtension = fileExtension,
)

}
Expand Down
33 changes: 28 additions & 5 deletions maestro-cli/src/main/java/maestro/cli/report/ReportFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@ package maestro.cli.report
enum class ReportFormat(
val fileExtension: String?
) {
JUNIT(".xml") {
override fun createReporter(testSuiteName: String?): TestSuiteReporter {
return JUnitTestSuiteReporter.xml(testSuiteName, fileExtension)
}
},
HTML(".html") {
override fun createReporter(testSuiteName: String?): TestSuiteReporter {
return HtmlTestSuiteReporter(fileExtension)
}
},
COMPOSITE(".composite") {
override fun createReporter(testSuiteName: String?): TestSuiteReporter {
return CompositeTestSuiteReporter(
setOf(
HTML.createReporter(testSuiteName),
JUNIT.createReporter(testSuiteName)
),
fileExtension
)
}
},
NOOP(null) {
override fun createReporter(testSuiteName: String?): TestSuiteReporter {
return TestSuiteReporter.NOOP
}
};

JUNIT(".xml"),
HTML(".html"),
NOOP(null),

}
abstract fun createReporter(testSuiteName: String?): TestSuiteReporter
}
13 changes: 2 additions & 11 deletions maestro-cli/src/main/java/maestro/cli/report/ReporterFactory.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package maestro.cli.report

import maestro.cli.model.TestExecutionSummary
import okio.BufferedSink

object ReporterFactory {

fun buildReporter(format: ReportFormat, testSuiteName: String?): TestSuiteReporter {
return when (format) {
ReportFormat.JUNIT -> JUnitTestSuiteReporter.xml(testSuiteName)
ReportFormat.NOOP -> TestSuiteReporter.NOOP
ReportFormat.HTML -> HtmlTestSuiteReporter()
}
return format.createReporter(testSuiteName)
}

}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package maestro.cli.report

import maestro.cli.model.TestExecutionSummary
import okio.Sink
import java.io.File

interface TestSuiteReporter {
val fileExtension: String?

fun report(
summary: TestExecutionSummary,
out: Sink,
out: File,
)

companion object {
val NOOP: TestSuiteReporter = object : TestSuiteReporter {
override fun report(summary: TestExecutionSummary, out: Sink) {
override val fileExtension: String? = null

override fun report(summary: TestExecutionSummary, out: File) {
// no-op
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestSuiteInteractor(

fun runTestSuite(
executionPlan: WorkspaceExecutionPlanner.ExecutionPlan,
reportOut: Sink?,
reportOut: File?,
env: Map<String, String>,
debugOutputPath: Path
): TestExecutionSummary {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package maestro.cli.report

import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test

class CompositeTestSuiteReporterTest {

@Test
fun `Composite test report created`() {
// Given
val reportFormat = ReportFormat.COMPOSITE

// When
val reporter = reportFormat.createReporter("testSuiteName")

// Then
assertThat(reporter is CompositeTestSuiteReporter).isTrue()
}

@Test
fun `Composite test report contains JUNIT and HTML reports`() {
// Given
val reportFormat = ReportFormat.COMPOSITE

// When
val reporter = reportFormat.createReporter("testSuiteName")
val reporters = (reporter as CompositeTestSuiteReporter).reporters

// Then
assertThat(reporters.any { it is JUnitTestSuiteReporter }).isTrue()
assertThat(reporters.any { it is HtmlTestSuiteReporter }).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package maestro.cli.report
import com.google.common.truth.Truth.assertThat
import maestro.cli.model.FlowStatus
import maestro.cli.model.TestExecutionSummary
import okio.Buffer
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.time.Duration.Companion.seconds

class JUnitTestSuiteReporterTest {

@Test
fun `XML - Test passed`() {
// Given
val testee = JUnitTestSuiteReporter.xml()
val testee = JUnitTestSuiteReporter.xml(null, null)

val summary = TestExecutionSummary(
passed = true,
Expand All @@ -38,14 +38,14 @@ class JUnitTestSuiteReporterTest {
)
)
)
val sink = Buffer()
val reportOutput = File("tmpReportOutput")

// When
testee.report(
summary = summary,
out = sink
out = reportOutput
)
val resultStr = sink.readUtf8()
val resultStr = reportOutput.readText(Charsets.UTF_8)

// Then
assertThat(resultStr).isEqualTo(
Expand All @@ -65,7 +65,7 @@ class JUnitTestSuiteReporterTest {
@Test
fun `XML - Test failed`() {
// Given
val testee = JUnitTestSuiteReporter.xml()
val testee = JUnitTestSuiteReporter.xml(null, null)

val summary = TestExecutionSummary(
passed = false,
Expand All @@ -91,14 +91,14 @@ class JUnitTestSuiteReporterTest {
)
)
)
val sink = Buffer()
val reportOutput = File("tmpReportOutput")

// When
testee.report(
summary = summary,
out = sink
out = reportOutput
)
val resultStr = sink.readUtf8()
val resultStr = reportOutput.readText(Charsets.UTF_8)

// Then
assertThat(resultStr).isEqualTo(
Expand All @@ -120,7 +120,7 @@ class JUnitTestSuiteReporterTest {
@Test
fun `XML - Custom test suite name is used when present`() {
// Given
val testee = JUnitTestSuiteReporter.xml("Custom test suite name")
val testee = JUnitTestSuiteReporter.xml("Custom test suite name", "fileExtension")

val summary = TestExecutionSummary(
passed = true,
Expand All @@ -145,14 +145,14 @@ class JUnitTestSuiteReporterTest {
)
)
)
val sink = Buffer()
val reportOutput = File("tmpReportOutput")

// When
testee.report(
summary = summary,
out = sink
out = reportOutput
)
val resultStr = sink.readUtf8()
val resultStr = reportOutput.readText(Charsets.UTF_8)

// Then
assertThat(resultStr).isEqualTo(
Expand Down

0 comments on commit 0d45e6a

Please sign in to comment.