Skip to content

Add option to specify multiple threshold values for each threshold types #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/coverlet.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

using ConsoleTables;
Expand Down Expand Up @@ -102,7 +103,6 @@ static int Main(string[] args)
process.WaitForExit();

var dOutput = output.HasValue() ? output.Value() : Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar.ToString();
var dThreshold = threshold.HasValue() ? double.Parse(threshold.Value()) : 0;
var dThresholdTypes = thresholdTypes.HasValue() ? thresholdTypes.Values : new List<string>(new string[] { "line", "branch", "method" });
var dThresholdStat = thresholdStat.HasValue() ? Enum.Parse<ThresholdStatistic>(thresholdStat.Value(), true) : Enum.Parse<ThresholdStatistic>("minimum", true);

Expand Down Expand Up @@ -147,20 +147,47 @@ static int Main(string[] args)
}

var thresholdTypeFlags = ThresholdTypeFlags.None;

var thresholdTypeFlagQueue = new Queue<ThresholdTypeFlags>();
foreach (var thresholdType in dThresholdTypes)
{
if (thresholdType.Equals("line", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlags |= ThresholdTypeFlags.Line;
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Line);
}
else if (thresholdType.Equals("branch", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Branch);
}
else if (thresholdType.Equals("method", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlags |= ThresholdTypeFlags.Method;
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Method);
}
}

Dictionary<ThresholdTypeFlags, double> thresholdTypeFlagValues = new Dictionary<ThresholdTypeFlags, double>();
if (threshold.HasValue() && threshold.Value().Contains(','))
{
var thresholdValues = threshold.Value().Split(',', StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim());
if (thresholdValues.Count() != thresholdTypeFlagQueue.Count())
{
throw new Exception($"Threshold type flag count ({thresholdTypeFlagQueue.Count()}) and values count ({thresholdValues.Count()}) doesnt match");
}

foreach (var thresholdValue in thresholdValues)
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = double.Parse(thresholdValue);
}
}
else
{
double thresholdValue = threshold.HasValue() ? double.Parse(threshold.Value()) : 0;

while (thresholdTypeFlagQueue.Any())
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = thresholdValue;
}
}

Expand Down Expand Up @@ -199,28 +226,30 @@ static int Main(string[] args)
coverageTable.AddRow("Average", $"{averageLinePercent}%", $"{averageBranchPercent}%", $"{averageMethodPercent}%");

logger.LogInformation(coverageTable.ToStringAlternative());

if (process.ExitCode > 0)
{
exitCode += (int)CommandExitCodes.TestFailed;
}
thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, dThreshold, thresholdTypeFlags, dThresholdStat);

thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, thresholdTypeFlagValues, thresholdTypeFlags, dThresholdStat);
if (thresholdTypeFlags != ThresholdTypeFlags.None)
{
exitCode += (int)CommandExitCodes.CoverageBelowThreshold;
var exceptionMessageBuilder = new StringBuilder();
if ((thresholdTypeFlags & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine($"The {dThresholdStat.ToString().ToLower()} line coverage is below the specified {dThreshold}");
exceptionMessageBuilder.AppendLine($"The {dThresholdStat.ToString().ToLower()} line coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Line]}");
}

if ((thresholdTypeFlags & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine($"The {dThresholdStat.ToString().ToLower()} branch coverage is below the specified {dThreshold}");
exceptionMessageBuilder.AppendLine($"The {dThresholdStat.ToString().ToLower()} branch coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Branch]}");
}

if ((thresholdTypeFlags & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine($"The {dThresholdStat.ToString().ToLower()} method coverage is below the specified {dThreshold}");
exceptionMessageBuilder.AppendLine($"The {dThresholdStat.ToString().ToLower()} method coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Method]}");
}

throw new Exception(exceptionMessageBuilder.ToString());
Expand Down
20 changes: 10 additions & 10 deletions src/coverlet.core/CoverageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal void Merge(Modules modules)
}
}

public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summary, double threshold, ThresholdTypeFlags thresholdTypes, ThresholdStatistic thresholdStat)
public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summary, Dictionary<ThresholdTypeFlags, double> thresholdTypeFlagValues, ThresholdTypeFlags thresholdTypes, ThresholdStatistic thresholdStat)
{
var thresholdTypeFlags = ThresholdTypeFlags.None;
switch (thresholdStat)
Expand All @@ -125,19 +125,19 @@ public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summar

if ((thresholdTypes & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
{
if (line < threshold)
if (line < thresholdTypeFlagValues[ThresholdTypeFlags.Line])
thresholdTypeFlags |= ThresholdTypeFlags.Line;
}

if ((thresholdTypes & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
{
if (branch < threshold)
if (branch < thresholdTypeFlagValues[ThresholdTypeFlags.Branch])
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
}

if ((thresholdTypes & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
{
if (method < threshold)
if (method < thresholdTypeFlagValues[ThresholdTypeFlags.Method])
thresholdTypeFlags |= ThresholdTypeFlags.Method;
}
}
Expand All @@ -151,19 +151,19 @@ public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summar

if ((thresholdTypes & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
{
if (line < threshold)
if ((line / numModules) < thresholdTypeFlagValues[ThresholdTypeFlags.Line])
thresholdTypeFlags |= ThresholdTypeFlags.Line;
}

if ((thresholdTypes & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
{
if (branch < threshold)
if ((branch / numModules) < thresholdTypeFlagValues[ThresholdTypeFlags.Branch])
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
}

if ((thresholdTypes & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
{
if (method < threshold)
if ((method / numModules) < thresholdTypeFlagValues[ThresholdTypeFlags.Method])
thresholdTypeFlags |= ThresholdTypeFlags.Method;
}
}
Expand All @@ -176,19 +176,19 @@ public ThresholdTypeFlags GetThresholdTypesBelowThreshold(CoverageSummary summar

if ((thresholdTypes & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
{
if (line < threshold)
if (line < thresholdTypeFlagValues[ThresholdTypeFlags.Line])
thresholdTypeFlags |= ThresholdTypeFlags.Line;
}

if ((thresholdTypes & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
{
if (branch < threshold)
if (branch < thresholdTypeFlagValues[ThresholdTypeFlags.Branch])
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
}

if ((thresholdTypes & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
{
if (method < threshold)
if (method < thresholdTypeFlagValues[ThresholdTypeFlags.Method])
thresholdTypeFlags |= ThresholdTypeFlags.Method;
}
}
Expand Down
44 changes: 36 additions & 8 deletions src/coverlet.msbuild.tasks/CoverageResultTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -17,7 +18,7 @@ public class CoverageResultTask : Task
{
private string _output;
private string _format;
private double _threshold;
private string _threshold;
private string _thresholdType;
private string _thresholdStat;
private ITaskItem _instrumenterState;
Expand All @@ -38,7 +39,7 @@ public string OutputFormat
}

[Required]
public double Threshold
public string Threshold
{
get { return _threshold; }
set { _threshold = value; }
Expand Down Expand Up @@ -130,24 +131,51 @@ public override bool Execute()
}

var thresholdTypeFlags = ThresholdTypeFlags.None;
var thresholdStat = ThresholdStatistic.Minimum;

var thresholdTypeFlagQueue = new Queue<ThresholdTypeFlags>();
foreach (var thresholdType in _thresholdType.Split(',').Select(t => t.Trim()))
{
if (thresholdType.Equals("line", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlags |= ThresholdTypeFlags.Line;
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Line);
}
else if (thresholdType.Equals("branch", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlags |= ThresholdTypeFlags.Branch;
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Branch);
}
else if (thresholdType.Equals("method", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlags |= ThresholdTypeFlags.Method;
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Method );
}
}

Dictionary<ThresholdTypeFlags, double> thresholdTypeFlagValues = new Dictionary<ThresholdTypeFlags, double>();
if (_threshold.Contains(','))
{
var thresholdValues = _threshold.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim());
if(thresholdValues.Count() != thresholdTypeFlagQueue.Count())
{
throw new Exception($"Threshold type flag count ({thresholdTypeFlagQueue.Count()}) and values count ({thresholdValues.Count()}) doesnt match");
}

foreach (var threshold in thresholdValues)
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = double.Parse(threshold);
}
}
else
{
double thresholdValue = double.Parse(_threshold);

while (thresholdTypeFlagQueue.Any())
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = thresholdValue;
}
}

var thresholdStat = ThresholdStatistic.Minimum;
if (_thresholdStat.Equals("average", StringComparison.OrdinalIgnoreCase))
{
thresholdStat = ThresholdStatistic.Average;
Expand Down Expand Up @@ -194,23 +222,23 @@ public override bool Execute()

Console.WriteLine(coverageTable.ToStringAlternative());

thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, _threshold, thresholdTypeFlags, thresholdStat);
thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, thresholdTypeFlagValues, thresholdTypeFlags, thresholdStat);
if (thresholdTypeFlags != ThresholdTypeFlags.None)
{
var exceptionMessageBuilder = new StringBuilder();
if ((thresholdTypeFlags & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine($"The {thresholdStat.ToString().ToLower()} line coverage is below the specified {_threshold}");
exceptionMessageBuilder.AppendLine($"The {thresholdStat.ToString().ToLower()} line coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Line]}");
}

if ((thresholdTypeFlags & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine($"The {thresholdStat.ToString().ToLower()} branch coverage is below the specified {_threshold}");
exceptionMessageBuilder.AppendLine($"The {thresholdStat.ToString().ToLower()} branch coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Branch]}");
}

if ((thresholdTypeFlags & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine($"The {thresholdStat.ToString().ToLower()} method coverage is below the specified {_threshold}");
exceptionMessageBuilder.AppendLine($"The {thresholdStat.ToString().ToLower()} method coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Method]}");
}

throw new Exception(exceptionMessageBuilder.ToString());
Expand Down
Loading