diff --git a/README.md b/README.md index 6b89cd1..66dc294 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/pact/provider_verifier/app.rb b/lib/pact/provider_verifier/app.rb index c7fe7e7..bda7294 100644 --- a/lib/pact/provider_verifier/app.rb +++ b/lib/pact/provider_verifier/app.rb @@ -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 @@ -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 diff --git a/lib/pact/provider_verifier/cli/verify.rb b/lib/pact/provider_verifier/cli/verify.rb index 94f052b..2c63b75 100644 --- a/lib/pact/provider_verifier/cli/verify.rb +++ b/lib/pact/provider_verifier/cli/verify.rb @@ -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' diff --git a/spec/lib/pact/provider_verifier/app_spec.rb b/spec/lib/pact/provider_verifier/app_spec.rb index a7cfb4c..8f1da71 100644 --- a/spec/lib/pact/provider_verifier/app_spec.rb +++ b/spec/lib/pact/provider_verifier/app_spec.rb @@ -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, @@ -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