Skip to content

Commit

Permalink
Allow filtering of failed interactions when performing a verify
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Nov 26, 2017
1 parent 6028d1f commit 012501f
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 8 deletions.
5 changes: 3 additions & 2 deletions PactNet.Tests/Core/PactCoreHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class TestHostConfig : IPactCoreHostConfig
public string Arguments { get; }
public bool WaitForExit { get; }
public IEnumerable<IOutput> Outputters { get; }
public IDictionary<string, string> Environment { get; }

public TestHostConfig(string script, IEnumerable<IOutput> outputters)
{
Expand Down Expand Up @@ -42,7 +43,7 @@ private IPactCoreHost GetSubject(string script)
}

[Fact]
public void Start_WhenStdOutIsWrittendTo_LinesAreWrittenToTheOutputters()
public void Start_WhenStdOutIsWrittenTo_LinesAreWrittenToTheOutputters()
{
var pactCoreHost = GetSubject("write-to-stdout");

Expand All @@ -54,7 +55,7 @@ public void Start_WhenStdOutIsWrittendTo_LinesAreWrittenToTheOutputters()
}

[Fact]
public void Start_WhenStdErrIsWrittendTo_LinesAreWrittenToTheOutputters()
public void Start_WhenStdErrIsWrittenTo_LinesAreWrittenToTheOutputters()
{
var pactCoreHost = GetSubject("write-to-stderr");

Expand Down
59 changes: 57 additions & 2 deletions PactNet.Tests/Core/PactVerifierHostConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ namespace PactNet.Tests.Core
{
public class PactVerifierHostConfigTests
{
private IPactCoreHostConfig GetSubject(Uri baseUri = null, string pactUri = "../test/pact.json", PactUriOptions pactBrokerUriOptions = null, Uri providerStateSetupUri = null, PactVerifierConfig verifierConfig = null)
private IPactCoreHostConfig GetSubject(Uri baseUri = null, string pactUri = "../test/pact.json", PactUriOptions pactBrokerUriOptions = null, Uri providerStateSetupUri = null, PactVerifierConfig verifierConfig = null, IDictionary<string, string> environment = null)
{
return new PactVerifierHostConfig(
baseUri ?? new Uri("http://localhost:2833"),
pactUri,
pactBrokerUriOptions,
providerStateSetupUri,
verifierConfig);
verifierConfig,
environment);
}

[Fact]
Expand Down Expand Up @@ -159,6 +160,46 @@ public void Ctor_WhenCalledWithVerboseTrue_SetsTheCorrectArgs()
Assert.Equal(expectedArguments, config.Arguments);
}

[Fact]
public void Ctor_WhenCalledWithNoEnvironment_NoAdditionalEnvironmentVariablesAreAdded()
{
var baseUri = new Uri("http://127.0.0.1");
var pactUri = "./tester-pact/pact-file.json";
var providerStateSetupUri = new Uri("http://127.0.0.1/states/");

var verifierConfig = new PactVerifierConfig
{
ProviderVersion = "1.0.0"
};

var config = GetSubject(baseUri: baseUri, pactUri: pactUri, providerStateSetupUri: providerStateSetupUri, verifierConfig: verifierConfig);

AssertEnvironmentIsCorrectlySet(null, config.Environment);
}

[Fact]
public void Ctor_WhenCalledWithEnvironmentSet_AdditionalEnvironmentVariablesAreAdded()
{
var baseUri = new Uri("http://127.0.0.1");
var pactUri = "./tester-pact/pact-file.json";
var providerStateSetupUri = new Uri("http://127.0.0.1/states/");

var verifierConfig = new PactVerifierConfig
{
ProviderVersion = "1.0.0"
};

var environment = new Dictionary<string, string>
{
{ "PACT_DESCRIPTION", "Test1" },
{ "PACT_PROVIDER_STATE", "Test2" }
};

var config = GetSubject(baseUri: baseUri, pactUri: pactUri, providerStateSetupUri: providerStateSetupUri, verifierConfig: verifierConfig, environment: environment);

AssertEnvironmentIsCorrectlySet(environment, config.Environment);
}

[Fact]
public void Ctor_WhenVerifierConfigIsNull_SetsOutputtersToNull()
{
Expand All @@ -167,6 +208,20 @@ public void Ctor_WhenVerifierConfigIsNull_SetsOutputtersToNull()
Assert.Equal(null, config.Outputters);
}

private void AssertEnvironmentIsCorrectlySet(IDictionary<string, string> expectedEnv, IDictionary<string, string> actualEnv)
{
expectedEnv = expectedEnv ?? new Dictionary<string, string>();

Assert.Equal(expectedEnv.Count + 1, actualEnv.Count);

foreach(var envVar in expectedEnv)
{
Assert.Equal(envVar.Value, actualEnv[envVar.Key]);
}

Assert.NotEmpty(actualEnv["PACT_INTERACTION_RERUN_COMMAND"]);
}

private string BuildExpectedArguments(
Uri baseUri,
string pactUri,
Expand Down
1 change: 1 addition & 0 deletions PactNet/Core/IPactCoreHostConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ internal interface IPactCoreHostConfig
string Arguments { get; }
bool WaitForExit { get; }
IEnumerable<IOutput> Outputters { get; }
IDictionary<string, string> Environment { get; }
}
}
1 change: 1 addition & 0 deletions PactNet/Core/MockProviderHostConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class MockProviderHostConfig : IPactCoreHostConfig
public string Arguments { get; }
public bool WaitForExit { get; }
public IEnumerable<IOutput> Outputters { get; }
public IDictionary<string, string> Environment { get; }

public MockProviderHostConfig(int port, bool enableSsl, string consumerName, string providerName, PactConfig config, IPAddress host)
{
Expand Down
12 changes: 12 additions & 0 deletions PactNet/Core/PactCoreHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public PactCoreHost(T config)
}
}

if(config.Environment != null)
{
foreach (var envVar in config.Environment)
{
#if USE_NET4X
startInfo.EnvironmentVariables[envVar.Key] = envVar.Value;
#else
startInfo.Environment[envVar.Key] = envVar.Value;
#endif
}
}

_process = new Process
{
StartInfo = startInfo
Expand Down
15 changes: 14 additions & 1 deletion PactNet/Core/PactVerifierHostConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ internal class PactVerifierHostConfig : IPactCoreHostConfig
public string Arguments { get; }
public bool WaitForExit { get; }
public IEnumerable<IOutput> Outputters { get; }
public IDictionary<string, string> Environment { get; }

public PactVerifierHostConfig(Uri baseUri, string pactUri, PactUriOptions pactBrokerUriOptions, Uri providerStateSetupUri, PactVerifierConfig config)
public PactVerifierHostConfig(Uri baseUri, string pactUri, PactUriOptions pactBrokerUriOptions, Uri providerStateSetupUri, PactVerifierConfig config, IDictionary<string, string> environment)
{
var providerStateOption = providerStateSetupUri != null ? $" --provider-states-setup-url {providerStateSetupUri.OriginalString}" : string.Empty;
var brokerCredentials = pactBrokerUriOptions != null ? $" --broker-username \"{pactBrokerUriOptions.Username}\" --broker-password \"{pactBrokerUriOptions.Password}\"" : string.Empty;
Expand All @@ -25,6 +26,18 @@ public PactVerifierHostConfig(Uri baseUri, string pactUri, PactUriOptions pactBr
Arguments = $"\"{FixPathForRuby(pactUri)}\" --provider-base-url {baseUri.OriginalString}{providerStateOption}{brokerCredentials}{publishResults}{customHeader}{verbose}";
WaitForExit = true;
Outputters = config?.Outputters;
Environment = new Dictionary<string, string>
{
{ "PACT_INTERACTION_RERUN_COMMAND", "To re-run just this failing interaction, change the verify method to '.Verify(description: \"<PACT_DESCRIPTION>\", providerState: \"<PACT_PROVIDER_STATE>\")'" }
};

if(environment != null)
{
foreach (var envVar in environment)
{
Environment.Add(envVar.Key, envVar.Value);
}
}
}

private string FixPathForRuby(string path)
Expand Down
2 changes: 1 addition & 1 deletion PactNet/IPactVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public interface IPactVerifier
IPactVerifier ServiceProvider(string providerName, string baseUri);
IPactVerifier HonoursPactWith(string consumerName);
IPactVerifier PactUri(string fileUri, PactUriOptions options = null);
void Verify();
void Verify(string description = null, string providerState = null);
}
}
15 changes: 13 additions & 2 deletions PactNet/PactVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using PactNet.Core;
using static System.String;
using System.Collections.Generic;

namespace PactNet
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public IPactVerifier PactUri(string fileUri, PactUriOptions options = null)
return this;
}

public void Verify()
public void Verify(string description = null, string providerState = null)
{
if (ServiceBaseUri == null)
{
Expand All @@ -113,8 +114,18 @@ public void Verify()
"PactFileUri has not been set, please supply a uri using the PactUri method.");
}

IDictionary<string, string> env = null;
if(!IsNullOrEmpty(description) || !IsNullOrEmpty(providerState))
{
env = new Dictionary<string, string>
{
{ "PACT_DESCRIPTION", description },
{ "PACT_PROVIDER_STATE", providerState }
};
}

var pactVerifier = _pactVerifierHostFactory(
new PactVerifierHostConfig(ServiceBaseUri, PactFileUri, PactUriOptions, ProviderStateSetupUri, _config));
new PactVerifierHostConfig(ServiceBaseUri, PactFileUri, PactUriOptions, ProviderStateSetupUri, _config, env));
pactVerifier.Start();
}
}
Expand Down

0 comments on commit 012501f

Please sign in to comment.