summaryrefslogtreecommitdiff
path: root/spec/support/stub_env.rb
diff options
context:
space:
mode:
authorPhil Hughes <me@iamphill.com>2017-06-30 12:31:41 +0100
committerPhil Hughes <me@iamphill.com>2017-06-30 12:37:35 +0100
commit531a4d124b0a17280b4118d7fa7c66f11042917b (patch)
treea2ee668d403ed538d6046f942f31bce2c1224563 /spec/support/stub_env.rb
parentf5681fb55a762c9b9bb35c80bf87d727d8d8fd06 (diff)
parent81dba76b9d7d120cd22e3619a4058bd4885be9bc (diff)
downloadgitlab-ce-experimental-breadcrumbs.tar.gz
Merge branch 'master' into experimental-breadcrumbsexperimental-breadcrumbs
Diffstat (limited to 'spec/support/stub_env.rb')
-rw-r--r--spec/support/stub_env.rb36
1 files changed, 27 insertions, 9 deletions
diff --git a/spec/support/stub_env.rb b/spec/support/stub_env.rb
index 2999bcd9fb1..b8928867174 100644
--- a/spec/support/stub_env.rb
+++ b/spec/support/stub_env.rb
@@ -1,15 +1,33 @@
+# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb
module StubENV
- def stub_env(key, value)
- allow(ENV).to receive(:[]).and_call_original unless @env_already_stubbed
- @env_already_stubbed ||= true
+ def stub_env(key_or_hash, value = nil)
+ init_stub unless env_stubbed?
+ 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)
allow(ENV).to receive(:[]).with(key).and_return(value)
+ 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]
end
-end
-# It's possible that the state of the class variables are not reset across
-# test runs.
-RSpec.configure do |config|
- config.after(:each) do
- @env_already_stubbed = nil
+ def init_stub
+ allow(ENV).to receive(:[]).and_call_original
+ allow(ENV).to receive(:fetch).and_call_original
+ add_stubbed_value(STUBBED_KEY, true)
end
end