Skip to content

Commit

Permalink
Merge pull request #67 from jjacobskind/error_when_no_pacts_found
Browse files Browse the repository at this point in the history
feat: support --fail-if-no-pacts-found option
  • Loading branch information
bethesque authored Mar 29, 2021
2 parents d77adf5 + c7cd77a commit 81c4b9e
Show file tree
Hide file tree
Showing 4 changed files with 95 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
55 changes: 55 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,60 @@ 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

context "multiple pacts need to be verified" do
before do
allow(AggregatePactConfigs).to receive(:call).and_return([{}, {}])
end

context "at least one pact fails verification" do
before do
allow(Cli::RunPactVerification).to receive(:call).and_return(1)
end

it "all pacts get verified" do
expect(Cli::RunPactVerification).to receive(:call).twice
subject
end
end
end
end
end
end
Expand Down

0 comments on commit 81c4b9e

Please sign in to comment.