From 09abb9f01fbd44a49979301992415addc18a418a Mon Sep 17 00:00:00 2001 From: Winston Hung Date: Wed, 17 Jan 2024 10:36:30 +0800 Subject: [PATCH] There are two reasons for the error in the total instruction number: - The current method of ignoring instructions based on the number of code changes has issues. The current logic only ignores bytecode in LINENUMBER node blocks, leading to some unforeseen situations in Kotlin. I have submitted a pull request that ignores all Linenumber bytecode within a method that is not part of the code changes. This adjustment yields the desired result in the graph. - The algorithm for determining the result line numbers of code changes also contributes to miscalculating the total line number. I am currently investigating JGit to explore if we can employ an algorithm similar to 'git diff' to select the most suitable method for calculating the code change results. If JGit solution is not found, the alternative would be to instruct the user to run the diff command separately and input the diff result to generate the report. For example: 'git diff' default output is, --- a/app/src/main/java/org/angmarch/jacococompose/ComposeComponent.kt +++ b/app/src/main/java/org/angmarch/jacococompose/ComposeComponent.kt @@ -16,4 +16,8 @@ fun ComposeComponent(text: String) { Surface { Text(text = text) } +} <- this would cause wrong instruction being retained in pull request coverage report. + +fun catchMeIfYouCan() { + println("I'm a testable function") } A more favorable outcome could be achieved by issuing the command 'git diff -b' to ignore space changes. --- a/app/src/main/java/org/angmarch/jacococompose/ComposeComponent.kt +++ b/app/src/main/java/org/angmarch/jacococompose/ComposeComponent.kt @@ -17,3 +17,7 @@ fun ComposeComponent(text: String) { Text(text = text) } } + +fun catchMeIfYouCan() { + println("I'm a testable function") +} --- .../src/main/kotlin/com/form/coverage/diff/git/JgitDiff.kt | 3 +++ .../kotlin/com/form/coverage/filters/ModifiedLinesFilter.kt | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/diff/git/JgitDiff.kt b/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/diff/git/JgitDiff.kt index d7b49e5..d324f64 100644 --- a/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/diff/git/JgitDiff.kt +++ b/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/diff/git/JgitDiff.kt @@ -3,6 +3,7 @@ package com.form.coverage.diff.git import org.eclipse.jgit.api.Git import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.diff.DiffFormatter +import org.eclipse.jgit.diff.RawTextComparator import org.eclipse.jgit.lib.ConfigConstants import org.eclipse.jgit.lib.Constants import org.eclipse.jgit.lib.ObjectId @@ -40,6 +41,8 @@ class JgitDiff(workingDir: File) { DiffFormatter(diffContent).apply { initialize() + setDiffComparator(RawTextComparator.WS_IGNORE_ALL) + obtainDiffEntries(git, revision).forEach { format(it) } diff --git a/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/filters/ModifiedLinesFilter.kt b/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/filters/ModifiedLinesFilter.kt index af865b0..1ddc32f 100644 --- a/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/filters/ModifiedLinesFilter.kt +++ b/jacoco-filtering-extension/src/main/kotlin/com/form/coverage/filters/ModifiedLinesFilter.kt @@ -42,6 +42,10 @@ class ModifiedLinesFilter(private val codeUpdateInfo: CodeUpdateInfo) : IFilter log.debug("\tlines: $it") } } + + if (groupedModifiedLines[true].isNullOrEmpty()) { + output.ignore(methodNode.instructions.first, methodNode.instructions.last) + } } private fun collectLineNodes(instructionNodes: InsnList): Sequence {