From 0f3bfea7ead4e2d81d81f39ce4bfa92ae8002615 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 30 Oct 2017 15:57:43 +1100 Subject: [PATCH] feat(can-i-deploy): raise error when no --version is supplied for a --pacticipant --- lib/pact_broker/client/cli/broker.rb | 9 ++++++++- .../pact_broker/client/cli/broker_can_i_deploy_spec.rb | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 0bb0f223..d91ba7d1 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -4,6 +4,7 @@ require 'pact_broker/client/cli/custom_thor' require 'pact_broker/client/publish_pacts' require 'rake/file_list' +require 'thor/error' module PactBroker module Client @@ -24,6 +25,7 @@ class Broker < CustomThor def can_i_deploy(*ignored_but_necessary) selectors = VersionSelectorOptionsParser.call(ARGV) + validate_can_i_deploy_selectors(selectors) result = CanIDeploy.call(options.broker_base_url, selectors, {output: options.output}, pact_broker_client_options) $stdout.puts result.message exit(1) unless result.success @@ -58,10 +60,15 @@ def self.exit_on_failure? def validate_pact_files pact_files unless pact_files && pact_files.any? - raise RequiredArgumentMissingError, "No value provided for required pact_files" + raise ::Thor::RequiredArgumentMissingError, "No value provided for required pact_files" end end + def validate_can_i_deploy_selectors selectors + pacticipants_without_versions = selectors.select{ |s| s[:version].nil? }.collect{ |s| s[:pacticipant] } + raise ::Thor::RequiredArgumentMissingError, "No --version provided for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any? + end + def publish_pacts pact_files PactBroker::Client::PublishPacts.call( options.broker_base_url, diff --git a/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb b/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb index 0949bdb4..0cea12bf 100644 --- a/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb @@ -15,7 +15,7 @@ module CLI let(:result) { instance_double('PactBroker::Client::CanIDeploy::Result', success: success, message: message) } let(:success) { true } let(:message) { 'message' } - let(:version_selectors) { ['selector1'] } + let(:version_selectors) { [{pacticipant: "Foo", version: "1"}] } let(:minimum_valid_options) do { broker_base_url: 'http://pact-broker', @@ -36,6 +36,14 @@ module CLI invoke_can_i_deploy end + context "with a missing --version" do + let(:version_selectors) { [{pacticipant: "Foo", version: nil}] } + + it "raises an error" do + expect { invoke_can_i_deploy }.to raise_error Thor::RequiredArgumentMissingError, "No --version provided for pacticipant Foo" + end + end + context "with basic auth" do before do subject.options = OpenStruct.new(minimum_valid_options.merge(broker_username: 'foo', broker_password: 'bar'))