Skip to content

Commit

Permalink
create XML coverage file without BOM (#1604)
Browse files Browse the repository at this point in the history
* create XML coverge file without BOM

* incorporate review comments

* nit

---------

Co-authored-by: David Müller <muellerdavid4@gmail.com>
  • Loading branch information
Bertk and daveMueller authored Feb 1, 2024
1 parent 37f6b8c commit 9321d2e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/coverlet.core/Reporters/CoberturaReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ public string Report(CoverageResult result, ISourceRootTranslator sourceRootTran
coverage.Add(packages);
xml.Add(coverage);

var stream = new MemoryStream();
xml.Save(stream);
using var stream = new MemoryStream();
using var streamWriter = new StreamWriter(stream, new UTF8Encoding(false));
xml.Save(streamWriter);

return Encoding.UTF8.GetString(stream.ToArray());
}
Expand Down
5 changes: 3 additions & 2 deletions src/coverlet.core/Reporters/OpenCoverReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ public string Report(CoverageResult result, ISourceRootTranslator sourceRootTran
coverage.Add(modules);
xml.Add(coverage);

var stream = new MemoryStream();
xml.Save(stream);
using var stream = new MemoryStream();
using var streamWriter = new StreamWriter(stream, new UTF8Encoding(false));
xml.Save(streamWriter);

return Encoding.UTF8.GetString(stream.ToArray());
}
Expand Down
28 changes: 24 additions & 4 deletions test/coverlet.core.tests/Reporters/CoberturaReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void TestReport()

Assert.NotEmpty(report);

var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
var doc = XDocument.Load(new StringReader(report));

IEnumerable<XAttribute> matchingRateAttributes = doc.Descendants().Attributes().Where(attr => attr.Name.LocalName.EndsWith("-rate"));
IEnumerable<string> rateParentNodeNames = matchingRateAttributes.Select(attr => attr.Parent.Name.LocalName);
Expand Down Expand Up @@ -101,6 +101,24 @@ public void TestReport()
}
}

[Fact]
public void CoberturaTestReportDoesNotContainBom()
{
var result = new CoverageResult { Parameters = new CoverageParameters(), Identifier = Guid.NewGuid().ToString() };
var documents = new Documents();
var classes = new Classes { { "Class", new Methods() } };

documents.Add(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\doc.cs" : @"/doc.cs", classes);

result.Modules = new Modules { { "Module", documents } };

var reporter = new CoberturaReporter();
string report = reporter.Report(result, new Mock<ISourceRootTranslator>().Object);

byte[] preamble = Encoding.UTF8.GetBytes(report)[..3];
Assert.NotEqual(Encoding.UTF8.GetPreamble(), preamble);
}

[Theory]
[InlineData(
"Test.SearchRequest::pb::Google.Protobuf.IMessage.get_Descriptor()",
Expand Down Expand Up @@ -151,7 +169,8 @@ public void TestEnsureParseMethodStringCorrectly(

Assert.NotEmpty(report);

var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
var doc = XDocument.Load(new StringReader(report));

var methodAttrs = doc.Descendants()
.Where(o => o.Name.LocalName == "method")
.Attributes()
Expand Down Expand Up @@ -220,7 +239,7 @@ public void TestReportWithDifferentDirectories()
var reporter = new CoberturaReporter();
string report = reporter.Report(result, new Mock<ISourceRootTranslator>().Object);

var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
var doc = XDocument.Load(new StringReader(report));

var basePaths = doc.Element("coverage").Element("sources").Elements().Select(e => e.Value).ToList();
var relativePaths = doc.Element("coverage").Element("packages").Element("package")
Expand Down Expand Up @@ -262,7 +281,8 @@ public void TestReportWithSourcelinkPaths()
var reporter = new CoberturaReporter();
string report = reporter.Report(result, new Mock<ISourceRootTranslator>().Object);

var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
var doc = XDocument.Load(new StringReader(report));

string fileName = doc.Element("coverage").Element("packages").Element("package").Element("classes").Elements()
.Select(e => e.Attribute("filename").Value).Single();

Expand Down
18 changes: 17 additions & 1 deletion test/coverlet.core.tests/Reporters/OpenCoverReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestReport()
var reporter = new OpenCoverReporter();
string report = reporter.Report(result, new Mock<ISourceRootTranslator>().Object);
Assert.NotEmpty(report);
var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
var doc = XDocument.Load(new StringReader(report));
Assert.Empty(doc.Descendants().Attributes("sequenceCoverage").Where(v => v.Value != "33.33"));
Assert.Empty(doc.Descendants().Attributes("branchCoverage").Where(v => v.Value != "25"));
Assert.Empty(doc.Descendants().Attributes("nPathComplexity").Where(v => v.Value != "4"));
Expand Down Expand Up @@ -77,6 +77,22 @@ public void TestLineBranchCoverage()
Assert.Contains(@"<SequencePoint vc=""1"" uspid=""4"" ordinal=""3"" sl=""4"" sc=""1"" el=""4"" ec=""2"" bec=""3"" bev=""2"" fileid=""1"" />", xml);
}

[Fact]
public void OpenCoverTestReportDoesNotContainBom()
{
var result = new CoverageResult
{
Identifier = Guid.NewGuid().ToString(),
Modules = new Modules { { "Coverlet.Core.Reporters.Tests", CreateBranchCoverageDocuments() } },
Parameters = new CoverageParameters()
};

string report = new OpenCoverReporter().Report(result, new Mock<ISourceRootTranslator>().Object);

byte[] preamble = Encoding.UTF8.GetBytes(report)[..3];
Assert.NotEqual(Encoding.UTF8.GetPreamble(), preamble);
}

private static Documents CreateFirstDocuments()
{
var lines = new Lines();
Expand Down

0 comments on commit 9321d2e

Please sign in to comment.