Skip to content

Commit

Permalink
Merge pull request coverlet-coverage#169 from tonerdo/fix-badimagefor…
Browse files Browse the repository at this point in the history
…matexception

Fix BadImageFormatException
  • Loading branch information
tonerdo committed Aug 11, 2018
2 parents cdb802b + 3b0f0d9 commit eb59ae3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/coverlet.core/Coverage.cs
Expand Up @@ -173,11 +173,7 @@ private void CalculateCoverage()
continue;

bool isBranch = info[0] == "B";

if (!result.Documents.TryGetValue(info[1], out var document))
{
continue;
}
var document = result.Documents.ElementAt(int.Parse(info[1])).Value;

int start = int.Parse(info[2]);
int hits = int.Parse(info[4]);
Expand Down
16 changes: 14 additions & 2 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Expand Up @@ -175,19 +175,25 @@ private void InstrumentIL(MethodDefinition method)

private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor processor, Instruction instruction, SequencePoint sequencePoint)
{
int documentIndex = 0;
if (!_result.Documents.TryGetValue(sequencePoint.Document.Url, out var document))
{
document = new Document { Path = sequencePoint.Document.Url };
documentIndex = _result.Documents.Count;
_result.Documents.Add(document.Path, document);
}
else
{
documentIndex = _result.Documents.Keys.ToList().IndexOf(document.Path);
}

for (int i = sequencePoint.StartLine; i <= sequencePoint.EndLine; i++)
{
if (!document.Lines.ContainsKey(i))
document.Lines.Add(i, new Line { Number = i, Class = method.DeclaringType.FullName, Method = method.FullName });
}

string marker = $"L,{document.Path},{sequencePoint.StartLine},{sequencePoint.EndLine}";
string marker = $"L,{documentIndex},{sequencePoint.StartLine},{sequencePoint.EndLine}";

var pathInstr = Instruction.Create(OpCodes.Ldstr, _result.HitsFilePath);
var markInstr = Instruction.Create(OpCodes.Ldstr, marker);
Expand All @@ -202,11 +208,17 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor

private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor processor, Instruction instruction, BranchPoint branchPoint)
{
int documentIndex = 0;
if (!_result.Documents.TryGetValue(branchPoint.Document, out var document))
{
document = new Document { Path = branchPoint.Document };
documentIndex = _result.Documents.Count;
_result.Documents.Add(document.Path, document);
}
else
{
documentIndex = _result.Documents.Keys.ToList().IndexOf(document.Path);
}

var key = (branchPoint.StartLine, (int)branchPoint.Ordinal);
if (!document.Branches.ContainsKey(key))
Expand All @@ -223,7 +235,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
}
);

string marker = $"B,{document.Path},{branchPoint.StartLine},{branchPoint.Ordinal}";
string marker = $"B,{documentIndex},{branchPoint.StartLine},{branchPoint.Ordinal}";

var pathInstr = Instruction.Create(OpCodes.Ldstr, _result.HitsFilePath);
var markInstr = Instruction.Create(OpCodes.Ldstr, marker);
Expand Down

0 comments on commit eb59ae3

Please sign in to comment.