Skip to content
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

Issue #92 : Added a ThresholdType switch #93

Merged
merged 1 commit into from May 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 64 additions & 14 deletions src/coverlet.msbuild.tasks/CoverageResultTask.cs
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ConsoleTables;

using Coverlet.Core;
using Coverlet.Core.Reporters;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand All @@ -16,6 +15,7 @@ public class CoverageResultTask : Task
private string _filename;
private string _format;
private int _threshold;
private string _thresholdTypes;

[Required]
public string Output
Expand All @@ -38,17 +38,26 @@ public int Threshold
set { _threshold = value; }
}

[Required]
public string ThresholdType
{
get { return _thresholdTypes; }
set { _thresholdTypes = value; }
}

public override bool Execute()
{
try
{
Console.WriteLine("\nCalculating coverage result...");
var coverage = InstrumentationTask.Coverage;
CoverageResult result = coverage.GetCoverageResult();
var result = coverage.GetCoverageResult();

var directory = Path.GetDirectoryName(_filename);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

var formats = _format.Split(',');
foreach (var format in formats)
Expand All @@ -62,25 +71,66 @@ public override bool Execute()
File.WriteAllText(report, reporter.Report(result));
}

double total = 0;
CoverageSummary summary = new CoverageSummary();
ConsoleTable table = new ConsoleTable("Module", "Line", "Branch", "Method");
var branchTotal = 0d;
var methodTotal = 0d;
var lineTotal = 0d;
var summary = new CoverageSummary();
var table = new ConsoleTable("Module", "Line", "Branch", "Method");

foreach (var module in result.Modules)
{
double linePercent = summary.CalculateLineCoverage(module.Value).Percent * 100;
double branchPercent = summary.CalculateBranchCoverage(module.Value).Percent * 100;
double methodPercent = summary.CalculateMethodCoverage(module.Value).Percent * 100;
var linePercent = summary.CalculateLineCoverage(module.Value).Percent * 100;
var branchPercent = summary.CalculateBranchCoverage(module.Value).Percent * 100;
var methodPercent = summary.CalculateMethodCoverage(module.Value).Percent * 100;
table.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%");
total += linePercent;

lineTotal += linePercent;
branchTotal += branchPercent;
methodTotal += methodPercent;
}

Console.WriteLine();
Console.WriteLine(table.ToStringAlternative());

double average = total / result.Modules.Count;
if (average < _threshold)
throw new Exception($"Overall average coverage '{average}%' is lower than specified threshold '{_threshold}%'");
if (_threshold > 0)
{
var thresholdFailed = false;
var exceptionBuilder = new StringBuilder();
var lineAverage = lineTotal / result.Modules.Count;
var branchAverage = branchTotal / result.Modules.Count;
var methodAverage = methodTotal / result.Modules.Count;
var thresholdTypes = _thresholdTypes.Split(',').Select(t => t.ToLower());
foreach (var thresholdType in thresholdTypes)
{
if (thresholdType == "line" && lineAverage < _threshold)
{
thresholdFailed = true;
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{lineAverage}%' is lower than specified threshold '{_threshold}%'");
}

else if (thresholdType == "branch" && branchAverage < _threshold)
{
thresholdFailed = true;
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{branchAverage}%' is lower than specified threshold '{_threshold}%'");
}

else if (thresholdType == "method" && methodAverage < _threshold)
{
thresholdFailed = true;
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{methodAverage}%' is lower than specified threshold '{_threshold}%'");
}

else if (thresholdType != "line" && thresholdType != "branch" && thresholdType != "method")
{
Console.WriteLine($"Threshold type of {thresholdType} is not recognized/supported and will be ignored.");
}
}

if (thresholdFailed)
{
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
}
}
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/coverlet.msbuild/coverlet.msbuild.props
Expand Up @@ -7,6 +7,7 @@
<CoverletOutput>$([MSBuild]::EnsureTrailingSlash('$(CoverletOutputDirectory)'))$(CoverletOutputName)</CoverletOutput>
<Exclude Condition="$(Exclude) == ''"></Exclude>
<Threshold Condition="$(Threshold) == ''">0</Threshold>
<ThresholdType Condition="$(ThresholdType) == ''">line,branch,method</ThresholdType>
<CollectCoverage Condition="$(CollectCoverage) == ''">false</CollectCoverage>
</PropertyGroup>

Expand Down
3 changes: 2 additions & 1 deletion src/coverlet.msbuild/coverlet.msbuild.targets
Expand Up @@ -22,7 +22,8 @@
Condition="$(CollectCoverage) == 'true'"
Output="$(CoverletOutput)"
OutputFormat="$(CoverletOutputFormat)"
Threshold="$(Threshold)" />
Threshold="$(Threshold)"
ThresholdType="$(ThresholdType)" />
</Target>

</Project>