From 461c665cf52b2106a6a8e94a9a6d0e3a8dd40b86 Mon Sep 17 00:00:00 2001 From: irongut Date: Wed, 14 Apr 2021 23:18:00 +0100 Subject: [PATCH] reworked Main + improved error handling --- src/CodeCoverageSummary/Program.cs | 151 ++++++++++++++++++----------- 1 file changed, 95 insertions(+), 56 deletions(-) diff --git a/src/CodeCoverageSummary/Program.cs b/src/CodeCoverageSummary/Program.cs index 94f0a0f..cd19bf6 100644 --- a/src/CodeCoverageSummary/Program.cs +++ b/src/CodeCoverageSummary/Program.cs @@ -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(); + } } }