module StubENV
  def stub_env(key, value)
    allow(ENV).to receive(:[]).and_call_original unless @env_already_stubbed
    @env_already_stubbed ||= true
    allow(ENV).to receive(:[]).with(key).and_return(value)
  end
end

module StubENV
  def stub_env(key, value)
    allow(ENV).to receive(:[]).and_call_original unless @env_already_stubbed
    @env_already_stubbed ||= true
    allow(ENV).to receive(:[]).with(key).and_return(value)
  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
  end
end

Reset the state of StubENV's @env_already_stubbed after each spec run
Reset the state of StubENV's @env_already_stubbed after each spec run

In certain combination of tests, @env_already_stubbed could be set
to `true` even though it never ran for that specific test.
