From f0f142e253ef606c2bce92610af5a2dd6e7e5c50 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 18 Jan 2023 10:47:27 +1100 Subject: [PATCH 1/4] feat: allow setting fail_if_no_pacts_found in honours_pacts_from_pact_broker defaults to true, if not set (existing behaviour) --- lib/pact/cli/run_pact_verification.rb | 1 - .../fetch_pact_uris_for_verification.rb | 27 +++++++++++++------ .../pact_verification_from_broker.rb | 10 +++++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/pact/cli/run_pact_verification.rb b/lib/pact/cli/run_pact_verification.rb index 333120b5..daa50e94 100644 --- a/lib/pact/cli/run_pact_verification.rb +++ b/lib/pact/cli/run_pact_verification.rb @@ -72,7 +72,6 @@ def run_with_pact_uri_object def run_with_configured_pacts_from_pact_helper pact_urls = Pact.provider_world.pact_urls - raise "Please configure a pact to verify" if pact_urls.empty? Pact::Provider::PactSpecRunner.new(pact_urls, pact_spec_options).run end diff --git a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb index 631562d3..8c2bc3ee 100644 --- a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb +++ b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb @@ -39,15 +39,17 @@ def self.call(provider, consumer_version_selectors, provider_version_branch, pro end def call - if index.can?(PACTS_FOR_VERIFICATION_RELATION) || index.can?(PACTS_FOR_VERIFICATION_RELATION_BETA) - log_message - pacts_for_verification - else - old_selectors = consumer_version_selectors.collect do | selector | - { name: selector[:tag], all: !selector[:latest], fallback: selector[:fallbackTag]} + handling_no_pacts_found do + if index.can?(PACTS_FOR_VERIFICATION_RELATION) || index.can?(PACTS_FOR_VERIFICATION_RELATION_BETA) + log_message + pacts_for_verification + else + old_selectors = consumer_version_selectors.collect do | selector | + { name: selector[:tag], all: !selector[:latest], fallback: selector[:fallbackTag]} + end + # Fall back to old method of fetching pacts + FetchPacts.call(provider, old_selectors, broker_base_url, http_client_options) end - # Fall back to old method of fetching pacts - FetchPacts.call(provider, old_selectors, broker_base_url, http_client_options) end end @@ -96,6 +98,15 @@ def symbolize_keys(hash) def log_message Pact.configuration.output_stream.puts "INFO: #{pact_selection_description(provider, consumer_version_selectors, options, broker_base_url)}" end + + def handling_no_pacts_found + pacts_found = yield + if pacts_found.empty? && options[:fail_if_no_pacts_found] != false + raise "No pacts found to verify" + else + pacts_found + end + end end end end diff --git a/lib/pact/provider/configuration/pact_verification_from_broker.rb b/lib/pact/provider/configuration/pact_verification_from_broker.rb index 9a1a6346..71b20abe 100644 --- a/lib/pact/provider/configuration/pact_verification_from_broker.rb +++ b/lib/pact/provider/configuration/pact_verification_from_broker.rb @@ -15,7 +15,7 @@ class PactVerificationFromBroker # in parent scope, it will clash with these ones, # so put an underscore in front of the name to be safer. - attr_accessor :_provider_name, :_pact_broker_base_url, :_consumer_version_tags, :_provider_version_branch, :_provider_version_tags, :_basic_auth_options, :_enable_pending, :_include_wip_pacts_since, :_verbose, :_consumer_version_selectors + attr_accessor :_provider_name, :_pact_broker_base_url, :_consumer_version_tags, :_provider_version_branch, :_provider_version_tags, :_basic_auth_options, :_enable_pending, :_include_wip_pacts_since, :_verbose, :_consumer_version_selectors, :_fail_if_no_pacts_found def initialize(provider_name, provider_version_branch, provider_version_tags) @_provider_name = provider_name @@ -26,6 +26,7 @@ def initialize(provider_name, provider_version_branch, provider_version_tags) @_enable_pending = false @_include_wip_pacts_since = nil @_verbose = false + @_fail_if_no_pacts_found = true # CLI defaults to false, unfortunately for consistency end dsl do @@ -46,6 +47,11 @@ def enable_pending enable_pending self._enable_pending = enable_pending end + # Underlying code defaults to true if not specified + def fail_if_no_pacts_found fail_if_no_pacts_found + self._fail_if_no_pacts_found = fail_if_no_pacts_found + end + def include_wip_pacts_since since self._include_wip_pacts_since = if since.respond_to?(:xmlschema) since.xmlschema @@ -74,7 +80,7 @@ def create_pact_verification _provider_version_tags, _pact_broker_base_url, _basic_auth_options.merge(verbose: _verbose), - { include_pending_status: _enable_pending, include_wip_pacts_since: _include_wip_pacts_since } + { include_pending_status: _enable_pending, include_wip_pacts_since: _include_wip_pacts_since, fail_if_no_pacts_found: _fail_if_no_pacts_found } ) Pact.provider_world.add_pact_uri_source fetch_pacts From 0145d2da4526286a831cb5d59e8b21252c264153 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Tue, 6 Aug 2024 21:01:17 +0100 Subject: [PATCH 2/4] fix: handle case in no_pacts_found - undefined method empty? for nil --- lib/pact/pact_broker/fetch_pact_uris_for_verification.rb | 2 +- .../pact/pact_broker/fetch_pact_uris_for_verification_spec.rb | 4 ++-- .../configuration/pact_verification_from_broker_spec.rb | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb index 8c2bc3ee..42ac64b3 100644 --- a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb +++ b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb @@ -101,7 +101,7 @@ def log_message def handling_no_pacts_found pacts_found = yield - if pacts_found.empty? && options[:fail_if_no_pacts_found] != false + if pacts_found.blank? && options[:fail_if_no_pacts_found] != false raise "No pacts found to verify" else pacts_found diff --git a/spec/lib/pact/pact_broker/fetch_pact_uris_for_verification_spec.rb b/spec/lib/pact/pact_broker/fetch_pact_uris_for_verification_spec.rb index e0690663..c17800b2 100644 --- a/spec/lib/pact/pact_broker/fetch_pact_uris_for_verification_spec.rb +++ b/spec/lib/pact/pact_broker/fetch_pact_uris_for_verification_spec.rb @@ -14,7 +14,7 @@ module PactBroker let(:consumer_version_selectors) { [{ tag: "cmaster", latest: true, fallbackTag: 'blah' }] } let(:provider_version_branch) { "pbranch" } let(:provider_version_tags) { ["pmaster"] } - + subject { FetchPactURIsForVerification.call(provider, consumer_version_selectors, provider_version_branch, provider_version_tags, broker_base_url, http_client_options)} context "when there is an error retrieving the index resource" do @@ -62,7 +62,7 @@ module PactBroker it "calls the old fetch pacts code" do expect(FetchPacts).to receive(:call).with(provider, [{ name: "cmaster", all: false, fallback: "blah" }], broker_base_url, http_client_options) - subject + expect { subject }.to raise_error( "No pacts found to verify" ) end end end diff --git a/spec/lib/pact/provider/configuration/pact_verification_from_broker_spec.rb b/spec/lib/pact/provider/configuration/pact_verification_from_broker_spec.rb index 79c5c1ae..2bb838f8 100644 --- a/spec/lib/pact/provider/configuration/pact_verification_from_broker_spec.rb +++ b/spec/lib/pact/provider/configuration/pact_verification_from_broker_spec.rb @@ -37,7 +37,7 @@ module Configuration let(:fetch_pacts) { double('FetchPacts') } let(:basic_auth_opts) { basic_auth_options.merge(verbose: true) } - let(:options) { { include_pending_status: true, include_wip_pacts_since: "2020-01-01" }} + let(:options) { { fail_if_no_pacts_found: true, include_pending_status: true, include_wip_pacts_since: "2020-01-01" }} let(:consumer_version_selectors) { [ { tag: 'master', latest: true }] } it "creates a instance of Pact::PactBroker::FetchPactURIsForVerification" do @@ -70,6 +70,7 @@ module Configuration anything, anything, { + fail_if_no_pacts_found: true, include_pending_status: true, include_wip_pacts_since: since.xmlschema } From b342955ec32a9b6fd4f327e16f576748ba2bb33b Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Tue, 6 Aug 2024 22:30:27 +0100 Subject: [PATCH 3/4] chore: warn if fail_if_no_pacts_found is true and no pacts found --- lib/pact/pact_broker/fetch_pact_uris_for_verification.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb index 42ac64b3..1eecad23 100644 --- a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb +++ b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb @@ -101,11 +101,11 @@ def log_message def handling_no_pacts_found pacts_found = yield - if pacts_found.blank? && options[:fail_if_no_pacts_found] != false - raise "No pacts found to verify" - else - pacts_found + raise "No pacts found to verify" if pacts_found.blank? && options[:fail_if_no_pacts_found] != false + if pacts_found.blank? && options[:fail_if_no_pacts_found] == false + Pact.configuration.output_stream.puts "WARN: No pacts found to verify & fail_if_no_pacts_found is set to false." end + pacts_found end end end From 9e03e10e7fe50bd0c238aec2a242aeefe5b4c5d2 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Tue, 6 Aug 2024 22:52:36 +0100 Subject: [PATCH 4/4] chore: add example of fail_if_no_pacts_found from pact broker --- .../animal-service/spec/service_consumers/pact_helper.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/example/animal-service/spec/service_consumers/pact_helper.rb b/example/animal-service/spec/service_consumers/pact_helper.rb index fd4824c6..da2d2080 100644 --- a/example/animal-service/spec/service_consumers/pact_helper.rb +++ b/example/animal-service/spec/service_consumers/pact_helper.rb @@ -8,4 +8,11 @@ pact_uri '../zoo-app/spec/pacts/zoo_app-animal_service.json' end + ## For pact contracts from a Pact Broker + + # honours_pacts_from_pact_broker do + # pact_broker_base_url 'http://localhost:9292' + # # fail_if_no_pacts_found false # defaults to true + # end + end