Skip to content

Commit

Permalink
build list of reporters from factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed May 5, 2018
1 parent 227a7b0 commit ad60a1c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 51 deletions.
12 changes: 5 additions & 7 deletions src/coverlet.core/Reporters/ReporterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ namespace Coverlet.Core.Reporters
{
public class ReporterFactory
{
private IEnumerable<string> _formats;
private string _format;
private IReporter[] _reporters;

public ReporterFactory(string formats)
public ReporterFactory(string format)
{
_formats = formats.Split(',');
_format = format;
_reporters = new IReporter[] {
new JsonReporter(), new LcovReporter(),
new OpenCoverReporter(), new CoberturaReporter()
};
}

public IEnumerable<IReporter> CreateReporters()
{
return _reporters.Where(r => _formats.Contains(r.Format));
}
public IReporter CreateReporter()
=> _reporters.FirstOrDefault(r => r.Format == _format);
}
}
17 changes: 9 additions & 8 deletions src/coverlet.msbuild.tasks/CoverageResultTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ public override bool Execute()
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

var reporters = new List<IReporter>(new ReporterFactory(_format).CreateReporters());
if (reporters.Count != _format.Split(',').Length)
throw new Exception($"Specified output format '{_format}' is not supported");

foreach(var reporter in reporters)
var formats = _format.Split(',');
foreach(var format in formats)
{
var reportFilename = _filename + "." + reporter.Extension;
Console.WriteLine($" Generating report '{reportFilename}'");
File.WriteAllText(reportFilename, reporter.Report(result));
var reporter = new ReporterFactory(format).CreateReporter();
if (reporter == null)
throw new Exception($"Specified output format '{format}' is not supported");

var report = _filename + "." + reporter.Extension;
Console.WriteLine($" Generating report '{report}'");
File.WriteAllText(report, reporter.Report(result));
}

double total = 0;
Expand Down
2 changes: 0 additions & 2 deletions src/coverlet.msbuild/coverlet.msbuild.props
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>

<CoverletOutputFormat Condition="$(CoverletOutputFormat) == ''">json</CoverletOutputFormat>
<CoverletOutputDirectory Condition="$(CoverletOutputDirectory) == ''">$(MSBuildProjectDirectory)</CoverletOutputDirectory>
<CoverletOutputName Condition=" '$(CoverletOutputName)' == '' ">coverage</CoverletOutputName>
<CoverletOutput>$([MSBuild]::EnsureTrailingSlash('$(CoverletOutputDirectory)'))$(CoverletOutputName)</CoverletOutput>
<Exclude Condition="$(Exclude) == ''"></Exclude>
<Threshold Condition="$(Threshold) == ''">0</Threshold>
<CollectCoverage Condition="$(CollectCoverage) == ''">false</CollectCoverage>

</PropertyGroup>

</Project>
40 changes: 6 additions & 34 deletions test/coverlet.core.tests/Reporters/ReporterFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,13 @@ namespace Coverlet.Core.Reporters.Tests
public class ReporterFactoryTests
{
[Fact]
public void TestCreateReportersWithSingleFormat()
public void TestCreateReporter()
{
Assert.Collection(
new ReporterFactory("json").CreateReporters(),
reporter => Assert.IsType<JsonReporter>(reporter));
Assert.Collection(
new ReporterFactory("lcov").CreateReporters(),
reporter => Assert.IsType<LcovReporter>(reporter));
Assert.Collection(
new ReporterFactory("opencover").CreateReporters(),
reporter => Assert.IsType<OpenCoverReporter>(reporter));
Assert.Collection(
new ReporterFactory("cobertura").CreateReporters(),
reporter => Assert.IsType<CoberturaReporter>(reporter));
Assert.Empty(new ReporterFactory("").CreateReporters());
}

[Fact]
public void TestCreateReportersWithMultipleFormats()
{
Assert.Collection(
new ReporterFactory("json,lcov").CreateReporters(),
reporter => Assert.IsType<JsonReporter>(reporter),
reporter => Assert.IsType<LcovReporter>(reporter));
Assert.Collection(
new ReporterFactory("json,lcov,opencover").CreateReporters(),
reporter => Assert.IsType<JsonReporter>(reporter),
reporter => Assert.IsType<LcovReporter>(reporter),
reporter => Assert.IsType<OpenCoverReporter>(reporter));
Assert.Collection(
new ReporterFactory("json,lcov,opencover,cobertura").CreateReporters(),
reporter => Assert.IsType<JsonReporter>(reporter),
reporter => Assert.IsType<LcovReporter>(reporter),
reporter => Assert.IsType<OpenCoverReporter>(reporter),
reporter => Assert.IsType<CoberturaReporter>(reporter));
Assert.Equal(typeof(JsonReporter), new ReporterFactory("json").CreateReporter().GetType());
Assert.Equal(typeof(LcovReporter), new ReporterFactory("lcov").CreateReporter().GetType());
Assert.Equal(typeof(OpenCoverReporter), new ReporterFactory("opencover").CreateReporter().GetType());
Assert.Equal(typeof(CoberturaReporter), new ReporterFactory("cobertura").CreateReporter().GetType());
Assert.Null(new ReporterFactory("").CreateReporter());
}
}
}

0 comments on commit ad60a1c

Please sign in to comment.