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 Aug 11, 2016
1 parent edea66c commit cd40f71
Show file tree
Hide file tree
Showing 17 changed files with 615 additions and 258 deletions.
2 changes: 2 additions & 0 deletions src/Cake.Common.Tests/Cake.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<Compile Include="Fixtures\Tools\DNU\DNUFixture.cs" />
<Compile Include="Fixtures\Tools\DNU\Pack\DNUPackerFixture.cs" />
<Compile Include="Fixtures\Tools\DNU\Restorer\DNURestorerFixture.cs" />
<Compile Include="Fixtures\Tools\DotCover\Report\DotCoverReporterFixture.cs" />
<Compile Include="Fixtures\Tools\OctopusDeployPusherFixture.cs" />
<Compile Include="Fixtures\Tools\VSTestRunnerFixture.cs" />
<Compile Include="Fixtures\Tools\SpecFlow\StepDefinitionReport\SpecFlowStepDefinitionReporterFixture.cs" />
Expand Down Expand Up @@ -257,6 +258,7 @@
<Compile Include="Unit\Tools\DotCover\Analyse\DotCoverAnalyserTests.cs" />
<Compile Include="Unit\Tools\DotCover\Analyse\DotCoverAnalyseSettingsTests.cs" />
<Compile Include="Unit\Tools\DotCover\Cover\DotCoverCovererTests.cs" />
<Compile Include="Unit\Tools\DotCover\Report\DotCoverReporterTests.cs" />
<Compile Include="Unit\Tools\DotNetCore\Build\DotNetCoreBuilderTests.cs" />
<Compile Include="Unit\Tools\DotNetCore\Execute\DotNetCoreExecutorTests.cs" />
<Compile Include="Unit\Tools\DotNetCore\Pack\DotNetCorePackerTests.cs" />
Expand Down
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,101 @@
// 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);
}
}
}
}
8 changes: 6 additions & 2 deletions src/Cake.Common/Cake.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,17 @@
<Compile Include="Tools\DotCover\Analyse\DotCoverAnalyser.cs" />
<Compile Include="Tools\DotCover\Cover\DotCoverCoverSettings.cs" />
<Compile Include="Tools\DotCover\Cover\DotCoverCoverer.cs" />
<Compile Include="Tools\DotCover\DotCoverCoverageTool.cs" />
<Compile Include="Tools\DotCover\DotCoverSettings.cs" />
<Compile Include="Tools\DotCover\DotCoverReportType.cs" />
<Compile Include="Tools\DotCover\DotCoverContext.cs" />
<Compile Include="Tools\DotCover\DotCoverProcessRunner.cs" />
<Compile Include="Tools\DotCover\DotCoverAliases.cs" />
<Compile Include="Tools\DotCover\DotCoverSettings.cs" />
<Compile Include="Tools\DotCover\DotCoverSettingsExtensions.cs" />
<Compile Include="Tools\DotCover\DotCoverCoverageSettings.cs" />
<Compile Include="Tools\DotCover\DotCoverCoverageSettingsExtensions.cs" />
<Compile Include="Tools\DotCover\DotCoverTool.cs" />
<Compile Include="Tools\DotCover\Report\DotCoverReporter.cs" />
<Compile Include="Tools\DotCover\Report\DotCoverReportSettings.cs" />
<Compile Include="Tools\DotNetCore\Build\DotNetCoreBuilder.cs" />
<Compile Include="Tools\DotNetCore\Build\DotNetCoreBuildSettings.cs" />
<Compile Include="Tools\DotNetCore\DotNetCoreAliases.cs" />
Expand Down
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
48 changes: 11 additions & 37 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,51 +61,22 @@ public void Analyse(ICakeContext context,
throw new ArgumentNullException("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.
if (context.Settings != null && context.Settings.Arguments != null)
{
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 @@ -117,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
{
}
}
48 changes: 11 additions & 37 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,58 +61,32 @@ public void Cover(ICakeContext context,
throw new ArgumentNullException("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.
if (context.Settings != null && context.Settings.Arguments != null)
{
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
Loading

0 comments on commit cd40f71

Please sign in to comment.