Skip to content

Wrong coverage for await foreach with generic method #1312

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

Merged
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
5 changes: 5 additions & 0 deletions Documentation/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed
-Await foreach has wrong branch coverage when method is generic [#1210](https://github.com/coverlet-coverage/coverlet/issues/1210)

## Release date 2022-02-06
### Packages
coverlet.msbuild 3.1.2
Expand Down
13 changes: 9 additions & 4 deletions src/coverlet.core/Symbols/CecilSymbolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,11 @@ static bool CheckForAsyncEnumerator(List<Instruction> instructions, Instruction
(instructions[currentIndex - 2].OpCode == OpCodes.Ldarg ||
instructions[currentIndex - 2].OpCode == OpCodes.Ldarg_0) &&
instructions[currentIndex - 1].OpCode == OpCodes.Ldfld &&
instructions[currentIndex - 1].Operand is FieldDefinition field &&
IsCompilerGenerated(field) && field.FieldType.FullName.StartsWith("System.Collections.Generic.IAsyncEnumerator"))
(
(instructions[currentIndex - 1].Operand is FieldDefinition field && IsCompilerGenerated(field) && field.FieldType.FullName.StartsWith("System.Collections.Generic.IAsyncEnumerator")) ||
(instructions[currentIndex - 1].Operand is FieldReference fieldRef && IsCompilerGenerated(fieldRef.Resolve()) && fieldRef.FieldType.FullName.StartsWith("System.Collections.Generic.IAsyncEnumerator"))
)
)
{
return true;
}
Expand Down Expand Up @@ -538,8 +541,10 @@ static bool CheckIfExceptionThrown(List<Instruction> instructions, Instruction i
for (int i = currentIndex - 1; i >= minFieldIndex; --i)
{
if (instructions[i].OpCode == OpCodes.Ldfld &&
instructions[i].Operand is FieldDefinition field &&
IsCompilerGenerated(field) && field.FieldType.FullName == "System.Object")
(
(instructions[i].Operand is FieldDefinition field && IsCompilerGenerated(field) && field.FieldType.FullName == "System.Object") ||
(instructions[i].Operand is FieldReference fieldRef && IsCompilerGenerated(fieldRef.Resolve()) && fieldRef.FieldType.FullName == "System.Object")
))
{
// We expect the call to GetResult() to be no more than four
// instructions before the loading of the field's value.
Expand Down
10 changes: 7 additions & 3 deletions test/coverlet.core.tests/Coverage/CoverageTests.AsyncForeach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void AsyncForeach()
int res = ((ValueTask<int>)instance.SumWithATwist(AsyncEnumerable.Range(1, 5))).GetAwaiter().GetResult();
res += ((ValueTask<int>)instance.Sum(AsyncEnumerable.Range(1, 3))).GetAwaiter().GetResult();
res += ((ValueTask<int>)instance.SumEmpty()).GetAwaiter().GetResult();
((ValueTask)instance.GenericAsyncForeach<object>(AsyncEnumerable.Range(1, 3))).GetAwaiter().GetResult();

return Task.CompletedTask;
}, persistPrepareResultToFile: pathSerialize[0]);
Expand All @@ -43,7 +44,9 @@ public void AsyncForeach()
// Sum(IAsyncEnumerable<int>)
(34, 1), (35, 1), (37, 9), (38, 3), (39, 3), (40, 3), (42, 1), (43, 1),
// SumEmpty()
(47, 1), (48, 1), (50, 3), (51, 0), (52, 0), (53, 0), (55, 1), (56, 1)
(47, 1), (48, 1), (50, 3), (51, 0), (52, 0), (53, 0), (55, 1), (56, 1),
// GenericAsyncForeach
(59,1), (60, 9), (61, 3), (62, 3), (63, 3), (64, 1)
)
.AssertBranchesCovered(BuildConfiguration.Debug,
// SumWithATwist(IAsyncEnumerable<int>)
Expand All @@ -53,9 +56,10 @@ public void AsyncForeach()
// SumEmpty()
// If we never entered the loop, that's a branch not taken, which is
// what we want to see.
(50, 0, 1), (50, 1, 0)
(50, 0, 1), (50, 1, 0),
(60, 0, 1), (60, 1, 3)
)
.ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 4);
.ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 5);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ async public ValueTask<int> SumEmpty()

return sum;
}

public async ValueTask GenericAsyncForeach<T>(IAsyncEnumerable<int> ints)
{
await foreach (int obj in ints)
{
await Task.Delay(1);
}
}
}
}