Skip to content

Commit e97876c

Browse files
authored
Branch coverage issue with several awaits (#1201)
Branch coverage issue with several awaits
1 parent 624ef0f commit e97876c

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

Documentation/Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
### Fixed
10+
-Fix branch coverage issue for il switch [#1177](https://github.com/coverlet-coverage/coverlet/issues/1177)
11+
-Fix branch coverage with using statement and several awaits[#1176](https://github.com/coverlet-coverage/coverlet/issues/1176)
12+
713
## Release date 2021-07-19
814
### Packages
915
coverlet.msbuild 3.1.0

src/coverlet.core/Symbols/CecilSymbolHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ private static bool SkipMoveNextPrologueBranches(Instruction instruction)
124124
If method is a generated MoveNext we'll skip first branches (could be a switch or a series of branches)
125125
that check state machine value to jump to correct state (for instance after a true async call)
126126
Check if it's a Cond_Branch on state machine current value int num = <>1__state;
127-
We are on branch OpCode so we need to go back by max 2 operation to reach ldloc.0 the load of "num"
128-
Max 2 because we handle following patterns
127+
We are on branch OpCode so we need to go back by max 3 operation to reach ldloc.0 the load of "num"
128+
Max 3 because we handle following patterns
129129
130130
Swich
131131
@@ -168,7 +168,7 @@ so we know that current branch are checking that field and we're not interested
168168
*/
169169

170170
Instruction current = instruction.Previous;
171-
for (int instructionBefore = 2; instructionBefore > 0 && current.Previous != null; current = current.Previous, instructionBefore--)
171+
for (int instructionBefore = 3; instructionBefore > 0 && current.Previous != null; current = current.Previous, instructionBefore--)
172172
{
173173
if (
174174
(current.OpCode == OpCodes.Ldloc && current.Operand is VariableDefinition vo && vo.Index == 0) ||
@@ -1446,4 +1446,4 @@ public int Compare(Instruction x, Instruction y)
14461446
}
14471447
}
14481448
}
1449-
}
1449+
}

test/coverlet.core.tests/Coverage/CoverageTests.AsyncAwait.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Linq;
23
using System.Threading.Tasks;
34

45
using Coverlet.Core.Samples.Tests;
@@ -125,5 +126,33 @@ public void AsyncAwait_Issue_669_2()
125126
File.Delete(path);
126127
}
127128
}
129+
130+
[Fact]
131+
public void AsyncAwait_Issue_1177()
132+
{
133+
string path = Path.GetTempFileName();
134+
try
135+
{
136+
FunctionExecutor.Run(async (string[] pathSerialize) =>
137+
{
138+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Issue_1177>(instance =>
139+
{
140+
((Task)instance.Test()).ConfigureAwait(false).GetAwaiter().GetResult();
141+
return Task.CompletedTask;
142+
},
143+
persistPrepareResultToFile: pathSerialize[0]);
144+
145+
return 0;
146+
}, new string[] { path });
147+
148+
var document = TestInstrumentationHelper.GetCoverageResult(path).Document("Instrumentation.AsyncAwait.cs");
149+
document.AssertLinesCovered(BuildConfiguration.Debug, (133, 1), (134, 1), (135, 1), (136, 1), (137, 1));
150+
Assert.DoesNotContain(document.Branches, x => x.Key.Line == 134);
151+
}
152+
finally
153+
{
154+
File.Delete(path);
155+
}
156+
}
128157
}
129158
}

test/coverlet.core.tests/Samples/Instrumentation.AsyncAwait.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ public interface IService
125125
Task Process(string cat);
126126
}
127127
}
128+
129+
public class Issue_1177
130+
{
131+
async public Task Test()
132+
{
133+
await Task.CompletedTask;
134+
using var _ = new System.IO.MemoryStream();
135+
await Task.CompletedTask;
136+
await Task.CompletedTask;
137+
await Task.CompletedTask;
138+
}
139+
}
128140
}

0 commit comments

Comments
 (0)