diff options
author | Homu <homu@barosl.com> | 2016-01-10 16:14:54 +0900 |
---|---|---|
committer | Homu <homu@barosl.com> | 2016-01-10 16:14:54 +0900 |
commit | 9cb0cfd8e9437b21729e98e64f37080043ed7b8b (patch) | |
tree | f0c1955391c7de4ae480459d0c9f518d7b159954 | |
parent | 449dc3c539bfebd69c49b8a067463e834c55d8bb (diff) | |
parent | 91b9df7020a9214219169ce2ccf77c1fbd540845 (diff) | |
download | bundler-9cb0cfd8e9437b21729e98e64f37080043ed7b8b.tar.gz |
Auto merge of #4208 - RochesterinNYC:finish-adding-unit-tests-for-shared-helpers, r=segiddins
Finish adding unit tests for `Bundler::SharedHelpers` module
If this PR and #4207 are accepted/merged, then the `Bundler::SharedHelpers` module will have all its public methods unit tested. The only thing that's missing coverage in the spec file for `Bundler::SharedHelpers` is `clean_load_path`, which is used in the `Bundler::Runtime` class. This'll be hopefully addressed/covered as I look more into increasing reasonable unit test coverage to near 100%.
-rw-r--r-- | spec/bundler/shared_helpers_spec.rb | 129 | ||||
-rw-r--r-- | spec/spec_helper.rb | 14 |
2 files changed, 138 insertions, 5 deletions
diff --git a/spec/bundler/shared_helpers_spec.rb b/spec/bundler/shared_helpers_spec.rb index 930dd6f17c..a72fc34a74 100644 --- a/spec/bundler/shared_helpers_spec.rb +++ b/spec/bundler/shared_helpers_spec.rb @@ -2,6 +2,13 @@ require "spec_helper" require "bundler/shared_helpers" describe Bundler::SharedHelpers do + before do + ext_lock_double = double(:ext_lock) + allow(Bundler.rubygems).to receive(:ext_lock).and_return(ext_lock_double) + allow(ext_lock_double).to receive(:synchronize) do |&block| + block.call + end + end subject { Bundler::SharedHelpers } describe "#default_gemfile" do before do @@ -72,6 +79,107 @@ describe Bundler::SharedHelpers do end end end + describe "#in_bundle?" do + it "calls the find_gemfile method" do + expect(subject).to receive(:find_gemfile) + subject.in_bundle? + end + shared_examples_for "correctly determines whether to return a Gemfile path" do + context "currently in directory with a Gemfile" do + before do + File.new("Gemfile", "w") + end + it "returns path of the bundle gemfile" do + expect(subject.in_bundle?).to eq("#{bundled_app}/Gemfile") + end + end + context "currently in directory without a Gemfile" do + it "returns nil" do + expect(subject.in_bundle?).to eq(nil) + end + end + end + context "ENV['BUNDLE_GEMFILE'] set" do + before do + ENV["BUNDLE_GEMFILE"] = "/path/Gemfile" + end + it "returns ENV['BUNDLE_GEMFILE']" do + expect(subject.in_bundle?).to eq("/path/Gemfile") + end + end + context "ENV['BUNDLE_GEMFILE'] not set" do + before do + ENV["BUNDLE_GEMFILE"] = nil + end + it_behaves_like "correctly determines whether to return a Gemfile path" + end + context "ENV['BUNDLE_GEMFILE'] is blank" do + before do + ENV["BUNDLE_GEMFILE"] = "" + end + it_behaves_like "correctly determines whether to return a Gemfile path" + end + end + describe "#chdir" do + before do + Dir.mkdir "chdir_test_dir" + end + it "executes the passed block while in the specified directory" do + op_block = proc { Dir.mkdir "nested_dir" } + subject.chdir("chdir_test_dir", &op_block) + expect(Pathname.new("chdir_test_dir/nested_dir")).to exist + end + end + describe "#pwd" do + it "returns the current absolute path" do + expect(subject.pwd).to eq(bundled_app) + end + end + describe "#with_clean_git_env" do + before do + ENV["GIT_DIR"] = "ORIGINAL_ENV_GIT_DIR" + ENV["GIT_WORK_TREE"] = "ORIGINAL_ENV_GIT_WORK_TREE" + end + it "executes the passed block" do + with_clean_git_env_block = proc do + Dir.mkdir "with_clean_git_env_test_dir" + end + subject.with_clean_git_env(&with_clean_git_env_block) + expect(Pathname.new("with_clean_git_env_test_dir")).to exist + end + it "uses a fresh git env for execution" do + with_clean_git_env_block = proc do + Dir.mkdir "git_dir_test_dir" unless ENV["GIT_DIR"].nil? + Dir.mkdir "git_work_tree_test_dir" unless ENV["GIT_WORK_TREE"].nil? + end + subject.with_clean_git_env(&with_clean_git_env_block) + expect(Pathname.new("git_dir_test_dir")).to_not exist + expect(Pathname.new("git_work_tree_test_dir")).to_not exist + end + context "passed block does not throw errors" do + it "restores the git env after" do + with_clean_git_env_block = proc do + ENV["GIT_DIR"] = "NEW_ENV_GIT_DIR" + ENV["GIT_WORK_TREE"] = "NEW_ENV_GIT_WORK_TREE" + end + subject.with_clean_git_env(&with_clean_git_env_block) + expect(ENV["GIT_DIR"]).to eq("ORIGINAL_ENV_GIT_DIR") + expect(ENV["GIT_WORK_TREE"]).to eq("ORIGINAL_ENV_GIT_WORK_TREE") + end + end + context "passed block throws errors" do + it "restores the git env after" do + with_clean_git_env_block = proc do + ENV["GIT_DIR"] = "NEW_ENV_GIT_DIR" + ENV["GIT_WORK_TREE"] = "NEW_ENV_GIT_WORK_TREE" + raise RuntimeError.new + end + expect { subject.with_clean_git_env(&with_clean_git_env_block) }.to raise_error(RuntimeError) + expect(ENV["GIT_DIR"]).to eq("ORIGINAL_ENV_GIT_DIR") + expect(ENV["GIT_WORK_TREE"]).to eq("ORIGINAL_ENV_GIT_WORK_TREE") + end + end + end describe "#set_bundle_environment" do shared_examples_for "ENV['PATH'] gets set correctly" do before do @@ -170,6 +278,27 @@ describe Bundler::SharedHelpers do end end end + describe "#filesystem_access" do + context "system has proper permission access" do + it "performs the operation in the passed block" do + file_op_block = proc {|path| FileUtils.mkdir_p(path) } + subject.filesystem_access("./test_dir", &file_op_block) + expect(Pathname.new("test_dir")).to exist + end + end + context "system throws Errno::EACESS" do + it "raises a PermissionError" do + file_op_block = proc {|_path| raise Errno::EACCES } + expect { subject.filesystem_access("/path", &file_op_block) }.to raise_error(Bundler::PermissionError) + end + end + context "system throws Errno::EAGAIN" do + it "raises a TemporaryResourceError" do + file_op_block = proc {|_path| raise Errno::EAGAIN } + expect { subject.filesystem_access("/path", &file_op_block) }.to raise_error(Bundler::TemporaryResourceError) + 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 167af8f9ed..8150b0bbce 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -74,11 +74,13 @@ RSpec.configure do |config| config.filter_run :focused => true unless ENV["CI"] config.run_all_when_everything_filtered = true - original_wd = Dir.pwd - original_path = ENV["PATH"] - original_gem_home = ENV["GEM_HOME"] - original_ruby_opt = ENV["RUBYOPT"] - original_ruby_lib = ENV["RUBYLIB"] + original_wd = Dir.pwd + original_path = ENV["PATH"] + original_gem_home = ENV["GEM_HOME"] + original_ruby_opt = ENV["RUBYOPT"] + original_ruby_lib = ENV["RUBYLIB"] + original_git_dir = ENV["GIT_DIR"] + original_git_work_tree = ENV["GIT_WORK_TREE"] def pending_jruby_shebang_fix pending "JRuby executables do not have a proper shebang" if RUBY_PLATFORM == "java" @@ -108,6 +110,8 @@ RSpec.configure do |config| ENV["GEM_PATH"] = original_gem_home ENV["RUBYOPT"] = original_ruby_opt ENV["RUBYLIB"] = original_ruby_lib + ENV["GIT_DIR"] = original_git_dir + ENV["GIT_WORK_TREE"] = original_git_work_tree ENV["BUNDLE_PATH"] = nil ENV["BUNDLE_GEMFILE"] = nil ENV["BUNDLE_FROZEN"] = nil |