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

kover should able to exclude interface default method #623

Open
hanrw opened this issue Jun 5, 2024 · 8 comments
Open

kover should able to exclude interface default method #623

hanrw opened this issue Jun 5, 2024 · 8 comments
Assignees
Labels
Bug Bug issue type S: confirmed Status: bug is reproduced or present

Comments

@hanrw
Copy link

hanrw commented Jun 5, 2024

It's nice to have a filter for excluding the interface which has a default implementation
E.g

package org.koin.core.component

import org.koin.core.Koin
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.qualifier.Qualifier
import org.koin.mp.KoinPlatformTools

/**
 * KoinComponent interface marker to bring Koin extensions features
 *
 * @author Arnaud Giuliani
 */
interface KoinComponent {

    /**
     * Get the associated Koin instance
     */
    fun getKoin(): Koin = KoinPlatformTools.defaultContext().get()
}


data class AnthropicConfig(
    val apiKey: () -> String = { "CONFIG_API_KEY" },
    val baseUrl: () -> String = { Anthropic.BASE_URL },
    val anthropicVersion: () -> String = { Anthropic.ANTHROPIC_VERSION },
) : KoinComponent

Check here
image

the cover is not covered unless added a ut like below

class AnthropicConfigTest : AutoCloseKoinTest() {
    
    @Test
    fun `only fix the coverage by this case`() {
        startKoin {
            val r = AnthropicConfig().getKoin()
            assertNotNull(r)
        }
    }
}
@hanrw hanrw added Feature Feature request issue type S: untriaged Status: issue reported but unprocessed labels Jun 5, 2024
@shanshin
Copy link
Collaborator

shanshin commented Jun 5, 2024

We will consider adding similar filters in the future.

But could you describe your case in more detail, for what purpose do you have methods with default implementation in interfaces, but it is acceptable for you not to use them?

@shanshin shanshin added S: waiting for clarification Status: additional information required to proceed and removed S: untriaged Status: issue reported but unprocessed labels Jun 5, 2024
@hanrw
Copy link
Author

hanrw commented Jun 5, 2024

I discovered the issue after introducing Koin(https://github.com/InsertKoinIO/koin) as the dependency injection framework in my project. and found the interface KoinComponent has a default implementation.

@shanshin
Copy link
Collaborator

shanshin commented Jun 5, 2024

Do I understand correctly that the KoinComponent interface does not belong to your project and you did not write it?
In this case, you should exclude it from the reports.

The collection of coverage is primarily aimed at finding unused code that the project developer wrote.
If any external dependencies are appeared in the report, they should be completely exclude (because the code from external dependencies is not controlled by the developer).

@hanrw
Copy link
Author

hanrw commented Jun 5, 2024

yes. you are right. but i have my own code which extends external code like KoinComponent for this case is there better solution from your point of view?
let's say for the class AnthropicConfig I had covered call cases but the kover report that there's something that not cover yet.

@shanshin
Copy link
Collaborator

shanshin commented Jun 5, 2024

let's say for the class AnthropicConfig I had covered call cases but the kover report that there's something that not cover yet.

could you provide an XML report, at least part with class AnthropicConfig coverage?

The report you provided is generated using codecov, we do not control it directly, it may have its own rules for calculating coverage

@hanrw
Copy link
Author

hanrw commented Jun 5, 2024

Here you go build.gradle.kts

kover {
    reports {
        filters {
            excludes {
                classes(
                    "com.tddworks.**.*\$*$*", // Lambda functions like - LemonSqueezyLicenseApi$activeLicense$activationResult$1
                    "com.tddworks.**.*\$Companion", // Lambda functions like - LemonSqueezyLicenseApi$activeLicense$activationResult$1
                    "*.*\$\$serializer", // Kotlinx serializer)
                )
//            inheritedFrom("org.koin.core.component.KoinComponent")
//            annotatedBy("kotlinx.serialization.Serializable")
            }
            includes {
                classes("com.tddworks.*")
            }
        }

        verify {
            rule {
                bound {
                    minValue = 82
                }
            }
        }
    }
}

report xml

<package name="com/tddworks/anthropic/api">
<class name="com/tddworks/anthropic/api/AnthropicConfig" sourcefilename="AnthropicConfig.kt">
<method name="getKoin" desc="()Lorg/koin/core/Koin;">
<counter type="INSTRUCTION" missed="2" covered="0"/>
<counter type="BRANCH" missed="0" covered="0"/>
<counter type="LINE" missed="1" covered="0"/>
</method>
<method name="&lt;init>" desc="(Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V">
<counter type="INSTRUCTION" missed="0" covered="24"/>
<counter type="BRANCH" missed="0" covered="0"/>
<counter type="LINE" missed="0" covered="3"/>
</method>
<counter type="INSTRUCTION" missed="2" covered="24"/>
<counter type="BRANCH" missed="0" covered="0"/>
<counter type="LINE" missed="1" covered="3"/>
<counter type="METHOD" missed="1" covered="1"/>
</class>

and you can also found from - app.codecov.io

Thanks.

@shanshin
Copy link
Collaborator

shanshin commented Jun 5, 2024

It's bug in Kover reporter.

Reproducer


interface Parent {
    fun getFoo(): String = "foo"
}


data class Child(
    val s: () -> String = { "Text" },
) : Parent

report

<class name="org/jetbrains/kover/android/test/Child" sourcefilename="Extensions.kt">
<method name="&lt;init&gt;" desc="(Lkotlin/jvm/functions/Function0;)V">
<counter type="INSTRUCTION" missed="8" covered="0"/>
<counter type="BRANCH" missed="0" covered="0"/>
<counter type="LINE" missed="1" covered="0"/>
</method>
<method name="getFoo" desc="()Ljava/lang/String;">
<counter type="INSTRUCTION" missed="2" covered="0"/>
<counter type="BRANCH" missed="0" covered="0"/>
<counter type="LINE" missed="1" covered="0"/>
</method>
<counter type="INSTRUCTION" missed="10" covered="0"/>
<counter type="BRANCH" missed="0" covered="0"/>
<counter type="LINE" missed="2" covered="0"/>
<counter type="METHOD" missed="2" covered="0"/>
</class>

Kotlin 1.9.23 and 2.0.0, intellij reporter 1.0.752

@shanshin shanshin added Bug Bug issue type S: confirmed Status: bug is reproduced or present and removed Feature Feature request issue type S: waiting for clarification Status: additional information required to proceed labels Jun 5, 2024
@hanrw
Copy link
Author

hanrw commented Jun 6, 2024

thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug issue type S: confirmed Status: bug is reproduced or present
Projects
None yet
Development

No branches or pull requests

2 participants