Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented merging of binary reports in Kover CLI and Kover Features #679

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 does not exist, a new one is created. Otherwise, the existing file 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.

1 change: 1 addition & 0 deletions kover-features-jvm/api/kover-features-jvm.api
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public final class kotlinx/kover/features/jvm/KoverLegacyFeatures {
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
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
public final fun instrument (Ljava/io/File;Ljava/util/List;Lkotlinx/kover/features/jvm/ClassFilters;Z)V
public final fun mergeIc (Ljava/io/File;Ljava/util/List;)V
public final fun verify (Ljava/util/List;Ljava/io/File;Lkotlinx/kover/features/jvm/ClassFilters;Ljava/util/List;Ljava/util/List;)Ljava/util/List;
public final fun violationMessage (Ljava/util/List;)Ljava/lang/String;
}
Expand Down
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