Skip to content

Commit

Permalink
Merge pull request #186 from tonerdo/feature/merge-results
Browse files Browse the repository at this point in the history
Add support for merging results
  • Loading branch information
tonerdo authored Sep 6, 2018
2 parents 735daa4 + d8d7388 commit 307d33b
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Target Name="BuildAllProjects">
<RemoveDir Directories="$(OutputPath)" Condition="Exists('$(MSBuildThisFileDirectory)\build')" />
<Exec Command="dotnet build &quot;$(MSBuildThisFileDirectory)src\coverlet.msbuild.tasks\coverlet.msbuild.tasks.csproj&quot; -c $(Configuration)" />
<Exec Command="dotnet build &quot;$(MSBuildThisFileDirectory)src\coverlet.console\coverlet.console.csproj&quot; -c $(Configuration)" />
</Target>

<Target Name="PublishMSBuildTaskProject" AfterTargets="BuildAllProjects">
Expand Down
3 changes: 2 additions & 1 deletion src/coverlet.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static int Main(string[] args)
CommandOption excludeFilters = app.Option("--exclude", "Filter expressions to exclude specific modules and types.", CommandOptionType.MultipleValue);
CommandOption includeFilters = app.Option("--include", "Filter expressions to include only specific modules and types.", CommandOptionType.MultipleValue);
CommandOption excludedSourceFiles = app.Option("--exclude-by-file", "Glob patterns specifying source files to exclude.", CommandOptionType.MultipleValue);
CommandOption mergeWith = app.Option("--merge-with", "Path to existing coverage result to merge.", CommandOptionType.SingleValue);

app.OnExecute(() =>
{
Expand All @@ -43,7 +44,7 @@ static int Main(string[] args)
if (!target.HasValue())
throw new CommandParsingException(app, "Target must be specified.");

Coverage coverage = new Coverage(module.Value, excludeFilters.Values.ToArray(), includeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray());
Coverage coverage = new Coverage(module.Value, excludeFilters.Values.ToArray(), includeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray(), mergeWith.Value());
coverage.PrepareModules();

Process process = new Process();
Expand Down
18 changes: 13 additions & 5 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Coverlet.Core.Helpers;
using Coverlet.Core.Instrumentation;

using Newtonsoft.Json;

namespace Coverlet.Core
{
public class Coverage
Expand All @@ -15,19 +17,22 @@ public class Coverage
private string[] _excludeFilters;
private string[] _includeFilters;
private string[] _excludedSourceFiles;
private string _mergeWith;
private List<InstrumenterResult> _results;

public string Identifier
{
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>();
}
Expand Down Expand Up @@ -144,11 +149,14 @@ public CoverageResult GetCoverageResult()
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()
Expand Down
66 changes: 66 additions & 0 deletions src/coverlet.core/CoverageResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Coverlet.Core
{
Expand Down Expand Up @@ -38,5 +39,70 @@ public class CoverageResult
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;
}
}
}
}
}
}
}
}
}
}
}
}
9 changes: 8 additions & 1 deletion src/coverlet.msbuild.tasks/InstrumentationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class InstrumentationTask : Task
private string _exclude;
private string _include;
private string _excludeByFile;
private string _mergeWith;

internal static Coverage Coverage
{
Expand Down Expand Up @@ -43,6 +44,12 @@ public string ExcludeByFile
set { _excludeByFile = value; }
}

public string MergeWith
{
get { return _mergeWith; }
set { _mergeWith = value; }
}

public override bool Execute()
{
try
Expand All @@ -51,7 +58,7 @@ public override bool Execute()
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)
Expand Down
1 change: 1 addition & 0 deletions src/coverlet.msbuild/coverlet.msbuild.props
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions src/coverlet.msbuild/coverlet.msbuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Condition="'$(VSTestNoBuild)' == 'true' and $(CollectCoverage) == 'true'"
Exclude="$(Exclude)"
ExcludeByFile="$(ExcludeByFile)"
MergeWith="$(MergeWith)"
Path="$(TargetPath)" />
</Target>

Expand All @@ -16,6 +17,7 @@
Condition="'$(VSTestNoBuild)' != 'true' and $(CollectCoverage) == 'true'"
Exclude="$(Exclude)"
ExcludeByFile="$(ExcludeByFile)"
MergeWith="$(MergeWith)"
Path="$(TargetPath)" />
</Target>

Expand Down
2 changes: 1 addition & 1 deletion test/coverlet.core.tests/CoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestCoverage()
// 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();
Expand Down

0 comments on commit 307d33b

Please sign in to comment.