2019-04-14 02:01:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe "Bundler.with_env helpers" do
|
2020-06-03 14:47:59 -04:00
|
|
|
def bundle_exec_ruby(args, options = {})
|
2019-07-23 12:44:21 -04:00
|
|
|
build_bundler_context options
|
2020-06-03 14:45:36 -04:00
|
|
|
bundle "exec '#{Gem.ruby}' #{args}", options
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
|
2019-07-23 12:44:21 -04:00
|
|
|
def build_bundler_context(options = {})
|
2019-04-14 02:01:35 -04:00
|
|
|
bundle "config set path vendor/bundle"
|
|
|
|
gemfile ""
|
2019-07-23 12:44:21 -04:00
|
|
|
bundle "install", options
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def run_bundler_script(env, script)
|
|
|
|
system(env, "ruby", "-I#{lib_dir}", "-rbundler", script.to_s)
|
|
|
|
end
|
|
|
|
|
2019-04-14 02:01:35 -04:00
|
|
|
describe "Bundler.original_env" do
|
|
|
|
it "should return the PATH present before bundle was activated" do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print Bundler.original_env["PATH"]
|
|
|
|
RUBY
|
2019-04-14 02:01:35 -04:00
|
|
|
path = `getconf PATH`.strip + "#{File::PATH_SEPARATOR}/foo"
|
|
|
|
with_path_as(path) do
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby(bundled_app("source.rb").to_s)
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(last_command.stdboth).to eq(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the GEM_PATH present before bundle was activated" do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print Bundler.original_env['GEM_PATH']
|
|
|
|
RUBY
|
|
|
|
gem_path = ENV["GEM_PATH"] + "#{File::PATH_SEPARATOR}/foo"
|
2019-04-14 02:01:35 -04:00
|
|
|
with_gem_path_as(gem_path) do
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby(bundled_app("source.rb").to_s)
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(last_command.stdboth).to eq(gem_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-16 06:47:24 -04:00
|
|
|
it "works with nested bundle exec invocations", :ruby_repo do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("exe.rb", <<-'RUBY')
|
2019-04-14 02:01:35 -04:00
|
|
|
count = ARGV.first.to_i
|
|
|
|
exit if count < 0
|
2020-05-08 01:19:04 -04:00
|
|
|
STDERR.puts "#{count} #{ENV["PATH"].end_with?("#{File::PATH_SEPARATOR}/foo")}"
|
2019-04-14 02:01:35 -04:00
|
|
|
if count == 2
|
2020-05-08 01:19:04 -04:00
|
|
|
ENV["PATH"] = "#{ENV["PATH"]}#{File::PATH_SEPARATOR}/foo"
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
exec(Gem.ruby, __FILE__, (count - 1).to_s)
|
2020-05-08 01:19:04 -04:00
|
|
|
RUBY
|
2019-04-14 02:01:35 -04:00
|
|
|
path = `getconf PATH`.strip + File::PATH_SEPARATOR + File.dirname(Gem.ruby)
|
|
|
|
with_path_as(path) do
|
|
|
|
build_bundler_context
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby("#{bundled_app("exe.rb")} 2")
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
2019-06-01 05:49:40 -04:00
|
|
|
expect(err).to eq <<-EOS.strip
|
2019-04-14 02:01:35 -04:00
|
|
|
2 false
|
|
|
|
1 true
|
|
|
|
0 true
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2020-05-21 08:57:47 -04:00
|
|
|
it "removes variables that bundler added", :ruby_repo do
|
2019-07-23 02:16:17 -04:00
|
|
|
# Simulate bundler has not yet been loaded
|
2019-07-23 02:14:02 -04:00
|
|
|
ENV.replace(ENV.to_hash.delete_if {|k, _v| k.start_with?(Bundler::EnvironmentPreserver::BUNDLER_PREFIX) })
|
|
|
|
|
2020-06-03 14:47:59 -04:00
|
|
|
original = ruby('puts ENV.to_a.map {|e| e.join("=") }.sort.join("\n")')
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
puts Bundler.original_env.to_a.map {|e| e.join("=") }.sort.join("\n")
|
|
|
|
RUBY
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby bundled_app("source.rb")
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(out).to eq original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for "an unbundling helper" do
|
|
|
|
it "should delete BUNDLE_PATH" do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print #{modified_env}.has_key?('BUNDLE_PATH')
|
|
|
|
RUBY
|
2019-04-14 02:01:35 -04:00
|
|
|
ENV["BUNDLE_PATH"] = "./foo"
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby bundled_app("source.rb")
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(last_command.stdboth).to include "false"
|
|
|
|
end
|
|
|
|
|
2020-10-15 00:20:25 -04:00
|
|
|
it "should remove absolute path to 'bundler/setup' from RUBYOPT even if it was present in original env" do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print #{modified_env}['RUBYOPT']
|
|
|
|
RUBY
|
2020-10-15 00:20:25 -04:00
|
|
|
setup_require = "-r#{lib_dir}/bundler/setup"
|
|
|
|
ENV["BUNDLER_ORIG_RUBYOPT"] = "-W2 #{setup_require} #{ENV["RUBYOPT"]}"
|
|
|
|
simulate_bundler_version_when_missing_prerelease_default_gem_activation do
|
|
|
|
bundle_exec_ruby bundled_app("source.rb")
|
|
|
|
end
|
|
|
|
expect(last_command.stdboth).not_to include(setup_require)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should remove relative path to 'bundler/setup' from RUBYOPT even if it was present in original env" do
|
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print #{modified_env}['RUBYOPT']
|
|
|
|
RUBY
|
|
|
|
ENV["BUNDLER_ORIG_RUBYOPT"] = "-W2 -rbundler/setup #{ENV["RUBYOPT"]}"
|
2020-05-15 08:31:12 -04:00
|
|
|
simulate_bundler_version_when_missing_prerelease_default_gem_activation do
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby bundled_app("source.rb")
|
2020-05-15 08:31:12 -04:00
|
|
|
end
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(last_command.stdboth).not_to include("-rbundler/setup")
|
|
|
|
end
|
|
|
|
|
2020-05-16 06:47:24 -04:00
|
|
|
it "should restore RUBYLIB", :ruby_repo do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print #{modified_env}['RUBYLIB']
|
|
|
|
RUBY
|
2019-11-11 03:57:45 -05:00
|
|
|
ENV["RUBYLIB"] = lib_dir.to_s + File::PATH_SEPARATOR + "/foo"
|
|
|
|
ENV["BUNDLER_ORIG_RUBYLIB"] = lib_dir.to_s + File::PATH_SEPARATOR + "/foo-original"
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby bundled_app("source.rb")
|
2019-07-23 02:15:15 -04:00
|
|
|
expect(last_command.stdboth).to include("/foo-original")
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should restore the original MANPATH" do
|
2020-05-08 01:19:04 -04:00
|
|
|
create_file("source.rb", <<-RUBY)
|
|
|
|
print #{modified_env}['MANPATH']
|
|
|
|
RUBY
|
2019-04-14 02:01:35 -04:00
|
|
|
ENV["MANPATH"] = "/foo"
|
|
|
|
ENV["BUNDLER_ORIG_MANPATH"] = "/foo-original"
|
2020-06-03 14:47:59 -04:00
|
|
|
bundle_exec_ruby bundled_app("source.rb")
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(last_command.stdboth).to include("/foo-original")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.unbundled_env" do
|
|
|
|
let(:modified_env) { "Bundler.unbundled_env" }
|
|
|
|
|
|
|
|
it_behaves_like "an unbundling helper"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.clean_env", :bundler => 2 do
|
|
|
|
let(:modified_env) { "Bundler.clean_env" }
|
|
|
|
|
|
|
|
it_behaves_like "an unbundling helper"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.with_original_env" do
|
|
|
|
it "should set ENV to original_env in the block" do
|
|
|
|
expected = Bundler.original_env
|
2020-05-08 01:19:04 -04:00
|
|
|
actual = Bundler.with_original_env { Bundler::EnvironmentPreserver.env_to_hash(ENV) }
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(actual).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should restore the environment after execution" do
|
|
|
|
Bundler.with_original_env do
|
|
|
|
ENV["FOO"] = "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ENV).not_to have_key("FOO")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.with_clean_env", :bundler => 2 do
|
|
|
|
it "should set ENV to unbundled_env in the block" do
|
|
|
|
expected = Bundler.unbundled_env
|
2019-11-11 03:57:45 -05:00
|
|
|
|
|
|
|
actual = Bundler.ui.silence do
|
2020-05-08 01:19:04 -04:00
|
|
|
Bundler.with_clean_env { Bundler::EnvironmentPreserver.env_to_hash(ENV) }
|
2019-11-11 03:57:45 -05:00
|
|
|
end
|
|
|
|
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(actual).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should restore the environment after execution" do
|
2019-11-11 03:57:45 -05:00
|
|
|
Bundler.ui.silence do
|
|
|
|
Bundler.with_clean_env { ENV["FOO"] = "hello" }
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(ENV).not_to have_key("FOO")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.with_unbundled_env" do
|
|
|
|
it "should set ENV to unbundled_env in the block" do
|
|
|
|
expected = Bundler.unbundled_env
|
2020-05-08 01:19:04 -04:00
|
|
|
actual = Bundler.with_unbundled_env { Bundler::EnvironmentPreserver.env_to_hash(ENV) }
|
2019-04-14 02:01:35 -04:00
|
|
|
expect(actual).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should restore the environment after execution" do
|
|
|
|
Bundler.with_unbundled_env do
|
|
|
|
ENV["FOO"] = "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ENV).not_to have_key("FOO")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.original_system" do
|
2020-05-08 01:19:04 -04:00
|
|
|
before do
|
|
|
|
create_file("source.rb", <<-'RUBY')
|
|
|
|
Bundler.original_system("ruby", "-e", "exit(42) if ENV['BUNDLE_FOO'] == 'bar'")
|
2019-06-01 05:49:40 -04:00
|
|
|
|
|
|
|
exit $?.exitstatus
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2019-04-14 02:01:35 -04:00
|
|
|
it "runs system inside with_original_env" do
|
2020-05-08 01:19:04 -04:00
|
|
|
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
|
2019-06-01 05:49:40 -04:00
|
|
|
expect($?.exitstatus).to eq(42)
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.clean_system", :bundler => 2 do
|
2020-05-08 01:19:04 -04:00
|
|
|
before do
|
|
|
|
create_file("source.rb", <<-'RUBY')
|
|
|
|
Bundler.ui.silence { Bundler.clean_system("ruby", "-e", "exit(42) unless ENV['BUNDLE_FOO'] == 'bar'") }
|
2019-06-01 05:49:40 -04:00
|
|
|
|
|
|
|
exit $?.exitstatus
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2020-05-15 08:31:12 -04:00
|
|
|
it "runs system inside with_clean_env" do
|
2020-05-08 01:19:04 -04:00
|
|
|
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
|
2019-06-01 05:49:40 -04:00
|
|
|
expect($?.exitstatus).to eq(42)
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.unbundled_system" do
|
2020-05-08 01:19:04 -04:00
|
|
|
before do
|
|
|
|
create_file("source.rb", <<-'RUBY')
|
|
|
|
Bundler.unbundled_system("ruby", "-e", "exit(42) unless ENV['BUNDLE_FOO'] == 'bar'")
|
2019-06-01 05:49:40 -04:00
|
|
|
|
|
|
|
exit $?.exitstatus
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2020-05-15 08:31:12 -04:00
|
|
|
it "runs system inside with_unbundled_env" do
|
2020-05-08 01:19:04 -04:00
|
|
|
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
|
2019-06-01 05:49:40 -04:00
|
|
|
expect($?.exitstatus).to eq(42)
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.original_exec" do
|
2020-05-08 01:19:04 -04:00
|
|
|
before do
|
|
|
|
create_file("source.rb", <<-'RUBY')
|
2019-04-14 02:01:35 -04:00
|
|
|
Process.fork do
|
|
|
|
exit Bundler.original_exec(%(test "\$BUNDLE_FOO" = "bar"))
|
|
|
|
end
|
|
|
|
|
|
|
|
_, status = Process.wait2
|
|
|
|
|
|
|
|
exit(status.exitstatus)
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs exec inside with_original_env" do
|
2019-07-23 07:56:01 -04:00
|
|
|
skip "Fork not implemented" if Gem.win_platform?
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
|
2019-04-14 02:01:35 -04:00
|
|
|
expect($?.exitstatus).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.clean_exec", :bundler => 2 do
|
2020-05-08 01:19:04 -04:00
|
|
|
before do
|
|
|
|
create_file("source.rb", <<-'RUBY')
|
2019-04-14 02:01:35 -04:00
|
|
|
Process.fork do
|
2019-11-11 03:57:45 -05:00
|
|
|
exit Bundler.ui.silence { Bundler.clean_exec(%(test "\$BUNDLE_FOO" = "bar")) }
|
2019-04-14 02:01:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
_, status = Process.wait2
|
|
|
|
|
|
|
|
exit(status.exitstatus)
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs exec inside with_clean_env" do
|
2019-07-23 07:56:01 -04:00
|
|
|
skip "Fork not implemented" if Gem.win_platform?
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
|
2019-04-14 02:01:35 -04:00
|
|
|
expect($?.exitstatus).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Bundler.unbundled_exec" do
|
2020-05-08 01:19:04 -04:00
|
|
|
before do
|
|
|
|
create_file("source.rb", <<-'RUBY')
|
2019-04-14 02:01:35 -04:00
|
|
|
Process.fork do
|
|
|
|
exit Bundler.unbundled_exec(%(test "\$BUNDLE_FOO" = "bar"))
|
|
|
|
end
|
|
|
|
|
|
|
|
_, status = Process.wait2
|
|
|
|
|
|
|
|
exit(status.exitstatus)
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs exec inside with_clean_env" do
|
2019-07-23 07:56:01 -04:00
|
|
|
skip "Fork not implemented" if Gem.win_platform?
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
|
2019-04-14 02:01:35 -04:00
|
|
|
expect($?.exitstatus).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|