Skip to content

Commit cacc651

Browse files
committed
Implemented merging of binary reports in Kover CLI and Kover Features
Resolves #677
1 parent 9d72c74 commit cacc651

File tree

9 files changed

+729
-3
lines changed

9 files changed

+729
-3
lines changed

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22

3-
intellij-coverage = "1.0.761"
3+
intellij-coverage = "1.0.762"
44
junit = "5.9.0"
55
kotlinx-bcv = "0.13.2"
66
kotlinx-dokka = "1.8.10"

kover-cli/docs/index.md

+17
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,20 @@ Allows you to generate HTML and XML reports from the existing binary report.
4141
| --src <sources-path> | location of the source files root | + | + |
4242
| --title <html-title> | title in the HTML report | | |
4343
| --xml <xml-file-path> | generate a XML report in the specified path | | |
44+
45+
## Merging binary reports
46+
47+
Allows you to merge multiple files into single binary report.
48+
49+
If the target file did not exist, a new one is created, otherwise if the file already exists, it will be overwritten.
50+
51+
`java -jar kover-cli.jar merge [<binary-report-path> ...] --target <merged-report-path>`
52+
53+
| Option | Description | Required | Multiple |
54+
|-------------------------------|------------------------------|:--------:|:--------:|
55+
| <binary-report-path> | list of binary reports files | | + |
56+
| --target <merged-report-path> | merged binary report file | + | |
57+
58+
Example:
59+
60+
`java -jar kover-cli.jar merge build/reports/report1.ic build/reports/report2.ic --target build/reports/merged.ic`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2000-2024 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package kotlinx.kover.cli.commands
18+
19+
import kotlinx.kover.features.jvm.KoverLegacyFeatures
20+
import org.kohsuke.args4j.Argument
21+
import org.kohsuke.args4j.Option
22+
import java.io.File
23+
import java.io.PrintWriter
24+
25+
26+
internal class MergeCommand : Command {
27+
@Argument(usage = "list of binary reports files", metaVar = "<binary-report-path>")
28+
private var binaryReports: MutableList<File> = ArrayList()
29+
30+
@Option(
31+
name = "--target",
32+
usage = "merged binary report file",
33+
metaVar = "<merged-report-path>",
34+
required = true
35+
)
36+
private var targetReport: File? = null
37+
38+
override val name: String = "merge"
39+
40+
override val description: String = "Merge binary report files into one"
41+
42+
43+
override fun call(output: PrintWriter, errorWriter: PrintWriter): Int {
44+
try {
45+
KoverLegacyFeatures.mergeIc(targetReport!!, binaryReports)
46+
} catch (e: Exception) {
47+
errorWriter.println("Binary reports merging failed: " + e.message)
48+
return -1
49+
}
50+
return 0
51+
}
52+
}

kover-cli/src/main/kotlin/kotlinx/kover/cli/commands/RootCommand.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2000-2023 JetBrains s.r.o.
2+
* Copyright 2000-2024 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,7 +75,7 @@ internal class RootCommand : Command {
7575

7676
companion object {
7777
val commands: List<Command> =
78-
listOf(OfflineInstrumentCommand(), ReportCommand())
78+
listOf(OfflineInstrumentCommand(), ReportCommand(), MergeCommand())
7979

8080
private fun joinedCommandNames(): String {
8181
return commands.joinToString(" | ") { it.name ?: "" }

kover-cli/src/test/kotlin/kotlinx/kover/cli/tests/SimpleTests.kt

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlinx.kover.cli.invokeCli
2020
import java.io.File
2121
import kotlin.io.path.createTempDirectory
2222
import kotlin.test.Test
23+
import kotlin.test.assertContains
2324
import kotlin.test.assertEquals
2425

2526
private const val RESOURCES_PATH = "src/test/resources"
@@ -69,4 +70,29 @@ class SimpleTests {
6970
println("Output HTML path file://$targetPath")
7071
assertEquals(0, invokeCli(args.toTypedArray()))
7172
}
73+
74+
@Test
75+
fun merge() {
76+
// class `com.example.A` is present only in test1.ic and `com.example.B` only in test2.ic
77+
val ic1 = File("$RESOURCES_PATH/merge/test1.ic")
78+
val ic2 = File("$RESOURCES_PATH/merge/test2.ic")
79+
80+
val target = kotlin.io.path.createTempFile("kover-merge-test", ".ic").toFile()
81+
82+
val args = buildList {
83+
add("merge")
84+
add(ic1.canonicalPath)
85+
add(ic2.canonicalPath)
86+
add("--target")
87+
add(target.canonicalPath)
88+
}
89+
90+
println("Merge reports, args: " + args.joinToString(" "))
91+
assertEquals(0, invokeCli(args.toTypedArray()))
92+
93+
val contentAsUtf8 = target.readText()
94+
// check both classes are present in merged report
95+
assertContains(contentAsUtf8, "com.example.A")
96+
assertContains(contentAsUtf8, "com.example.B")
97+
}
7298
}

kover-cli/src/test/resources/merge/test1.ic

+310
Large diffs are not rendered by default.

kover-cli/src/test/resources/merge/test2.ic

+310
Large diffs are not rendered by default.

kover-features-jvm/api/kover-features-jvm.api

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public final class kotlinx/kover/features/jvm/KoverLegacyFeatures {
105105
public final fun generateHtmlReport (Ljava/io/File;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Lkotlinx/kover/features/jvm/ClassFilters;)V
106106
public final fun generateXmlReport (Ljava/io/File;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/lang/String;Lkotlinx/kover/features/jvm/ClassFilters;)V
107107
public final fun instrument (Ljava/io/File;Ljava/util/List;Lkotlinx/kover/features/jvm/ClassFilters;Z)V
108+
public final fun mergeIc (Ljava/io/File;Ljava/util/List;)V
108109
public final fun verify (Ljava/util/List;Ljava/io/File;Lkotlinx/kover/features/jvm/ClassFilters;Ljava/util/List;Ljava/util/List;)Ljava/util/List;
109110
public final fun violationMessage (Ljava/util/List;)Ljava/lang/String;
110111
}

kover-features-jvm/src/main/java/kotlinx/kover/features/jvm/KoverLegacyFeatures.kt

+10
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ public object KoverLegacyFeatures {
148148
AggregatorApi.aggregate(listOf(request), binaryReports, classfileDirs)
149149
}
150150

151+
/**
152+
* Merge several IC binaryReports into one file without extra filtering.
153+
*
154+
* @param icFile Target IC report file
155+
* @param binaryReports List of coverage binary reports in IC format
156+
*/
157+
public fun mergeIc(icFile: File, binaryReports: List<File>) {
158+
AggregatorApi.merge(binaryReports, icFile)
159+
}
160+
151161
/**
152162
* Get coverage values from binary reports.
153163
*

0 commit comments

Comments
 (0)