Skip to content

Commit

Permalink
feat: Optionally fail when no pacts are found
Browse files Browse the repository at this point in the history
  • Loading branch information
jjacobskind committed Mar 29, 2021
1 parent d77adf5 commit 772e8fb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Options:
# The log level
# Default: debug
[--fail-if-no-pacts-found]
# If specified, will fail when no pacts are found
Description:
The parameters used when fetching pacts dynamically from a Pact Broker are:
Expand Down
61 changes: 37 additions & 24 deletions lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ def self.call pact_urls, options

def call
setup

pact_urls = all_pact_urls
warn_empty_pact_set
wait_until_provider_available
exit_statuses = pact_urls.collect do |pact_uri|
pacts_pass_verification?
end

private

def pacts_pass_verification?
return false if all_pact_urls.empty? && options.fail_if_no_pacts_found

exit_statuses = all_pact_urls.collect do |pact_uri|
verify_pact pact_uri
end

exit_statuses.all?{ | status | status == 0 }
end

private


def setup
configure_output
Expand Down Expand Up @@ -187,25 +192,33 @@ def reset_pact_configuration
end

def all_pact_urls
http_client_options = {
username: options.broker_username || ENV['PACT_BROKER_USERNAME'],
password: options.broker_password || ENV['PACT_BROKER_PASSWORD'],
token: options.broker_token || ENV['PACT_BROKER_TOKEN'],
verbose: verbose?
}
opts = {
enable_pending: options.enable_pending,
include_wip_pacts_since: options.include_wip_pacts_since
}
AggregatePactConfigs.call(
pact_urls,
options.provider,
consumer_version_tags,
consumer_version_selectors,
provider_version_tags,
options.pact_broker_base_url || ENV['PACT_BROKER_BASE_URL'],
http_client_options,
opts)
@all_pact_urls ||= begin
http_client_options = {
username: options.broker_username || ENV['PACT_BROKER_USERNAME'],
password: options.broker_password || ENV['PACT_BROKER_PASSWORD'],
token: options.broker_token || ENV['PACT_BROKER_TOKEN'],
verbose: verbose?
}
opts = {
enable_pending: options.enable_pending,
include_wip_pacts_since: options.include_wip_pacts_since
}
AggregatePactConfigs.call(
pact_urls,
options.provider,
consumer_version_tags,
consumer_version_selectors,
provider_version_tags,
options.pact_broker_base_url || ENV['PACT_BROKER_BASE_URL'],
http_client_options,
opts)
end
end

def warn_empty_pact_set
if all_pact_urls.empty?
$stderr.puts "WARN: No pacts were found for the consumer versions selected"
end
end

def require_pact_project_pact_helper
Expand Down
1 change: 1 addition & 0 deletions lib/pact/provider_verifier/cli/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AuthError < ::Thor::Error; end
method_option :wait, banner: "SECONDS", required: false, type: :numeric, desc: "The number of seconds to poll for the provider to become available before running the verification", default: 0
method_option :log_dir, desc: "The directory for the pact.log file"
method_option :log_level, desc: "The log level", default: "debug"
method_option :fail_if_no_pacts_found, desc: "If specified, will fail when no pacts are found", required: false, type: :boolean, default: false

def verify(*pact_urls)
require 'pact/provider_verifier/app'
Expand Down
38 changes: 38 additions & 0 deletions spec/lib/pact/provider_verifier/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module ProviderVerifier
double('options',
provider_base_url: "http://provider",
provider_version_tag: ["foo"],
publish_verification_results: false,
wait: 1,
provider_states_url: nil,
log_level: :info,
Expand Down Expand Up @@ -56,6 +57,43 @@ module ProviderVerifier
subject
end
end

context "when fail_if_no_pacts_found is false" do
before do
allow(options).to receive(:fail_if_no_pacts_found).and_return(false)
end

context "when no pacts are found" do
before do
allow(AggregatePactConfigs).to receive(:call).and_return([])
end

it { is_expected.to be true }
end
end

context "when fail_if_no_pacts_found is true" do
before do
allow(options).to receive(:fail_if_no_pacts_found).and_return(true)
end

context "when no pacts are found" do
before do
allow(AggregatePactConfigs).to receive(:call).and_return([])
end

it { is_expected.to be false }
end

context "when pacts are found and successfully verified" do
before do
allow(AggregatePactConfigs).to receive(:call).and_return([{}])
allow(Cli::RunPactVerification).to receive(:call).and_return(0)
end

it { is_expected.to be true }
end
end
end
end
end
Expand Down

0 comments on commit 772e8fb

Please sign in to comment.