2017-06-27 09:00:10 -04:00
|
|
|
# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb
|
2017-01-09 12:19:48 -05:00
|
|
|
module StubENV
|
2017-08-31 05:47:03 -04:00
|
|
|
include Gitlab::CurrentSettings
|
|
|
|
|
2017-06-27 09:00:10 -04:00
|
|
|
def stub_env(key_or_hash, value = nil)
|
|
|
|
init_stub unless env_stubbed?
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-06-27 09:00:10 -04:00
|
|
|
if key_or_hash.is_a? Hash
|
|
|
|
key_or_hash.each { |k, v| add_stubbed_value(k, v) }
|
|
|
|
else
|
|
|
|
add_stubbed_value key_or_hash, value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
STUBBED_KEY = '__STUBBED__'.freeze
|
|
|
|
|
|
|
|
def add_stubbed_value(key, value)
|
2017-01-09 12:19:48 -05:00
|
|
|
allow(ENV).to receive(:[]).with(key).and_return(value)
|
2017-12-11 12:52:07 -05:00
|
|
|
allow(ENV).to receive(:key?).with(key).and_return(true)
|
2017-06-27 09:00:10 -04:00
|
|
|
allow(ENV).to receive(:fetch).with(key).and_return(value)
|
|
|
|
allow(ENV).to receive(:fetch).with(key, anything()) do |_, default_val|
|
|
|
|
value || default_val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def env_stubbed?
|
|
|
|
ENV[STUBBED_KEY]
|
2017-01-09 12:19:48 -05:00
|
|
|
end
|
2017-06-24 00:01:32 -04:00
|
|
|
|
2017-06-27 09:00:10 -04:00
|
|
|
def init_stub
|
|
|
|
allow(ENV).to receive(:[]).and_call_original
|
2017-12-11 12:52:07 -05:00
|
|
|
allow(ENV).to receive(:key?).and_call_original
|
2017-06-27 09:00:10 -04:00
|
|
|
allow(ENV).to receive(:fetch).and_call_original
|
|
|
|
add_stubbed_value(STUBBED_KEY, true)
|
2017-06-24 00:01:32 -04:00
|
|
|
end
|
|
|
|
end
|