diff --git a/Documentation/Changelog.md b/Documentation/Changelog.md index 1e84c2b..61510a3 100644 --- a/Documentation/Changelog.md +++ b/Documentation/Changelog.md @@ -7,9 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Fixed +-Fix branch coverage issue for finally block with await [#1233](https://github.com/coverlet-coverage/coverlet/issues/1233) -Fix threshold doesn't work when coverage empty [#1205](https://github.com/coverlet-coverage/coverlet/issues/1205) -Fix branch coverage issue for il switch [#1177](https://github.com/coverlet-coverage/coverlet/issues/1177) --Fix branch coverage with using statement and several awaits[#1176](https://github.com/coverlet-coverage/coverlet/issues/1176) +-Fix branch coverage with using statement and several awaits[#1176](https://github.com/coverlet-coverage/coverlet/issues/1176) ### Improvements -Improve logging in case of exception inside static ctor of NetstandardAwareAssemblyResolver [#1230](https://github.com/coverlet-coverage/coverlet/pull/1230) diff --git a/src/coverlet.core/Symbols/CecilSymbolHelper.cs b/src/coverlet.core/Symbols/CecilSymbolHelper.cs index 542a17a..ca0b0e9 100644 --- a/src/coverlet.core/Symbols/CecilSymbolHelper.cs +++ b/src/coverlet.core/Symbols/CecilSymbolHelper.cs @@ -543,9 +543,9 @@ namespace Coverlet.Core.Symbols instructions[i].Operand is FieldDefinition field && IsCompilerGenerated(field) && field.FieldType.FullName == "System.Object") { - // We expect the call to GetResult() to be no more than three + // We expect the call to GetResult() to be no more than four // instructions before the loading of the field's value. - int minCallIndex = Math.Max(0, i - 3); + int minCallIndex = Math.Max(0, i - 4); for (int j = i - 1; j >= minCallIndex; --j) { diff --git a/test/coverlet.core.tests/Coverage/CoverageTests.AsyncAwait.cs b/test/coverlet.core.tests/Coverage/CoverageTests.AsyncAwait.cs index bd5e99a..8727b7c 100644 --- a/test/coverlet.core.tests/Coverage/CoverageTests.AsyncAwait.cs +++ b/test/coverlet.core.tests/Coverage/CoverageTests.AsyncAwait.cs @@ -154,5 +154,33 @@ namespace Coverlet.Core.Tests File.Delete(path); } } + + [Fact] + public void AsyncAwait_Issue_1233() + { + string path = Path.GetTempFileName(); + try + { + FunctionExecutor.Run(async (string[] pathSerialize) => + { + CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run(instance => + { + ((Task)instance.Test()).ConfigureAwait(false).GetAwaiter().GetResult(); + return Task.CompletedTask; + }, + persistPrepareResultToFile: pathSerialize[0]); + + return 0; + }, new string[] { path }); + + var document = TestInstrumentationHelper.GetCoverageResult(path).Document("Instrumentation.AsyncAwait.cs"); + document.AssertLinesCovered(BuildConfiguration.Debug, (150, 1)); + Assert.DoesNotContain(document.Branches, x => x.Key.Line == 150); + } + finally + { + File.Delete(path); + } + } } } \ No newline at end of file diff --git a/test/coverlet.core.tests/Samples/Instrumentation.AsyncAwait.cs b/test/coverlet.core.tests/Samples/Instrumentation.AsyncAwait.cs index c6c00d8..0b4992c 100644 --- a/test/coverlet.core.tests/Samples/Instrumentation.AsyncAwait.cs +++ b/test/coverlet.core.tests/Samples/Instrumentation.AsyncAwait.cs @@ -137,4 +137,18 @@ namespace Coverlet.Core.Samples.Tests await Task.CompletedTask; } } + + public class Issue_1233 + { + async public Task Test() + { + try + { + } + finally + { + await Task.CompletedTask; + } + } + } }