add support for merging coverage results

This commit is contained in:
Toni Solarin-Sodara
2018-09-02 21:36:47 +01:00
parent 735daa4721
commit 45bc5bc843
6 changed files with 91 additions and 7 deletions
+13 -5
View File
@@ -6,6 +6,8 @@ using System.Linq;
using Coverlet.Core.Helpers;
using Coverlet.Core.Instrumentation;
using Newtonsoft.Json;
namespace Coverlet.Core
{
public class Coverage
@@ -15,6 +17,7 @@ namespace Coverlet.Core
private string[] _excludeFilters;
private string[] _includeFilters;
private string[] _excludedSourceFiles;
private string _mergeWith;
private List<InstrumenterResult> _results;
public string Identifier
@@ -22,12 +25,14 @@ namespace Coverlet.Core
get { return _identifier; }
}
public Coverage(string module, string[] excludeFilters, string[] includeFilters, string[] excludedSourceFiles)
public Coverage(string module, string[] excludeFilters, string[] includeFilters, string[] excludedSourceFiles, string mergeWith)
{
_module = module;
_excludeFilters = excludeFilters;
_includeFilters = includeFilters;
_excludedSourceFiles = excludedSourceFiles;
_mergeWith = mergeWith;
_identifier = Guid.NewGuid().ToString();
_results = new List<InstrumenterResult>();
}
@@ -144,11 +149,14 @@ namespace Coverlet.Core
InstrumentationHelper.RestoreOriginalModule(result.ModulePath, _identifier);
}
return new CoverageResult
var coverageResult = new CoverageResult { Identifier = _identifier, Modules = modules };
if (!string.IsNullOrEmpty(_mergeWith) && !string.IsNullOrWhiteSpace(_mergeWith))
{
Identifier = _identifier,
Modules = modules
};
string json = File.ReadAllText(_mergeWith);
coverageResult.Merge(JsonConvert.DeserializeObject<Modules>(json));
}
return coverageResult;
}
private void CalculateCoverage()
+66
View File
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Coverlet.Core
{
@@ -38,5 +39,70 @@ namespace Coverlet.Core
public Modules Modules;
internal CoverageResult() { }
internal void Merge(Modules modules)
{
foreach (var module in modules)
{
if (!this.Modules.ContainsKey(module.Key))
{
this.Modules.Add(module.Key, module.Value);
}
else
{
foreach (var document in module.Value)
{
if (!this.Modules[module.Key].ContainsKey(document.Key))
{
this.Modules[module.Key].Add(document.Key, document.Value);
}
else
{
foreach (var @class in document.Value)
{
if (!this.Modules[module.Key][document.Key].ContainsKey(@class.Key))
{
this.Modules[module.Key][document.Key].Add(@class.Key, @class.Value);
}
else
{
foreach (var method in @class.Value)
{
if (!this.Modules[module.Key][document.Key][@class.Key].ContainsKey(method.Key))
{
this.Modules[module.Key][document.Key][@class.Key].Add(method.Key, method.Value);
}
else
{
foreach (var line in method.Value.Lines)
{
if (!this.Modules[module.Key][document.Key][@class.Key][method.Key].Lines.ContainsKey(line.Key))
{
this.Modules[module.Key][document.Key][@class.Key][method.Key].Lines.Add(line.Key, line.Value);
}
else
{
this.Modules[module.Key][document.Key][@class.Key][method.Key].Lines[line.Key] += line.Value;
}
}
foreach (var branch in method.Value.Branches)
{
var branches = this.Modules[module.Key][document.Key][@class.Key][method.Key].Branches;
var branchInfo = branches.FirstOrDefault(b => b.EndOffset == branch.EndOffset && b.Line == branch.Line && b.Offset == branch.Offset && b.Ordinal == branch.Ordinal && b.Path == branch.Path);
if (branchInfo == null)
branches.Add(branch);
else
branchInfo.Hits += branch.Hits;
}
}
}
}
}
}
}
}
}
}
}
}
@@ -12,6 +12,7 @@ namespace Coverlet.MSbuild.Tasks
private string _exclude;
private string _include;
private string _excludeByFile;
private string _mergeWith;
internal static Coverage Coverage
{
@@ -43,6 +44,12 @@ namespace Coverlet.MSbuild.Tasks
set { _excludeByFile = value; }
}
public string MergeWith
{
get { return _mergeWith; }
set { _mergeWith = value; }
}
public override bool Execute()
{
try
@@ -51,7 +58,7 @@ namespace Coverlet.MSbuild.Tasks
var excludeFilters = _exclude?.Split(',');
var includeFilters = _include?.Split(',');
_coverage = new Coverage(_path, excludeFilters, includeFilters, excludedSourceFiles);
_coverage = new Coverage(_path, excludeFilters, includeFilters, excludedSourceFiles, _mergeWith);
_coverage.PrepareModules();
}
catch (Exception ex)
@@ -5,6 +5,7 @@
<CoverletOutput Condition="$(CoverletOutput) == ''">$([MSBuild]::EnsureTrailingSlash('$(MSBuildProjectDirectory)'))</CoverletOutput>
<Exclude Condition="$(Exclude) == ''"></Exclude>
<ExcludeByFile Condition="$(ExcludeByFile) == ''"></ExcludeByFile>
<MergeWith Condition="$(MergeWith) == ''"></MergeWith>
<Threshold Condition="$(Threshold) == ''">0</Threshold>
<ThresholdType Condition="$(ThresholdType) == ''">line,branch,method</ThresholdType>
</PropertyGroup>
@@ -8,6 +8,7 @@
Condition="'$(VSTestNoBuild)' == 'true' and $(CollectCoverage) == 'true'"
Exclude="$(Exclude)"
ExcludeByFile="$(ExcludeByFile)"
MergeWith="$(MergeWith)"
Path="$(TargetPath)" />
</Target>
@@ -16,6 +17,7 @@
Condition="'$(VSTestNoBuild)' != 'true' and $(CollectCoverage) == 'true'"
Exclude="$(Exclude)"
ExcludeByFile="$(ExcludeByFile)"
MergeWith="$(MergeWith)"
Path="$(TargetPath)" />
</Target>
+1 -1
View File
@@ -27,7 +27,7 @@ namespace Coverlet.Core.Tests
// Since Coverage only instruments dependancies, we need a fake module here
var testModule = Path.Combine(directory.FullName, "test.module.dll");
var coverage = new Coverage(testModule, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>());
var coverage = new Coverage(testModule, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), string.Empty);
coverage.PrepareModules();
var result = coverage.GetCoverageResult();