Skip to content

Commit

Permalink
Merge pull request #314 from pact-foundation/feat/update-fail-if-no-p…
Browse files Browse the repository at this point in the history
…acts-found-pr

Feat/update fail if no pacts found pr
  • Loading branch information
YOU54F authored Aug 7, 2024
2 parents 98a402c + 9e03e10 commit 84b406f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
7 changes: 7 additions & 0 deletions example/animal-service/spec/service_consumers/pact_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,6 +70,7 @@ module Configuration
anything,
anything,
{
fail_if_no_pacts_found: true,
include_pending_status: true,
include_wip_pacts_since: since.xmlschema
}
Expand Down

0 comments on commit 84b406f

Please sign in to comment.