summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2017-06-03 14:56:31 -0500
committerSamuel Giddins <segiddins@segiddins.me>2017-06-13 13:16:58 -0500
commit4337a499d0108fc3748084934aaed7591b355a26 (patch)
tree099dcf59f533ec5d0a3e40e2a1aec6959f03850e
parente8fd5795778379bd821e34173b678f76f240fa97 (diff)
downloadbundler-seg-original-env-all-keys.tar.gz
Ensure Bundler.original_env preserves _all_ keys bundler setsseg-original-env-all-keys
-rw-r--r--lib/bundler.rb4
-rw-r--r--lib/bundler/cli.rb2
-rw-r--r--lib/bundler/cli/install.rb2
-rw-r--r--lib/bundler/environment_preserver.rb30
-rw-r--r--lib/bundler/inline.rb2
-rw-r--r--lib/bundler/runtime.rb5
-rw-r--r--lib/bundler/shared_helpers.rb25
-rw-r--r--spec/bundler/environment_preserver_spec.rb2
-rw-r--r--spec/runtime/with_clean_env_spec.rb8
-rw-r--r--spec/spec_helper.rb2
10 files changed, 58 insertions, 24 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb
index 717c2d11e9..1228785734 100644
--- a/lib/bundler.rb
+++ b/lib/bundler.rb
@@ -15,7 +15,7 @@ require "bundler/constants"
require "bundler/current_ruby"
module Bundler
- environment_preserver = EnvironmentPreserver.new(ENV, %w[PATH GEM_PATH])
+ environment_preserver = EnvironmentPreserver.new(ENV, EnvironmentPreserver::BUNDLER_KEYS)
ORIGINAL_ENV = environment_preserver.restore
ENV.replace(environment_preserver.backup)
SUDO_MUTEX = Mutex.new
@@ -517,7 +517,7 @@ EOF
nil
end
- ENV["GEM_HOME"] = File.expand_path(bundle_path, root)
+ Bundler::SharedHelpers.set_env "GEM_HOME", File.expand_path(bundle_path, root)
Bundler.rubygems.clear_paths
end
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index 38db7db492..10287d3ac7 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -25,7 +25,7 @@ module Bundler
custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile]
if custom_gemfile && !custom_gemfile.empty?
- ENV["BUNDLE_GEMFILE"] = File.expand_path(custom_gemfile)
+ Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", File.expand_path(custom_gemfile)
Bundler.reset_paths!
end
diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb
index a6132751db..d57cad1e3f 100644
--- a/lib/bundler/cli/install.rb
+++ b/lib/bundler/cli/install.rb
@@ -25,7 +25,7 @@ module Bundler
normalize_groups
- ENV["RB_USER_INSTALL"] = "1" if Bundler::FREEBSD
+ Bundler::SharedHelpers.set_env "RB_USER_INSTALL", "1" if Bundler::FREEBSD
# Disable color in deployment mode
Bundler.ui.shell = Thor::Shell::Basic.new if options[:deployment]
diff --git a/lib/bundler/environment_preserver.rb b/lib/bundler/environment_preserver.rb
index a891f4854d..9a254bdd05 100644
--- a/lib/bundler/environment_preserver.rb
+++ b/lib/bundler/environment_preserver.rb
@@ -1,12 +1,26 @@
# frozen_string_literal: true
module Bundler
class EnvironmentPreserver
+ INTENTIONALLY_NIL = "BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL".freeze
+ BUNDLER_KEYS = %w[
+ BUNDLE_BIN_PATH
+ BUNDLE_GEMFILE
+ BUNDLER_ORIG_MANPATH
+ BUNDLER_VERSION
+ GEM_HOME
+ GEM_PATH
+ PATH
+ RUBYLIB
+ RUBYOPT
+ ].map(&:freeze).freeze
+ BUNDLER_PREFIX = "BUNDLER_ORIG_".freeze
+
# @param env [ENV]
# @param keys [Array<String>]
def initialize(env, keys)
@original = env.to_hash
@keys = keys
- @prefix = "BUNDLER_ORIG_"
+ @prefix = BUNDLER_PREFIX
end
# @return [Hash]
@@ -14,9 +28,10 @@ module Bundler
env = @original.clone
@keys.each do |key|
value = env[key]
- original_value = env[@prefix + key]
- if !value.nil? && !value.empty? && original_value.nil?
- env[@prefix + key] = value
+ if !value.nil? && !value.empty?
+ env[@prefix + key] ||= value
+ elsif value.nil?
+ env[@prefix + key] ||= INTENTIONALLY_NIL
end
end
env
@@ -27,10 +42,13 @@ module Bundler
env = @original.clone
@keys.each do |key|
value_original = env[@prefix + key]
- unless value_original.nil? || value_original.empty?
+ next if value_original.nil? || value_original.empty?
+ if value_original == INTENTIONALLY_NIL
+ env.delete(key)
+ else
env[key] = value_original
- env.delete(@prefix + key)
end
+ env.delete(@prefix + key)
end
env
end
diff --git a/lib/bundler/inline.rb b/lib/bundler/inline.rb
index 4d3791bfb2..7a4a1b0801 100644
--- a/lib/bundler/inline.rb
+++ b/lib/bundler/inline.rb
@@ -39,7 +39,7 @@ def gemfile(install = false, options = {}, &gemfile)
def Bundler.root
Bundler::SharedHelpers.pwd.expand_path
end
- ENV["BUNDLE_GEMFILE"] = "Gemfile"
+ Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", "Gemfile"
Bundler::Plugin.gemfile_install(&gemfile) if Bundler.feature_flag.plugins?
builder = Bundler::Dsl.new
diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb
index 5540509d74..152471db85 100644
--- a/lib/bundler/runtime.rb
+++ b/lib/bundler/runtime.rb
@@ -262,9 +262,6 @@ module Bundler
end
def setup_manpath
- # Store original MANPATH for restoration later in with_clean_env()
- ENV["BUNDLER_ORIG_MANPATH"] = ENV["MANPATH"]
-
# Add man/ subdirectories from activated bundles to MANPATH for man(1)
manuals = $LOAD_PATH.map do |path|
man_subdir = path.sub(/lib$/, "man")
@@ -272,7 +269,7 @@ module Bundler
end.compact
return if manuals.empty?
- ENV["MANPATH"] = manuals.concat(
+ Bundler::SharedHelpers.set_env "MANPATH", manuals.concat(
ENV["MANPATH"].to_s.split(File::PATH_SEPARATOR)
).uniq.join(File::PATH_SEPARATOR)
end
diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb
index b3354972d4..593d2c1e06 100644
--- a/lib/bundler/shared_helpers.rb
+++ b/lib/bundler/shared_helpers.rb
@@ -209,35 +209,46 @@ module Bundler
end
end
+ def set_env(key, value)
+ raise ArgumentError, "new key #{key}" unless EnvironmentPreserver::BUNDLER_KEYS.include?(key)
+ orig_key = "#{EnvironmentPreserver::BUNDLER_PREFIX}#{key}"
+ orig = ENV[key]
+ orig ||= EnvironmentPreserver::INTENTIONALLY_NIL
+ ENV[orig_key] ||= orig
+
+ ENV[key] = value
+ end
+ public :set_env
+
def set_bundle_variables
begin
- ENV["BUNDLE_BIN_PATH"] = Bundler.rubygems.bin_path("bundler", "bundle", VERSION)
+ Bundler::SharedHelpers.set_env "BUNDLE_BIN_PATH", Bundler.rubygems.bin_path("bundler", "bundle", VERSION)
rescue Gem::GemNotFoundException
- ENV["BUNDLE_BIN_PATH"] = File.expand_path("../../../exe/bundle", __FILE__)
+ Bundler::SharedHelpers.set_env "BUNDLE_BIN_PATH", File.expand_path("../../../exe/bundle", __FILE__)
end
# Set BUNDLE_GEMFILE
- ENV["BUNDLE_GEMFILE"] = find_gemfile.to_s
- ENV["BUNDLER_VERSION"] = Bundler::VERSION
+ Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", find_gemfile.to_s
+ Bundler::SharedHelpers.set_env "BUNDLER_VERSION", Bundler::VERSION
end
def set_path
paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
paths.unshift "#{Bundler.bundle_path}/bin"
- ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
+ Bundler::SharedHelpers.set_env "PATH", paths.uniq.join(File::PATH_SEPARATOR)
end
def set_rubyopt
rubyopt = [ENV["RUBYOPT"]].compact
return if !rubyopt.empty? && rubyopt.first =~ %r{-rbundler/setup}
rubyopt.unshift %(-rbundler/setup)
- ENV["RUBYOPT"] = rubyopt.join(" ")
+ Bundler::SharedHelpers.set_env "RUBYOPT", rubyopt.join(" ")
end
def set_rubylib
rubylib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR)
rubylib.unshift bundler_ruby_lib
- ENV["RUBYLIB"] = rubylib.uniq.join(File::PATH_SEPARATOR)
+ Bundler::SharedHelpers.set_env "RUBYLIB", rubylib.uniq.join(File::PATH_SEPARATOR)
end
def bundler_ruby_lib
diff --git a/spec/bundler/environment_preserver_spec.rb b/spec/bundler/environment_preserver_spec.rb
index 937211d9ac..530ca6f835 100644
--- a/spec/bundler/environment_preserver_spec.rb
+++ b/spec/bundler/environment_preserver_spec.rb
@@ -28,7 +28,7 @@ RSpec.describe Bundler::EnvironmentPreserver do
let(:env) { { "foo" => "" } }
it "should not create backup entries" do
- expect(subject.key?("BUNDLER_ORIG_foo")).to eq(false)
+ expect(subject).not_to have_key "BUNDLER_ORIG_foo"
end
end
diff --git a/spec/runtime/with_clean_env_spec.rb b/spec/runtime/with_clean_env_spec.rb
index 4903891e4b..b05e894484 100644
--- a/spec/runtime/with_clean_env_spec.rb
+++ b/spec/runtime/with_clean_env_spec.rb
@@ -45,6 +45,14 @@ RSpec.describe "Bundler.with_env helpers" do
0 true
EOS
end
+
+ it "removes variables that bundler added" do
+ system_gems :bundler
+ original = ruby!('puts ENV.to_a.map {|e| e.join("=") }.sort.join("\n")')
+ code = 'puts Bundler.original_env.to_a.map {|e| e.join("=") }.sort.join("\n")'
+ bundle!("exec ruby -e #{code.inspect}", :system_bundler => true)
+ expect(out).to eq original
+ end
end
describe "Bundler.clean_env" do
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 0a6b696fe7..f9285463b5 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -96,7 +96,7 @@ RSpec.configure do |config|
config.filter_run_when_matching :focus unless ENV["CI"]
original_wd = Dir.pwd
- original_env = ENV.to_hash
+ original_env = ENV.to_hash.delete_if {|k, _v| k.start_with?(Bundler::EnvironmentPreserver::BUNDLER_PREFIX) }
config.expect_with :rspec do |c|
c.syntax = :expect