Skip to content

Commit

Permalink
Merge pull request #78 from patros/multiple-report-formats
Browse files Browse the repository at this point in the history
Add support for multiple output formats separated by a comma
  • Loading branch information
tonerdo committed May 5, 2018
2 parents f61bc50 + dddcf26 commit 227a7b0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
13 changes: 8 additions & 5 deletions src/coverlet.core/Reporters/ReporterFactory.cs
@@ -1,22 +1,25 @@
using System.Linq;
using System.Collections.Generic;

namespace Coverlet.Core.Reporters
{
public class ReporterFactory
{
private string _format;
private IEnumerable<string> _formats;
private IReporter[] _reporters;

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

public IReporter CreateReporter()
=> _reporters.FirstOrDefault(r => r.Format == _format);
public IEnumerable<IReporter> CreateReporters()
{
return _reporters.Where(r => _formats.Contains(r.Format));
}
}
}
14 changes: 9 additions & 5 deletions src/coverlet.msbuild.tasks/CoverageResultTask.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using ConsoleTables;

Expand Down Expand Up @@ -49,13 +50,16 @@ public override bool Execute()
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

IReporter reporter = new ReporterFactory(_format).CreateReporter();
if (reporter == null)
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");

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

double total = 0;
CoverageSummary summary = new CoverageSummary();
Expand Down
40 changes: 34 additions & 6 deletions test/coverlet.core.tests/Reporters/ReporterFactoryTests.cs
Expand Up @@ -6,13 +6,41 @@ namespace Coverlet.Core.Reporters.Tests
public class ReporterFactoryTests
{
[Fact]
public void TestCreateReporter()
public void TestCreateReportersWithSingleFormat()
{
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());
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));
}
}
}

0 comments on commit 227a7b0

Please sign in to comment.