Skip to content

Commit

Permalink
Implemented merging of binary reports in Kover CLI and Kover Features
Browse files Browse the repository at this point in the history
Resolves #677
  • Loading branch information
shanshin committed Aug 26, 2024
1 parent 9d72c74 commit 5372d26
Show file tree
Hide file tree
Showing 8 changed files with 728 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]

intellij-coverage = "1.0.761"
intellij-coverage = "1.0.762"
junit = "5.9.0"
kotlinx-bcv = "0.13.2"
kotlinx-dokka = "1.8.10"
Expand Down
17 changes: 17 additions & 0 deletions kover-cli/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ Allows you to generate HTML and XML reports from the existing binary report.
| --src <sources-path> | location of the source files root | + | + |
| --title <html-title> | title in the HTML report | | |
| --xml <xml-file-path> | generate a XML report in the specified path | | |

## Merging binary reports

Allows you to merge multiple files into single binary report.

If the target file did not exist, a new one is created, otherwise if the file already exists, it will be overwritten.

`java -jar kover-cli.jar merge [<binary-report-path> ...] --target <merged-report-path>`

| Option | Description | Required | Multiple |
|-------------------------------|------------------------------|:--------:|:--------:|
| <binary-report-path> | list of binary reports files | | + |
| --target <merged-report-path> | merged binary report file | + | |

Example:

`java -jar kover-cli.jar merge build/reports/report1.ic build/reports/report2.ic --target build/reports/merged.ic`
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2024 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package kotlinx.kover.cli.commands

import kotlinx.kover.features.jvm.KoverLegacyFeatures
import org.kohsuke.args4j.Argument
import org.kohsuke.args4j.Option
import java.io.File
import java.io.PrintWriter


internal class MergeCommand : Command {
@Argument(usage = "list of binary reports files", metaVar = "<binary-report-path>")
private var binaryReports: MutableList<File> = ArrayList()

@Option(
name = "--target",
usage = "merged binary report file",
metaVar = "<merged-report-path>",
required = true
)
private var targetReport: File? = null

override val name: String = "merge"

override val description: String = "Merge binary report files into one"


override fun call(output: PrintWriter, errorWriter: PrintWriter): Int {
try {
KoverLegacyFeatures.mergeIc(targetReport!!, binaryReports)
} catch (e: Exception) {
errorWriter.println("Binary reports merging failed: " + e.message)
return -1
}
return 0
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2023 JetBrains s.r.o.
* Copyright 2000-2024 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -75,7 +75,7 @@ internal class RootCommand : Command {

companion object {
val commands: List<Command> =
listOf(OfflineInstrumentCommand(), ReportCommand())
listOf(OfflineInstrumentCommand(), ReportCommand(), MergeCommand())

private fun joinedCommandNames(): String {
return commands.joinToString(" | ") { it.name ?: "" }
Expand Down
26 changes: 26 additions & 0 deletions kover-cli/src/test/kotlin/kotlinx/kover/cli/tests/SimpleTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.kover.cli.invokeCli
import java.io.File
import kotlin.io.path.createTempDirectory
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals

private const val RESOURCES_PATH = "src/test/resources"
Expand Down Expand Up @@ -69,4 +70,29 @@ class SimpleTests {
println("Output HTML path file://$targetPath")
assertEquals(0, invokeCli(args.toTypedArray()))
}

@Test
fun merge() {
// class `com.example.A` is present only in test1.ic and `com.example.B` only in test2.ic
val ic1 = File("$RESOURCES_PATH/merge/test1.ic")
val ic2 = File("$RESOURCES_PATH/merge/test2.ic")

val target = kotlin.io.path.createTempFile("kover-merge-test", ".ic").toFile()

val args = buildList {
add("merge")
add(ic1.canonicalPath)
add(ic2.canonicalPath)
add("--target")
add(target.canonicalPath)
}

println("Merge reports, args: " + args.joinToString(" "))
assertEquals(0, invokeCli(args.toTypedArray()))

val contentAsUtf8 = target.readText()
// check both classes are present in merged report
assertContains(contentAsUtf8, "com.example.A")
assertContains(contentAsUtf8, "com.example.B")
}
}
310 changes: 310 additions & 0 deletions kover-cli/src/test/resources/merge/test1.ic

Large diffs are not rendered by default.

310 changes: 310 additions & 0 deletions kover-cli/src/test/resources/merge/test2.ic

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public object KoverLegacyFeatures {
AggregatorApi.aggregate(listOf(request), binaryReports, classfileDirs)
}

/**
* Merge several IC binaryReports into one file without extra filtering.
*
* @param icFile Target IC report file
* @param binaryReports List of coverage binary reports in IC format
*/
public fun mergeIc(icFile: File, binaryReports: List<File>) {
AggregatorApi.merge(binaryReports, icFile)
}

/**
* Get coverage values from binary reports.
*
Expand Down

0 comments on commit 5372d26

Please sign in to comment.