forked from lucaslorentz/minicover
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lucaslorentz#30 from havocbcn/master
Added mini-OpenCover report support to integrate SonarQube
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ coverage-html | |
coverage.json | ||
coverage.xml | ||
coverage-hits.txt | ||
opencovercoverage.xml | ||
bin | ||
obj | ||
.vs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using MiniCover.Model; | ||
|
||
namespace MiniCover.Reports | ||
{ | ||
public static class OpenCoverReport | ||
{ | ||
public static void Execute(InstrumentationResult result, string output, float threshold) | ||
{ | ||
var hits = File.Exists(result.HitsFile) | ||
? File.ReadAllLines(result.HitsFile).Select(h => int.Parse(h)).ToArray() | ||
: new int[0]; | ||
int fileIndex = 0; | ||
int sequencePointMegaCounter = 0; | ||
|
||
var data = new XProcessingInstruction("xml-stylesheet", "type='text/xsl' href='coverage.xsl'"); | ||
|
||
var document = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), data); | ||
|
||
var coverageElement = new XElement( | ||
XName.Get("CoverageSession") | ||
); | ||
|
||
var modulesListElement = result.Assemblies.Select(assembly => | ||
{ | ||
var moduleElement = new XElement( | ||
XName.Get("Module"), | ||
new XAttribute(XName.Get("hash"), Guid.NewGuid().ToString()) | ||
); | ||
|
||
var fullNameElement = new XElement( | ||
XName.Get("FullName"), | ||
new XText(assembly.Name) | ||
); | ||
|
||
var moduleNameElement = new XElement( | ||
XName.Get("ModuleName"), | ||
new XText(assembly.Name) | ||
); | ||
|
||
Dictionary<SourceFile, int> dctSourceFileCount = new Dictionary<SourceFile, int>(); | ||
|
||
var filesElement = assembly.SourceFiles.Select(file => | ||
{ | ||
dctSourceFileCount.Add(file.Value, ++fileIndex); | ||
var fileElement = new XElement( | ||
XName.Get("File"), | ||
new XAttribute(XName.Get("uid"), dctSourceFileCount[file.Value]), | ||
new XAttribute(XName.Get("fullPath"), Path.Combine(result.SourcePath, file.Key)) | ||
); | ||
|
||
return fileElement; | ||
}); | ||
|
||
var classesElement = assembly.SourceFiles.Select(file => | ||
{ | ||
var hitInstructions = file.Value.Instructions.Where(h => hits.Contains(h.Id)).ToArray(); | ||
|
||
return file.Value.Instructions | ||
.GroupBy(instruction => new { instruction.Class }) | ||
.Select(classes => | ||
{ | ||
var classElement = new XElement( | ||
XName.Get("Class") | ||
); | ||
|
||
var classfullNameElement = new XElement( | ||
XName.Get("FullName"), | ||
new XText(classes.Key.Class) | ||
); | ||
|
||
var methodsList = classes | ||
.GroupBy(instruction => new { instruction.Method, instruction.MethodFullName }) | ||
.Select(method => | ||
{ | ||
var nameElement = new XElement( | ||
XName.Get("Name"), | ||
new XText(method.Key.MethodFullName) | ||
); | ||
|
||
var fileRefElement = new XElement( | ||
XName.Get("FileRef"), | ||
new XAttribute(XName.Get("uid"), dctSourceFileCount[file.Value]) | ||
); | ||
|
||
int sequencePointMiniCounter = 0; | ||
|
||
var sequencePoints = method.Select(methodPoint => | ||
{ | ||
var hitCount = hitInstructions.Count(hit => hit.Equals(methodPoint)); | ||
|
||
return new XElement( | ||
XName.Get("SequencePoint"), | ||
new XAttribute(XName.Get("vc"), hitCount), | ||
new XAttribute(XName.Get("uspid"), ++sequencePointMegaCounter), | ||
new XAttribute(XName.Get("ordinal"), ++sequencePointMiniCounter), | ||
new XAttribute(XName.Get("sl"), methodPoint.StartLine), | ||
new XAttribute(XName.Get("sc"), methodPoint.StartColumn), | ||
new XAttribute(XName.Get("el"), methodPoint.EndLine), | ||
new XAttribute(XName.Get("ec"), methodPoint.EndColumn) | ||
); | ||
}); | ||
|
||
var methodElement = new XElement( | ||
XName.Get("Method"), | ||
new XAttribute(XName.Get("visited"), method.Any(p => hitInstructions.Any(hit => hit == p))), | ||
new XAttribute(XName.Get("isConstructor"), method.Key.Method == ".ctor") | ||
); | ||
|
||
methodElement.Add(nameElement); | ||
methodElement.Add(fileRefElement); | ||
methodElement.Add(sequencePoints); | ||
|
||
return methodElement; | ||
}); | ||
|
||
classElement.Add(classfullNameElement); | ||
classElement.Add(methodsList); | ||
|
||
return classElement; | ||
}); | ||
}); | ||
|
||
moduleElement.Add(fullNameElement); | ||
moduleElement.Add(moduleNameElement); | ||
moduleElement.Add(filesElement); | ||
moduleElement.Add(classesElement); | ||
|
||
return moduleElement; | ||
}); | ||
|
||
coverageElement.Add(modulesListElement); | ||
|
||
document.Add(coverageElement); | ||
|
||
File.WriteAllText(output, document.ToString()); | ||
} | ||
} | ||
} |