Skip to content

Commit

Permalink
Add DotCover Report and refactor
Browse files Browse the repository at this point in the history
Solves #724
  • Loading branch information
bjorkstromm committed Oct 13, 2016
1 parent 83183ee commit 8e7b783
Show file tree
Hide file tree
Showing 15 changed files with 606 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Cake.Common.Tools.DotCover.Report;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using NSubstitute;

namespace Cake.Common.Tests.Fixtures.Tools.DotCover.Report
{
internal sealed class DotCoverReporterFixture : DotCoverFixture<DotCoverReportSettings>
{
public FilePath SourceFile { get; set; }
public FilePath OutputFile { get; set; }

public DotCoverReporterFixture()
{
// Set the source file.
SourceFile = new FilePath("./result.dcvr");

// Setup the output file.
OutputFile = new FilePath("./result.xml");
}

protected override void RunTool()
{
var tool = new DotCoverReporter(FileSystem, Environment, ProcessRunner, Tools);
tool.Report(SourceFile, OutputFile, Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Tools.DotCover.Report;
using Cake.Common.Tools.DotCover;
using Cake.Common.Tools.NUnit;
using Cake.Common.Tools.XUnit;
using Cake.Core.IO;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotCover.Report
{
public sealed class DotCoverReporterTests
{
public sealed class TheReportMethod
{
[Fact]
public void Should_Throw_If_Source_File_Is_Null()
{
// Given
var fixture = new DotCoverReporterFixture();
fixture.SourceFile = null;

// When
var result = Record.Exception(() => fixture.Run());

// Then
Assert.IsArgumentNullException(result, "sourceFile");
}

[Fact]
public void Should_Throw_If_Output_File_Is_Null()
{
// Given
var fixture = new DotCoverReporterFixture();
fixture.OutputFile = null;

// When
var result = Record.Exception(() => fixture.Run());

// Then
Assert.IsArgumentNullException(result, "outputFile");
}

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotCoverReporterFixture();
fixture.Settings = null;

// When
var result = Record.Exception(() => fixture.Run());

// Then
Assert.IsArgumentNullException(result, "settings");
}

[Theory]
[InlineData(DotCoverReportType.DetailedXML, "DetailedXML")]
[InlineData(DotCoverReportType.HTML, "HTML")]
[InlineData(DotCoverReportType.JSON, "JSON")]
[InlineData(DotCoverReportType.NDependXML, "NDependXML")]
public void Should_Append_ReportType(DotCoverReportType reportType, string reportTypeString)
{
// Given
var fixture = new DotCoverReporterFixture();
fixture.Settings.ReportType = reportType;

// When
var result = fixture.Run();

// Then
Assert.Equal("Report " +
"/Source=\"/Working/result.dcvr\" " +
"/Output=\"/Working/result.xml\" " +
"/ReportType=" + reportTypeString, result.Args);
}

[Fact]
public void Should_Append_LogFile()
{
// Given
var fixture = new DotCoverReporterFixture();
fixture.Settings.LogFile = "./logfile.log";

// When
var result = fixture.Run();

// Then
Assert.Equal("Report " +
"/Source=\"/Working/result.dcvr\" " +
"/Output=\"/Working/result.xml\" " +
"/LogFile=\"/Working/logfile.log\"", result.Args);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Cake.Common.Tools.DotCover.Analyse
/// <summary>
/// Contains settings used by <see cref="DotCoverAnalyser" />.
/// </summary>
public sealed class DotCoverAnalyseSettings : DotCoverSettings
public sealed class DotCoverAnalyseSettings : DotCoverCoverageSettings
{
/// <summary>
/// Gets or sets the type of the report.
Expand Down
45 changes: 11 additions & 34 deletions src/Cake.Common/Tools/DotCover/Analyse/DotCoverAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Cake.Common.Tools.DotCover.Analyse
/// <summary>
/// DotCover Analyser builder.
/// </summary>
public sealed class DotCoverAnalyser : DotCoverTool<DotCoverAnalyseSettings>
public sealed class DotCoverAnalyser : DotCoverCoverageTool<DotCoverAnalyseSettings>
{
private readonly ICakeEnvironment _environment;

Expand Down Expand Up @@ -61,48 +61,22 @@ public void Analyse(ICakeContext context,
throw new ArgumentNullException(nameof(settings));
}

// Run the tool using the interceptor.
var interceptor = InterceptAction(context, action);

// Run the tool.
Run(settings, GetArguments(interceptor, settings, outputPath));
}

private static DotCoverContext InterceptAction(
ICakeContext context,
Action<ICakeContext> action)
{
var interceptor = new DotCoverContext(context);
action(interceptor);

// Validate arguments.
if (interceptor.FilePath == null)
{
throw new CakeException("No tool was started.");
}

return interceptor;
Run(settings, GetArguments(context, action, settings, outputPath));
}

private ProcessArgumentBuilder GetArguments(
DotCoverContext context,
ICakeContext context,
Action<ICakeContext> action,
DotCoverAnalyseSettings settings,
FilePath outputPath)
{
var builder = new ProcessArgumentBuilder();

builder.Append("Analyse");

// The target application to call.
builder.AppendSwitch("/TargetExecutable", "=", context.FilePath.FullPath.Quote());

// The arguments to the target application.
var arguments = context.Settings?.Arguments?.Render();
if (!string.IsNullOrWhiteSpace(arguments))
{
arguments = arguments.Replace("\"", "\\\"");
builder.AppendSwitch("/TargetArguments", "=", arguments.Quote());
}
// Get Target executable arguments
GetTargetArguments(context, action).CopyTo(builder);

// Set the output file.
outputPath = outputPath.MakeAbsolute(_environment);
Expand All @@ -114,8 +88,11 @@ private ProcessArgumentBuilder GetArguments(
builder.AppendSwitch("/ReportType", "=", settings.ReportType.ToString());
}

// Set common Coverage settings
settings.ToArguments(_environment).CopyTo(builder);
// Get Coverage arguments
GetCoverageArguments(settings).CopyTo(builder);

// Get base arguments
GetArguments(settings).CopyTo(builder);

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Cake.Common.Tools.DotCover.Cover
/// <summary>
/// Contains settings used by <see cref="DotCoverCoverer" />.
/// </summary>
public sealed class DotCoverCoverSettings : DotCoverSettings
public sealed class DotCoverCoverSettings : DotCoverCoverageSettings
{
}
}
45 changes: 11 additions & 34 deletions src/Cake.Common/Tools/DotCover/Cover/DotCoverCoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Cake.Common.Tools.DotCover.Cover
/// <summary>
/// DotCover Coverer builder.
/// </summary>
public sealed class DotCoverCoverer : DotCoverTool<DotCoverCoverSettings>
public sealed class DotCoverCoverer : DotCoverCoverageTool<DotCoverCoverSettings>
{
private readonly ICakeEnvironment _environment;

Expand Down Expand Up @@ -61,55 +61,32 @@ public void Cover(ICakeContext context,
throw new ArgumentNullException(nameof(settings));
}

// Run the tool using the interceptor.
var interceptor = InterceptAction(context, action);

// Run the tool.
Run(settings, GetArguments(interceptor, settings, outputPath));
}

private static DotCoverContext InterceptAction(
ICakeContext context,
Action<ICakeContext> action)
{
var interceptor = new DotCoverContext(context);
action(interceptor);

// Validate arguments.
if (interceptor.FilePath == null)
{
throw new CakeException("No tool was started.");
}

return interceptor;
Run(settings, GetArguments(context, action, settings, outputPath));
}

private ProcessArgumentBuilder GetArguments(
DotCoverContext context,
ICakeContext context,
Action<ICakeContext> action,
DotCoverCoverSettings settings,
FilePath outputPath)
{
var builder = new ProcessArgumentBuilder();

builder.Append("Cover");

// The target application to call.
builder.AppendSwitch("/TargetExecutable", "=", context.FilePath.FullPath.Quote());

// The arguments to the target application.
var arguments = context.Settings?.Arguments?.Render();
if (!string.IsNullOrWhiteSpace(arguments))
{
arguments = arguments.Replace("\"", "\\\"");
builder.AppendSwitch("/TargetArguments", "=", arguments.Quote());
}
// Get Target executable arguments
GetTargetArguments(context, action).CopyTo(builder);

// Set the output file.
outputPath = outputPath.MakeAbsolute(_environment);
builder.AppendSwitch("/Output", "=", outputPath.FullPath.Quote());

// Set common Coverage settings
settings.ToArguments(_environment).CopyTo(builder);
// Get Coverage arguments
GetCoverageArguments(settings).CopyTo(builder);

// Get base arguments
GetArguments(settings).CopyTo(builder);

return builder;
}
Expand Down
46 changes: 46 additions & 0 deletions src/Cake.Common/Tools/DotCover/DotCoverAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using Cake.Common.Tools.DotCover.Analyse;
using Cake.Common.Tools.DotCover.Cover;
using Cake.Common.Tools.DotCover.Report;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
Expand Down Expand Up @@ -123,5 +124,50 @@ public static void DotCoverCover(
// Run DotCover cover.
coverer.Cover(context, action, outputFile, settings);
}

/// <summary>
/// Runs <see href="https://www.jetbrains.com/dotcover/help/dotCover__Console_Runner_Commands.html#report">DotCover Report</see>
/// for the specified action and settings.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="sourceFile">The DotCover coverage snapshot file name.</param>
/// <param name="outputFile">The DotCover output file.</param>
/// <param name="settings">The settings</param>
/// <example>
/// <code>
/// DotCoverReport(new FilePath("./result.dcvr"),
/// new FilePath("./result.html"),
/// new DotCoverReportSettings {
/// ReportType = DotCoverReportType.HTML
/// });
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Report")]
[CakeNamespaceImport("Cake.Common.Tools.DotCover.Report")]
public static void DotCoverReport(
this ICakeContext context,
FilePath sourceFile,
FilePath outputFile,
DotCoverReportSettings settings)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

if (settings == null)
{
settings = new DotCoverReportSettings();
}

// Create the DotCover reporter.
var reporter = new DotCoverReporter(
context.FileSystem, context.Environment,
context.ProcessRunner, context.Tools);

// Run DotCover report.
reporter.Report(sourceFile, outputFile, settings);
}
}
}
Loading

0 comments on commit 8e7b783

Please sign in to comment.