Kotlin / kotlinx-kover

Apache License 2.0
1.37k stars 53 forks source link

Excluded annotated methods not ignored in Sonar #703

Open lawfulpepper opened 2 weeks ago

lawfulpepper commented 2 weeks ago

This is related to Issue https://github.com/Kotlin/kotlinx-kover/issues/576.

Applies to version 0.8.3 and 0.9.0-RC.

I have Kover XML reports setup with Sonar.

Unfortunately excluding @Composable functions are not excluded in the Sonar coverage report.

If ignored, the @Composable is not added to the Kover XML report.

However, adding a simple Data Class in the same file causes Sonar to understand that this function should be ignored. Adding the Data Class adds the file to the XML report because of the uncovered Data Class.

data class UIData(
    val someData: String
)

@Composable
fun MyComposable() {
    Text("some text")
}

In the other issue, you had asked them if the XML report is used with Sonar. For us, yes, we are using the xml report path with Sonar.

Thanks!

shanshin commented 1 week ago

Unfortunately excluding @Composable functions are not excluded in the Sonar coverage report.

If ignored, the @Composable is not added to the Kover XML report.

Hi, do I understand correctly that the marked function is missing in the xml report, but it is still displayed in Sonar?

lawfulpepper commented 1 week ago

Hi shanshin, thanks for your reply!

Yes that's correct. Sonar interprets the absence of any report on a class as "Uncovered".

When I run the xmlReport for the following example:

@Composable
fun MyComposeTest() {
    Text("some text")
}

MyComposeTest is not added to the reportDebug.xml file. This is what Sonar reports:

sonar2

When I add the extra UIData class at the top:

data class UIData(
    val someData: String
)

@Composable
fun MyComposeTest() {
    Text("some text")
}

This is what the XML file report looks like:

<class name="com/abcd/xyz/tools/ui/UIData" sourcefilename="MyComposeTest.kt">  
<method name="&lt;init&gt;" desc="(Ljava/lang/String;)V">  
<counter type="INSTRUCTION" missed="7" covered="0"/>  
<counter type="BRANCH" missed="0" covered="0"/>  
<counter type="LINE" missed="2" covered="0"/>  
</method>

When Sonar receives this report, it shows that everything else is ignored except for the UIData data class.

sonar

Surprisingly, Jacoco's Generated annotation is able to exclude code from Sonar.

Thank you for looking into this.

shanshin commented 3 days ago

@lawfulpepper, it seems that this is a feature of Sonar, it additionally analyze the classes present in the project. Unfortunately, the only thing that Kover can control is the contents of the XML report, its interpretation lies with an external tool. Perhaps additional parameters can be passed to Sonar to exclude certain classes, like https://community.sonarsource.com/t/how-to-exclude-java-files/61786

If it is not possible to exclude classes by annotation now, then it may be worth to create such a feature request on the Sonar issue tracker.

Surprisingly, Jacoco's Generated annotation is able to exclude code from Sonar.

As far as I understand, this was added to Sonar so that its behavior coincides with JaCoCo, because it also does not show classes marked with an annotation containing the word generated in the name.

lawfulpepper commented 3 days ago

Hi @shanshin , thanks for looking into this. Could you point me to where the XML report generation tool is used in the Kover codebase? I'll close this comment and try to see if there's a way to override the behavior internally.

Thanks