Skip to content

Commit

Permalink
Added the ability to specify a header for an XML report
Browse files Browse the repository at this point in the history
Resolves #527
PR #539
  • Loading branch information
shanshin authored Feb 15, 2024
1 parent 1d6f64a commit ec84816
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/gradle-plugin/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ koverReport {
// generate an XML report when running the `check` task
onCheck = false

// XML report title (the location depends on the library)
title.set("Custom XML report title")

// XML report file
setReportFile(layout.buildDirectory.file("my-project-report/result.xml"))

Expand Down Expand Up @@ -283,6 +286,9 @@ koverReport {
// generate an XML report when running the `check` task
onCheck = false

// XML report title (the location depends on the library)
title.set("Custom XML report title")

// XML report file
setReportFile(layout.buildDirectory.file("my-project-report/result.xml"))

Expand Down
1 change: 1 addition & 0 deletions kover-gradle-plugin/api/kover-gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ public abstract interface class kotlinx/kover/gradle/plugin/dsl/KoverXmlReportCo
public abstract fun filters (Lorg/gradle/api/Action;)V
public abstract fun getOnCheck ()Z
public fun getReportFile ()Ljava/lang/Void;
public abstract fun getTitle ()Lorg/gradle/api/provider/Property;
public fun overrideFilters (Lkotlin/jvm/functions/Function0;)V
public abstract fun setOnCheck (Z)V
public abstract fun setReportFile (Ljava/io/File;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.kover.gradle.plugin.test.functional.cases

import kotlinx.kover.gradle.plugin.test.functional.framework.checker.defaultReportsDir
import kotlinx.kover.gradle.plugin.test.functional.framework.configurator.*
import kotlinx.kover.gradle.plugin.test.functional.framework.starter.*
import kotlin.test.assertContains

internal class XmlReportTests {

@SlicedGeneratedTest(allTools = true)
fun BuildConfigurator.testDefaultXmlTitle() {
addProjectWithKover {

}

addProjectWithKover(":nested") {
sourcesFrom("simple")
}

run("koverXmlReport") {
subproject(":nested") {
file("$defaultReportsDir/report.xml") {
assertContains(readText(), "Kover Gradle Plugin XML report for :nested")
}
}
}
}

@SlicedGeneratedTest(allTools = true)
fun BuildConfigurator.testCustomXmlTitle() {
val title = "My Custom XML title"

addProjectWithKover {

}

addProjectWithKover(":nested") {
sourcesFrom("simple")

koverReport {
defaults {
xml {
this.title.set("My Custom XML title")
}
}
}
}

run("koverXmlReport") {
subproject(":nested") {
file("$defaultReportsDir/report.xml") {
assertContains(readText(), title)
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ internal class KoverXmlReportConfigWriter(private val writer: FormattedWriter) :
field = value
}

override val title: Property<String> = PropertyWriter("title", writer)

override fun setReportFile(xmlFile: File) {
writer.assign("reportFile", xmlFile.forScript())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ internal abstract class ReportsVariantApplier(
) {
reportConfig.binary.onCheck.convention(false)
reportConfig.binary.file.convention(project.layout.buildDirectory.file(binaryReportPath(variantName)))
reportConfig.xml.title.convention("Kover Gradle Plugin XML report for ${project.path}")

val runOnCheck = mutableListOf<Any>()

Expand All @@ -146,6 +147,7 @@ internal abstract class ReportsVariantApplier(

xmlTask.configure {
reportFile.convention(project.layout.file(reportConfig.xml.reportFileProperty))
title.convention(reportConfig.xml.title)
filters.set((reportConfig.xml.filters ?: reportConfig.filters ?: commonFilters).convert())
}
if (reportConfig.xml.onCheck) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public interface KoverReportsConfig {
* }
*
* onCheck = false
* title.set("Custom XML report title")
* setReportFile(layout.buildDirectory.file("my-project-report/result.xml"))
* }
* ```
Expand Down Expand Up @@ -621,6 +622,9 @@ public interface KoverHtmlReportConfig {
* // Generate an XML report when running the `check` task
* onCheck = false
*
* // XML report title (the location depends on the library)
* title.set("Custom XML report title")
*
* // Specify file to generate XML report
* setReportFile(layout.buildDirectory.file("my-xml-report.xml"))
* }
Expand All @@ -633,6 +637,13 @@ public interface KoverXmlReportConfig {
*/
public fun filters(config: Action<KoverReportFilters>)

/**
* Specify title in XML report.
*
* `"Kover Gradle Plugin XML report for $projectPath"` by default.
*/
public val title: Property<String>

/**
* Generate an XML report when running the `check` task.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ internal open class KoverHtmlReportConfigImpl @Inject constructor(private val ob
internal open class KoverXmlReportConfigImpl @Inject constructor(
private val objects: ObjectFactory
) : KoverXmlReportConfig {
override val title: Property<String> = objects.property()

override var onCheck: Boolean = false

internal var filters: KoverReportFiltersImpl? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
package kotlinx.kover.gradle.plugin.tasks.reports

import org.gradle.api.file.*
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*

@CacheableTask
internal abstract class KoverXmlTask : AbstractKoverReportTask() {
@get:OutputFile
internal abstract val reportFile: RegularFileProperty

@get:Input
abstract val title: Property<String>

@TaskAction
fun generate() {
val xmlFile = reportFile.get().asFile
xmlFile.parentFile.mkdirs()
tool.get().xmlReport(xmlFile, context())
tool.get().xmlReport(xmlFile, title.get(), context())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal interface CoverageTool {
/**
* Generate XML report.
*/
fun xmlReport(xmlFile: File, context: ReportContext)
fun xmlReport(xmlFile: File, title: String, context: ReportContext)

/**
* Generate binary report in IntelliJ format (Kover-only).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal fun ReportContext.jacocoHtmlReport(htmlDir: File, title: String, charse
}
}

internal fun ReportContext.jacocoXmlReport(xmlFile: File) {
callAntReport(projectPath) {
internal fun ReportContext.jacocoXmlReport(xmlFile: File, title: String) {
callAntReport(title) {
xmlFile.parentFile.mkdirs()
invokeMethod("xml", mapOf("destfile" to xmlFile))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ internal class JacocoTool(override val variant: CoverageToolVariant) : CoverageT
return buildJvmAgentArgs(jarFile, binReportFile, excludedClasses)
}

override fun xmlReport(xmlFile: File, context: ReportContext) {
context.jacocoXmlReport(xmlFile)
override fun xmlReport(xmlFile: File, title: String, context: ReportContext) {
context.jacocoXmlReport(xmlFile, title)
}

override fun htmlReport(htmlDir: File, title: String, charset: String?, context: ReportContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ internal fun ReportContext.koverHtmlReport(htmlReportDir: File, htmlTitle: Strin
}
}

internal fun ReportContext.koverXmlReport(xmlReportFile: File) {
internal fun ReportContext.koverXmlReport(xmlReportFile: File, xmlTitle: String) {
submitAction<XmlReportAction, XmlReportParameters> {
xmlFile.set(xmlReportFile)
title.convention(xmlTitle)
filters.convention(this@koverXmlReport.filters)

files.convention(this@koverXmlReport.files)
Expand All @@ -39,6 +40,7 @@ internal fun ReportContext.koverXmlReport(xmlReportFile: File) {

internal interface XmlReportParameters : ReportParameters {
val xmlFile: RegularFileProperty
val title: Property<String>
}

internal interface HtmlReportParameters : ReportParameters {
Expand All @@ -53,7 +55,7 @@ internal abstract class XmlReportAction : AbstractReportAction<XmlReportParamete

ReportApi.xmlReport(
parameters.xmlFile.get().asFile,
parameters.projectPath.get(),
parameters.title.get(),
files.reports.toList(),
files.outputs.toList(),
files.sources.toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ internal class KoverTool(override val variant: CoverageToolVariant) : CoverageTo
return buildJvmAgentArgs(jarFile, tempDir, binReportFile, excludedClasses)
}

override fun xmlReport(xmlFile: File, context: ReportContext) {
context.koverXmlReport(xmlFile)
override fun xmlReport(xmlFile: File, title: String, context: ReportContext) {
context.koverXmlReport(xmlFile, title)
}

override fun htmlReport(htmlDir: File, title: String, charset: String?, context: ReportContext) {
Expand Down

0 comments on commit ec84816

Please sign in to comment.