Skip to content

Commit

Permalink
Logging the PactVerifier output, so that it is visibile even if the t…
Browse files Browse the repository at this point in the history
…est framework redirects the console
  • Loading branch information
neilcampbell committed Jul 16, 2015
1 parent 859b8a9 commit 1240d7b
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private IProviderServiceValidator GetSubject()
_mockHttpRequestSender = Substitute.For<IHttpRequestSender>();
_mockReporter = Substitute.For<IReporter>();

return new ProviderServiceValidator(_mockResponseComparer, _mockHttpRequestSender, _mockReporter);
return new ProviderServiceValidator(_mockResponseComparer, _mockHttpRequestSender, _mockReporter, new PactVerifierConfig());
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions PactNet.Tests/PactVerifierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ private IPactVerifier GetSubject()
_mockProviderServiceValidator = Substitute.For<IProviderServiceValidator>();
_fakeHttpMessageHandler = new FakeHttpMessageHandler();

return new PactVerifier(() => {}, () => {}, _mockFileSystem, httpRequestSender =>
return new PactVerifier(() => {}, () => {}, _mockFileSystem, (httpRequestSender, reporter, config) =>
{
_providerServiceValidatorFactoryCallInfo = new Tuple<bool, IHttpRequestSender>(true, httpRequestSender);
return _mockProviderServiceValidator;
}, new HttpClient(_fakeHttpMessageHandler));
}, new HttpClient(_fakeHttpMessageHandler), null);
}

[Fact]
Expand Down
51 changes: 28 additions & 23 deletions PactNet.Tests/Reporters/ReporterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using NSubstitute;
using PactNet.Comparers;
using PactNet.Reporters;
Expand All @@ -8,29 +9,30 @@ namespace PactNet.Tests.Reporters
{
public class ReporterTests
{
private IReportOutputter _mockOutputter;
private Action<string> _reportOutputter;

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

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

[Fact]
public void ReportInfo_WhenCalled_CallsWriteInfoOnOutputterWithMessage()
public void Flush_WithReportedInfo_CallsOutputterWithEmptyString()
{
const string message = "Hello!";

var reporter = GetSubject();

reporter.ReportInfo(message);

reporter.Flush();

_mockOutputter.Received(1).WriteInfo(message);
_reportOutputter.Received(1)(message);
}

[Fact]
public void ReportSummary_WithFailuresOnComparisonResult_CallsWriteErrorOnOutputterWithMessage()
public void Flush_WithAReportedSummaryThatContainsFailuresOnTheComparisonResult_CallsOutputterWithMessage()
{
const string comparisonMessage = "The thing I am testing";

Expand All @@ -40,14 +42,15 @@ public void ReportSummary_WithFailuresOnComparisonResult_CallsWriteErrorOnOutput

var comparisonResult = new ComparisonResult(comparisonMessage);
comparisonResult.RecordFailure(new ErrorMessageComparisonFailure("It failed"));

reporter.ReportSummary(comparisonResult);
reporter.Flush();

_mockOutputter.Received(1).WriteError(expectedMessage, Arg.Any<int>());
_reportOutputter.Received(1)(expectedMessage);
}

[Fact]
public void ReportSummary_WithMultipleFailuresOnComparisonResult_CallsWriteErrorOnOutputterWithMessage()
public void Flush_WithAReportedSummaryThatContainsMultipleFailuresOnTheComparisonResult_CallsOutputterWithMessage()
{
const string comparisonMessage = "The thing I am testing";

Expand All @@ -60,12 +63,13 @@ public void ReportSummary_WithMultipleFailuresOnComparisonResult_CallsWriteError
comparisonResult.RecordFailure(new ErrorMessageComparisonFailure("Failure 2"));

reporter.ReportSummary(comparisonResult);
reporter.Flush();

_mockOutputter.Received(1).WriteError(expectedMessage, Arg.Any<int>());
_reportOutputter.Received(1)(expectedMessage);
}

[Fact]
public void ReportSummary_WithNoFailuresOnComparisonResult_CallsWriteSuccessOnOutputterWithMessage()
public void Flush_WithWithAReportedSummaryThatContainsNoFailuresOnTheComparisonResult_CallsOutputterWithMessage()
{
const string comparisonMessage = "The thing I am testing";

Expand All @@ -74,12 +78,13 @@ public void ReportSummary_WithNoFailuresOnComparisonResult_CallsWriteSuccessOnOu
var comparisonResult = new ComparisonResult(comparisonMessage);

reporter.ReportSummary(comparisonResult);
reporter.Flush();

_mockOutputter.Received(1).WriteSuccess(comparisonMessage, Arg.Any<int>());
_reportOutputter.Received(1)(comparisonMessage);
}

[Fact]
public void ReportSummary_WithChildResultMultipleFailuresOnComparisonResult_CallsWriteErrorOnOutputterWithMessage()
public void Flush_WithAReportedSummaryThatContainsAChildResultWithMultipleFailuresOnTheComparisonResult_CallsOutputterWithMessage()
{
const string comparisonMessage1 = "The thing I am testing";
const string comparisonMessage2 = "The thing I am testing 2";
Expand All @@ -95,13 +100,13 @@ public void ReportSummary_WithChildResultMultipleFailuresOnComparisonResult_Call
comparisonResult.AddChildResult(comparisonResult2);

reporter.ReportSummary(comparisonResult);
reporter.Flush();

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

[Fact]
public void ReportFailureReasons_WithFailuresOnComparisonResult_CallsWriteErrorOnOutputterWithFailures()
public void Flush_WithReportedFailureReasonThatContainsFailuresOnTheComparisonResult_CallsOutputterWithMessage()
{
const string comparisonMessage = "The thing I am testing";
const string comparisonFailureMessage1 = "It failed 1";
Expand All @@ -115,13 +120,13 @@ public void ReportFailureReasons_WithFailuresOnComparisonResult_CallsWriteErrorO
comparisonResult.RecordFailure(new ErrorMessageComparisonFailure(comparisonFailureMessage2));

reporter.ReportFailureReasons(comparisonResult);
reporter.Flush();

_mockOutputter.Received(1).WriteError(Environment.NewLine + "1) " + comparisonFailureMessage1, Arg.Any<int>());
_mockOutputter.Received(1).WriteError(Environment.NewLine + "2) " + comparisonFailureMessage2, Arg.Any<int>());
_reportOutputter.Received(1)(Arg.Is<string>(x => x.Contains("1) " + comparisonFailureMessage1) && x.Contains("2) " + comparisonFailureMessage2)));
}

[Fact]
public void ReportFailureReasons_WithNoFailuresOnComparisonResult_DoesNotCallTheOutputter()
public void Flush_WithReportedFailureReasonThatContainsNoFailuresOnTheComparisonResult_DoesNotCallTheOutputter()
{
const string comparisonMessage = "The thing I am testing";

Expand All @@ -130,9 +135,9 @@ public void ReportFailureReasons_WithNoFailuresOnComparisonResult_DoesNotCallThe
var comparisonResult = new ComparisonResult(comparisonMessage);

reporter.ReportFailureReasons(comparisonResult);
reporter.Flush();

_mockOutputter.DidNotReceive().WriteInfo(Arg.Any<String>(), Arg.Any<int>());
_mockOutputter.DidNotReceive().WriteError(Arg.Any<String>(), Arg.Any<int>());
_reportOutputter.DidNotReceive()(Arg.Any<string>());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PactNet.Mocks.MockHttpService
namespace PactNet
{
internal static class Constants
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using PactNet.Comparers;
using PactNet.Logging;
using PactNet.Mocks.MockHttpService.Comparers;
using PactNet.Mocks.MockHttpService.Models;
using PactNet.Models;
Expand All @@ -13,23 +14,28 @@ internal class ProviderServiceValidator : IProviderServiceValidator
private readonly IProviderServiceResponseComparer _providerServiceResponseComparer;
private readonly IHttpRequestSender _httpRequestSender;
private readonly IReporter _reporter;
private readonly PactVerifierConfig _config;

internal ProviderServiceValidator(
IProviderServiceResponseComparer providerServiceResponseComparer,
IHttpRequestSender httpRequestSender,
IReporter reporter)
IReporter reporter,
PactVerifierConfig config)
{
_providerServiceResponseComparer = providerServiceResponseComparer;
_httpRequestSender = httpRequestSender;
_reporter = reporter;
_config = config;
}

public ProviderServiceValidator(
IHttpRequestSender httpRequestSender,
IReporter reporter) : this(
IReporter reporter,
PactVerifierConfig config) : this(
new ProviderServiceResponseComparer(),
httpRequestSender,
reporter)
reporter,
config)
{
}

Expand Down Expand Up @@ -97,7 +103,7 @@ public void Validate(ProviderServicePactFile pactFile, ProviderStates providerSt
_reporter.Indent();
_reporter.ReportInfo(String.Format("with {0} {1}", interaction.Request.Method.ToString().ToUpper(), interaction.Request.Path));
}

try
{
var interactionComparisonResult = ValidateInteraction(interaction);
Expand All @@ -114,10 +120,12 @@ public void Validate(ProviderServicePactFile pactFile, ProviderStates providerSt

_reporter.ResetIndentation();
_reporter.ReportFailureReasons(comparisonResult);
_reporter.Flush();

if (comparisonResult.HasFailure)
{
throw new PactFailureException("See output for failure details.");
throw new PactFailureException(String.Format("See test output or {0} for failure details.",
!String.IsNullOrEmpty(_config.LoggerName) ? LogProvider.CurrentLogProvider.ResolveLogPath(_config.LoggerName) : "logs"));
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions PactNet/PactConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using PactNet.Mocks.MockHttpService;

namespace PactNet
{
public class PactConfig
Expand Down
6 changes: 3 additions & 3 deletions PactNet/PactNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<Compile Include="Mocks\MockHttpService\Comparers\IHttpPathComparer.cs" />
<Compile Include="Mocks\MockHttpService\Comparers\IHttpQueryStringComparer.cs" />
<Compile Include="Mocks\MockHttpService\Configuration\NancyConfig.cs" />
<Compile Include="Mocks\MockHttpService\Constants.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Mocks\MockHttpService\IHttpHost.cs" />
<Compile Include="Mocks\MockHttpService\Models\IHttpMessage.cs" />
<Compile Include="Mocks\MockHttpService\Nancy\NancyHttpHost.cs" />
Expand Down Expand Up @@ -177,6 +177,7 @@
<Compile Include="PactBuilder.cs" />
<Compile Include="Mocks\MockHttpService\Models\ProviderServiceRequest.cs" />
<Compile Include="Mocks\MockHttpService\Models\ProviderServiceResponse.cs" />
<Compile Include="PactVerifierConfig.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IPactVerifier.cs" />
<Compile Include="Mocks\MockHttpService\Comparers\HttpBodyComparer.cs" />
Expand All @@ -187,9 +188,8 @@
<Compile Include="Mocks\MockHttpService\Comparers\ProviderServiceRequestComparer.cs" />
<Compile Include="Models\ProviderState.cs" />
<Compile Include="Models\ProviderStates.cs" />
<Compile Include="Reporters\ConsoleReportOutputter.cs" />
<Compile Include="Reporters\FileReportOutputter.cs" />
<Compile Include="Reporters\IReporter.cs" />
<Compile Include="Reporters\IReportOutputter.cs" />
<Compile Include="Reporters\Reporter.cs" />
<Compile Include="Validators\IPactValidator.cs" />
</ItemGroup>
Expand Down
34 changes: 26 additions & 8 deletions PactNet/PactVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Net.Http;
using Newtonsoft.Json;
using PactNet.Extensions;
using PactNet.Logging;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Models;
using PactNet.Mocks.MockHttpService.Validators;
Expand All @@ -14,8 +16,10 @@ namespace PactNet
public class PactVerifier : IPactVerifier
{
private readonly IFileSystem _fileSystem;
private readonly Func<IHttpRequestSender, IProviderServiceValidator> _providerServiceValidatorFactory;
private readonly Func<IHttpRequestSender, IReporter, PactVerifierConfig, IProviderServiceValidator> _providerServiceValidatorFactory;
private readonly HttpClient _httpClient;
private readonly PactVerifierConfig _config;

private IHttpRequestSender _httpRequestSender;

public string ConsumerName { get; private set; }
Expand All @@ -27,12 +31,14 @@ internal PactVerifier(
Action setUp,
Action tearDown,
IFileSystem fileSystem,
Func<IHttpRequestSender, IProviderServiceValidator> providerServiceValidatorFactory,
HttpClient httpClient)
Func<IHttpRequestSender, IReporter, PactVerifierConfig, IProviderServiceValidator> providerServiceValidatorFactory,
HttpClient httpClient,
PactVerifierConfig config)
{
_fileSystem = fileSystem;
_providerServiceValidatorFactory = providerServiceValidatorFactory;
_httpClient = httpClient;
_config = config ?? new PactVerifierConfig();

ProviderStates = new ProviderStates(setUp, tearDown);
}
Expand All @@ -41,15 +47,17 @@ internal PactVerifier(
/// Define any set up and tear down state that is required when running the interaction verify.
/// We strongly recommend that any set up state is cleared using the tear down. This includes any state and IoC container overrides you may be doing.
/// </summary>
/// <param name="consumerName">The name of the consumer being verified.</param>
/// <param name="setUp">A set up action that will be run before each interaction verify. If no action is required please use an empty lambda () => {}.</param>
/// <param name="tearDown">A tear down action that will be run after each interaction verify. If no action is required please use an empty lambda () => {}.</param>
public PactVerifier(Action setUp, Action tearDown) : this(
/// <param name="config"></param>
public PactVerifier(Action setUp, Action tearDown, PactVerifierConfig config = null)
: this(
setUp,
tearDown,
new FileSystem(),
httpRequestSender => new ProviderServiceValidator(httpRequestSender, new Reporter()),
new HttpClient())
(httpRequestSender, reporter, verifierConfig) => new ProviderServiceValidator(httpRequestSender, reporter, verifierConfig),
new HttpClient(),
config)
{
}

Expand Down Expand Up @@ -212,7 +220,17 @@ public void Verify(string description = null, string providerState = null)
throw new ArgumentException("The specified description and/or providerState filter yielded no interactions.");
}

_providerServiceValidatorFactory(_httpRequestSender).Validate(pactFile, ProviderStates);
var loggerName = LogProvider.CurrentLogProvider.AddLogger(_config.LogDir, ProviderName.ToLowerSnakeCase(), "{0}_verifier.log");
_config.LoggerName = loggerName;

try
{
_providerServiceValidatorFactory(_httpRequestSender, new Reporter(_config), _config).Validate(pactFile, ProviderStates);
}
finally
{
LogProvider.CurrentLogProvider.RemoveLogger(_config.LoggerName);
}
}

private static bool IsWebUri(string uri)
Expand Down
14 changes: 14 additions & 0 deletions PactNet/PactVerifierConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace PactNet
{
public class PactVerifierConfig
{
public string LogDir { get; set; }

internal string LoggerName;

public PactVerifierConfig()
{
LogDir = Constants.DefaultLogDir;
}
}
}
Loading

0 comments on commit 1240d7b

Please sign in to comment.