-
Notifications
You must be signed in to change notification settings - Fork 382
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 #41 from tonerdo/corbetura-support
Add preliminary Cobertura support
- Loading branch information
Showing
16 changed files
with
211 additions
and
40 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
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,106 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml; | ||
|
||
namespace Coverlet.Core.Reporters | ||
{ | ||
public class CoberturaReporter : IReporter | ||
{ | ||
public string Format => "cobertura"; | ||
|
||
public string Extension => "xml"; | ||
|
||
public string Report(CoverageResult result) | ||
{ | ||
XmlDocument xml = new XmlDocument(); | ||
XmlElement coverage = xml.CreateElement("coverage"); | ||
coverage.SetAttribute("line-rate", "0"); | ||
coverage.SetAttribute("branch-rate", "0"); | ||
coverage.SetAttribute("version", "1.9"); | ||
coverage.SetAttribute("timestamp", "0"); | ||
|
||
XmlElement sources = xml.CreateElement("sources"); | ||
foreach (var src in GetSources(result.Modules)) | ||
{ | ||
XmlElement source = xml.CreateElement("source"); | ||
source.AppendChild(xml.CreateTextNode(src)); | ||
sources.AppendChild(source); | ||
} | ||
|
||
XmlElement packages = xml.CreateElement("packages"); | ||
foreach (var module in result.Modules) | ||
{ | ||
XmlElement package = xml.CreateElement("package"); | ||
package.SetAttribute("line-rate", "0"); | ||
package.SetAttribute("branch-rate", "0"); | ||
package.SetAttribute("complexity", "0"); | ||
|
||
XmlElement classes = xml.CreateElement("classes"); | ||
foreach (var document in module.Value) | ||
{ | ||
foreach (var cls in document.Value) | ||
{ | ||
XmlElement @class = xml.CreateElement("class"); | ||
@class.SetAttribute("name", cls.Key); | ||
@class.SetAttribute("filename", Path.GetFileName(document.Key)); | ||
@class.SetAttribute("line-rate", "0"); | ||
@class.SetAttribute("branch-rate", "0"); | ||
@class.SetAttribute("complexity", "0"); | ||
|
||
XmlElement methods = xml.CreateElement("methods"); | ||
foreach (var meth in cls.Value) | ||
{ | ||
XmlElement method = xml.CreateElement("method"); | ||
method.SetAttribute("name", meth.Key.Split(':')[2].Split('(')[0]); | ||
method.SetAttribute("signature", meth.Key); | ||
method.SetAttribute("line-rate", "0"); | ||
method.SetAttribute("branch-rate", "0"); | ||
|
||
XmlElement lines = xml.CreateElement("lines"); | ||
foreach (var ln in meth.Value) | ||
{ | ||
XmlElement line = xml.CreateElement("line"); | ||
line.SetAttribute("number", ln.Key.ToString()); | ||
line.SetAttribute("hits", ln.Value.Hits.ToString()); | ||
line.SetAttribute("branch", ln.Value.IsBranchPoint.ToString()); | ||
|
||
lines.AppendChild(line); | ||
} | ||
|
||
method.AppendChild(lines); | ||
methods.AppendChild(method); | ||
} | ||
|
||
@class.AppendChild(methods); | ||
classes.AppendChild(@class); | ||
} | ||
} | ||
|
||
package.AppendChild(classes); | ||
packages.AppendChild(package); | ||
} | ||
|
||
coverage.AppendChild(sources); | ||
coverage.AppendChild(packages); | ||
xml.AppendChild(coverage); | ||
|
||
StringWriter writer = new StringWriter(); | ||
xml.Save(writer); | ||
|
||
return writer.ToString(); | ||
} | ||
|
||
private string[] GetSources(Modules modules) | ||
{ | ||
List<string> sources = new List<string>(); | ||
foreach (var module in modules) | ||
{ | ||
sources.AddRange( | ||
module.Value.Select(d => Path.GetDirectoryName(d.Key))); | ||
} | ||
|
||
return sources.Distinct().ToArray(); | ||
} | ||
} | ||
} |
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
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,22 @@ | ||
using System.Linq; | ||
|
||
namespace Coverlet.Core.Reporters | ||
{ | ||
public class ReporterFactory | ||
{ | ||
private string _format; | ||
private IReporter[] _reporters; | ||
|
||
public ReporterFactory(string format) | ||
{ | ||
_format = format; | ||
_reporters = new IReporter[] { | ||
new JsonReporter(), new LcovReporter(), | ||
new OpenCoverReporter(), new CoberturaReporter() | ||
}; | ||
} | ||
|
||
public IReporter CreateReporter() | ||
=> _reporters.FirstOrDefault(r => r.Format == _format); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
|
||
<PropertyGroup> | ||
|
||
<CoverletOutputFormat Condition="$(CoverletOutputFormat) == ''">json</CoverletOutputFormat> | ||
<CoverletOutputDirectory Condition="$(CoverletOutputDirectory) == ''">$(MSBuildProjectDirectory)</CoverletOutputDirectory> | ||
<Threshold Condition="$(Threshold) == ''">0</Threshold> | ||
<CoverletOutputExtension Condition="$(CoverletOutputFormat) == 'lcov'">.info</CoverletOutputExtension> | ||
<CoverletOutputExtension Condition="$(CoverletOutputFormat) == 'opencover'">.xml</CoverletOutputExtension> | ||
<CoverletOutputExtension Condition="$(CoverletOutputFormat) != 'lcov' and $(CoverletOutputFormat) != 'opencover'">.json</CoverletOutputExtension> | ||
<CoverletOutput>$(CoverletOutputDirectory)\coverage</CoverletOutput> | ||
|
||
<CoverletOutput>$(CoverletOutputDirectory)\coverage$(CoverletOutputExtension)</CoverletOutput> | ||
<Threshold Condition="$(Threshold) == ''">0</Threshold> | ||
<CollectCoverage Condition="$(CollectCoverage) == ''">false</CollectCoverage> | ||
|
||
</PropertyGroup> | ||
|
||
</Project> |
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
29 changes: 29 additions & 0 deletions
29
test/coverlet.core.tests/Reporters/CoberturaReporterTests.cs
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,29 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Coverlet.Core.Reporters.Tests | ||
{ | ||
public class CoberturaReporterTests | ||
{ | ||
[Fact] | ||
public void TestFormat() | ||
{ | ||
CoverageResult result = new CoverageResult(); | ||
result.Identifier = Guid.NewGuid().ToString(); | ||
Lines lines = new Lines(); | ||
lines.Add(1, new LineInfo { Hits = 1 }); | ||
lines.Add(2, new LineInfo { Hits = 0 }); | ||
Methods methods = new Methods(); | ||
methods.Add("System.Void Coverlet.Core.Reporters.Tests.CoberturaReporterTests::TestFormat()", lines); | ||
Classes classes = new Classes(); | ||
classes.Add("Coverlet.Core.Reporters.Tests.CoberturaReporterTests", methods); | ||
Documents documents = new Documents(); | ||
documents.Add("doc.cs", classes); | ||
result.Modules = new Modules(); | ||
result.Modules.Add("module", documents); | ||
|
||
CoberturaReporter reporter = new CoberturaReporter(); | ||
Assert.NotEqual(string.Empty, reporter.Report(result)); | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
test/coverlet.core.tests/Reporters/ReporterFactoryTests.cs
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,18 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Coverlet.Core.Reporters.Tests | ||
{ | ||
public class ReporterFactoryTests | ||
{ | ||
[Fact] | ||
public void TestCreateReporter() | ||
{ | ||
Assert.Equal(typeof(JsonReporter), new ReporterFactory("json").CreateReporter().GetType()); | ||
Assert.Equal(typeof(LcovReporter), new ReporterFactory("lcov").CreateReporter().GetType()); | ||
Assert.Equal(typeof(OpenCoverReporter), new ReporterFactory("opencover").CreateReporter().GetType()); | ||
Assert.Equal(typeof(CoberturaReporter), new ReporterFactory("cobertura").CreateReporter().GetType()); | ||
Assert.Null(new ReporterFactory("").CreateReporter()); | ||
} | ||
} | ||
} |