Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple output formats separated by a comma #78

Merged
merged 1 commit into from May 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
}
}
}