From 4a0dc323d389fe85fa23f5750f79c6addf3e0c18 Mon Sep 17 00:00:00 2001 From: irongut Date: Mon, 18 Oct 2021 01:24:48 +0100 Subject: [PATCH] added Thresholds parameter to CLI #15 --- src/CodeCoverageSummary/CommandLineOptions.cs | 3 ++ src/CodeCoverageSummary/GlobalSuppressions.cs | 1 + src/CodeCoverageSummary/Program.cs | 46 ++++++++++++++++++- .../Properties/launchSettings.json | 2 +- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/CodeCoverageSummary/CommandLineOptions.cs b/src/CodeCoverageSummary/CommandLineOptions.cs index 1b588c9..7a85b0f 100644 --- a/src/CodeCoverageSummary/CommandLineOptions.cs +++ b/src/CodeCoverageSummary/CommandLineOptions.cs @@ -15,5 +15,8 @@ namespace CodeCoverageSummary [Option(longName: "output", Required = false, HelpText = "Output Type - console, file or both.", Default = "console")] public string Output { get; set; } + + [Option(longName: "thresholds", Required = false, HelpText = "Badge colour thresholds.", Default = "50 75")] + public string Thresholds { get; set; } } } diff --git a/src/CodeCoverageSummary/GlobalSuppressions.cs b/src/CodeCoverageSummary/GlobalSuppressions.cs index 920e60b..8163dc6 100644 --- a/src/CodeCoverageSummary/GlobalSuppressions.cs +++ b/src/CodeCoverageSummary/GlobalSuppressions.cs @@ -6,3 +6,4 @@ using System.Diagnostics.CodeAnalysis; [assembly: SuppressMessage("Performance", "RCS1197:Optimize StringBuilder.Append/AppendLine call.", Scope = "type", Target = "~T:CodeCoverageSummary.Program")] +[assembly: SuppressMessage("Style", "IDE0057:Use range operator", Scope = "member", Target = "~M:CodeCoverageSummary.Program.SetThresholds(System.String)")] diff --git a/src/CodeCoverageSummary/Program.cs b/src/CodeCoverageSummary/Program.cs index 27b4c2b..bd801f2 100644 --- a/src/CodeCoverageSummary/Program.cs +++ b/src/CodeCoverageSummary/Program.cs @@ -9,6 +9,9 @@ namespace CodeCoverageSummary { internal static class Program { + private static double lowerThreshold = 0.5; + private static double upperThreshold = 0.75; + private static int Main(string[] args) { return Parser.Default.ParseArguments(args) @@ -32,6 +35,10 @@ namespace CodeCoverageSummary } else { + // set health badge thresholds + if (!string.IsNullOrWhiteSpace(o.Thresholds)) + SetThresholds(o.Thresholds); + // generate badge string badgeUrl = o.Badge ? GenerateBadge(summary) : null; @@ -158,14 +165,49 @@ namespace CodeCoverageSummary } } + private static void SetThresholds(string thresholds) + { + int lowerPercentage; + int upperPercentage = (int)(upperThreshold * 100); + int s = thresholds.IndexOf(" "); + if (s == 0) + { + throw new ArgumentException("Threshold parameter set incorrectly."); + } + else if (s < 0) + { + if (!int.TryParse(thresholds, out lowerPercentage)) + throw new ArgumentException("Threshold parameter set incorrectly."); + } + else + { + if (!int.TryParse(thresholds.Substring(0, s), out lowerPercentage)) + throw new ArgumentException("Threshold parameter set incorrectly."); + + if (!int.TryParse(thresholds.Substring(s + 1), out upperPercentage)) + throw new ArgumentException("Threshold parameter set incorrectly."); + } + lowerThreshold = lowerPercentage / 100.0; + upperThreshold = upperPercentage / 100.0; + + if (lowerThreshold > 1.0) + lowerThreshold = 1.0; + + if (lowerThreshold > upperThreshold) + upperThreshold = lowerThreshold + 0.1; + + if (upperThreshold > 1.0) + upperThreshold = 1.0; + } + private static string GenerateBadge(CodeSummary summary) { string colour; - if (summary.LineRate < 0.5) + if (summary.LineRate < lowerThreshold) { colour = "critical"; } - else if (summary.LineRate < 0.75) + else if (summary.LineRate < upperThreshold) { colour = "yellow"; } diff --git a/src/CodeCoverageSummary/Properties/launchSettings.json b/src/CodeCoverageSummary/Properties/launchSettings.json index 77ec29b..d4e9905 100644 --- a/src/CodeCoverageSummary/Properties/launchSettings.json +++ b/src/CodeCoverageSummary/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "CodeCoverageSummary": { "commandName": "Project", - "commandLineArgs": "../../../../coverage.gcovr.xml --format=md --badge=true" + "commandLineArgs": "../../../../coverage.cobertura.xml --format=md --badge=true --thresholds=\"80 90\"" }, "Docker": { "commandName": "Docker",