-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
108 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copied from https://gitlab.com/gitlab-org/gitlab/-/blob/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb | ||
module StubENV | ||
# Stub ENV variables | ||
# | ||
# You can provide either a key and value as separate params or both in a Hash format | ||
# | ||
# Keys and values will always be converted to String, to comply with how ENV behaves | ||
# | ||
# @param key_or_hash [String, Hash<String,String>] | ||
# @param value [String] | ||
def stub_env(key_or_hash, value = nil) | ||
init_stub unless env_stubbed? | ||
|
||
if key_or_hash.is_a? Hash | ||
key_or_hash.each do |key, value| | ||
add_stubbed_value(key, ensure_env_type(value)) | ||
end | ||
else | ||
add_stubbed_value key_or_hash, ensure_env_type(value) | ||
end | ||
end | ||
|
||
private | ||
|
||
STUBBED_KEY = "__STUBBED__" | ||
|
||
def add_stubbed_value(key, value) | ||
allow(ENV).to receive(:[]).with(key).and_return(value) | ||
allow(ENV).to receive(:key?).with(key).and_return(true) | ||
allow(ENV).to receive(:fetch).with(key) do |_| | ||
value || raise(KeyError, "key not found: \"#{key}\"") | ||
end | ||
allow(ENV).to receive(:fetch).with(key, anything) do |_, default_val| | ||
value || default_val | ||
end | ||
end | ||
|
||
def env_stubbed? | ||
ENV.fetch(STUBBED_KEY, false) | ||
end | ||
|
||
def init_stub | ||
allow(ENV).to receive(:[]).and_call_original | ||
allow(ENV).to receive(:key?).and_call_original | ||
allow(ENV).to receive(:fetch).and_call_original | ||
add_stubbed_value(STUBBED_KEY, true) | ||
end | ||
|
||
def ensure_env_type(value) | ||
value.nil? ? value : value.to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require "support/stub_env" | ||
|
||
# Copied from https://gitlab.com/gitlab-org/gitlab/-/blob/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb | ||
RSpec.describe "StubENV" do | ||
include StubENV | ||
|
||
describe "#stub_env" do | ||
it "stubs the environment variable for all ways to read it" do | ||
stub_env("MY_TEST_ENV_VAR", "the value") | ||
|
||
expect(ENV["MY_TEST_ENV_VAR"]).to eq("the value") # rubocop:disable Style/FetchEnvVar | ||
expect(ENV.key?("MY_TEST_ENV_VAR")).to be(true) | ||
expect(ENV.fetch("MY_TEST_ENV_VAR")).to eq("the value") | ||
expect(ENV.fetch("MY_TEST_ENV_VAR", "some default")).to eq("the value") | ||
end | ||
|
||
context "when stubbed to be nil" do | ||
it "uses a default value for fetch and raises if no default given" do | ||
stub_env("MY_TEST_ENV_VAR", nil) | ||
|
||
expect(ENV.fetch("MY_TEST_ENV_VAR", "some default")).to eq("some default") | ||
expect { ENV.fetch("MY_TEST_ENV_VAR") }.to raise_error(KeyError) | ||
end | ||
end | ||
end | ||
end |