Skip to content

Commit 222eef7

Browse files
committed
Implementation exclusion of files according to source file paths (including file globbing)
1 parent f0ed61d commit 222eef7

File tree

10 files changed

+64
-28
lines changed

10 files changed

+64
-28
lines changed

src/coverlet.core/Coverage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class Coverage
1212
{
1313
private string _module;
1414
private string _identifier;
15+
private IEnumerable<string> _excludedFiles;
1516
private List<InstrumenterResult> _results;
1617

17-
public Coverage(string module, string identifier)
18+
public Coverage(string module, string identifier, IEnumerable<string> excludedFiles)
1819
{
1920
_module = module;
2021
_identifier = identifier;
22+
_excludedFiles = excludedFiles;
2123
_results = new List<InstrumenterResult>();
2224
}
2325

@@ -26,7 +28,7 @@ public void PrepareModules()
2628
string[] modules = InstrumentationHelper.GetDependencies(_module);
2729
foreach (var module in modules)
2830
{
29-
var instrumenter = new Instrumenter(module, _identifier);
31+
var instrumenter = new Instrumenter(module, _identifier, _excludedFiles);
3032
if (instrumenter.CanInstrument())
3133
{
3234
InstrumentationHelper.BackupOriginalModule(module, _identifier);

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using System.IO;
44
using System.Linq;
55
using System.Reflection.PortableExecutable;
6+
using Microsoft.Extensions.FileSystemGlobbing;
67

78
using Coverlet.Core.Instrumentation;
9+
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
810

911
namespace Coverlet.Core.Helpers
1012
{
11-
internal static class InstrumentationHelper
13+
public static class InstrumentationHelper
1214
{
1315
public static string[] GetDependencies(string module)
1416
{
@@ -106,5 +108,26 @@ public static void DeleteHitsFile(string path)
106108

107109
RetryHelper.Retry(() => File.Delete(path), retryStrategy, 10);
108110
}
111+
112+
public static string[] GetExcludedFiles(string[] exclusionRules, string parentDir) {
113+
if (exclusionRules == null || exclusionRules.Length == 0 ) return null;
114+
var matcher = new Matcher();
115+
foreach (var exclusionRule in exclusionRules)
116+
{
117+
matcher.AddInclude(exclusionRule);
118+
}
119+
120+
DirectoryInfo directoryInfo = new DirectoryInfo(parentDir);
121+
122+
var fileMatchResult = matcher.Execute(new DirectoryInfoWrapper(directoryInfo));
123+
return fileMatchResult.Files
124+
.Select(
125+
f => System.IO.Path.GetFullPath(
126+
System.IO.Path.Combine(directoryInfo.ToString(), f.Path)
127+
)
128+
)
129+
.ToArray();
130+
}
109131
}
110-
}
132+
}
133+

src/coverlet.core/Instrumentation/Instrumenter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
using Mono.Cecil;
99
using Mono.Cecil.Cil;
1010
using Mono.Cecil.Rocks;
11+
using System;
12+
using System.Xml.Linq;
1113

1214
namespace Coverlet.Core.Instrumentation
1315
{
1416
internal class Instrumenter
1517
{
1618
private string _module;
1719
private string _identifier;
20+
private IEnumerable<string> _excludedFiles;
1821
private InstrumenterResult _result;
1922

20-
public Instrumenter(string module, string identifier)
23+
public Instrumenter(string module, string identifier, IEnumerable<string> excludedFiles)
2124
{
2225
_module = module;
2326
_identifier = identifier;
27+
_excludedFiles = excludedFiles;
2428
}
2529

2630
public bool CanInstrument() => InstrumentationHelper.HasPdb(_module);
@@ -61,6 +65,10 @@ private void InstrumentModule()
6165

6266
foreach (var method in type.Methods)
6367
{
68+
var sourceFiles = method.DebugInformation.SequencePoints.Select(s => s.Document.Url).Distinct();
69+
if (sourceFiles.Any(_excludedFiles.Contains)) {
70+
continue;
71+
}
6472
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
6573
InstrumentMethod(method);
6674
}

src/coverlet.core/coverlet.core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PackageReference Include="Jil" Version="2.15.4" />
1010
<PackageReference Include="Mono.Cecil" Version="0.10.0" />
1111
<PackageReference Include="System.Reflection.Metadata" Version="1.5.0" />
12+
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="2.0.1" />
1213
</ItemGroup>
1314

1415
</Project>

src/coverlet.msbuild.tasks/InstrumentationTask.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
using System;
22
using Coverlet.Core;
3-
3+
using Coverlet.Core.Helpers;
44
using Microsoft.Build.Framework;
5-
using Microsoft.Build.Utilities;
6-
5+
using Microsoft.Build.Utilities;
6+
77
namespace Coverlet.MSbuild.Tasks
88
{
99
public class InstrumentationTask : Task
10-
{
11-
private string _path;
12-
private static Coverage _coverage;
13-
14-
internal static Coverage Coverage
15-
{
16-
get { return _coverage; }
17-
private set { _coverage = value; }
18-
}
10+
{
11+
internal static Coverage Coverage { get; private set; }
1912

2013
[Required]
21-
public string Path
22-
{
23-
get { return _path; }
24-
set { _path = value; }
25-
}
26-
14+
public string Path { get; set; }
15+
16+
[Required]
17+
public string[] ExclusionRules { get; set; }
18+
19+
public string ExclusionParentDir { get; set; }
20+
2721
public override bool Execute()
2822
{
2923
try
3024
{
31-
_coverage = new Coverage(_path, Guid.NewGuid().ToString());
32-
_coverage.PrepareModules();
25+
var excludedFiles = InstrumentationHelper.GetExcludedFiles(
26+
ExclusionRules, ExclusionParentDir);
27+
Coverage = new Coverage(Path, Guid.NewGuid().ToString(), excludedFiles);
28+
Coverage.PrepareModules();
3329
}
3430
catch(Exception ex)
3531
{

src/coverlet.msbuild/coverlet.msbuild.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<CoverletOutputDirectory Condition="$(CoverletOutputDirectory) == ''">$(MSBuildProjectDirectory)</CoverletOutputDirectory>
77
<CoverletOutputName Condition=" '$(CoverletOutputName)' == '' ">coverage</CoverletOutputName>
88
<CoverletOutput>$([MSBuild]::EnsureTrailingSlash('$(CoverletOutputDirectory)'))$(CoverletOutputName)</CoverletOutput>
9+
<CoverletExclusionParentDir Condition="$(CoverletExclusionParentDir) == ''">$(MSBuildProjectDirectory)</CoverletExclusionParentDir>
910

1011
<Threshold Condition="$(Threshold) == ''">0</Threshold>
1112
<CollectCoverage Condition="$(CollectCoverage) == ''">false</CollectCoverage>

src/coverlet.msbuild/coverlet.msbuild.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
<Target Name="InstrumentModulesNoBuild" BeforeTargets="VSTest">
77
<Coverlet.MSbuild.Tasks.InstrumentationTask
88
Condition="'$(VSTestNoBuild)' == 'true' and $(CollectCoverage) == 'true'"
9+
ExclusionRules="$(ExclusionRules)"
10+
ExclusionParentDir="$(CoverletExclusionParentDir)"
911
Path="$(TargetPath)" />
1012
</Target>
1113

1214
<Target Name="InstrumentModulesAfterBuild" AfterTargets="BuildProject">
1315
<Coverlet.MSbuild.Tasks.InstrumentationTask
1416
Condition="'$(VSTestNoBuild)' != 'true' and $(CollectCoverage) == 'true'"
17+
ExclusionRules="$(ExclusionRules)"
18+
ExclusionParentDir="$(CoverletExclusionParentDir)"
1519
Path="$(TargetPath)" />
1620
</Target>
1721

test/coverlet.core.tests/CoverageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void TestCoverage()
2121

2222
File.Copy(module, tempModule, true);
2323

24-
var coverage = new Coverage(tempModule, identifier);
24+
var coverage = new Coverage(tempModule, identifier, null);
2525
coverage.PrepareModules();
2626

2727
var result = coverage.GetCoverageResult();

test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Xunit;
55
using Coverlet.Core.Instrumentation;
6+
using Coverlet.MSbuild.Tasks;
67

78
namespace Coverlet.Core.Instrumentation.Tests
89
{
@@ -21,8 +22,7 @@ public void TestInstrument()
2122
File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true);
2223

2324
module = Path.Combine(directory.FullName, Path.GetFileName(module));
24-
25-
Instrumenter instrumenter = new Instrumenter(module, identifier);
25+
Instrumenter instrumenter = new Instrumenter(module, identifier, null);
2626
var result = instrumenter.Instrument();
2727

2828
Assert.Equal(Path.GetFileNameWithoutExtension(module), result.Module);

test/coverlet.core.tests/coverlet.core.tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<ItemGroup>
1919
<ProjectReference Include="..\..\src\coverlet.core\coverlet.core.csproj" />
20+
<ProjectReference Include="..\..\src\coverlet.msbuild.tasks\coverlet.msbuild.tasks.csproj" />
2021
</ItemGroup>
2122

2223
<Import Project="$(MSBuildThisFileDirectory)\..\..\build\$(Configuration)\coverlet.msbuild.targets" />

0 commit comments

Comments
 (0)