mirror of
https://github.com/irongut/CodeCoverageSummary.git
synced 2026-05-20 08:50:14 +02:00
reworked Main + improved error handling
This commit is contained in:
@@ -18,71 +18,37 @@ namespace CodeCoverageSummary
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!File.Exists(o.Filename))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error: Code coverage file not found.");
|
||||||
|
return -2; // error
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse code coverage file
|
||||||
Console.WriteLine($"Code Coverage File: {o.Filename}");
|
Console.WriteLine($"Code Coverage File: {o.Filename}");
|
||||||
CodeSummary summary = ParseTestResults(o.Filename);
|
CodeSummary summary = ParseTestResults(o.Filename);
|
||||||
if (summary == null)
|
if (summary == null)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("Error: Parsing code coverage file.");
|
||||||
return -2; // error
|
return -2; // error
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string badgeUrl = null;
|
// generate badge
|
||||||
|
string badgeUrl = o.Badge ? GenerateBadge(summary) : null;
|
||||||
|
|
||||||
if (o.Badge)
|
// generate output
|
||||||
{
|
string output;
|
||||||
string colour;
|
string fileExt;
|
||||||
if (summary.LineRate < 0.5)
|
|
||||||
{
|
|
||||||
colour = "critical";
|
|
||||||
}
|
|
||||||
else if (summary.LineRate < 0.7)
|
|
||||||
{
|
|
||||||
colour = "yellow";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
colour = "success";
|
|
||||||
}
|
|
||||||
badgeUrl = $"https://img.shields.io/badge/Code%20Coverage-{summary.LineRate * 100:N0}%25-{colour}?style=flat";
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder summaryText = new();
|
|
||||||
string fileExt = "txt";
|
|
||||||
if (o.Format.Equals("text", StringComparison.OrdinalIgnoreCase))
|
if (o.Format.Equals("text", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(badgeUrl))
|
fileExt = "txt";
|
||||||
{
|
output = GenerateTextOutput(summary, badgeUrl);
|
||||||
summaryText.AppendLine(badgeUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
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}")
|
|
||||||
.AppendLine($"Complexity = {summary.Complexity}");
|
|
||||||
|
|
||||||
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 if (o.Format.Equals("md", StringComparison.OrdinalIgnoreCase) || o.Format.Equals("markdown", StringComparison.OrdinalIgnoreCase))
|
else if (o.Format.Equals("md", StringComparison.OrdinalIgnoreCase) || o.Format.Equals("markdown", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
fileExt = "md";
|
fileExt = "md";
|
||||||
|
output = GenerateMarkdownOutput(summary, badgeUrl);
|
||||||
if (!string.IsNullOrWhiteSpace(badgeUrl))
|
|
||||||
{
|
|
||||||
summaryText.AppendLine($"");
|
|
||||||
}
|
|
||||||
|
|
||||||
summaryText.AppendLine("Package | Line Rate | Branch Rate | Complexity")
|
|
||||||
.AppendLine("-------- | --------- | ----------- | ----------");
|
|
||||||
|
|
||||||
foreach (CodeCoverage package in summary.Packages)
|
|
||||||
{
|
|
||||||
summaryText.AppendLine($"{package.Name} | {package.LineRate * 100:N0}% | {package.BranchRate * 100:N0}% | {package.Complexity}");
|
|
||||||
}
|
|
||||||
|
|
||||||
summaryText.Append($"**Summary** | **{summary.LineRate * 100:N0}%** ({summary.LinesCovered} / {summary.LinesValid}) | ")
|
|
||||||
.AppendLine($"**{summary.BranchRate * 100:N0}%** ({summary.BranchesCovered} / {summary.BranchesValid}) | {summary.Complexity}");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -90,21 +56,32 @@ namespace CodeCoverageSummary
|
|||||||
return -2; // error
|
return -2; // error
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.Output.Equals("console", StringComparison.OrdinalIgnoreCase) || o.Output.Equals("both", StringComparison.OrdinalIgnoreCase))
|
// output
|
||||||
|
if (o.Output.Equals("console", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
Console.WriteLine(summaryText.ToString());
|
Console.WriteLine(output);
|
||||||
}
|
}
|
||||||
|
else if (o.Output.Equals("file", StringComparison.OrdinalIgnoreCase))
|
||||||
if (o.Output.Equals("file", StringComparison.OrdinalIgnoreCase) || o.Output.Equals("both", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
{
|
||||||
File.WriteAllText($"code-coverage-results.{fileExt}", summaryText.ToString());
|
File.WriteAllText($"code-coverage-results.{fileExt}", output);
|
||||||
|
}
|
||||||
|
else if (o.Output.Equals("both", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
Console.WriteLine(output);
|
||||||
|
File.WriteAllText($"code-coverage-results.{fileExt}", output);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error: Unknown output type.");
|
||||||
|
return -2; // error
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0; // success
|
return 0; // success
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine($"Error: {ex.GetType()} - {ex.Message}");
|
||||||
return -3; // unhandled error
|
return -3; // unhandled error
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -180,5 +157,67 @@ namespace CodeCoverageSummary
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string GenerateBadge(CodeSummary summary)
|
||||||
|
{
|
||||||
|
string colour;
|
||||||
|
if (summary.LineRate < 0.5)
|
||||||
|
{
|
||||||
|
colour = "critical";
|
||||||
|
}
|
||||||
|
else if (summary.LineRate < 0.75)
|
||||||
|
{
|
||||||
|
colour = "yellow";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
colour = "success";
|
||||||
|
}
|
||||||
|
return $"https://img.shields.io/badge/Code%20Coverage-{summary.LineRate * 100:N0}%25-{colour}?style=flat";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GenerateTextOutput(CodeSummary summary, string badgeUrl)
|
||||||
|
{
|
||||||
|
StringBuilder textOutput = new();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(badgeUrl))
|
||||||
|
{
|
||||||
|
textOutput.AppendLine(badgeUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
textOutput.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}")
|
||||||
|
.AppendLine($"Complexity = {summary.Complexity}");
|
||||||
|
|
||||||
|
foreach (CodeCoverage package in summary.Packages)
|
||||||
|
{
|
||||||
|
textOutput.AppendLine($"{package.Name}: Line Rate = {package.LineRate * 100:N0}%, Branch Rate = {package.BranchRate * 100:N0}%, Complexity = {package.Complexity}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return textOutput.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GenerateMarkdownOutput(CodeSummary summary, string badgeUrl)
|
||||||
|
{
|
||||||
|
StringBuilder markdownOutput = new();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(badgeUrl))
|
||||||
|
{
|
||||||
|
markdownOutput.AppendLine($"");
|
||||||
|
}
|
||||||
|
|
||||||
|
markdownOutput.AppendLine("Package | Line Rate | Branch Rate | Complexity")
|
||||||
|
.AppendLine("-------- | --------- | ----------- | ----------");
|
||||||
|
|
||||||
|
foreach (CodeCoverage package in summary.Packages)
|
||||||
|
{
|
||||||
|
markdownOutput.AppendLine($"{package.Name} | {package.LineRate * 100:N0}% | {package.BranchRate * 100:N0}% | {package.Complexity}");
|
||||||
|
}
|
||||||
|
|
||||||
|
markdownOutput.Append($"**Summary** | **{summary.LineRate * 100:N0}%** ({summary.LinesCovered} / {summary.LinesValid}) | ")
|
||||||
|
.AppendLine($"**{summary.BranchRate * 100:N0}%** ({summary.BranchesCovered} / {summary.BranchesValid}) | {summary.Complexity}");
|
||||||
|
|
||||||
|
return markdownOutput.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user