From cb66dbd9e259c997f6644f6dd95f79975e6bbabd Mon Sep 17 00:00:00 2001 From: "Sergey.Shanshin" Date: Wed, 14 Feb 2024 20:41:58 +0100 Subject: [PATCH] Added the ability to specify a header for an XML report Resolves #527 --- docs/gradle-plugin/configuring.md | 6 ++ gradle/libs.versions.toml | 2 +- .../test/functional/cases/XmlReportTests.kt | 61 +++++++++++++++++++ .../framework/writer/KoverReportWriter.kt | 2 + .../appliers/reports/ReportsVariantApplier.kt | 2 + .../gradle/plugin/dsl/KoverReportExtension.kt | 11 ++++ .../kover/gradle/plugin/dsl/KoverVersions.kt | 2 +- .../dsl/internal/KoverReportExtension.kt | 2 + .../plugin/tasks/reports/KoverXmlTask.kt | 6 +- .../kover/gradle/plugin/tools/CoverageTool.kt | 2 +- .../tools/jacoco/JacocoHtmlOrXmlReport.kt | 4 +- .../gradle/plugin/tools/jacoco/JacocoTool.kt | 4 +- .../tools/kover/KoverHtmlOrXmlReport.kt | 5 +- .../gradle/plugin/tools/kover/KoverTool.kt | 4 +- 14 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/XmlReportTests.kt diff --git a/docs/gradle-plugin/configuring.md b/docs/gradle-plugin/configuring.md index 0ee6dc89..a7194e84 100644 --- a/docs/gradle-plugin/configuring.md +++ b/docs/gradle-plugin/configuring.md @@ -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")) @@ -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")) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0ac9e047..bb853375 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] -intellij-coverage = "1.0.741" +intellij-coverage = "1.0.748" junit = "5.9.0" kotlinx-bcv = "0.13.2" kotlinx-dokka = "1.8.10" diff --git a/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/XmlReportTests.kt b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/XmlReportTests.kt new file mode 100644 index 00000000..1d253366 --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/XmlReportTests.kt @@ -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) + } + } + } + } + +} diff --git a/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/framework/writer/KoverReportWriter.kt b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/framework/writer/KoverReportWriter.kt index bc97f7f1..88b45301 100644 --- a/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/framework/writer/KoverReportWriter.kt +++ b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/framework/writer/KoverReportWriter.kt @@ -135,6 +135,8 @@ internal class KoverXmlReportConfigWriter(private val writer: FormattedWriter) : field = value } + override val title: Property = PropertyWriter("title", writer) + override fun setReportFile(xmlFile: File) { writer.assign("reportFile", xmlFile.forScript()) } diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/appliers/reports/ReportsVariantApplier.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/appliers/reports/ReportsVariantApplier.kt index a44aee01..45591e05 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/appliers/reports/ReportsVariantApplier.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/appliers/reports/ReportsVariantApplier.kt @@ -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() @@ -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) { diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverReportExtension.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverReportExtension.kt index ede9af7a..a8675035 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverReportExtension.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverReportExtension.kt @@ -221,6 +221,7 @@ public interface KoverReportsConfig { * } * * onCheck = false + * title.set("Custom XML report title") * setReportFile(layout.buildDirectory.file("my-project-report/result.xml")) * } * ``` @@ -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")) * } @@ -633,6 +637,13 @@ public interface KoverXmlReportConfig { */ public fun filters(config: Action) + /** + * Specify title in XML report. + * + * `"Kover Gradle Plugin XML report for $projectPath"` by default. + */ + public val title: Property + /** * Generate an XML report when running the `check` task. */ diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverVersions.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverVersions.kt index 6f87dc40..4a481a94 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverVersions.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/KoverVersions.kt @@ -15,7 +15,7 @@ public object KoverVersions { /** * Kover coverage tool version. */ - public const val KOVER_TOOL_VERSION = "1.0.741" + public const val KOVER_TOOL_VERSION = "1.0.748" /** * JaCoCo coverage tool version used by default. diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/internal/KoverReportExtension.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/internal/KoverReportExtension.kt index 061d439e..a5882123 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/internal/KoverReportExtension.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/dsl/internal/KoverReportExtension.kt @@ -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 = objects.property() + override var onCheck: Boolean = false internal var filters: KoverReportFiltersImpl? = null diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tasks/reports/KoverXmlTask.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tasks/reports/KoverXmlTask.kt index 3bbe7541..7cd52239 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tasks/reports/KoverXmlTask.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tasks/reports/KoverXmlTask.kt @@ -5,6 +5,7 @@ package kotlinx.kover.gradle.plugin.tasks.reports import org.gradle.api.file.* +import org.gradle.api.provider.Property import org.gradle.api.tasks.* @CacheableTask @@ -12,10 +13,13 @@ internal abstract class KoverXmlTask : AbstractKoverReportTask() { @get:OutputFile internal abstract val reportFile: RegularFileProperty + @get:Input + abstract val title: Property + @TaskAction fun generate() { val xmlFile = reportFile.get().asFile xmlFile.parentFile.mkdirs() - tool.get().xmlReport(xmlFile, context()) + tool.get().xmlReport(xmlFile, title.get(), context()) } } diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/CoverageTool.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/CoverageTool.kt index 9e90d43a..e804560a 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/CoverageTool.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/CoverageTool.kt @@ -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). diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoHtmlOrXmlReport.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoHtmlOrXmlReport.kt index 6b63339e..b25f47de 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoHtmlOrXmlReport.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoHtmlOrXmlReport.kt @@ -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)) } diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoTool.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoTool.kt index bacde428..403877b2 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoTool.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/jacoco/JacocoTool.kt @@ -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) { diff --git a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/kover/KoverHtmlOrXmlReport.kt b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/kover/KoverHtmlOrXmlReport.kt index c90a1153..1bbcc748 100644 --- a/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/kover/KoverHtmlOrXmlReport.kt +++ b/kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/tools/kover/KoverHtmlOrXmlReport.kt @@ -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 { xmlFile.set(xmlReportFile) + title.convention(xmlTitle) filters.convention(this@koverXmlReport.filters) files.convention(this@koverXmlReport.files) @@ -39,6 +40,7 @@ internal fun ReportContext.koverXmlReport(xmlReportFile: File) { internal interface XmlReportParameters : ReportParameters { val xmlFile: RegularFileProperty + val title: Property } internal interface HtmlReportParameters : ReportParameters { @@ -53,6 +55,7 @@ internal abstract class XmlReportAction : AbstractReportAction