From ea18aced1a72a2ccb333444c3f77c75c83f1ca59 Mon Sep 17 00:00:00 2001 From: Daniel Palme Date: Sat, 9 Nov 2024 16:29:18 +0100 Subject: [PATCH] #704 Improved handling of file filters (unified behavior for all report formats) --- .../Reporting/Builders/CloverReportBuilder.cs | 8 ++++++-- .../Builders/CoberturaReportBuilder.cs | 6 +++++- .../Builders/CodeClimateReportBuilder.cs | 7 ++++++- .../Builders/CsvSummaryReportBuilder.cs | 13 ++++++++----- .../Builders/HtmlReportBuilderBase.cs | 16 ++++++++++------ .../Builders/JsonSummaryReportBuilder.cs | 12 ++++++++---- .../Builders/LCovSummaryReportBuilder.cs | 8 ++++++-- .../Builders/LatexReportBuilderBase.cs | 14 +++++++++----- .../MarkdownAssembliesSummaryReportBuilder.cs | 8 ++++++-- .../MarkdownSummaryGithubReportBuilder.cs | 14 +++++++++----- .../Builders/MarkdownSummaryReportBuilder.cs | 14 +++++++++----- .../Builders/OpenCoverReportBuilder.cs | 18 +++++++++++------- .../Builders/TextSummaryReportBuilder.cs | 18 +++++++++++------- .../Builders/XmlSummaryReportBuilder.cs | 12 ++++++++---- 14 files changed, 112 insertions(+), 56 deletions(-) diff --git a/src/ReportGenerator.Core/Reporting/Builders/CloverReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/CloverReportBuilder.cs index 4dc45527..a948532e 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/CloverReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/CloverReportBuilder.cs @@ -246,7 +246,11 @@ public void CreateSummaryReport(SummaryResult summaryResult) int totalProjectNcloc = 0; int totalProjectFiles = 0; - foreach (var assembly in summaryResult.Assemblies) + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + + foreach (var assembly in assembliesWithClasses) { if (this.packageElementsByName.TryGetValue(assembly.Name, out XElement packageElement)) { @@ -356,7 +360,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) new XAttribute("loc", totalProjectLoc.ToString(CultureInfo.InvariantCulture)), new XAttribute("ncloc", totalProjectNcloc.ToString(CultureInfo.InvariantCulture)), new XAttribute("files", totalProjectFiles.ToString(CultureInfo.InvariantCulture)), - new XAttribute("packages", summaryResult.Assemblies.Count.ToString(CultureInfo.InvariantCulture)))); + new XAttribute("packages", assembliesWithClasses.Length.ToString(CultureInfo.InvariantCulture)))); var testProjectElement = new XElement( "testproject", diff --git a/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs index 2cc8842a..c840efd1 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/CoberturaReportBuilder.cs @@ -150,7 +150,11 @@ public void CreateSummaryReport(SummaryResult summaryResult) { decimal? summaryComplexity = null; - foreach (var assembly in summaryResult.Assemblies) + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + + foreach (var assembly in assembliesWithClasses) { decimal? assemblyComplexity = null; if (this.packageElementsByName.TryGetValue(assembly.Name, out XElement packageElement)) diff --git a/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs index a270d170..2bdfd7e1 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Text; using System.Text.Json; using Palmmedia.ReportGenerator.Core.Common; @@ -120,7 +121,11 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(" \"source_files\": ["); - foreach (var assembly in summaryResult.Assemblies) + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + + foreach (var assembly in assembliesWithClasses) { foreach (var clazz in assembly.Classes) { diff --git a/src/ReportGenerator.Core/Reporting/Builders/CsvSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/CsvSummaryReportBuilder.cs index 480acf1e..62aa7e6e 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/CsvSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/CsvSummaryReportBuilder.cs @@ -3,7 +3,6 @@ using System.Globalization; using System.IO; using System.Linq; -using System.Text; using Palmmedia.ReportGenerator.Core.Common; using Palmmedia.ReportGenerator.Core.Logging; using Palmmedia.ReportGenerator.Core.Parser.Analysis; @@ -83,6 +82,10 @@ public void CreateSummaryReport(SummaryResult summaryResult) using (var reportTextWriter = File.CreateText(targetPath)) { + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + reportTextWriter.WriteLine(ReportResources.Summary); reportTextWriter.WriteLine( "{0};{1}", @@ -95,15 +98,15 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine( "{0};{1}", ReportResources.Assemblies2, - summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)); + assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine( "{0};{1}", ReportResources.Classes, - summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); + assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine( "{0};{1}", ReportResources.Files2, - summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); + assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine( "{0};{1}", ReportResources.Coverage2, @@ -125,7 +128,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) ReportResources.TotalLines, summaryResult.TotalLines.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { reportTextWriter.WriteLine(); reportTextWriter.WriteLine( diff --git a/src/ReportGenerator.Core/Reporting/Builders/HtmlReportBuilderBase.cs b/src/ReportGenerator.Core/Reporting/Builders/HtmlReportBuilderBase.cs index 08548072..2140fe2b 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/HtmlReportBuilderBase.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/HtmlReportBuilderBase.cs @@ -238,12 +238,16 @@ public virtual void CreateSummaryReport(IHtmlRenderer reportRenderer, SummaryRes reportRenderer.BeginSummaryReport(this.CreateTargetDirectory(), null, title); reportRenderer.HeaderWithGithubLinks(title); + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + var infoCardItems = new List() { new CardLineItem(ReportResources.Parser, summaryResult.UsedParser, null, CardLineItemAlignment.Left), - new CardLineItem(ReportResources.Assemblies2, summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture), null), - new CardLineItem(ReportResources.Classes, summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture), null), - new CardLineItem(ReportResources.Files2, summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture), null) + new CardLineItem(ReportResources.Assemblies2, assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture), null), + new CardLineItem(ReportResources.Classes, assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture), null), + new CardLineItem(ReportResources.Files2, assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture), null) }; if (this.ReportContext.ReportConfiguration.Tag != null) @@ -356,12 +360,12 @@ public virtual void CreateSummaryReport(IHtmlRenderer reportRenderer, SummaryRes reportRenderer.Header(ReportResources.Coverage3); - if (summaryResult.Assemblies.Any()) + if (assembliesWithClasses.Any()) { reportRenderer.BeginSummaryTable(); reportRenderer.BeginSummaryTable(summaryResult.SupportsBranchCoverage, proVersion); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { reportRenderer.SummaryAssembly(assembly, summaryResult.SupportsBranchCoverage, proVersion); @@ -384,7 +388,7 @@ public virtual void CreateSummaryReport(IHtmlRenderer reportRenderer, SummaryRes } reportRenderer.CustomSummary( - summaryResult.Assemblies, + assembliesWithClasses, this.ReportContext.RiskHotspotAnalysisResult.RiskHotspots, summaryResult.SupportsBranchCoverage, proVersion); diff --git a/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs index 8ffed037..5e0045c0 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs @@ -79,13 +79,17 @@ public void CreateSummaryReport(SummaryResult summaryResult) using (var reportTextWriter = new StreamWriter(new FileStream(targetPath, FileMode.Create), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false))) { + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + reportTextWriter.WriteLine("{"); reportTextWriter.WriteLine(" \"summary\": {"); reportTextWriter.WriteLine($" \"generatedon\": \"{DateTime.Now.ToUniversalTime().ToString("s")}Z\","); reportTextWriter.WriteLine($" \"parser\": \"{summaryResult.UsedParser}\","); - reportTextWriter.WriteLine($" \"assemblies\": {summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)},"); - reportTextWriter.WriteLine($" \"classes\": {summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)},"); - reportTextWriter.WriteLine($" \"files\": {summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)},"); + reportTextWriter.WriteLine($" \"assemblies\": {assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture)},"); + reportTextWriter.WriteLine($" \"classes\": {assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)},"); + reportTextWriter.WriteLine($" \"files\": {assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)},"); reportTextWriter.WriteLine($" \"coveredlines\": {summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)},"); reportTextWriter.WriteLine($" \"uncoveredlines\": {(summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture)},"); reportTextWriter.WriteLine($" \"coverablelines\": {summaryResult.CoverableLines.ToString(CultureInfo.InvariantCulture)},"); @@ -161,7 +165,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) int assemblyCounter = 0; - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { if (assemblyCounter > 0) { diff --git a/src/ReportGenerator.Core/Reporting/Builders/LCovSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/LCovSummaryReportBuilder.cs index 7f69a276..990a7365 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/LCovSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/LCovSummaryReportBuilder.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Text; +using System.Linq; using Palmmedia.ReportGenerator.Core.Common; using Palmmedia.ReportGenerator.Core.Logging; using Palmmedia.ReportGenerator.Core.Parser.Analysis; @@ -85,7 +85,11 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine("TN:"); long branchCounter = 0; - foreach (var assembly in summaryResult.Assemblies) + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + + foreach (var assembly in assembliesWithClasses) { foreach (var @class in assembly.Classes) { diff --git a/src/ReportGenerator.Core/Reporting/Builders/LatexReportBuilderBase.cs b/src/ReportGenerator.Core/Reporting/Builders/LatexReportBuilderBase.cs index 6edb3f8d..51bd3522 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/LatexReportBuilderBase.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/LatexReportBuilderBase.cs @@ -191,10 +191,14 @@ public virtual void CreateSummaryReport(ILatexRenderer reportRenderer, SummaryRe reportRenderer.KeyValueRow(ReportResources.CoverageDate, summaryResult.CoverageDate()); } + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + reportRenderer.KeyValueRow(ReportResources.Parser, summaryResult.UsedParser); - reportRenderer.KeyValueRow(ReportResources.Assemblies2, summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)); - reportRenderer.KeyValueRow(ReportResources.Classes, summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); - reportRenderer.KeyValueRow(ReportResources.Files2, summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); + reportRenderer.KeyValueRow(ReportResources.Assemblies2, assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture)); + reportRenderer.KeyValueRow(ReportResources.Classes, assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); + reportRenderer.KeyValueRow(ReportResources.Files2, assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); reportRenderer.KeyValueRow(ReportResources.CoveredLines, summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)); reportRenderer.KeyValueRow(ReportResources.UncoveredLines, (summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture)); reportRenderer.KeyValueRow(ReportResources.CoverableLines, summaryResult.CoverableLines.ToString(CultureInfo.InvariantCulture)); @@ -252,11 +256,11 @@ public virtual void CreateSummaryReport(ILatexRenderer reportRenderer, SummaryRe reportRenderer.Header(ReportResources.Coverage3); - if (summaryResult.Assemblies.Any()) + if (assembliesWithClasses.Any()) { reportRenderer.BeginSummaryTable(summaryResult.SupportsBranchCoverage, true); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { reportRenderer.SummaryAssembly(assembly, summaryResult.SupportsBranchCoverage, true); diff --git a/src/ReportGenerator.Core/Reporting/Builders/MarkdownAssembliesSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/MarkdownAssembliesSummaryReportBuilder.cs index ece70dfb..c57f0ccf 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/MarkdownAssembliesSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/MarkdownAssembliesSummaryReportBuilder.cs @@ -111,7 +111,11 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(); reportTextWriter.WriteLine(); - if (summaryResult.Assemblies.Any()) + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + + if (assembliesWithClasses.Any()) { reportTextWriter.Write( "|**{0}**|**{1}**|", @@ -148,7 +152,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { reportTextWriter.Write("|**{0}**", assembly.Name); reportTextWriter.Write("|**{0}**", assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) + "%" : string.Empty); diff --git a/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryGithubReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryGithubReportBuilder.cs index 6171d519..3ba616cd 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryGithubReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryGithubReportBuilder.cs @@ -86,6 +86,10 @@ public void CreateSummaryReport(SummaryResult summaryResult) using (var reportTextWriter = File.CreateText(targetPath)) { + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + if (this.ReportContext.ReportConfiguration.Title != null) { reportTextWriter.WriteLine("# {0} - {1}", ReportResources.Summary, this.ReportContext.ReportConfiguration.Title); @@ -111,9 +115,9 @@ public void CreateSummaryReport(SummaryResult summaryResult) } reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Parser, summaryResult.UsedParser); - reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Assemblies2, summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)); - reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Classes, summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); - reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Files2, summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Assemblies2, assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Classes, assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Files2, assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine("| **{0}** | {1} |", ReportResources.Coverage2, summaryResult.CoverageQuota.HasValue ? $"{summaryResult.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture)}% ({summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)} {ReportResources.Of} {summaryResult.CoverableLines.ToString(CultureInfo.InvariantCulture)})" : string.Empty); reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.CoveredLines, summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.UncoveredLines, (summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture)); @@ -158,9 +162,9 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(); reportTextWriter.WriteLine("## {0}", ReportResources.Coverage3); - if (summaryResult.Assemblies.Any()) + if (assembliesWithClasses.Any()) { - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { reportTextWriter.WriteLine( "
{0} - {1}", diff --git a/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryReportBuilder.cs index 5d3f55ee..7ba804b2 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/MarkdownSummaryReportBuilder.cs @@ -85,6 +85,10 @@ public void CreateSummaryReport(SummaryResult summaryResult) using (var reportTextWriter = File.CreateText(targetPath)) { + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + if (this.ReportContext.ReportConfiguration.Title != null) { reportTextWriter.WriteLine("# {0} - {1}", ReportResources.Summary, this.ReportContext.ReportConfiguration.Title); @@ -104,9 +108,9 @@ public void CreateSummaryReport(SummaryResult summaryResult) } reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Parser, summaryResult.UsedParser); - reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Assemblies2, summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)); - reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Classes, summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); - reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Files2, summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Assemblies2, assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Classes, assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Files2, assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine("| **{0}** | {1} |", ReportResources.Coverage2, summaryResult.CoverageQuota.HasValue ? $"{summaryResult.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture)}% ({summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)} {ReportResources.Of} {summaryResult.CoverableLines.ToString(CultureInfo.InvariantCulture)})" : string.Empty); reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.CoveredLines, summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.UncoveredLines, (summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture)); @@ -147,7 +151,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(); - if (summaryResult.Assemblies.Any()) + if (assembliesWithClasses.Any()) { reportTextWriter.Write( "|**{0}**|**{1}**|**{2}**|**{3}**|**{4}**|**{5}**|", @@ -192,7 +196,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { reportTextWriter.Write("|**{0}**", assembly.Name); reportTextWriter.Write("|**{0}**", assembly.CoveredLines); diff --git a/src/ReportGenerator.Core/Reporting/Builders/OpenCoverReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/OpenCoverReportBuilder.cs index c88109c8..519a3973 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/OpenCoverReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/OpenCoverReportBuilder.cs @@ -249,6 +249,10 @@ public void CreateSummaryReport(SummaryResult summaryResult) return; } + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + var rootElement = new XElement( "CoverageSession", new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"), @@ -263,19 +267,19 @@ public void CreateSummaryReport(SummaryResult summaryResult) new XAttribute("visitedBranchPoints", summaryResult.CoveredBranches.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)), new XAttribute("sequenceCoverage", summaryResult.CoverageQuota.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)), new XAttribute("branchCoverage", summaryResult.BranchCoverageQuota.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)), - new XAttribute("maxCyclomaticComplexity", summaryResult.Assemblies.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CyclomaticComplexity).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Max().ToString(CultureInfo.InvariantCulture)), - new XAttribute("minCyclomaticComplexity", summaryResult.Assemblies.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CyclomaticComplexity).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Min().ToString(CultureInfo.InvariantCulture)), - new XAttribute("maxCrapScore", summaryResult.Assemblies.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CrapScore).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Max().ToString(CultureInfo.InvariantCulture)), - new XAttribute("minCrapScore", summaryResult.Assemblies.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CrapScore).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Min().ToString(CultureInfo.InvariantCulture)), - new XAttribute("visitedClasses", summaryResult.Assemblies.SelectMany(a => a.Classes).Count(c => c.CoverageQuota.GetValueOrDefault() > 0).ToString(CultureInfo.InvariantCulture)), - new XAttribute("numClasses", summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)), + new XAttribute("maxCyclomaticComplexity", assembliesWithClasses.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CyclomaticComplexity).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Max().ToString(CultureInfo.InvariantCulture)), + new XAttribute("minCyclomaticComplexity", assembliesWithClasses.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CyclomaticComplexity).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Min().ToString(CultureInfo.InvariantCulture)), + new XAttribute("maxCrapScore", assembliesWithClasses.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CrapScore).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Max().ToString(CultureInfo.InvariantCulture)), + new XAttribute("minCrapScore", assembliesWithClasses.SelectMany(f => f.Classes).SelectMany(c => c.Files).SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics).Where(m => m.Name == ReportResources.CrapScore).Select(m => m.Value.GetValueOrDefault()).DefaultIfEmpty().Min().ToString(CultureInfo.InvariantCulture)), + new XAttribute("visitedClasses", assembliesWithClasses.SelectMany(a => a.Classes).Count(c => c.CoverageQuota.GetValueOrDefault() > 0).ToString(CultureInfo.InvariantCulture)), + new XAttribute("numClasses", assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)), new XAttribute("visitedMethods", summaryResult.CoveredCodeElements.ToString(CultureInfo.InvariantCulture)), new XAttribute("numMethods", summaryResult.TotalCodeElements.ToString(CultureInfo.InvariantCulture)))); var modulesElement = new XElement("Modules"); rootElement.Add(modulesElement); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { if (this.packageElementsByName.TryGetValue(assembly.Name, out XElement packageElement)) { diff --git a/src/ReportGenerator.Core/Reporting/Builders/TextSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/TextSummaryReportBuilder.cs index 4c2c9d9a..b715eed4 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/TextSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/TextSummaryReportBuilder.cs @@ -82,6 +82,10 @@ public void CreateSummaryReport(SummaryResult summaryResult) using (var reportTextWriter = File.CreateText(targetPath)) { + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + if (this.ReportContext.ReportConfiguration.Title != null) { reportTextWriter.WriteLine($"{ReportResources.Summary} - {this.ReportContext.ReportConfiguration.Title}"); @@ -99,9 +103,9 @@ public void CreateSummaryReport(SummaryResult summaryResult) } reportTextWriter.WriteLine(" {0} {1}", ReportResources.Parser, summaryResult.UsedParser); - reportTextWriter.WriteLine(" {0} {1}", ReportResources.Assemblies2, summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)); - reportTextWriter.WriteLine(" {0} {1}", ReportResources.Classes, summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); - reportTextWriter.WriteLine(" {0} {1}", ReportResources.Files2, summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine(" {0} {1}", ReportResources.Assemblies2, assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine(" {0} {1}", ReportResources.Classes, assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); + reportTextWriter.WriteLine(" {0} {1}", ReportResources.Files2, assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine(" {0} {1}", ReportResources.Coverage2, summaryResult.CoverageQuota.HasValue ? summaryResult.CoverageQuota.Value.ToString("f1", CultureInfo.InvariantCulture) + "%" : string.Empty); reportTextWriter.WriteLine(" {0} {1}", ReportResources.CoveredLines, summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)); reportTextWriter.WriteLine(" {0} {1}", ReportResources.UncoveredLines, (summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture)); @@ -133,14 +137,14 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(" {0} {1}", ReportResources.Tag, this.ReportContext.ReportConfiguration.Tag); } - if (summaryResult.Assemblies.Any()) + if (assembliesWithClasses.Any()) { - var maximumNameLength = summaryResult.Assemblies + var maximumNameLength = assembliesWithClasses .SelectMany(a => a.Classes).Select(c => c.DisplayName) - .Union(summaryResult.Assemblies.Select(a => a.Name)) + .Union(assembliesWithClasses.Select(a => a.Name)) .Max(n => n.Length); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { string assemblyQuota = assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString("f1", CultureInfo.InvariantCulture) + "%" : string.Empty; reportTextWriter.WriteLine(); diff --git a/src/ReportGenerator.Core/Reporting/Builders/XmlSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/XmlSummaryReportBuilder.cs index 47db6a14..73da3834 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/XmlSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/XmlSummaryReportBuilder.cs @@ -55,14 +55,18 @@ public virtual void CreateClassReport(Class @class, IEnumerable fi /// The summary result. public void CreateSummaryReport(SummaryResult summaryResult) { + var assembliesWithClasses = summaryResult.Assemblies + .Where(a => a.Classes.Any()) + .ToArray(); + var rootElement = new XElement("CoverageReport", new XAttribute("scope", "Summary")); var summaryElement = new XElement("Summary"); summaryElement.Add(new XElement("Generatedon", DateTime.Now.ToShortDateString() + " - " + DateTime.Now.ToLongTimeString())); summaryElement.Add(new XElement("Parser", summaryResult.UsedParser)); - summaryElement.Add(new XElement("Assemblies", summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture))); - summaryElement.Add(new XElement("Classes", summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture))); - summaryElement.Add(new XElement("Files", summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture))); + summaryElement.Add(new XElement("Assemblies", assembliesWithClasses.Count().ToString(CultureInfo.InvariantCulture))); + summaryElement.Add(new XElement("Classes", assembliesWithClasses.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture))); + summaryElement.Add(new XElement("Files", assembliesWithClasses.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture))); summaryElement.Add(new XElement("Coveredlines", summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture))); summaryElement.Add(new XElement("Uncoveredlines", (summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture))); @@ -122,7 +126,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) var coverageElement = new XElement("Coverage"); - foreach (var assembly in summaryResult.Assemblies) + foreach (var assembly in assembliesWithClasses) { var assemblyElement = new XElement("Assembly");