From 3af5abbf5d45f0eea2f66cacafa8f99000e8f6e0 Mon Sep 17 00:00:00 2001 From: Levan Nozadze Date: Tue, 27 May 2025 21:16:12 +0400 Subject: [PATCH 1/2] feat(verifier): Added support of set_no_pacts_is_error feature --- .../Verifier/IPactVerifierSource.cs | 7 +++++++ src/PactNet/Interop/NativeInterop.cs | 3 +++ src/PactNet/Verifier/IVerifierProvider.cs | 8 ++++++++ .../Verifier/InteropVerifierProvider.cs | 11 +++++++++++ src/PactNet/Verifier/PactVerifierSource.cs | 11 +++++++++++ .../Verifier/InteropVerifierProviderTests.cs | 1 + .../Verifier/PactVerifierSourceTests.cs | 18 ++++++++++++++++++ 7 files changed, 59 insertions(+) diff --git a/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs b/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs index d6bae4e9..d7cd0705 100644 --- a/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs +++ b/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs @@ -53,6 +53,13 @@ public interface IPactVerifierSource /// Fluent builder IPactVerifierSource WithCustomHeader(string name, string value); + /// + /// Return an error or not if no pacts were found when looking up from a broker. Default value is true. + /// + /// fail or not + /// Fluent builder + IPactVerifierSource FailIfNoPactsFound(bool fail); + /// /// Verify provider interactions /// diff --git a/src/PactNet/Interop/NativeInterop.cs b/src/PactNet/Interop/NativeInterop.cs index 92bdc911..08e6943f 100644 --- a/src/PactNet/Interop/NativeInterop.cs +++ b/src/PactNet/Interop/NativeInterop.cs @@ -167,6 +167,9 @@ public static extern void VerifierBrokerSourceWithSelectors(IntPtr handle, [DllImport(DllName, EntryPoint = "pactffi_verifier_output")] public static extern IntPtr VerifierOutput(IntPtr handle, byte stripAnsi); + [DllImport(DllName, EntryPoint = "pactffi_verifier_set_no_pacts_is_error")] + public static extern void VerifierSetNoPactsIsError(IntPtr handle, bool isError); + #endregion } } diff --git a/src/PactNet/Verifier/IVerifierProvider.cs b/src/PactNet/Verifier/IVerifierProvider.cs index 369ba00c..753aa85d 100644 --- a/src/PactNet/Verifier/IVerifierProvider.cs +++ b/src/PactNet/Verifier/IVerifierProvider.cs @@ -131,6 +131,14 @@ void AddBrokerSource(Uri url, ICollection consumerVersionSelectors, ICollection consumerVersionTags); + /// + /// Configures the verifier to return an error + /// if no pacts were found when looking up from a broker + /// + /// + /// Fluent builder + void SetNoPactsIsError(bool isError); + /// /// Verify the pact from the given args /// diff --git a/src/PactNet/Verifier/InteropVerifierProvider.cs b/src/PactNet/Verifier/InteropVerifierProvider.cs index c0245296..1c13d671 100644 --- a/src/PactNet/Verifier/InteropVerifierProvider.cs +++ b/src/PactNet/Verifier/InteropVerifierProvider.cs @@ -215,6 +215,17 @@ public void AddBrokerSource(Uri url, (ushort)consumerVersionTags.Count); } + /// + /// Configures the verifier to return an error + /// if no pacts were found when looking up from a broker + /// + /// + /// Fluent builder + public void SetNoPactsIsError(bool isError) + { + NativeInterop.VerifierSetNoPactsIsError(this.handle, isError); + } + /// /// Verify the pact from the given args /// diff --git a/src/PactNet/Verifier/PactVerifierSource.cs b/src/PactNet/Verifier/PactVerifierSource.cs index f2bd619f..ab5f266e 100644 --- a/src/PactNet/Verifier/PactVerifierSource.cs +++ b/src/PactNet/Verifier/PactVerifierSource.cs @@ -100,6 +100,17 @@ public IPactVerifierSource WithCustomHeader(string name, string value) return this; } + /// + /// Return an error or not if no pacts were found when looking up from a broker. Default value is true. + /// + /// fail or not + /// Fluent builder + public IPactVerifierSource FailIfNoPactsFound(bool fail) + { + this.provider.SetNoPactsIsError(fail); + return this; + } + /// /// Verify provider interactions /// diff --git a/tests/PactNet.Tests/Verifier/InteropVerifierProviderTests.cs b/tests/PactNet.Tests/Verifier/InteropVerifierProviderTests.cs index 91d4b023..e59f12a5 100644 --- a/tests/PactNet.Tests/Verifier/InteropVerifierProviderTests.cs +++ b/tests/PactNet.Tests/Verifier/InteropVerifierProviderTests.cs @@ -50,6 +50,7 @@ public void HappyPathIntegrationTest() "main", new[] { @"{""branch"":""main""}" }, new[] { "consumer-tag" }); + provider.SetNoPactsIsError(true); Action action = () => provider.Execute(); diff --git a/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs b/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs index 5bdec907..3cb56e24 100644 --- a/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs +++ b/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs @@ -110,5 +110,23 @@ public void Verify_VerificationError_ThrowsPactFailureException() action.Should().Throw(); } + + [Fact] + public void FailIfNoPactsFound_WhenTrue_SetsNoPactsIsErrorTrue() + { + this.verifier.FailIfNoPactsFound(true); + this.verifier.Verify(); + + this.mockProvider.Verify(p => p.SetNoPactsIsError(true)); + } + + [Fact] + public void FailIfNoPactsFound_WhenFalse_SetsNoPactsIsErrorFalse() + { + this.verifier.FailIfNoPactsFound(false); + this.verifier.Verify(); + + this.mockProvider.Verify(p => p.SetNoPactsIsError(false)); + } } } From ede25eae0f4ab6054c21f6bbe4d1f3f92f03f1ea Mon Sep 17 00:00:00 2001 From: Levan Nozadze Date: Sat, 14 Jun 2025 21:39:40 +0400 Subject: [PATCH 2/2] fix: Move NoPactsIsError to PactBrokerOptions --- .../OrdersApi/Provider.Tests/ProviderTests.cs | 2 +- .../Verifier/IPactBrokerOptions.cs | 7 +++++++ .../Verifier/IPactVerifierSource.cs | 7 ------- src/PactNet/Verifier/IVerifierProvider.cs | 6 ++---- .../Verifier/InteropVerifierProvider.cs | 6 ++---- src/PactNet/Verifier/PactBrokerOptions.cs | 11 +++++++++++ src/PactNet/Verifier/PactVerifierSource.cs | 11 ----------- .../Verifier/PactBrokerOptionsTests.cs | 14 ++++++++++++++ .../Verifier/PactVerifierSourceTests.cs | 18 ------------------ 9 files changed, 37 insertions(+), 45 deletions(-) diff --git a/samples/OrdersApi/Provider.Tests/ProviderTests.cs b/samples/OrdersApi/Provider.Tests/ProviderTests.cs index 1feb302e..05daad1a 100644 --- a/samples/OrdersApi/Provider.Tests/ProviderTests.cs +++ b/samples/OrdersApi/Provider.Tests/ProviderTests.cs @@ -38,7 +38,7 @@ public ProviderTests(ITestOutputHelper output) .Build(); this.server.Start(); - + this.verifier = new PactVerifier("Orders API", new PactVerifierConfig { LogLevel = PactLogLevel.Debug, diff --git a/src/PactNet.Abstractions/Verifier/IPactBrokerOptions.cs b/src/PactNet.Abstractions/Verifier/IPactBrokerOptions.cs index 7ba2cc40..47fb86f3 100644 --- a/src/PactNet.Abstractions/Verifier/IPactBrokerOptions.cs +++ b/src/PactNet.Abstractions/Verifier/IPactBrokerOptions.cs @@ -104,5 +104,12 @@ public interface IPactBrokerOptions /// Configure the publish options /// Fluent builder IPactBrokerOptions PublishResults(bool condition, string providerVersion, Action configure); + + /// + /// Return an error when no pacts are found on the Pact Broker. By default, an error is returned. + /// + /// return error or not + /// Fluent builder + IPactBrokerOptions NoPactsIsError(bool isError); } } diff --git a/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs b/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs index d7cd0705..d6bae4e9 100644 --- a/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs +++ b/src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs @@ -53,13 +53,6 @@ public interface IPactVerifierSource /// Fluent builder IPactVerifierSource WithCustomHeader(string name, string value); - /// - /// Return an error or not if no pacts were found when looking up from a broker. Default value is true. - /// - /// fail or not - /// Fluent builder - IPactVerifierSource FailIfNoPactsFound(bool fail); - /// /// Verify provider interactions /// diff --git a/src/PactNet/Verifier/IVerifierProvider.cs b/src/PactNet/Verifier/IVerifierProvider.cs index 753aa85d..615efc2d 100644 --- a/src/PactNet/Verifier/IVerifierProvider.cs +++ b/src/PactNet/Verifier/IVerifierProvider.cs @@ -132,11 +132,9 @@ void AddBrokerSource(Uri url, ICollection consumerVersionTags); /// - /// Configures the verifier to return an error - /// if no pacts were found when looking up from a broker + /// Configures the verifier to return an error when no pacts are found on the Pact Broker. By default, an error is returned. /// - /// - /// Fluent builder + /// return error or not void SetNoPactsIsError(bool isError); /// diff --git a/src/PactNet/Verifier/InteropVerifierProvider.cs b/src/PactNet/Verifier/InteropVerifierProvider.cs index 1c13d671..f9884fae 100644 --- a/src/PactNet/Verifier/InteropVerifierProvider.cs +++ b/src/PactNet/Verifier/InteropVerifierProvider.cs @@ -216,11 +216,9 @@ public void AddBrokerSource(Uri url, } /// - /// Configures the verifier to return an error - /// if no pacts were found when looking up from a broker + /// Configures the verifier to return an error when no pacts are found on the Pact Broker. By default, an error is returned. /// - /// - /// Fluent builder + /// return error or not public void SetNoPactsIsError(bool isError) { NativeInterop.VerifierSetNoPactsIsError(this.handle, isError); diff --git a/src/PactNet/Verifier/PactBrokerOptions.cs b/src/PactNet/Verifier/PactBrokerOptions.cs index 987fd1e3..77245cee 100644 --- a/src/PactNet/Verifier/PactBrokerOptions.cs +++ b/src/PactNet/Verifier/PactBrokerOptions.cs @@ -200,6 +200,17 @@ public IPactBrokerOptions PublishResults(bool condition, string providerVersion) public IPactBrokerOptions PublishResults(bool condition, string providerVersion, Action configure) => condition ? this.PublishResults(providerVersion, configure) : this; + /// + /// Return an error when no pacts are found on the Pact Broker. By default, an error is returned. + /// + /// return error or not + /// Fluent builder + public IPactBrokerOptions NoPactsIsError(bool isError) + { + this.provider.SetNoPactsIsError(isError); + return this; + } + /// /// Finalise the configuration with the provider /// diff --git a/src/PactNet/Verifier/PactVerifierSource.cs b/src/PactNet/Verifier/PactVerifierSource.cs index ab5f266e..f2bd619f 100644 --- a/src/PactNet/Verifier/PactVerifierSource.cs +++ b/src/PactNet/Verifier/PactVerifierSource.cs @@ -100,17 +100,6 @@ public IPactVerifierSource WithCustomHeader(string name, string value) return this; } - /// - /// Return an error or not if no pacts were found when looking up from a broker. Default value is true. - /// - /// fail or not - /// Fluent builder - public IPactVerifierSource FailIfNoPactsFound(bool fail) - { - this.provider.SetNoPactsIsError(fail); - return this; - } - /// /// Verify provider interactions /// diff --git a/tests/PactNet.Tests/Verifier/PactBrokerOptionsTests.cs b/tests/PactNet.Tests/Verifier/PactBrokerOptionsTests.cs index 742cbf0f..fee1fbbb 100644 --- a/tests/PactNet.Tests/Verifier/PactBrokerOptionsTests.cs +++ b/tests/PactNet.Tests/Verifier/PactBrokerOptionsTests.cs @@ -177,6 +177,20 @@ public void PublishResults_ConditionNotMet_DoesNotAddPublishArgs() Times.Never); } + [Fact] + public void NoPactsIsError_WhenTrue_SetsNoPactsIsErrorTrue() + { + this.options.NoPactsIsError(true); + this.mockProvider.Verify(p => p.SetNoPactsIsError(true)); + } + + [Fact] + public void NoPactsIsError_WhenFalse_SetsNoPactsIsErrorFalse() + { + this.options.NoPactsIsError(false); + this.mockProvider.Verify(p => p.SetNoPactsIsError(false)); + } + private void Verify(string username = null, string password = null, string token = null, diff --git a/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs b/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs index 3cb56e24..5bdec907 100644 --- a/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs +++ b/tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs @@ -110,23 +110,5 @@ public void Verify_VerificationError_ThrowsPactFailureException() action.Should().Throw(); } - - [Fact] - public void FailIfNoPactsFound_WhenTrue_SetsNoPactsIsErrorTrue() - { - this.verifier.FailIfNoPactsFound(true); - this.verifier.Verify(); - - this.mockProvider.Verify(p => p.SetNoPactsIsError(true)); - } - - [Fact] - public void FailIfNoPactsFound_WhenFalse_SetsNoPactsIsErrorFalse() - { - this.verifier.FailIfNoPactsFound(false); - this.verifier.Verify(); - - this.mockProvider.Verify(p => p.SetNoPactsIsError(false)); - } } }