Move with_temp_env out of test

This commit is contained in:
Luke Carrier 2020-01-23 11:20:36 +00:00
parent a1abc177bd
commit 16a9635553
2 changed files with 22 additions and 17 deletions

View File

@ -272,6 +272,26 @@ module Vagrant
end
end
end
# This helper provides temporary environmental variable changes.
def self.with_temp_env(environment)
# Build up the new environment, preserving the old values so we
# can replace them back in later.
old_env = {}
environment.each do |key, value|
key = key.to_s
old_env[key] = ENV[key]
ENV[key] = value
end
# Call the block, returning its return value
return yield
ensure
# Reset the environment no matter what
old_env.each do |key, value|
ENV[key] = value
end
end
end
# Default I18n to load the en locale

View File

@ -110,23 +110,8 @@ shared_context "unit" do
end
# This helper provides temporary environmental variable changes.
def with_temp_env(environment)
# Build up the new environment, preserving the old values so we
# can replace them back in later.
old_env = {}
environment.each do |key, value|
key = key.to_s
old_env[key] = ENV[key]
ENV[key] = value
end
# Call the block, returning its return value
return yield
ensure
# Reset the environment no matter what
old_env.each do |key, value|
ENV[key] = value
end
def with_temp_env(environment, &block)
Vagrant::with_temp_env(environment, &block)
end
# This helper provides a randomly available port(s) for each argument to the