From 0a1c1cac447aba9eaf101823de57bb4d1b9d73f4 Mon Sep 17 00:00:00 2001 From: James Wen Date: Fri, 8 Jan 2016 21:28:27 -0500 Subject: Separate concerns of `Bundler::SharedHelpers#set_bundle_environment` - Splits the implementation details/sections of `Bundler::SharedHelpers#set_bundle_environment` into three more modular, singularly responsible private methods: `set_path`, `set_rubyopt`, `set_rubylib` - Adds unit tests for `set_bundle_environment` that also contain coverage for the new methods - Add ENV['RUBYOPT'] and ENV['RUBYLIB'] to after-spec resets --- lib/bundler/shared_helpers.rb | 39 +++++++++------ spec/bundler/shared_helpers_spec.rb | 98 +++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 4 ++ 3 files changed, 125 insertions(+), 16 deletions(-) diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index d0c3f430aa..48038ac78b 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -74,22 +74,9 @@ module Bundler end def set_bundle_environment - # Set PATH - paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR) - paths.unshift "#{Bundler.bundle_path}/bin" - ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR) - - # Set RUBYOPT - rubyopt = [ENV["RUBYOPT"]].compact - if rubyopt.empty? || rubyopt.first !~ %r{-rbundler/setup} - rubyopt.unshift %(-rbundler/setup) - ENV["RUBYOPT"] = rubyopt.join(" ") - end - - # Set RUBYLIB - rubylib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR) - rubylib.unshift File.expand_path("../..", __FILE__) - ENV["RUBYLIB"] = rubylib.uniq.join(File::PATH_SEPARATOR) + set_path + set_rubyopt + set_rubylib end # Rescues permissions errors raised by file system operations @@ -167,6 +154,26 @@ module Bundler end 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) + end + + def set_rubyopt + rubyopt = [ENV["RUBYOPT"]].compact + if rubyopt.empty? || rubyopt.first !~ %r{-rbundler/setup} + rubyopt.unshift %(-rbundler/setup) + ENV["RUBYOPT"] = rubyopt.join(" ") + end + end + + def set_rubylib + rubylib = (ENV["RUBYLIB"] || "").split(File::PATH_SEPARATOR) + rubylib.unshift File.expand_path("../..", __FILE__) + ENV["RUBYLIB"] = rubylib.uniq.join(File::PATH_SEPARATOR) + end + def clean_load_path # handle 1.9 where system gems are always on the load path if defined?(::Gem) diff --git a/spec/bundler/shared_helpers_spec.rb b/spec/bundler/shared_helpers_spec.rb index 7384b085ec..930dd6f17c 100644 --- a/spec/bundler/shared_helpers_spec.rb +++ b/spec/bundler/shared_helpers_spec.rb @@ -72,6 +72,104 @@ describe Bundler::SharedHelpers do end end end + describe "#set_bundle_environment" do + shared_examples_for "ENV['PATH'] gets set correctly" do + before do + Dir.mkdir ".bundle" + end + it "ensures bundle bin path is in ENV['PATH']" do + subject.set_bundle_environment + paths = (ENV["PATH"]).split(File::PATH_SEPARATOR) + expect(paths.include? "#{Bundler.bundle_path}/bin").to eq(true) + end + end + shared_examples_for "ENV['RUBYOPT'] gets set correctly" do + it "ensures -rbundler/setup is at the beginning of ENV['RUBYOPT']" do + subject.set_bundle_environment + expect(ENV["RUBYOPT"].split(" ").first.include? "-rbundler/setup").to eq(true) + end + end + shared_examples_for "ENV['RUBYLIB'] gets set correctly" do + before do + @ruby_lib_path = "stubbed_ruby_lib_dir" + allow(File).to receive(:expand_path).and_return(@ruby_lib_path) + end + it "ensures bundler's ruby version lib path is in ENV['RUBYLIB']" do + subject.set_bundle_environment + paths = (ENV["RUBYLIB"]).split(File::PATH_SEPARATOR) + expect(paths.include? @ruby_lib_path).to eq(true) + end + end + it "calls the appropriate set methods" do + expect(subject).to receive(:set_path) + expect(subject).to receive(:set_rubyopt) + expect(subject).to receive(:set_rubylib) + subject.set_bundle_environment + end + context "ENV['PATH'] does not exist" do + before { ENV.delete("PATH") } + it_behaves_like "ENV['PATH'] gets set correctly" + end + context "ENV['PATH'] is empty" do + before { ENV["PATH"] = "" } + it_behaves_like "ENV['PATH'] gets set correctly" + end + context "ENV['PATH'] exists" do + before { ENV["PATH"] = "/some_path/bin" } + it_behaves_like "ENV['PATH'] gets set correctly" + end + context "ENV['PATH'] already contains the bundle bin path" do + before do + @bundle_path = "#{Bundler.bundle_path}/bin" + ENV["PATH"] = @bundle_path + end + it_behaves_like "ENV['PATH'] gets set correctly" + it "ENV['PATH'] should only contain one instance of bundle bin path" do + subject.set_bundle_environment + paths = (ENV["PATH"]).split(File::PATH_SEPARATOR) + expect(paths.count(@bundle_path)).to eq(1) + end + end + context "ENV['RUBYOPT'] does not exist" do + before { ENV.delete("RUBYOPT") } + it_behaves_like "ENV['RUBYOPT'] gets set correctly" + end + context "ENV['RUBYOPT'] exists without -rbundler/setup" do + before { ENV["RUBYOPT"] = "-I/some_app_path/lib" } + it_behaves_like "ENV['RUBYOPT'] gets set correctly" + end + context "ENV['RUBYOPT'] exists and contains -rbundler/setup" do + before do + ENV["RUBYOPT"] = "-rbundler/setup" + end + it_behaves_like "ENV['RUBYOPT'] gets set correctly" + end + context "ENV['RUBYLIB'] does not exist" do + before { ENV.delete("RUBYLIB") } + it_behaves_like "ENV['RUBYLIB'] gets set correctly" + end + context "ENV['RUBYLIB'] is empty" do + before { ENV["PATH"] = "" } + it_behaves_like "ENV['RUBYLIB'] gets set correctly" + end + context "ENV['RUBYLIB'] exists" do + before { ENV["PATH"] = "/some_path/bin" } + it_behaves_like "ENV['RUBYLIB'] gets set correctly" + end + context "ENV['RUBYLIB'] already contains the bundler's ruby version lib path" do + before do + @ruby_lib_path = "stubbed_ruby_lib_dir" + allow(File).to receive(:expand_path).and_return(@ruby_lib_path) + ENV["RUBYLIB"] = @ruby_lib_path + end + it_behaves_like "ENV['RUBYLIB'] gets set correctly" + it "ENV['RUBYLIB'] should only contain one instance of bundler's ruby version lib path" do + subject.set_bundle_environment + paths = (ENV["RUBYLIB"]).split(File::PATH_SEPARATOR) + expect(paths.count(@ruby_lib_path)).to eq(1) + end + end + end describe "#const_get_safely" do module TargetNamespace VALID_CONSTANT = 1 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ede6ffaf53..167af8f9ed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -77,6 +77,8 @@ RSpec.configure do |config| original_wd = Dir.pwd original_path = ENV["PATH"] original_gem_home = ENV["GEM_HOME"] + original_ruby_opt = ENV["RUBYOPT"] + original_ruby_lib = ENV["RUBYLIB"] def pending_jruby_shebang_fix pending "JRuby executables do not have a proper shebang" if RUBY_PLATFORM == "java" @@ -104,6 +106,8 @@ RSpec.configure do |config| ENV["PATH"] = original_path ENV["GEM_HOME"] = original_gem_home ENV["GEM_PATH"] = original_gem_home + ENV["RUBYOPT"] = original_ruby_opt + ENV["RUBYLIB"] = original_ruby_lib ENV["BUNDLE_PATH"] = nil ENV["BUNDLE_GEMFILE"] = nil ENV["BUNDLE_FROZEN"] = nil -- cgit v1.2.1