support multiple cobertura files in CLI #19

This commit is contained in:
irongut
2021-11-14 23:51:30 +00:00
parent 9caa66feee
commit 1c2edd9230
4 changed files with 31 additions and 24 deletions
+1 -4
View File
@@ -31,9 +31,6 @@ namespace CodeCoverageSummary
public List<CodeCoverage> Packages { get; set; }
public CodeSummary()
{
Packages = new List<CodeCoverage>();
}
public CodeSummary() => Packages = new();
}
}
@@ -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<string> 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; }
+26 -17
View File
@@ -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")
@@ -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",