diff --git a/lib/pact_broker/client/cli/matrix_commands.rb b/lib/pact_broker/client/cli/matrix_commands.rb index a8b5c1d..f4a7cd0 100644 --- a/lib/pact_broker/client/cli/matrix_commands.rb +++ b/lib/pact_broker/client/cli/matrix_commands.rb @@ -31,6 +31,7 @@ def can_i_deploy(*ignored_but_necessary) selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| !s[:ignore] } ignore_selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] } + ignore_selectors_from_environment_variable validate_can_i_deploy_selectors(selectors) + validate_can_i_deploy_options dry_run = options.dry_run || ENV["PACT_BROKER_CAN_I_DEPLOY_DRY_RUN"] == "true" can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval, dry_run: dry_run, verbose: options.verbose } result = CanIDeploy.call(selectors, { to_tag: options.to, to_environment: options.to_environment, limit: options.limit, ignore_selectors: ignore_selectors }, can_i_deploy_options, pact_broker_client_options) @@ -82,6 +83,12 @@ def validate_can_i_deploy_selectors selectors raise ::Thor::RequiredArgumentMissingError, "The version must be specified using `--version VERSION`, `--branch BRANCH` `--latest`, `--latest TAG`, or `--all TAG` for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any? end + def validate_can_i_deploy_options + if options[:to_environment] && options[:to_environment].blank? + raise ::Thor::RequiredArgumentMissingError, "The environment name cannot be blank" + end + end + def ignore_selectors_from_environment_variable ENV.fetch("PACT_BROKER_CAN_I_DEPLOY_IGNORE", "").split(",").collect(&:strip).collect{ |i| { pacticipant: i } } end 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 a89e251..3f4ef32 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 @@ -70,6 +70,16 @@ module CLI expect(CanIDeploy).to receive(:call).with(anything, {to_tag: nil, to_environment: 'prod', limit: 1000, ignore_selectors: []}, anything, anything) invoke_can_i_deploy end + + context "when the environment is an empty string" do + before do + subject.options.to_environment = ' ' + end + + it "raises an error" do + expect { invoke_can_i_deploy }.to raise_error ::Thor::RequiredArgumentMissingError + end + end end context "with basic auth" do