Skip to content

Commit 8183f3f

Browse files
authored
Merge pull request #377 from tonyhallett/common-exclude-include-assembly
Common exclude include assembly
2 parents eb2ad6d + 451e157 commit 8183f3f

File tree

8 files changed

+184
-81
lines changed

8 files changed

+184
-81
lines changed

FineCodeCoverageTests/AppOptionsProvider_Tests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ internal void Should_Use_Deseralized_String_From_Store_For_AppOption_Property(Fu
307307
{ nameof(IAppOptions.ShowPartiallyCoveredInOverviewMargin),true},
308308
{ nameof(IAppOptions.ShowUncoveredInOverviewMargin),true},
309309
{ nameof(IAppOptions.ShowToolWindowToolbar),true},
310+
{nameof(IAppOptions.ExcludeAssemblies),new string[]{ "Exclude"} },
311+
{nameof(IAppOptions.IncludeAssemblies),new string[]{ "Include"} },
310312
};
311313
var mockJsonConvertService = autoMocker.GetMock<IJsonConvertService>();
312314
mockJsonConvertService.Setup(

FineCodeCoverageTests/CoverageProject_Settings_Tests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using AutoMoq;
12
using FineCodeCoverage.Core.Utilities;
23
using FineCodeCoverage.Engine.Model;
34
using FineCodeCoverage.Options;
@@ -12,7 +13,7 @@
1213
using System.Threading.Tasks;
1314
using System.Xml.Linq;
1415

15-
namespace Test
16+
namespace FineCodeCoverageTests
1617
{
1718
public class CoverageProject_Settings_Tests
1819
{
@@ -675,5 +676,34 @@ public async Task Should_Provide_The_Merged_Result_Using_Project_Settings()
675676
var coverageProjectSettings = await coverageProjectSettingsManager.GetSettingsAsync(coverageProject);
676677
Assert.AreSame(mergedSettings, coverageProjectSettings);
677678
}
679+
680+
[Test]
681+
public async Task Should_Add_Common_Assembly_Excludes_Includes()
682+
{
683+
var mockAppOptions = new Mock<IAppOptions>();
684+
mockAppOptions.SetupAllProperties();
685+
var appOptions = mockAppOptions.Object;
686+
appOptions.Exclude = new string[] { "oldexclude" };
687+
appOptions.Include = new string[] { "oldinclude" };
688+
appOptions.ModulePathsExclude = new string[] { "msexclude" };
689+
appOptions.ModulePathsInclude = new string[] { "msinclude" };
690+
appOptions.ExcludeAssemblies = new string[] { "excludeassembly" };
691+
appOptions.IncludeAssemblies = new string[] { "includeassembly" };
692+
693+
var autoMoqer = new AutoMoqer();
694+
var coverageProjectSettingsManager = autoMoqer.Create<CoverageProjectSettingsManager>();
695+
autoMoqer.GetMock<ISettingsMerger>().Setup(settingsMerger => settingsMerger.Merge(
696+
It.IsAny<IAppOptions>(),
697+
It.IsAny<List<XElement>>(),
698+
It.IsAny<XElement>()
699+
)).Returns(appOptions);
700+
701+
var settings = await coverageProjectSettingsManager.GetSettingsAsync(new Mock<ICoverageProject>().Object);
702+
703+
Assert.That(settings.Exclude, Is.EquivalentTo(new string[] { "oldexclude", "[excludeassembly]*" }));
704+
Assert.That(settings.Include, Is.EquivalentTo(new string[] { "oldinclude", "[includeassembly]*" }));
705+
Assert.That(settings.ModulePathsExclude, Is.EquivalentTo(new string[] { "msexclude", ".*\\excludeassembly.dll$" }));
706+
Assert.That(settings.ModulePathsInclude, Is.EquivalentTo(new string[] { "msinclude", ".*\\includeassembly.dll$" }));
707+
}
678708
}
679709
}

FineCodeCoverageTests/MsCodeCoverage/RunSettingsTemplateReplacementsFactory_Tests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ internal class TestMsCodeCoverageOptions : IMsCodeCoverageOptions
3030

3131
public bool IncludeTestAssembly { get; set; }
3232
public bool IncludeReferencedProjects { get; set; }
33+
public string[] ExcludeAssemblies { get; set; }
34+
public string[] IncludeAssemblies { get; set; }
3335
}
3436

3537
internal static class ReplacementsAssertions
@@ -51,6 +53,7 @@ public static void AssertAllEmpty(IRunSettingsTemplateReplacements replacements)
5153
}
5254
}
5355

56+
5457
internal class RunSettingsTemplateReplacementsFactory_UserRunSettings_Tests
5558
{
5659
private RunSettingsTemplateReplacementsFactory runSettingsTemplateReplacementsFactory;
@@ -694,5 +697,7 @@ internal class TestCoverageProjectOptions : IAppOptions
694697
public bool ShowToolWindowToolbar { get; set; }
695698
public bool Hide0Coverable { get; set; }
696699
public bool Hide0Coverage { get; set; }
700+
public string[] ExcludeAssemblies { get; set; }
701+
public string[] IncludeAssemblies { get; set; }
697702
}
698703
}

README.md

Lines changed: 84 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tool for most developers. It is currently in Beta.
2020

2121
With the old coverage it was possible for FCC to provide an abstraction over each tool's exclusion / inclusion options. This abstraction does not work for MS code coverage.
2222
Thus you will find that there are separate configuration options for Ms coverage vs old coverage and options that are common to the two.
23+
Assembly level exclusions and inclusions can be achieved - see ExcludeAssemblies and IncludeAssemblies.
2324
Configuration is ( mostly ) determined from Visual Studio options, finecodecoverage-settings.xml files and project msbuild properties. All of these settings are optional.
2425
For options that have a project scope, these settings form a hierarchy where lower levels override or, for collections, override or merge with the level above. This is described in detail further on.
2526

@@ -227,101 +228,107 @@ If you are using option 1) then project and global options will only be used whe
227228

228229

229230
#### Options
230-
```
231-
*** Common
232-
CoverageColoursFromFontsAndColours Specify true to use Environment / Fonts and Colors / Text Editor for editor Coverage colouring ( if present).
233-
Coverage Touched Area / Coverage Not Touched Area / Coverage Partially Touched Area.
234-
When false colours used are Green, Red and Gold.
231+
|Option |Description|
232+
|--|---|
233+
|**Common**||
234+
|CoverageColoursFromFontsAndColours|Specify true to use Environment / Fonts and Colors / Text Editor for editor Coverage colouring ( if present). Coverage Touched Area / Coverage Not Touched Area / Coverage Partially Touched Area. When false colours used are Green, Red and Gold.|
235+
|ShowCoverageInOverviewMargin|Set to false to prevent coverage marks in the overview margin|
236+
|ShowCoveredInOverviewMargin|Set to false to prevent covered marks in the overview margin|
237+
|ShowUncoveredInOverviewMargin|Set to false to prevent uncovered marks in the overview margin|
238+
|ShowPartiallyCoveredInOverviewMargin|Set to false to prevent partially covered marks in the overview margin|
239+
|ShowToolWindowToolbar|Set to false to hide the toolbar on the tool window. Requires restarting Visual Studio. The toolbar has buttons for viewing the Cobertura xml and the risk hotspots.|
240+
|FCCSolutionOutputDirectoryName|To have fcc output visible in a sub folder of your solution provide this name|
241+
|ToolsDirectory|Folder to which copy tools subfolder. Must alredy exist. Requires restart of VS.|
242+
|ThresholdForCyclomaticComplexity| When [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) exceeds this value for a method then the method will be present in the risk hotspots tab. |
243+
|StickyCoverageTable|Set to true for coverage table to have a sticky thead.|
244+
|NamespacedClasses|Set to false to show classes in report in short form. Affects grouping.|
245+
|HideFullyCovered|Set to true to hide classes, namespaces and assemblies that are fully covered.|
246+
|Hide0Coverage|Set to true to hide classes, namespaces and assemblies that have 0% coverage.|
247+
|Hide0Coverable|Set to false to show classes, namespaces and assemblies that are not coverable.|
248+
|Enabled|Specifies whether or not coverage output is enabled|
249+
|RunWhenTestsFail|By default coverage runs when tests fail. Set to false to prevent this. **Cannot be used in conjunction with RunInParallel**|
250+
|RunWhenTestsExceed|Specify a value to only run coverage based upon the number of executing tests. **Cannot be used in conjunction with RunInParallel**|
251+
|RunMsCodeCoverage|Change to IfInRunSettings to only collect with configured runsettings. Yes for runsettings generation.|
252+
|IncludeTestAssembly|Specifies whether to report code coverage of the test assembly|
253+
|IncludeReferencedProjects|Set to true to add all directly referenced projects to Include.|
254+
|IncludeAssemblies|Provide a list of assemblies to include in coverage. The dll name without extension is used for matching.|
255+
|ExcludeAssemblies| Provide a list of assemblies to exclude from coverage. The dll name without extension is used for matching.|
256+
|<br>||
257+
|**OpenCover / Coverlet**||
258+
|AdjacentBuildOutput|If your tests are dependent upon their path set this to true.|
259+
|Exclude|Filter expressions to exclude specific modules and types (multiple values)|
260+
|Include|Filter expressions to include specific modules and types (multiple values)|
261+
|ExcludeByFile|Glob patterns specifying source files to exclude e.g. **/Migrations/* (multiple values)|
262+
|ExcludeByAttribute|Attributes to exclude from code coverage (multiple values)|
263+
|RunInParallel|By default OpenCover / Coverlet tests run and then coverage is performed. Set to true to run coverage immediately|
264+
|<br>||
265+
|**Ms code coverage**|Each of below is an array of regexes to be transformed into runsettings elements [see](https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022#include-or-exclude-assemblies-and-members)|
266+
|ModulePathsExclude|Exclude - Matches assemblies specified by assembly name or file path.|
267+
|ModulePathsInclude|Include - Matches assemblies specified by assembly name or file path.|
268+
|CompanyNamesExclude|Exclude - Matches assemblies by the Company attribute.|
269+
|CompanyNamesInclude|Include - Matches assemblies by the Company attribute.|
270+
|PublicKeyTokensExclude|Exclude - Matches signed assemblies by the public key token.|
271+
|PublicKeyTokensInclude|Include - Matches signed assemblies by the public key token.|
272+
|SourcesExclude|Exclude - Matches elements by the path name of the source file in which they're defined.|
273+
|SourcesInclude|Include - Matches elements by the path name of the source file in which they're defined.|
274+
|AttributesExclude|Exclude - Matches elements that have the specified attribute. Specify the full name of the attribute|
275+
|AttributesInclude|Include - Matches elements that have the specified attribute. Specify the full name of the attribute|
276+
|FunctionsExclude|Exclude - Matches procedures, functions, or methods by fully qualified name, including the parameter list.|
277+
|FunctionsInclude|Include - Matches procedures, functions, or methods by fully qualified name, including the parameter list.|
278+
|<br>||
279+
|**Coverlet**||
280+
|RunSettingsOnly|Specify false for global and project options to be used for coverlet data collector configuration elements when not specified in runsettings|
281+
|CoverletCollectorDirectoryPath|Specify path to directory containing coverlet collector files if you need functionality that the FCC version does not provide.|
282+
|CoverletConsoleLocal|Specify true to use your own dotnet tools local install of coverlet console.|
283+
|CoverletConsoleCustomPath|Specify path to coverlet console exe if you need functionality that the FCC version does not provide.|
284+
|CoverletConsoleGlobal|Specify true to use your own dotnet tools global install of coverlet console.|
285+
|**The "CoverletConsole" settings have precedence Local / CustomPath / Global.**||
286+
|<br>||
287+
|**OpenCover**||
288+
|OpenCoverCustomPath|Specify path to open cover exe if you need functionality that the FCC version does not provide.|
289+
|ThresholdForNPathComplexity|When [npath complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) exceeds this value for a method then the method will be present in the risk hotspots tab.|
290+
|ThresholdForCrapScore|When [crap score](https://testing.googleblog.com/2011/02/this-code-is-crap.html) exceeds this value for a method then the method will be present in the risk hotspots tab.|
291+
235292

236-
ShowCoverageInOverviewMargin Set to false to prevent coverage marks in the overview margin
237-
ShowCoveredInOverviewMargin Set to false to prevent covered marks in the overview margin
238-
ShowUncoveredInOverviewMargin Set to false to prevent uncovered marks in the overview margin
239-
ShowPartiallyCoveredInOverviewMargin Set to false to prevent partially covered marks in the overview margin
293+
## Exclusions and inclusions
294+
You probably want to set IncludeReferencedProjects to true. This will ensure that you do not get coverage for testing frameworks - only your code.
240295

241-
ShowToolWindowToolbar Set to false to hide the toolbar on the tool window. Requires restarting Visual Studio. The toolbar has buttons for viewing
242-
the Cobertura xml and the risk hotspots.
296+
Coverlet and OpenCover use filter expressions.
297+
Filter expressions
243298

244-
FCCSolutionOutputDirectoryName To have fcc output visible in a sub folder of your solution provide this name
299+
Wildcards
245300

246-
ToolsDirectory Folder to which copy tools subfolder. Must alredy exist. Requires restart of VS.
301+
\* => matches zero or more characters
302+
303+
Examples
247304

248-
ThresholdForCyclomaticComplexity When [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) exceeds this value for a method then the method will be present in the risk hotspots tab.
305+
[\*]* => All types in all assemblies.
249306

250-
StickyCoverageTable Set to true for coverage table to have a sticky thead.
251-
NamespacedClasses Set to false to show classes in report in short form. Affects grouping.
252-
HideFullyCovered Set to true to hide classes, namespaces and assemblies that are fully covered.
253-
Hide0Coverage Set to true to hide classes, namespaces and assemblies that have 0% coverage.
254-
Hide0Coverable Set to false to show classes, namespaces and assemblies that are not coverable.
307+
[coverlet\.\*]Coverlet.Core.Coverage => The Coverage class in the Coverlet.Core namespace belonging to any assembly that matches coverlet.* (e.g coverlet.core)
255308

256-
Enabled Specifies whether or not coverage output is enabled
257-
RunWhenTestsFail By default coverage runs when tests fail. Set to false to prevent this. **Cannot be used in conjunction with RunInParallel**
258-
RunWhenTestsExceed Specify a value to only run coverage based upon the number of executing tests. **Cannot be used in conjunction with RunInParallel**
259-
RunMsCodeCoverage Change to IfInRunSettings to only collect with configured runsettings. Yes for runsettings generation.
309+
[\*\]Coverlet.Core.Instrumentation.* => All types belonging to Coverlet.Core.Instrumentation namespace in any assembly
260310

261-
IncludeTestAssembly Specifies whether to report code coverage of the test assembly
262-
IncludeReferencedProjects Set to true to add all referenced projects to Include.
311+
[coverlet\.\*.tests]* => All types in any assembly starting with coverlet. and ending with .tests
263312

264-
*** OpenCover / Coverlet
265-
AdjacentBuildOutput If your tests are dependent upon their path set this to true.
313+
Both 'Exclude' and 'Include' options can be used together but 'Exclude' takes precedence.
266314

267-
Exclude Filter expressions to exclude specific modules and types (multiple values)
268-
Include Filter expressions to include specific modules and types (multiple values)
269-
ExcludeByFile Glob patterns specifying source files to exclude e.g. **/Migrations/* (multiple values)
270-
ExcludeByAttribute Attributes to exclude from code coverage (multiple values)
271-
RunInParallel By default OpenCover / Coverlet tests run and then coverage is performed. Set to true to run coverage immediately
315+
Ms code coverage uses [regexes](https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022#regular-expressions).
316+
You can include or exclude assemblies or specific types and members from code coverage analysis. If the Include section is empty or omitted, then all assemblies that are loaded and have associated PDB files are included. If an assembly or member matches a clause in the Exclude section, then it is excluded from code coverage. The Exclude section takes precedence over the Include section: if an assembly is listed in both Include and Exclude, it will not be included in code coverage.
272317

273-
Filter expressions
274-
Wildcards
275-
* => matches zero or more characters
276-
277-
Examples
278-
[*]* => All types in all assemblies (nothing is instrumented)
279-
[coverlet.*]Coverlet.Core.Coverage => The Coverage class in the Coverlet.Core namespace belonging to any assembly that matches coverlet.* (e.g coverlet.core)
280-
[*]Coverlet.Core.Instrumentation.* => All types belonging to Coverlet.Core.Instrumentation namespace in any assembly
281-
[coverlet.*.tests]* => All types in any assembly starting with coverlet. and ending with .tests
282318

319+
You can ignore a method or an entire class from code coverage by applying the [ExcludeFromCodeCoverage] attribute present in the System.Diagnostics.CodeAnalysis namespace.
283320

284-
Both 'Exclude' and 'Include' options can be used together but 'Exclude' takes precedence.
321+
For .Net Framework this attribute cannot be applied at the assembly level. See FCCExcludeFromCodeCoverage above for similar functinality.
285322

286-
You can ignore a method or an entire class from code coverage by creating and applying the [ExcludeFromCodeCoverage] attribute present in the System.Diagnostics.CodeAnalysis namespace.
287-
You can also ignore additional attributes by adding to the 'ExcludeByAttributes' list (short name or full name supported) e.g. :
288-
[GeneratedCode] => Present in System.CodeDom.Compiler namespace
289-
[MyCustomExcludeFromCodeCoverage] => Any custom attribute that you may define
323+
You can also ignore additional attributes by adding to the 'ExcludeByAttributes' list for Coverlet/OpenCover (short name or full name supported)
290324

291-
*** MS Code Coverage each multiple regexes to be transformed into runsettings elements
292-
ModulePathsExclude
293-
ModulePathsInclude
294-
CompanyNamesExclude
295-
CompanyNamesInclude
296-
PublicKeyTokensExclude
297-
PublicKeyTokensInclude
298-
SourcesExclude
299-
SourcesInclude
300-
AttributesExclude
301-
AttributesInclude
302-
FunctionsExclude
303-
FunctionsInclude
304-
305-
*** Coverlet
306-
RunSettingsOnly Specify false for global and project options to be used for coverlet data collector configuration elements when not specified in runsettings
307-
CoverletCollectorDirectoryPath Specify path to directory containing coverlet collector files if you need functionality that the FCC version does not provide.
308-
CoverletConsoleLocal Specify true to use your own dotnet tools local install of coverlet console.
309-
CoverletConsoleCustomPath Specify path to coverlet console exe if you need functionality that the FCC version does not provide.
310-
CoverletConsoleGlobal Specify true to use your own dotnet tools global install of coverlet console.
311-
312-
The "CoverletConsole" settings have precedence Local / CustomPath / Global.
313-
314-
*** OpenCover
315-
OpenCoverCustomPath Specify path to open cover exe if you need functionality that the FCC version does not provide.
316-
ThresholdForNPathComplexity When [npath complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) exceeds this value for a method then the method will be present in the risk hotspots tab. OpenCover only.
317-
ThresholdForCrapScore When [crap score](https://testing.googleblog.com/2011/02/this-code-is-crap.html) exceeds this value for a method then the method will be present in the risk hotspots tab. OpenCover only.
325+
e.g. :
318326

327+
[GeneratedCode] => Present in System.CodeDom.Compiler namespace
319328

320-
321-
```
322-
## Exclusions and inclusions
323-
You probably want to set IncludeReferencedProjects to true. This will ensure that you do not get coverage for testing frameworks - only your code.
329+
[MyCustomExcludeFromCodeCoverage] => Any custom attribute that you may define
324330

331+
or for ms code coverage - AttributesExclude
325332

326333
## FCC Output
327334
FCC outputs, by default, inside each test project's Debug folder.

0 commit comments

Comments
 (0)