Skip to content

Commit

Permalink
#374: Added support for .netconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Sep 8, 2020
2 parents 681ffc3 + 2798f93 commit b4866d3
Show file tree
Hide file tree
Showing 7 changed files with 497 additions and 35 deletions.
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,52 @@ Examples:
"-reports:coverage.xml" "-targetdir:C:\report" "-assemblyfilters:+Included;-Excluded.*"
```

**MSBuild**
#### .netconfig support

All the above parameters can also be persisted in a [.netconfig](https://dotnetconfig.org) file, under a `[ReportGenerator]`
section. Examples:

```
[ReportGenerator]
reports = coverage.xml
targetdir = "C:\report"
reporttypes = Latex;HtmlSummary
assemblyfilters = +Test;-Test
classfilters = +Test2;-Test2
```

All the plural options can also be specified as multiple singular entries, like:

```
[ReportGenerator]
report = coverage1.xml
report = coverage2.xml
reporttype = Latex
reporttype = HtmlSummary
assemblyfilter = +Test
assemblyfilter = -Test
classfilter = +Test2
classfilter = -Test2
filefilter = +cs
filefilter = -vb
sourcedir = src
sourcedir = test
```

Adding/removing values is trivial using the [dotnet-config](https://dotnetconfig.org) CLI:

```
# set a single-valued variable
dotnet config reportgenerator.reporttypes Latex;HtmlSummary
# add to multi-valued variable
dotnet config --add reportgenerator.report coverage3.xml
# clear all multi-valued entries for a variable
dotnet config --unset-all reportgenerator.assemblyfilter
```

Of course it's equally trivial to just edit the `.netconfig` file by hand.

#### MSBuild
A MSBuild task also exists:

```xml
Expand All @@ -116,11 +161,41 @@ A MSBuild task also exists:
<ItemGroup>
<CoverageFiles Include="OpenCover.xml" />
</ItemGroup>
<ReportGenerator ReportFiles="@(CoverageFiles)" TargetDirectory="report" ReportTypes="Html;Latex" HistoryDirectory="history" Plugins="CustomReports.dll" AssemblyFilters="+Include;-Excluded" VerbosityLevel="Verbose" />
<ReportGenerator ProjectDirectory="$(MSBuildProjectDirectory)" ReportFiles="@(CoverageFiles)" TargetDirectory="report" ReportTypes="Html;Latex" HistoryDirectory="history" Plugins="CustomReports.dll" AssemblyFilters="+Include;-Excluded" VerbosityLevel="Verbose" />
</Target>
</Project>
```

The MSBuild task parameters can be complemented with the `.netconfig`, if used. That means that parameters
can be omitted if they are provided via `.netconfig`, which is useful when reusing fixed settings across
multiple projects in a solution, where the MSBuild task is only provided the dynamic values for the current
project:

Given the following `.netconfig`:

```
[ReportGenerator]
reporttypes = Html;Latex
targetdirectory = report
historydirectory = history
assemblyfilters = +Include;-Excluded
verbosityLevel = Verbose
```

The above target could be simplified as:

```xml
<Target Name="Coverage">
<ItemGroup>
<CoverageFiles Include="OpenCover.xml" />
</ItemGroup>
<ReportGenerator ProjectDirectory="$(MSBuildProjectDirectory)"
ReportFiles="@(CoverageFiles)"
Plugins="CustomReports.dll" />
</Target>
```


## Supported input and output file formats
*ReportGenerator* supports several input and output formats.
The wiki explains the different [output formats](https://github.com/danielpalme/ReportGenerator/wiki/Output-formats) or you can download [sample reports](https://danielpalme.github.io/ReportGenerator/resources/SampleReports.zip) of all formats.
Expand Down
2 changes: 2 additions & 0 deletions src/Deployment/nuget/ReportGenerator.Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ https://github.com/danielpalme/ReportGenerator/wiki/Custom-history-storage</desc
<dependency id="Microsoft.Extensions.Configuration.CommandLine" version="2.2.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="2.2.0" />
<dependency id="SixLabors.ImageSharp.Drawing" version="1.0.0-beta0008" />
<dependency id="DotNetConfig" version="1.0.0-rc" />
</group>

<group targetFramework="netcoreapp">
Expand All @@ -37,6 +38,7 @@ https://github.com/danielpalme/ReportGenerator/wiki/Custom-history-storage</desc
<dependency id="Microsoft.Extensions.Configuration.CommandLine" version="2.2.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="2.2.0" />
<dependency id="SixLabors.ImageSharp.Drawing" version="1.0.0-beta0008" />
<dependency id="DotNetConfig" version="1.0.0-rc" />
<dependency id="McMaster.NETCore.Plugins" version="0.2.4" />
</group>
</dependencies>
Expand Down
1 change: 1 addition & 0 deletions src/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CHANGELOG

4.6.7.0

* New: #374: Added support for .netconfig
* Fix: #363: Improved handling of HTML generation (several HTML reports at once)
* Fix: #385: Added filtering by namespace for classfilters in DynamicCodeCoverageParser
* Fix: #388: Removed branch coverage columns if no branch coverage is available
Expand Down
96 changes: 95 additions & 1 deletion src/ReportGenerator.Core.Test/ReportConfigurationBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Linq;
using DotNetConfig;
using Palmmedia.ReportGenerator.Core.Logging;
using Xunit;

Expand All @@ -10,17 +12,22 @@ namespace Palmmedia.ReportGenerator.Core.Test
/// to contain all ReportConfigurationBuilder Unit Tests
/// </summary>
[Collection("FileManager")]
public class ReportConfigurationBuilderTest
public class ReportConfigurationBuilderTest : IDisposable
{
private static readonly string ReportPath = Path.Combine(FileManager.GetCSharpReportDirectory(), "OpenCover.xml");

private ReportConfigurationBuilder reportConfigurationBuilder;

private string currentDir;

public ReportConfigurationBuilderTest()
{
this.reportConfigurationBuilder = new ReportConfigurationBuilder();
this.currentDir = Directory.GetCurrentDirectory();
}

public void Dispose() => Directory.SetCurrentDirectory(this.currentDir);

[Fact]
public void InitWithNamedArguments_OldFilters_AllPropertiesApplied()
{
Expand Down Expand Up @@ -70,5 +77,92 @@ public void InitWithNamedArguments_NewFilters_AllPropertiesApplied()
Assert.NotNull(configuration.AssemblyFilters);
Assert.NotNull(configuration.ClassFilters);
}


[Fact]
public void ConfigProvidesMissingArguments()
{
var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(dir);
var config = Config.Build(dir).GetSection(ReportConfigurationBuilder.SectionName);

config.SetString("reports", ReportPath);
config.SetString("targetdir", "C:\\temp");
config.SetString("reporttype", "Latex");
config.SetString("assemblyfilters", "+Test;-Test");
config.SetString("classfilters", "+Test2;-Test2");
config.SetString("verbosity", VerbosityLevel.Info.ToString());

Directory.SetCurrentDirectory(dir);

string[] namedArguments = new string[0];

var configuration = this.reportConfigurationBuilder.Create(namedArguments);

Assert.True(configuration.ReportFiles.Contains(ReportPath), "ReportPath does not exist in ReportFiles.");
Assert.Equal("C:\\temp", configuration.TargetDirectory);
Assert.True(configuration.ReportTypes.Contains("Latex"), "Wrong report type applied.");
Assert.True(configuration.AssemblyFilters.Contains("+Test"), "AssemblyFilters does not exist in ReportFiles.");
Assert.True(configuration.ClassFilters.Contains("+Test2"), "ClassFilters does not exist in ReportFiles.");
Assert.True(configuration.AssemblyFilters.Contains("-Test"), "AssemblyFilters does not exist in ReportFiles.");
Assert.True(configuration.ClassFilters.Contains("-Test2"), "ClassFilters does not exist in ReportFiles.");
Assert.NotNull(configuration.ReportFiles);
Assert.NotNull(configuration.AssemblyFilters);
Assert.NotNull(configuration.ClassFilters);
}

[Fact]
public void ConfigProvidesMultiValuedSettings()
{
var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(dir);
var config = Config.Build(dir).GetSection(ReportConfigurationBuilder.SectionName);

config.SetString("reports", ReportPath);

config.AddString("reporttype", "Latex");
config.AddString("reporttype", "Html");

config.AddString("assemblyfilter", "+Test");
config.AddString("assemblyfilter", "-Test");

config.AddString("classfilter", "+Test2");
config.AddString("classfilter", "-Test2");

config.AddString("filefilter", "+cs");
config.AddString("filefilter", "-vb");

config.AddString("sourcedir", "src");
config.AddString("sourcedir", "test");

config.AddString("plugin", "xunit");
config.AddString("plugin", "moq");

Directory.SetCurrentDirectory(dir);

string[] namedArguments = new string[0];

var configuration = this.reportConfigurationBuilder.Create(namedArguments);

Assert.Contains(ReportPath, configuration.ReportFiles);

Assert.Contains("Latex", configuration.ReportTypes);
Assert.Contains("Html", configuration.ReportTypes);

Assert.Contains("+Test", configuration.AssemblyFilters);
Assert.Contains("-Test", configuration.AssemblyFilters);

Assert.Contains("+Test2", configuration.ClassFilters);
Assert.Contains("-Test2", configuration.ClassFilters);

Assert.Contains("+cs", configuration.FileFilters);
Assert.Contains("-vb", configuration.FileFilters);

Assert.Contains("src", configuration.SourceDirectories);
Assert.Contains("test", configuration.SourceDirectories);

Assert.Contains("xunit", configuration.Plugins);
Assert.Contains("moq", configuration.Plugins);
}
}
}
Loading

0 comments on commit b4866d3

Please sign in to comment.