Tweak use of with_env
- Use it in more places where it saves code - Allow using symbolic keys for a cleaner interface - Use `HOMEBREW_GEM_*` instead of `GEM_OLD_*`
This commit is contained in:
parent
7caca57073
commit
ca189437e4
@ -70,8 +70,8 @@ then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Save value to use for installing gems
|
# Save value to use for installing gems
|
||||||
export GEM_OLD_HOME="$GEM_HOME"
|
export HOMEBREW_GEM_HOME="$GEM_HOME"
|
||||||
export GEM_OLD_PATH="$GEM_PATH"
|
export HOMEBREW_GEM_PATH="$GEM_PATH"
|
||||||
|
|
||||||
# Users may have these set, pointing the system Ruby
|
# Users may have these set, pointing the system Ruby
|
||||||
# at non-system gem paths
|
# at non-system gem paths
|
||||||
|
@ -114,7 +114,7 @@ module FileUtils
|
|||||||
|
|
||||||
if superenv?
|
if superenv?
|
||||||
make_name = File.basename(make_path)
|
make_name = File.basename(make_path)
|
||||||
with_env "HOMEBREW_MAKE" => make_name do
|
with_env(HOMEBREW_MAKE: make_name) do
|
||||||
system "make", *args
|
system "make", *args
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -1615,33 +1615,30 @@ class Formula
|
|||||||
# @private
|
# @private
|
||||||
def run_test
|
def run_test
|
||||||
@prefix_returns_versioned_prefix = true
|
@prefix_returns_versioned_prefix = true
|
||||||
old_home = ENV["HOME"]
|
|
||||||
old_java_opts = ENV["_JAVA_OPTIONS"]
|
|
||||||
old_curl_home = ENV["CURL_HOME"]
|
|
||||||
old_tmpdir = ENV["TMPDIR"]
|
|
||||||
old_temp = ENV["TEMP"]
|
|
||||||
old_tmp = ENV["TMP"]
|
|
||||||
old_term = ENV["TERM"]
|
|
||||||
old_path = ENV["PATH"]
|
|
||||||
old_homebrew_path = ENV["HOMEBREW_PATH"]
|
|
||||||
|
|
||||||
ENV["CURL_HOME"] = old_curl_home || old_home
|
test_env = {
|
||||||
ENV["TMPDIR"] = ENV["TEMP"] = ENV["TMP"] = HOMEBREW_TEMP
|
CURL_HOME: ENV["CURL_HOME"] || ENV["HOME"],
|
||||||
ENV["TERM"] = "dumb"
|
TMPDIR: HOMEBREW_TEMP,
|
||||||
ENV["PATH"] = PATH.new(old_path).append(HOMEBREW_PREFIX/"bin")
|
TEMP: HOMEBREW_TEMP,
|
||||||
ENV["HOMEBREW_PATH"] = nil
|
TMP: HOMEBREW_TEMP,
|
||||||
ENV["_JAVA_OPTIONS"] = "#{old_java_opts} -Duser.home=#{HOMEBREW_CACHE}/java_cache"
|
TERM: "dumb",
|
||||||
|
PATH: PATH.new(ENV["PATH"]).append(HOMEBREW_PREFIX/"bin"),
|
||||||
|
HOMEBREW_PATH: nil,
|
||||||
|
_JAVA_OPTIONS: "#{ENV["_JAVA_OPTIONS"]} -Duser.home=#{HOMEBREW_CACHE}/java_cache",
|
||||||
|
}
|
||||||
|
|
||||||
ENV.clear_sensitive_environment!
|
ENV.clear_sensitive_environment!
|
||||||
|
|
||||||
mktemp("#{name}-test") do |staging|
|
mktemp("#{name}-test") do |staging|
|
||||||
staging.retain! if ARGV.keep_tmp?
|
staging.retain! if ARGV.keep_tmp?
|
||||||
@testpath = staging.tmpdir
|
@testpath = staging.tmpdir
|
||||||
ENV["HOME"] = @testpath
|
test_env[:HOME] = @testpath
|
||||||
setup_home @testpath
|
setup_home @testpath
|
||||||
begin
|
begin
|
||||||
with_logging("test") do
|
with_logging("test") do
|
||||||
test
|
with_env(test_env) do
|
||||||
|
test
|
||||||
|
end
|
||||||
end
|
end
|
||||||
rescue Exception # rubocop:disable Lint/RescueException
|
rescue Exception # rubocop:disable Lint/RescueException
|
||||||
staging.retain! if ARGV.debug?
|
staging.retain! if ARGV.debug?
|
||||||
@ -1650,15 +1647,6 @@ class Formula
|
|||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
@testpath = nil
|
@testpath = nil
|
||||||
ENV["HOME"] = old_home
|
|
||||||
ENV["_JAVA_OPTIONS"] = old_java_opts
|
|
||||||
ENV["CURL_HOME"] = old_curl_home
|
|
||||||
ENV["TMPDIR"] = old_tmpdir
|
|
||||||
ENV["TEMP"] = old_temp
|
|
||||||
ENV["TMP"] = old_tmp
|
|
||||||
ENV["TERM"] = old_term
|
|
||||||
ENV["PATH"] = old_path
|
|
||||||
ENV["HOMEBREW_PATH"] = old_homebrew_path
|
|
||||||
@prefix_returns_versioned_prefix = false
|
@prefix_returns_versioned_prefix = false
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1891,32 +1879,27 @@ class Formula
|
|||||||
env_home = buildpath/".brew_home"
|
env_home = buildpath/".brew_home"
|
||||||
mkdir_p env_home
|
mkdir_p env_home
|
||||||
|
|
||||||
old_home = ENV["HOME"]
|
stage_env = {
|
||||||
old_java_opts = ENV["_JAVA_OPTIONS"]
|
HOMEBREW_PATH: nil,
|
||||||
old_curl_home = ENV["CURL_HOME"]
|
}
|
||||||
old_path = ENV["HOMEBREW_PATH"]
|
|
||||||
|
|
||||||
unless ARGV.interactive?
|
unless ARGV.interactive?
|
||||||
ENV["HOME"] = env_home
|
stage_env[:HOME] = env_home
|
||||||
ENV["_JAVA_OPTIONS"] = "#{old_java_opts} -Duser.home=#{HOMEBREW_CACHE}/java_cache"
|
stage_env[:_JAVA_OPTIONS] =
|
||||||
ENV["CURL_HOME"] = old_curl_home || old_home
|
"#{ENV["_JAVA_OPTIONS"]} -Duser.home=#{HOMEBREW_CACHE}/java_cache"
|
||||||
|
stage_env[:CURL_HOME] = ENV["CURL_HOME"] || ENV["HOME"]
|
||||||
end
|
end
|
||||||
ENV["HOMEBREW_PATH"] = nil
|
|
||||||
|
|
||||||
setup_home env_home
|
setup_home env_home
|
||||||
|
|
||||||
ENV.clear_sensitive_environment!
|
ENV.clear_sensitive_environment!
|
||||||
|
|
||||||
begin
|
begin
|
||||||
yield staging
|
with_env(stage_env) do
|
||||||
|
yield staging
|
||||||
|
end
|
||||||
ensure
|
ensure
|
||||||
@buildpath = nil
|
@buildpath = nil
|
||||||
unless ARGV.interactive?
|
|
||||||
ENV["HOME"] = old_home
|
|
||||||
ENV["_JAVA_OPTIONS"] = old_java_opts
|
|
||||||
ENV["CURL_HOME"] = old_curl_home
|
|
||||||
end
|
|
||||||
ENV["HOMEBREW_PATH"] = old_path
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -274,13 +274,13 @@ describe "globally-scoped helper methods" do
|
|||||||
describe "#with_env" do
|
describe "#with_env" do
|
||||||
it "sets environment variables within the block" do
|
it "sets environment variables within the block" do
|
||||||
expect(ENV["PATH"]).not_to eq("/bin")
|
expect(ENV["PATH"]).not_to eq("/bin")
|
||||||
with_env "PATH" => "/bin" do
|
with_env(PATH: "/bin") do
|
||||||
expect(ENV["PATH"]).to eq("/bin")
|
expect(ENV["PATH"]).to eq("/bin")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "restores ENV after the block" do
|
it "restores ENV after the block" do
|
||||||
with_env "PATH" => "/bin" do
|
with_env(PATH: "/bin") do
|
||||||
expect(ENV["PATH"]).to eq("/bin")
|
expect(ENV["PATH"]).to eq("/bin")
|
||||||
end
|
end
|
||||||
expect(ENV["PATH"]).not_to eq("/bin")
|
expect(ENV["PATH"]).not_to eq("/bin")
|
||||||
@ -288,7 +288,7 @@ describe "globally-scoped helper methods" do
|
|||||||
|
|
||||||
it "restores ENV if an exception is raised" do
|
it "restores ENV if an exception is raised" do
|
||||||
expect {
|
expect {
|
||||||
with_env "PATH" => "/bin" do
|
with_env(PATH: "/bin") do
|
||||||
raise StandardError, "boom"
|
raise StandardError, "boom"
|
||||||
end
|
end
|
||||||
}.to raise_error(StandardError)
|
}.to raise_error(StandardError)
|
||||||
|
@ -189,9 +189,11 @@ module Homebrew
|
|||||||
|
|
||||||
def install_gem_setup_path!(name, version = nil, executable = name)
|
def install_gem_setup_path!(name, version = nil, executable = name)
|
||||||
# Respect user's preferences for where gems should be installed.
|
# Respect user's preferences for where gems should be installed.
|
||||||
ENV["GEM_HOME"] = ENV["GEM_OLD_HOME"].to_s
|
ENV["GEM_HOME"] = ENV["HOMEBREW_GEM_HOME"].to_s
|
||||||
ENV["GEM_HOME"] = Gem.user_dir if ENV["GEM_HOME"].empty?
|
ENV["GEM_HOME"] = Gem.user_dir if ENV["GEM_HOME"].empty?
|
||||||
ENV["GEM_PATH"] = ENV["GEM_OLD_PATH"] unless ENV["GEM_OLD_PATH"].to_s.empty?
|
unless ENV["HOMEBREW_GEM_PATH"].to_s.empty?
|
||||||
|
ENV["GEM_PATH"] = ENV["HOMEBREW_GEM_PATH"]
|
||||||
|
end
|
||||||
|
|
||||||
# Make rubygems notice env changes.
|
# Make rubygems notice env changes.
|
||||||
Gem.clear_paths
|
Gem.clear_paths
|
||||||
@ -263,31 +265,25 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def with_system_path
|
def with_system_path
|
||||||
old_path = ENV["PATH"]
|
with_env(PATH: PATH.new("/usr/bin", "/bin")) do
|
||||||
ENV["PATH"] = PATH.new("/usr/bin", "/bin")
|
yield
|
||||||
yield
|
end
|
||||||
ensure
|
|
||||||
ENV["PATH"] = old_path
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_homebrew_path
|
def with_homebrew_path
|
||||||
old_path = ENV["PATH"]
|
with_env(PATH: PATH.new(ENV["HOMEBREW_PATH"])) do
|
||||||
ENV["PATH"] = ENV["HOMEBREW_PATH"]
|
yield
|
||||||
yield
|
end
|
||||||
ensure
|
|
||||||
ENV["PATH"] = old_path
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_custom_locale(locale)
|
def with_custom_locale(locale)
|
||||||
old_locale = ENV["LC_ALL"]
|
with_env(LC_ALL: locale) do
|
||||||
ENV["LC_ALL"] = locale
|
yield
|
||||||
yield
|
end
|
||||||
ensure
|
|
||||||
ENV["LC_ALL"] = old_locale
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_as_not_developer(&_block)
|
def run_as_not_developer
|
||||||
with_env "HOMEBREW_DEVELOPER" => nil do
|
with_env(HOMEBREW_DEVELOPER: nil) do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -536,7 +532,7 @@ end
|
|||||||
# Calls the given block with the passed environment variables
|
# Calls the given block with the passed environment variables
|
||||||
# added to ENV, then restores ENV afterwards.
|
# added to ENV, then restores ENV afterwards.
|
||||||
# Example:
|
# Example:
|
||||||
# with_env "PATH" => "/bin" do
|
# with_env(PATH: "/bin") do
|
||||||
# system "echo $PATH"
|
# system "echo $PATH"
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
@ -547,6 +543,7 @@ def with_env(hash)
|
|||||||
old_values = {}
|
old_values = {}
|
||||||
begin
|
begin
|
||||||
hash.each do |key, value|
|
hash.each do |key, value|
|
||||||
|
key = key.to_s
|
||||||
old_values[key] = ENV.delete(key)
|
old_values[key] = ENV.delete(key)
|
||||||
ENV[key] = value
|
ENV[key] = value
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user