Skip to content

Commit

Permalink
#192, #209: Parsing of invalid Cobertura files
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Feb 13, 2019
1 parent c1fa888 commit 1f4fb6a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ CHANGELOG

4.0.12.0

* Fix: Issue #209: Added warning if Cobertura file is invalid
* Fix: Issue #192, #209: Parsing of invalid Cobertura files

4.0.11.0

Expand Down
8 changes: 0 additions & 8 deletions src/ReportGenerator.Core/Parser/CoberturaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ public override ParserResult Parse(XContainer report)
var modules = report.Descendants("package")
.ToArray();

if (modules.Length == 0)
{
if (report.Descendants("packages").Elements("class").Any())
{
Logger.Error(" " + Resources.ErrorInvalidCoberturaReport);
}
}

var assemblyNames = modules
.Select(m => m.Attribute("name").Value)
.Distinct()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Palmmedia.ReportGenerator.Core.Logging;
using Palmmedia.ReportGenerator.Core.Properties;

namespace Palmmedia.ReportGenerator.Core.Parser.Preprocessing
{
Expand All @@ -9,12 +11,39 @@ namespace Palmmedia.ReportGenerator.Core.Parser.Preprocessing
/// </summary>
internal class CoberturaReportPreprocessor
{
/// <summary>
/// The Logger.
/// </summary>
private static readonly ILogger Logger = LoggerFactory.GetLogger(typeof(CoberturaReportPreprocessor));

/// <summary>
/// Executes the preprocessing of the report.
/// </summary>
/// <param name="report">The report.</param>
internal void Execute(XContainer report)
{
var modules = report.Descendants("package")
.ToArray();

if (modules.Length == 0)
{
if (report.Descendants("packages").Elements("class").Any())
{
Logger.Error(" " + Resources.ErrorInvalidCoberturaReport);

// Fix malformed report files (See issues: #192, #209)
foreach (var packagesElement in report.Descendants("packages").ToArray())
{
packagesElement.Name = "classes";

var parent = packagesElement.Parent;

packagesElement.Remove();
parent.Add(new XElement("packages", new XElement("package", new XAttribute("name", "AutoGenerated"), packagesElement)));
}
}
}

var sources = report.Descendants("sources")
.Elements("source")
.Select(s => s.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,17 @@ private static string GetClassReportFilename(string assemblyName, string classNa

if (!FileNameByClass.TryGetValue(key, out fileName))
{
string shortClassName = className.Substring(className.LastIndexOf('.') + 1);
string shortClassName = null;

if (className.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
{
shortClassName = className.Substring(0, className.LastIndexOf('.'));
}
else
{
shortClassName = className.Substring(className.LastIndexOf('.') + 1);
}

fileName = RendererBase.ReplaceInvalidPathChars(assemblyName + "_" + shortClassName) + ".htm";

if (fileName.Length > 100)
Expand Down

0 comments on commit 1f4fb6a

Please sign in to comment.