Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 15 additions & 3 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public CoverageResult GetCoverageResult()
if (methods.TryGetValue(branch.Method, out Method method))
{
method.Branches.Add(new BranchInfo
{ Line = branch.Number, Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal, IsAsyncStateMachineBranch = branch.IsAsyncStateMachineBranch }
{ Line = branch.Number, Hits = branch.Hits, Offset = branch.Offset, EndOffset = branch.EndOffset, Path = branch.Path, Ordinal = branch.Ordinal }
);
}
else
Expand Down Expand Up @@ -168,7 +168,7 @@ public CoverageResult GetCoverageResult()
InstrumentationHelper.RestoreOriginalModule(result.ModulePath, _identifier);
}

var coverageResult = new CoverageResult { Identifier = _identifier, Modules = modules };
var coverageResult = new CoverageResult { Identifier = _identifier, Modules = modules, InstrumentedResults = _results };

if (!string.IsNullOrEmpty(_mergeWith) && !string.IsNullOrWhiteSpace(_mergeWith) && File.Exists(_mergeWith))
{
Expand Down Expand Up @@ -239,7 +239,7 @@ private void CalculateCoverage()
foreach (var branch in document.Value.Branches)
{
//if one branch is covered we search the other one only if it's not covered
if (branch.Value.IsAsyncStateMachineBranch && branch.Value.Method.EndsWith("::MoveNext()") && branch.Value.Hits > 0)
if (IsAsyncStateMachineMethod(branch.Value.Method) && branch.Value.Hits > 0)
{
foreach (var moveNextBranch in document.Value.Branches)
{
Expand All @@ -260,6 +260,18 @@ private void CalculateCoverage()
}
}

private bool IsAsyncStateMachineMethod(string method)
{
foreach (var instrumentationResult in _results)
{
if (instrumentationResult.AsyncMachineStateMethod.Contains(method))
{
return true;
}
}
return false;
}

private string GetSourceLinkUrl(Dictionary<string, string> sourceLinkDocuments, string document)
{
if (sourceLinkDocuments.TryGetValue(document, out string url))
Expand Down
17 changes: 15 additions & 2 deletions src/coverlet.core/CoverageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using Coverlet.Core.Enums;
using Coverlet.Core.Instrumentation;
using Coverlet.Core.Symbols;

namespace Coverlet.Core
Expand All @@ -15,7 +16,6 @@ public class BranchInfo
public int Path { get; set; }
public uint Ordinal { get; set; }
public int Hits { get; set; }
public bool IsAsyncStateMachineBranch { get; set; }
}

public class Lines : SortedDictionary<int, int> { }
Expand All @@ -41,6 +41,7 @@ public class CoverageResult
{
public string Identifier;
public Modules Modules;
internal List<InstrumenterResult> InstrumentedResults;

internal CoverageResult() { }

Expand Down Expand Up @@ -121,7 +122,7 @@ internal void Merge(Modules modules)
{
foreach (var branch in method.Value.Branches)
{
if (branch.IsAsyncStateMachineBranch && method.Key.EndsWith("::MoveNext()") && branch.Hits > 0)
if (IsAsyncStateMachineMethod(method.Key))
{
foreach (var moveNextBranch in method.Value.Branches)
{
Expand All @@ -142,6 +143,18 @@ internal void Merge(Modules modules)
}
}

private bool IsAsyncStateMachineMethod(string method)
{
foreach (var instrumentedResult in InstrumentedResults)
{
if (instrumentedResult.AsyncMachineStateMethod.Contains(method))
{
return true;
}
}
return false;
}

public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summary, double threshold, ThresholdTypeFlags thresholdTypes, ThresholdStatistic thresholdStat)
{
var thresholdTypeFlags = ThresholdTypeFlags.None;
Expand Down
21 changes: 19 additions & 2 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal class Instrumenter
private ILProcessor _customTrackerClassConstructorIl;
private TypeDefinition _customTrackerTypeDef;
private MethodReference _customTrackerRecordHitMethod;
private List<string> _asyncMachineStateMethod;

public Instrumenter(string module, string identifier, string[] excludeFilters, string[] includeFilters, string[] excludedFiles, string[] excludedAttributes)
{
Expand Down Expand Up @@ -59,6 +60,8 @@ public InstrumenterResult Instrument()

InstrumentModule();

_result.AsyncMachineStateMethod = _asyncMachineStateMethod == null ? Array.Empty<string>() : _asyncMachineStateMethod.ToArray();

return _result;
}

Expand Down Expand Up @@ -326,6 +329,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor

var key = (branchPoint.StartLine, (int)branchPoint.Ordinal);
if (!document.Branches.ContainsKey(key))
{
document.Branches.Add(key,
new Branch
{
Expand All @@ -335,11 +339,24 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
Offset = branchPoint.Offset,
EndOffset = branchPoint.EndOffset,
Path = branchPoint.Path,
Ordinal = branchPoint.Ordinal,
IsAsyncStateMachineBranch = IsAsyncStateMachineBranch(method.DeclaringType, method)
Ordinal = branchPoint.Ordinal
}
);

if (IsAsyncStateMachineBranch(method.DeclaringType, method))
{
if (_asyncMachineStateMethod == null)
{
_asyncMachineStateMethod = new List<string>();
}

if (!_asyncMachineStateMethod.Contains(method.FullName))
{
_asyncMachineStateMethod.Add(method.FullName);
}
}
}

var entry = (true, document.Index, branchPoint.StartLine, (int)branchPoint.Ordinal);
_result.HitCandidates.Add(entry);

Expand Down
2 changes: 1 addition & 1 deletion src/coverlet.core/Instrumentation/InstrumenterResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal class Branch : Line
public int EndOffset;
public int Path;
public uint Ordinal;
public bool IsAsyncStateMachineBranch;
}

internal class Document
Expand All @@ -42,6 +41,7 @@ public InstrumenterResult()
}

public string Module;
public string[] AsyncMachineStateMethod;
public string HitsFilePath;
public string ModulePath;
public string SourceLink;
Expand Down