From f0f142e253ef606c2bce92610af5a2dd6e7e5c50 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 18 Jan 2023 10:47:27 +1100 Subject: [PATCH] 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