diff --git a/src/CodeCoverageSummary/CommandLineOptions.cs b/src/CodeCoverageSummary/CommandLineOptions.cs index ca4c016..6ca33ec 100644 --- a/src/CodeCoverageSummary/CommandLineOptions.cs +++ b/src/CodeCoverageSummary/CommandLineOptions.cs @@ -13,6 +13,11 @@ namespace CodeCoverageSummary public bool Badge => BadgeString.Equals("true", StringComparison.OrdinalIgnoreCase); + [Option(longName: "fail", Required = false, HelpText = "Fail if overall Line Rate below lower threshold - true or false.", Default = "false")] + public string FailString { get; set; } + + public bool FailBelowThreshold => FailString.Equals("true", StringComparison.OrdinalIgnoreCase); + [Option(longName: "format", Required = false, HelpText = "Output Format - markdown or text.", Default = "text")] public string Format { get; set; } @@ -24,7 +29,7 @@ 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 and health indicator threshold percentages.", Default = "50 75")] + [Option(longName: "thresholds", Required = false, HelpText = "Threshold percentages for badge and health indicators, lower threshold can also be used to fail the action.", Default = "50 75")] public string Thresholds { get; set; } } } diff --git a/src/CodeCoverageSummary/Program.cs b/src/CodeCoverageSummary/Program.cs index 89ad2c3..ad0e30b 100644 --- a/src/CodeCoverageSummary/Program.cs +++ b/src/CodeCoverageSummary/Program.cs @@ -49,11 +49,15 @@ namespace CodeCoverageSummary { fileExt = "txt"; output = GenerateTextOutput(summary, badgeUrl, o.Indicators); + if (o.FailBelowThreshold) + output += $"Minimum allowed line rate is {lowerThreshold * 100:N0}%{Environment.NewLine}"; } else if (o.Format.Equals("md", StringComparison.OrdinalIgnoreCase) || o.Format.Equals("markdown", StringComparison.OrdinalIgnoreCase)) { fileExt = "md"; output = GenerateMarkdownOutput(summary, badgeUrl, o.Indicators); + if (o.FailBelowThreshold) + output += $"{Environment.NewLine}_Minimum allowed line rate is `{lowerThreshold * 100:N0}%`_{Environment.NewLine}"; } else { @@ -81,6 +85,12 @@ namespace CodeCoverageSummary return -2; // error } + if (o.FailBelowThreshold && summary.LineRate < lowerThreshold) + { + Console.WriteLine($"FAIL: Overall line rate below minimum threshold of {lowerThreshold * 100:N0}%."); + return -2; + } + return 0; // success } } diff --git a/src/CodeCoverageSummary/Properties/launchSettings.json b/src/CodeCoverageSummary/Properties/launchSettings.json index 583b9e3..25f7867 100644 --- a/src/CodeCoverageSummary/Properties/launchSettings.json +++ b/src/CodeCoverageSummary/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "CodeCoverageSummary": { "commandName": "Project", - "commandLineArgs": "../../../../coverage.cobertura.xml --format=md --badge true --thresholds=\"80 90\"" + "commandLineArgs": "../../../../coverage.cobertura.xml --format=md --badge true --thresholds=\"85 90\" --fail true" }, "Docker": { "commandName": "Docker",