mirror of
https://github.com/irongut/CodeCoverageSummary.git
synced 2026-05-20 08:50:14 +02:00
added output format arg
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CodeCoverageSummary
|
||||||
|
{
|
||||||
|
public class CodeCoverage
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public double LineRate { get; set; }
|
||||||
|
|
||||||
|
public double BranchRate { get; set; }
|
||||||
|
|
||||||
|
public int Complexity { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CodeSummary
|
||||||
|
{
|
||||||
|
public double LineRate { get; set; }
|
||||||
|
|
||||||
|
public int LinesCovered { get; set; }
|
||||||
|
|
||||||
|
public int LinesValid { get; set; }
|
||||||
|
|
||||||
|
public double BranchRate { get; set; }
|
||||||
|
|
||||||
|
public int BranchesCovered { get; set; }
|
||||||
|
|
||||||
|
public int BranchesValid { get; set; }
|
||||||
|
|
||||||
|
public List<CodeCoverage> Packages { get; set; }
|
||||||
|
|
||||||
|
public CodeSummary()
|
||||||
|
{
|
||||||
|
Packages = new List<CodeCoverage>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,5 +6,8 @@ namespace CodeCoverageSummary
|
|||||||
{
|
{
|
||||||
[Value(index: 0, Required = true, HelpText = "Code coverage file to analyse.")]
|
[Value(index: 0, Required = true, HelpText = "Code coverage file to analyse.")]
|
||||||
public string Filename { get; set; }
|
public string Filename { get; set; }
|
||||||
|
|
||||||
|
[Option(shortName: 'f', longName: "format", Required = false, HelpText = "Output Format.", Default = "text")]
|
||||||
|
public string Format { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,24 +9,40 @@ namespace CodeCoverageSummary
|
|||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
//private const string _filename = "D:\\Dev\\Csharp\\CodeCoverageSummary\\coverage.cobertura.xml";
|
// test file: /Dev/Csharp/CodeCoverageSummary/coverage.cobertura.xml
|
||||||
|
|
||||||
private static int Main(string[] args)
|
private static int Main(string[] args)
|
||||||
{
|
{
|
||||||
return Parser.Default.ParseArguments<CommandLineOptions>(args)
|
return Parser.Default.ParseArguments<CommandLineOptions>(args)
|
||||||
.MapResult<CommandLineOptions, int>(o =>
|
.MapResult(o =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Parsing Code Coverage File: {o.Filename}{Environment.NewLine}");
|
Console.WriteLine($"Code Coverage File: {o.Filename}");
|
||||||
string summary = ParseTestResults(o.Filename);
|
CodeSummary summary = ParseTestResults(o.Filename);
|
||||||
if (string.IsNullOrWhiteSpace(summary))
|
if (summary == null)
|
||||||
{
|
{
|
||||||
return -2; // error
|
return -2; // error
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine(summary);
|
StringBuilder summaryText = new();
|
||||||
|
if (o.Format.Equals("text", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
summaryText.AppendLine($"Line Rate = {summary.LineRate * 100:N0}%, Lines Covered = {summary.LinesCovered} / {summary.LinesValid}")
|
||||||
|
.AppendLine($"Branch Rate = {summary.BranchRate * 100:N0}%, Branches Covered = {summary.BranchesCovered} / {summary.BranchesValid}");
|
||||||
|
foreach (CodeCoverage package in summary.Packages)
|
||||||
|
{
|
||||||
|
summaryText.AppendLine($"{package.Name}: Line Rate = {package.LineRate * 100:N0}%, Branch Rate = {package.BranchRate * 100:N0}%, Complexity = {package.Complexity}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error: Unknown output format.");
|
||||||
|
return -2; // error
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine(summaryText.ToString());
|
||||||
return 0; // success
|
return 0; // success
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,9 +54,9 @@ namespace CodeCoverageSummary
|
|||||||
errs => -1); // invalid arguments
|
errs => -1); // invalid arguments
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ParseTestResults(string filename)
|
private static CodeSummary ParseTestResults(string filename)
|
||||||
{
|
{
|
||||||
StringBuilder summaryText = new StringBuilder();
|
CodeSummary summary = new();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string rss = File.ReadAllText(filename);
|
string rss = File.ReadAllText(filename);
|
||||||
@@ -53,32 +69,32 @@ namespace CodeCoverageSummary
|
|||||||
var lineR = from item in coverage.Attributes()
|
var lineR = from item in coverage.Attributes()
|
||||||
where item.Name == "line-rate"
|
where item.Name == "line-rate"
|
||||||
select item;
|
select item;
|
||||||
double lineRate = double.Parse(lineR.First().Value);
|
summary.LineRate = double.Parse(lineR.First().Value);
|
||||||
|
|
||||||
var linesCovered = from item in coverage.Attributes()
|
var linesCovered = from item in coverage.Attributes()
|
||||||
where item.Name == "lines-covered"
|
where item.Name == "lines-covered"
|
||||||
select item;
|
select item;
|
||||||
|
summary.LinesCovered = int.Parse(linesCovered.First().Value);
|
||||||
|
|
||||||
var linesValid = from item in coverage.Attributes()
|
var linesValid = from item in coverage.Attributes()
|
||||||
where item.Name == "lines-valid"
|
where item.Name == "lines-valid"
|
||||||
select item;
|
select item;
|
||||||
|
summary.LinesValid = int.Parse(linesValid.First().Value);
|
||||||
|
|
||||||
var branchR = from item in coverage.Attributes()
|
var branchR = from item in coverage.Attributes()
|
||||||
where item.Name == "branch-rate"
|
where item.Name == "branch-rate"
|
||||||
select item;
|
select item;
|
||||||
double branchRate = double.Parse(branchR.First().Value);
|
summary.BranchRate = double.Parse(branchR.First().Value);
|
||||||
|
|
||||||
var branchesCovered = from item in coverage.Attributes()
|
var branchesCovered = from item in coverage.Attributes()
|
||||||
where item.Name == "branches-covered"
|
where item.Name == "branches-covered"
|
||||||
select item;
|
select item;
|
||||||
|
summary.BranchesCovered = int.Parse(branchesCovered.First().Value);
|
||||||
|
|
||||||
var branchesValid = from item in coverage.Attributes()
|
var branchesValid = from item in coverage.Attributes()
|
||||||
where item.Name == "branches-valid"
|
where item.Name == "branches-valid"
|
||||||
select item;
|
select item;
|
||||||
|
summary.BranchesValid = int.Parse(branchesValid.First().Value);
|
||||||
summaryText.AppendLine("Code Coverage Results:")
|
|
||||||
.AppendLine($"Line Rate = {lineRate * 100:N0}%, Lines Covered = {linesCovered.First().Value} / {linesValid.First().Value}")
|
|
||||||
.AppendLine($"Branch Rate = {branchRate * 100:N0}%, Branches Covered = {branchesCovered.First().Value} / {branchesValid.First().Value}");
|
|
||||||
|
|
||||||
// test coverage for individual packages
|
// test coverage for individual packages
|
||||||
var packages = from item in coverage.Descendants("package")
|
var packages = from item in coverage.Descendants("package")
|
||||||
@@ -86,21 +102,22 @@ namespace CodeCoverageSummary
|
|||||||
|
|
||||||
foreach (var item in packages)
|
foreach (var item in packages)
|
||||||
{
|
{
|
||||||
string pName = item.Attribute("name").Value;
|
CodeCoverage packageCoverage = new()
|
||||||
double pLineRate = double.Parse(item.Attribute("line-rate").Value);
|
{
|
||||||
double pBranchRate = double.Parse(item.Attribute("branch-rate").Value);
|
Name = item.Attribute("name").Value,
|
||||||
int pComplexity = int.Parse(item.Attribute("complexity").Value);
|
LineRate = double.Parse(item.Attribute("line-rate").Value),
|
||||||
summaryText.AppendLine($"{pName}: Line Rate = {pLineRate * 100:N0}%, Branch Rate = {pBranchRate * 100:N0}%, Complexity = {pComplexity}");
|
BranchRate = double.Parse(item.Attribute("branch-rate").Value),
|
||||||
|
Complexity = int.Parse(item.Attribute("complexity").Value)
|
||||||
|
};
|
||||||
|
summary.Packages.Add(packageCoverage);
|
||||||
}
|
}
|
||||||
|
|
||||||
summaryText.AppendLine();
|
return summary;
|
||||||
|
|
||||||
return summaryText.ToString();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Parse Error: {ex.Message}");
|
Console.WriteLine($"Parse Error: {ex.Message}");
|
||||||
return string.Empty;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user