Skip to content

Commit

Permalink
feat: allow broker base url, username and password to be set by envir…
Browse files Browse the repository at this point in the history
…onment variables
  • Loading branch information
bethesque committed Oct 31, 2017
1 parent c2ede03 commit 3516103
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/pact_broker/client/cli/custom_thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3516103

Please sign in to comment.