diff --git a/lib/pact_broker/client/cli/custom_thor.rb b/lib/pact_broker/client/cli/custom_thor.rb index fcd119a1..33c389a0 100644 --- a/lib/pact_broker/client/cli/custom_thor.rb +++ b/lib/pact_broker/client/cli/custom_thor.rb @@ -16,7 +16,24 @@ def self.start given_args = ARGV, config = {} end def self.massage_args argv - turn_muliple_tag_options_into_array(argv) + add_broker_config_from_environment_variables(turn_muliple_tag_options_into_array(argv)) + end + + def self.add_broker_config_from_environment_variables argv + return argv if argv[0] == 'version' || argv[0] == 'help' + + new_argv = add_option_from_environment_variable(argv, 'broker-base-url', 'b', 'PACT_BROKER_BASE_URL') + new_argv = add_option_from_environment_variable(new_argv, 'broker-username', 'u', 'PACT_BROKER_USERNAME') + add_option_from_environment_variable(new_argv, 'broker-password', 'p', 'PACT_BROKER_PASSWORD') + end + + def self.add_option_from_environment_variable argv, long_name, short_name, environment_variable_name + option_options = ["--#{long_name}", "--#{long_name.gsub('-','_')}", "-#{short_name}"] + if (argv & option_options).empty? && ENV[environment_variable_name] + argv + ["--#{long_name}", ENV[environment_variable_name]] + else + argv + end end # other task names, help, and the help shortcuts diff --git a/spec/lib/pact_broker/client/cli/custom_thor_spec.rb b/spec/lib/pact_broker/client/cli/custom_thor_spec.rb index 06377288..d8ce2e9b 100644 --- a/spec/lib/pact_broker/client/cli/custom_thor_spec.rb +++ b/spec/lib/pact_broker/client/cli/custom_thor_spec.rb @@ -18,6 +18,14 @@ def test_multiple_options Delegate.call(options) end + desc '', '' + method_option :broker_base_url, required: true, aliases: "-b" + method_option :broker_username, aliases: "-u" + method_option :broker_password, aliases: "-p" + def test_using_env_vars + Delegate.call(options) + end + default_command :test_default end @@ -29,6 +37,32 @@ def test_multiple_options TestThor.start(%w{test_multiple_options --multi one --multi two}) end + context "with broker configuration in the environment variables" do + before do + ENV['PACT_BROKER_BASE_URL'] = 'http://foo' + ENV['PACT_BROKER_USERNAME'] = 'username' + ENV['PACT_BROKER_PASSWORD'] = 'password' + end + + it "populates the options from the environment variables" do + expect(Delegate).to receive(:call) do | options | + expect(options.broker_base_url).to eq 'http://foo' + expect(options.broker_username).to eq 'username' + expect(options.broker_password).to eq 'password' + end + TestThor.start(%w{test_using_env_vars}) + end + + it "does not override a value specifed on the command line" do + expect(Delegate).to receive(:call) do | options | + expect(options.broker_base_url).to eq 'http://bar' + expect(options.broker_username).to eq 'username' + expect(options.broker_password).to eq 'password' + end + TestThor.start(%w{test_using_env_vars --broker-base-url http://bar}) + end + end + describe ".turn_muliple_tag_options_into_array" do it "turns '--tag foo --tag bar' into '--tag foo bar'" do input = %w{--ignore this --tag foo --tag bar --wiffle --that} diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b0ade0e4..2b603ee5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,3 +3,11 @@ WebMock.disable_net_connect!(allow_localhost: true) require "./spec/support/shared_context.rb" + +RSpec.configure do | config | + + config.before(:each) do + ENV.delete('PACT_BROKER_BASE_URL') + end + +end