reworked Main + improved error handling

This commit is contained in:
irongut
2021-04-14 23:18:00 +01:00
parent 2383c49f26
commit 461c665cf5
+95 -56
View File
@@ -18,71 +18,37 @@ namespace CodeCoverageSummary
{
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}");
CodeSummary summary = ParseTestResults(o.Filename);
if (summary == null)
{
Console.WriteLine("Error: Parsing code coverage file.");
return -2; // error
}
else
{
string badgeUrl = null;
// generate badge
string badgeUrl = o.Badge ? GenerateBadge(summary) : null;
if (o.Badge)
{
string colour;
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";
// generate output
string output;
string fileExt;
if (o.Format.Equals("text", StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrWhiteSpace(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}");
}
fileExt = "txt";
output = GenerateTextOutput(summary, badgeUrl);
}
else if (o.Format.Equals("md", StringComparison.OrdinalIgnoreCase) || o.Format.Equals("markdown", StringComparison.OrdinalIgnoreCase))
{
fileExt = "md";
if (!string.IsNullOrWhiteSpace(badgeUrl))
{
summaryText.AppendLine($"![Code Coverage]({badgeUrl})");
}
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}");
output = GenerateMarkdownOutput(summary, badgeUrl);
}
else
{
@@ -90,21 +56,32 @@ namespace CodeCoverageSummary
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);
}
if (o.Output.Equals("file", StringComparison.OrdinalIgnoreCase) || o.Output.Equals("both", StringComparison.OrdinalIgnoreCase))
else if (o.Output.Equals("file", 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
}
}
catch
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.GetType()} - {ex.Message}");
return -3; // unhandled error
}
},
@@ -180,5 +157,67 @@ namespace CodeCoverageSummary
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($"![Code Coverage]({badgeUrl})");
}
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();
}
}
}