From 1c2edd923012b513ebef2b972469274a875232f4 Mon Sep 17 00:00:00 2001 From: irongut Date: Sun, 14 Nov 2021 23:51:30 +0000 Subject: [PATCH] support multiple cobertura files in CLI #19 --- src/CodeCoverageSummary/CodeSummary.cs | 5 +-- src/CodeCoverageSummary/CommandLineOptions.cs | 5 ++- src/CodeCoverageSummary/Program.cs | 43 +++++++++++-------- .../Properties/launchSettings.json | 2 +- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/CodeCoverageSummary/CodeSummary.cs b/src/CodeCoverageSummary/CodeSummary.cs index b73296c..d94906b 100644 --- a/src/CodeCoverageSummary/CodeSummary.cs +++ b/src/CodeCoverageSummary/CodeSummary.cs @@ -31,9 +31,6 @@ namespace CodeCoverageSummary public List Packages { get; set; } - public CodeSummary() - { - Packages = new List(); - } + public CodeSummary() => Packages = new(); } } diff --git a/src/CodeCoverageSummary/CommandLineOptions.cs b/src/CodeCoverageSummary/CommandLineOptions.cs index b277fd8..dbc64b5 100644 --- a/src/CodeCoverageSummary/CommandLineOptions.cs +++ b/src/CodeCoverageSummary/CommandLineOptions.cs @@ -1,12 +1,13 @@ using CommandLine; using System; +using System.Collections.Generic; namespace CodeCoverageSummary { public class CommandLineOptions { - [Value(index: 0, Required = true, HelpText = "Code coverage file to analyse.")] - public string Filename { get; set; } + [Option(longName: "files", Separator = ',', Required = true, HelpText = "A comma separated list of code coverage files to analyse.")] + public IEnumerable Files { get; set; } [Option(longName: "badge", Required = false, HelpText = "Include a Line Rate coverage badge in the output using shields.io - true or false.", Default = "false")] public string BadgeString { get; set; } diff --git a/src/CodeCoverageSummary/Program.cs b/src/CodeCoverageSummary/Program.cs index b907318..f29e17d 100644 --- a/src/CodeCoverageSummary/Program.cs +++ b/src/CodeCoverageSummary/Program.cs @@ -19,18 +19,29 @@ namespace CodeCoverageSummary { try { - if (!File.Exists(o.Filename)) + // check files exist + foreach (var file in o.Files) { - Console.WriteLine("Error: Code coverage file not found."); - return -2; // error + if (!File.Exists(file)) + { + Console.WriteLine($"Error: Code coverage file not found - {file}."); + return -2; // error + } } // parse code coverage file - Console.WriteLine($"Code Coverage File: {o.Filename}"); - CodeSummary summary = ParseTestResults(o.Filename); - if (summary == null) + CodeSummary summary = new(); + foreach (var file in o.Files) { - Console.WriteLine("Error: Parsing code coverage file."); + Console.WriteLine($"Code Coverage File: {file}"); + summary = ParseTestResults(file, summary); + } + summary.LineRate /= o.Files.Count(); + summary.BranchRate /= o.Files.Count(); + + if (summary.Packages.Count == 0) + { + Console.WriteLine("Error: Parsing code coverage file, no packages found."); return -2; // error } else @@ -103,9 +114,9 @@ namespace CodeCoverageSummary _ => -1); // invalid arguments } - private static CodeSummary ParseTestResults(string filename) + private static CodeSummary ParseTestResults(string filename, CodeSummary summary) { - CodeSummary summary = new(); + //CodeSummary summary = new(); try { string rss = File.ReadAllText(filename); @@ -118,34 +129,32 @@ namespace CodeCoverageSummary var lineR = from item in coverage.Attributes() where item.Name == "line-rate" select item; - summary.LineRate = double.Parse(lineR.First().Value); + summary.LineRate += double.Parse(lineR.First().Value); var linesCovered = from item in coverage.Attributes() where item.Name == "lines-covered" select item; - summary.LinesCovered = int.Parse(linesCovered.First().Value); + summary.LinesCovered += int.Parse(linesCovered.First().Value); var linesValid = from item in coverage.Attributes() where item.Name == "lines-valid" select item; - summary.LinesValid = int.Parse(linesValid.First().Value); + summary.LinesValid += int.Parse(linesValid.First().Value); var branchR = from item in coverage.Attributes() where item.Name == "branch-rate" select item; - summary.BranchRate = double.Parse(branchR.First().Value); + summary.BranchRate += double.Parse(branchR.First().Value); var branchesCovered = from item in coverage.Attributes() where item.Name == "branches-covered" select item; - summary.BranchesCovered = int.Parse(branchesCovered.First().Value); + summary.BranchesCovered += int.Parse(branchesCovered.First().Value); var branchesValid = from item in coverage.Attributes() where item.Name == "branches-valid" select item; - summary.BranchesValid = int.Parse(branchesValid.First().Value); - - summary.Complexity = 0; + summary.BranchesValid += int.Parse(branchesValid.First().Value); // test coverage for individual packages var packages = from item in coverage.Descendants("package") diff --git a/src/CodeCoverageSummary/Properties/launchSettings.json b/src/CodeCoverageSummary/Properties/launchSettings.json index 25f7867..736ad24 100644 --- a/src/CodeCoverageSummary/Properties/launchSettings.json +++ b/src/CodeCoverageSummary/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "CodeCoverageSummary": { "commandName": "Project", - "commandLineArgs": "../../../../coverage.cobertura.xml --format=md --badge true --thresholds=\"85 90\" --fail true" + "commandLineArgs": "--files ../../../../coverage.cobertura.xml,../../../../coverage.cobertura.xml --format=text --badge true --thresholds=\"85 90\" --fail true" }, "Docker": { "commandName": "Docker",