1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/bundler/runtime/with_unbundled_env_spec.rb
David Rodríguez 5a384e2c08 Fix some bundler specs (#2380)
* These seem to consistenly pass already

* Show actual command when running `make test-bundler`

Current the setup command that installs the necessary gems for testing
bundler was printed, but not the actual command that runs the tests.
That was a bit confusing.

* Borrow trick from setproctitle specs

* A title that long doesn't get set sometimes

No idea why, but the test doesn't need that the title is that long.

* Fix most gem helper spec ruby-core failures

* Fix the rest of the gem helper failures

* Fix version spec by improving the assertion

* Remove unnecessary `BUNDLE_RUBY` environment var

We can use `RUBY` when necessary, and `BUNDLE_RUBY` is not a good name
because bundler considers `BUNDLE_*` variables as settings.

* Rename `BUNDLE_GEM` to `GEM_COMMAND`

This is more descriptive I think, and also friendlier for bundler
because `BUNDLE_` env variables are interpreted by bundler as settings,
and this is not a bundler setting.

This fixes one bundler spec failure in config specs against ruby-core.

* Fix quality spec when run in core

Use the proper path helper.

* Fix dummy lib builder to never load default gems

If a dummy library is named as a default gem, when requiring the library
from its executable, the default gem would be loaded when running from
core, because in core all default gems share path with bundler, and thus
they are always in the $LOAD_PATH. We fix the issue by loading lib
relatively inside dummy lib executables.

* More exact assertions

Sometimes I have the problem that I do some "print debugging" inside
specs, and suddently the spec passes. This happens when the assertion is
too relaxed, and the things I print make it match, specially when they
are simple strings like "1.0" than can be easily be part of gem paths
that I print for debugging.

I fix this by making a more exact assertion.

* Detect the correct shebang when ENV["RUBY"] is set

* Relax assertion

So that the spec passes even if another paths containing "ext" are in
the load path. This works to fix a ruby-core issue, but it's a better
assertion in general. We just want to know that the extension path was
added.

* Use folder structure independent path helper

It should fix this spec for ruby-core.

* Fix the last failing spec on ruby-core

* Skip `bundle open <default_gem>` spec when no default gems
2019-08-20 09:46:31 +09:00

272 lines
7.8 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "Bundler.with_env helpers" do
def bundle_exec_ruby!(code, options = {})
build_bundler_context options
bundle! "exec '#{Gem.ruby}' -e #{code}", options
end
def build_bundler_context(options = {})
bundle "config set path vendor/bundle"
gemfile ""
bundle "install", options
end
describe "Bundler.original_env" do
it "should return the PATH present before bundle was activated" do
code = "print Bundler.original_env['PATH']"
path = `getconf PATH`.strip + "#{File::PATH_SEPARATOR}/foo"
with_path_as(path) do
bundle_exec_ruby!(code.dump)
expect(last_command.stdboth).to eq(path)
end
end
it "should return the GEM_PATH present before bundle was activated" do
code = "print Bundler.original_env['GEM_PATH']"
gem_path = ENV["GEM_PATH"] + ":/foo"
with_gem_path_as(gem_path) do
bundle_exec_ruby!(code.dump)
expect(last_command.stdboth).to eq(gem_path)
end
end
it "works with nested bundle exec invocations" do
create_file("exe.rb", <<-'RB')
count = ARGV.first.to_i
exit if count < 0
STDERR.puts "#{count} #{ENV["PATH"].end_with?(":/foo")}"
if count == 2
ENV["PATH"] = "#{ENV["PATH"]}:/foo"
end
exec(Gem.ruby, __FILE__, (count - 1).to_s)
RB
path = `getconf PATH`.strip + File::PATH_SEPARATOR + File.dirname(Gem.ruby)
with_path_as(path) do
build_bundler_context
bundle! "exec '#{Gem.ruby}' #{bundled_app("exe.rb")} 2"
end
expect(err).to eq <<-EOS.strip
2 false
1 true
0 true
EOS
end
it "removes variables that bundler added", :ruby_repo do
# Simulate bundler has not yet been loaded
ENV.replace(ENV.to_hash.delete_if {|k, _v| k.start_with?(Bundler::EnvironmentPreserver::BUNDLER_PREFIX) })
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! code.dump
expect(out).to eq original
end
end
shared_examples_for "an unbundling helper" do
it "should delete BUNDLE_PATH" do
code = "print #{modified_env}.has_key?('BUNDLE_PATH')"
ENV["BUNDLE_PATH"] = "./foo"
bundle_exec_ruby! code.dump
expect(last_command.stdboth).to include "false"
end
it "should remove '-rbundler/setup' from RUBYOPT" do
code = "print #{modified_env}['RUBYOPT']"
ENV["RUBYOPT"] = "-W2 -rbundler/setup #{ENV["RUBYOPT"]}"
bundle_exec_ruby! code.dump, :env => { "BUNDLER_SPEC_DISABLE_DEFAULT_BUNDLER_GEM" => "true" }
expect(last_command.stdboth).not_to include("-rbundler/setup")
end
it "should restore RUBYLIB", :ruby_repo do
code = "print #{modified_env}['RUBYLIB']"
ENV["RUBYLIB"] = root.join("lib").to_s + File::PATH_SEPARATOR + "/foo"
ENV["BUNDLER_ORIG_RUBYLIB"] = root.join("lib").to_s + File::PATH_SEPARATOR + "/foo-original"
bundle_exec_ruby! code.dump
expect(last_command.stdboth).to include("/foo-original")
end
it "should restore the original MANPATH" do
code = "print #{modified_env}['MANPATH']"
ENV["MANPATH"] = "/foo"
ENV["BUNDLER_ORIG_MANPATH"] = "/foo-original"
bundle_exec_ruby! code.dump
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
actual = Bundler.with_original_env { ENV.to_hash }
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
actual = Bundler.with_clean_env { ENV.to_hash }
expect(actual).to eq(expected)
end
it "should restore the environment after execution" do
Bundler.with_clean_env do
ENV["FOO"] = "hello"
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
actual = Bundler.with_unbundled_env { ENV.to_hash }
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
let(:code) do
<<~RUBY
Bundler.original_system(%([ "\$BUNDLE_FOO" = "bar" ] && exit 42))
exit $?.exitstatus
RUBY
end
it "runs system inside with_original_env" do
lib = File.expand_path("../../lib", __dir__)
system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'")
expect($?.exitstatus).to eq(42)
end
end
describe "Bundler.clean_system", :bundler => 2 do
let(:code) do
<<~RUBY
Bundler.clean_system(%([ "\$BUNDLE_FOO" = "bar" ] || exit 42))
exit $?.exitstatus
RUBY
end
it "runs system inside with_clean_env" do
lib = File.expand_path("../../lib", __dir__)
system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'")
expect($?.exitstatus).to eq(42)
end
end
describe "Bundler.unbundled_system" do
let(:code) do
<<~RUBY
Bundler.unbundled_system(%([ "\$BUNDLE_FOO" = "bar" ] || exit 42))
exit $?.exitstatus
RUBY
end
it "runs system inside with_unbundled_env" do
lib = File.expand_path("../../lib", __dir__)
system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'")
expect($?.exitstatus).to eq(42)
end
end
describe "Bundler.original_exec" do
let(:code) do
<<~RUBY
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
skip "Fork not implemented" if Gem.win_platform?
lib = File.expand_path("../../lib", __dir__)
system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'")
expect($?.exitstatus).to eq(0)
end
end
describe "Bundler.clean_exec", :bundler => 2 do
let(:code) do
<<~RUBY
Process.fork do
exit Bundler.clean_exec(%(test "\$BUNDLE_FOO" = "bar"))
end
_, status = Process.wait2
exit(status.exitstatus)
RUBY
end
it "runs exec inside with_clean_env" do
skip "Fork not implemented" if Gem.win_platform?
lib = File.expand_path("../../lib", __dir__)
system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'")
expect($?.exitstatus).to eq(1)
end
end
describe "Bundler.unbundled_exec" do
let(:code) do
<<~RUBY
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
skip "Fork not implemented" if Gem.win_platform?
lib = File.expand_path("../../lib", __dir__)
system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'")
expect($?.exitstatus).to eq(1)
end
end
end