Skip to content

Commit

Permalink
Merge branch 'finnnewick-justgiving-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Jan 10, 2016
2 parents 200a024 + 64d4205 commit e227c5c
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 40 deletions.
1 change: 1 addition & 0 deletions PactNet.Tests/PactNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="PactUriOptionsTests.cs" />
<Compile Include="ProviderServicePactFileTests.cs" />
<Compile Include="PactVerifierTests.cs" />
<Compile Include="Reporters\Outputters\FileReportOutputterTests.cs" />
<Compile Include="Reporters\ReporterTests.cs" />
<Compile Include="Mocks\MockHttpService\Comparers\ProviderServiceResponseComparerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
32 changes: 32 additions & 0 deletions PactNet.Tests/Reporters/Outputters/FileReportOutputterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using NSubstitute;
using PactNet.Logging;
using PactNet.Reporters;
using PactNet.Reporters.Outputters;
using Xunit;

namespace PactNet.Tests.Reporters.Outputters
{
public class FileReportOutputterTests
{
private ILog _log;

private IReportOutputter GetSubject()
{
_log = Substitute.For<ILog>();

return new FileReportOutputter(() => _log);
}

[Fact]
public void Write_WithReport_CallsDebugOnTheLoggerWithReport()
{
const string report = "Hello!";
var outputter = GetSubject();

outputter.Write(report);

_log.Received(1).Debug(report);
}
}
}
21 changes: 11 additions & 10 deletions PactNet.Tests/Reporters/ReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
using NSubstitute;
using PactNet.Comparers;
using PactNet.Reporters;
using PactNet.Reporters.Outputters;
using Xunit;

namespace PactNet.Tests.Reporters
{
public class ReporterTests
{
private Action<string> _reportOutputter;
private IReportOutputter _reportOutputter;

private IReporter GetSubject()
{
_reportOutputter = Substitute.For<Action<string>>();
_reportOutputter = Substitute.For<IReportOutputter>();

return new Reporter(new List<Action<string>> { _reportOutputter });
return new Reporter(new List<IReportOutputter> { _reportOutputter });
}

[Fact]
Expand All @@ -28,7 +29,7 @@ public void Flush_WithReportedInfo_CallsOutputterWithEmptyString()

reporter.Flush();

_reportOutputter.Received(1)(message);
_reportOutputter.Received(1).Write(message);
}

[Fact]
Expand All @@ -46,7 +47,7 @@ public void Flush_WithAReportedSummaryThatContainsFailuresOnTheComparisonResult_
reporter.ReportSummary(comparisonResult);
reporter.Flush();

_reportOutputter.Received(1)(expectedMessage);
_reportOutputter.Received(1).Write(expectedMessage);
}

[Fact]
Expand All @@ -65,7 +66,7 @@ public void Flush_WithAReportedSummaryThatContainsMultipleFailuresOnTheCompariso
reporter.ReportSummary(comparisonResult);
reporter.Flush();

_reportOutputter.Received(1)(expectedMessage);
_reportOutputter.Received(1).Write(expectedMessage);
}

[Fact]
Expand All @@ -80,7 +81,7 @@ public void Flush_WithWithAReportedSummaryThatContainsNoFailuresOnTheComparisonR
reporter.ReportSummary(comparisonResult);
reporter.Flush();

_reportOutputter.Received(1)(comparisonMessage);
_reportOutputter.Received(1).Write(comparisonMessage);
}

[Fact]
Expand All @@ -102,7 +103,7 @@ public void Flush_WithAReportedSummaryThatContainsAChildResultWithMultipleFailur
reporter.ReportSummary(comparisonResult);
reporter.Flush();

_reportOutputter.Received(1)(Arg.Is<string>(x => x.Contains(comparisonMessage1 + " (FAILED - 1)") && x.Contains(comparisonMessage2 + " (FAILED - 2)")));
_reportOutputter.Received(1).Write(Arg.Is<string>(x => x.Contains(comparisonMessage1 + " (FAILED - 1)") && x.Contains(comparisonMessage2 + " (FAILED - 2)")));
}

[Fact]
Expand All @@ -122,7 +123,7 @@ public void Flush_WithReportedFailureReasonThatContainsFailuresOnTheComparisonRe
reporter.ReportFailureReasons(comparisonResult);
reporter.Flush();

_reportOutputter.Received(1)(Arg.Is<string>(x => x.Contains("1) " + comparisonFailureMessage1) && x.Contains("2) " + comparisonFailureMessage2)));
_reportOutputter.Received(1).Write(Arg.Is<string>(x => x.Contains("1) " + comparisonFailureMessage1) && x.Contains("2) " + comparisonFailureMessage2)));
}

[Fact]
Expand All @@ -137,7 +138,7 @@ public void Flush_WithReportedFailureReasonThatContainsNoFailuresOnTheComparison
reporter.ReportFailureReasons(comparisonResult);
reporter.Flush();

_reportOutputter.DidNotReceive()(Arg.Any<string>());
_reportOutputter.DidNotReceive().Write(Arg.Any<string>());
}
}
}
4 changes: 3 additions & 1 deletion PactNet/PactNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@
<Compile Include="Mocks\MockHttpService\Comparers\ProviderServiceRequestComparer.cs" />
<Compile Include="Models\ProviderState.cs" />
<Compile Include="Models\ProviderStates.cs" />
<Compile Include="Reporters\FileReportOutputter.cs" />
<Compile Include="Reporters\Outputters\ConsoleReportOutputter.cs" />
<Compile Include="Reporters\Outputters\FileReportOutputter.cs" />
<Compile Include="Reporters\IReporter.cs" />
<Compile Include="Reporters\Outputters\IReportOutputter.cs" />
<Compile Include="Reporters\Reporter.cs" />
<Compile Include="Validators\IPactValidator.cs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions PactNet/PactVerifierConfig.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
using System.Collections.Generic;
using PactNet.Reporters.Outputters;

namespace PactNet
{
public class PactVerifierConfig
{
public string LogDir { get; set; }

public IList<IReportOutputter> ReportOutputters { get; private set; }

internal string LoggerName;

public PactVerifierConfig()
{
LogDir = Constants.DefaultLogDir;
ReportOutputters = new List<IReportOutputter>
{
new ConsoleReportOutputter(),
new FileReportOutputter(() => LoggerName)
};
}
}
}
19 changes: 0 additions & 19 deletions PactNet/Reporters/FileReportOutputter.cs

This file was deleted.

12 changes: 12 additions & 0 deletions PactNet/Reporters/Outputters/ConsoleReportOutputter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace PactNet.Reporters.Outputters
{
internal class ConsoleReportOutputter : IReportOutputter
{
public void Write(string report)
{
Console.WriteLine(report);
}
}
}
25 changes: 25 additions & 0 deletions PactNet/Reporters/Outputters/FileReportOutputter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using PactNet.Logging;

namespace PactNet.Reporters.Outputters
{
internal class FileReportOutputter : IReportOutputter
{
private readonly Func<ILog> _logFactory;

internal FileReportOutputter(Func<ILog> logFactory)
{
_logFactory = logFactory;
}

public FileReportOutputter(Func<string> loggerNameGenerator)
: this(() => LogProvider.GetLogger(loggerNameGenerator()))
{
}

public void Write(string report)
{
_logFactory().Debug(report);
}
}
}
7 changes: 7 additions & 0 deletions PactNet/Reporters/Outputters/IReportOutputter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PactNet.Reporters.Outputters
{
public interface IReportOutputter
{
void Write(string report);
}
}
15 changes: 6 additions & 9 deletions PactNet/Reporters/Reporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@
using System.Linq;
using System.Text;
using PactNet.Comparers;
using PactNet.Reporters.Outputters;

namespace PactNet.Reporters
{
internal class Reporter : IReporter
{
private readonly IEnumerable<Action<string>> _outputters;
private readonly IEnumerable<IReportOutputter> _outputters;

private int _currentTabDepth;
private int _failureInfoCount;
private int _failureCount;

private readonly IList<string> _reportLines = new List<string>();

internal Reporter(IEnumerable<Action<string>> outputters)
internal Reporter(IEnumerable<IReportOutputter> outputters)
{
_outputters = outputters;
}

public Reporter(PactVerifierConfig config)
: this(new List<Action<string>>
{
Console.WriteLine,
new FileReportOutputter(config.LoggerName).Write
})
: this(config.ReportOutputters)
{
}

Expand Down Expand Up @@ -57,14 +54,14 @@ public void ResetIndentation()

public void Flush()
{
if (!_reportLines.Any())
if (!_reportLines.Any() || _outputters == null)
{
return;
}

foreach (var outputter in _outputters)
{
outputter(String.Join(Environment.NewLine, _reportLines));
outputter.Write(String.Join(Environment.NewLine, _reportLines));
}
}

Expand Down
19 changes: 18 additions & 1 deletion Samples/EventApi/Provider.Api.Web.Tests/EventAPITests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
using Microsoft.Owin.Testing;
using PactNet;
using PactNet.Reporters.Outputters;
using Xunit;

namespace Provider.Api.Web.Tests
{
public class CustomOutputter : IReportOutputter
{
public string Output { get; set; }

public void Write(string report)
{
Output += report;
}
}

public class EventApiTests
{
[Fact]
public void EnsureEventApiHonoursPactWithConsumer()
{
//Arrange
IPactVerifier pactVerifier = new PactVerifier(() => {}, () => {});
var outputter = new CustomOutputter();
var config = new PactVerifierConfig();
config.ReportOutputters.Add(outputter);
IPactVerifier pactVerifier = new PactVerifier(() => {}, () => {}, config);

pactVerifier
.ProviderState(
Expand All @@ -30,6 +44,9 @@ public void EnsureEventApiHonoursPactWithConsumer()
.PactUri("../../../Consumer.Tests/pacts/consumer-event_api.json")
.Verify();
}

// Verify that verifaction log is also sent to additional reporters defined in the config
Assert.Contains("Verifying a Pact between Consumer and Event API", outputter.Output);
}

private void EnsureOneDetailsViewEventExists()
Expand Down

0 comments on commit e227c5c

Please sign in to comment.