Skip to content

Commit

Permalink
feat: allow setting fail_if_no_pacts_found in honours_pacts_from_pact…
Browse files Browse the repository at this point in the history
…_broker

defaults to true, if not set (existing behaviour)
  • Loading branch information
bethesque authored and YOU54F committed Aug 6, 2024
1 parent d512570 commit f0f142e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/pact/cli/run_pact_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 19 additions & 8 deletions lib/pact/pact_broker/fetch_pact_uris_for_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
10 changes: 8 additions & 2 deletions lib/pact/provider/configuration/pact_verification_from_broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f0f142e

Please sign in to comment.