coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.99k stars 386 forks source link

Incorrect coverage for methods returning `IAsyncEnumerable` in generic classes #1383

Closed arseni-mourzenko closed 1 year ago

arseni-mourzenko commented 2 years ago

Hi.

Using coverlet.msbuild.3.1.2, I noticed an issue with code coverage for a method which returns IAsyncEnumerable, as soon as the method is inside a generic class.

Here's a short example which reproduces the issue:

public class A
{
    public static async IAsyncEnumerable<int> Demo()
    {
        yield return 5;
        yield return 2;
    }
}

public class A<T>
{
    public static async IAsyncEnumerable<int> Demo()
    {
        yield return 5;
        yield return 2;
    }
}

Given the following tests:

[Fact] public async Task Test1() => Assert.Equal(new [] { 5, 2 }, await A.Demo().ToListAsync());
[Fact] public async Task Test2() => Assert.Equal(new [] { 5, 2 }, await A<string>.Demo().ToListAsync());

the coverage report will show that the Demo method inside the class A has 100% branch coverage, whereas the method Demo inside A<T> has only partial coverage (one branch out of two) on the line {, as well as on the following two yield return lines.

The code of the two methods and the corresponding tests being identical, the coverage should instead be identical, that is, 100% in both cases.

daveMueller commented 2 years ago

Thanks for reporting this.

RobARichardson commented 2 years ago

Seeing similar behavior where a Generic Class has a method that returns an IAsyncEnumerable, the method is marked with ExcludeFromCodeCoverage however coverage is reported as zero instead of being ignored.

daveMueller commented 1 year ago

@RobARichardson I created another issue for that (#1431). I think it is another bug and my PR will not fix this.

arseni-mourzenko commented 1 year ago

@MarcoRossignoli, do you know when the new version of Coverlet would be available through NuGet?