diff options
author | David RodrÃguez <deivid.rodriguez@riseup.net> | 2020-01-10 09:56:42 +0100 |
---|---|---|
committer | David RodrÃguez <deivid.rodriguez@riseup.net> | 2020-01-12 20:46:33 +0100 |
commit | 7dc38a75680f991bf32320cca850c32d1dcd90ca (patch) | |
tree | e532f1f4eb42a0b9f438ef40380a1ab9ae810975 /spec/bundler | |
parent | 55630c23f4e0683e820116af917888cf23569020 (diff) | |
download | bundler-7dc38a75680f991bf32320cca850c32d1dcd90ca.tar.gz |
Remove global directory switching from specstests/less_flakyness
`Dir.chdir` is not thread safe, so it makes our parallel specs flaky.
Instead, use the following alternatives:
* Use `:chdir` parameter to `Open3` methods for specs that shell out.
* Stub `find_gemfile` or other relevant helpers for unit tests.
Diffstat (limited to 'spec/bundler')
-rw-r--r-- | spec/bundler/bundler_spec.rb | 2 | ||||
-rw-r--r-- | spec/bundler/definition_spec.rb | 4 | ||||
-rw-r--r-- | spec/bundler/dsl_spec.rb | 6 | ||||
-rw-r--r-- | spec/bundler/env_spec.rb | 16 | ||||
-rw-r--r-- | spec/bundler/gem_helper_spec.rb | 38 | ||||
-rw-r--r-- | spec/bundler/plugin/index_spec.rb | 11 | ||||
-rw-r--r-- | spec/bundler/plugin_spec.rb | 7 | ||||
-rw-r--r-- | spec/bundler/settings_spec.rb | 2 | ||||
-rw-r--r-- | spec/bundler/shared_helpers_spec.rb | 39 |
9 files changed, 70 insertions, 55 deletions
diff --git a/spec/bundler/bundler_spec.rb b/spec/bundler/bundler_spec.rb index 27477465bb..7237f644e2 100644 --- a/spec/bundler/bundler_spec.rb +++ b/spec/bundler/bundler_spec.rb @@ -210,6 +210,8 @@ EOF gem "rack" G + allow(Bundler).to receive(:root).and_return(bundled_app) + Bundler.mkdir_p(bundled_app.join("foo", "bar")) expect(bundled_app.join("foo", "bar")).to exist end diff --git a/spec/bundler/definition_spec.rb b/spec/bundler/definition_spec.rb index ad6d4e433e..d0ebb37933 100644 --- a/spec/bundler/definition_spec.rb +++ b/spec/bundler/definition_spec.rb @@ -210,6 +210,8 @@ RSpec.describe Bundler::Definition do source "#{file_uri_for(gem_repo1)}" gem "foo" G + + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) end it "should get a locked specs list when updating all" do @@ -267,6 +269,8 @@ RSpec.describe Bundler::Definition do BUNDLED WITH 1.13.0 L + + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) end it "should not eagerly unlock shared dependency with bundle install conservative updating behavior" do diff --git a/spec/bundler/dsl_spec.rb b/spec/bundler/dsl_spec.rb index 56a5bfdb48..319472d4b0 100644 --- a/spec/bundler/dsl_spec.rb +++ b/spec/bundler/dsl_spec.rb @@ -72,7 +72,7 @@ RSpec.describe Bundler::Dsl do describe "#method_missing" do it "raises an error for unknown DSL methods" do - expect(Bundler).to receive(:read_file).with(bundled_app("Gemfile").to_s). + expect(Bundler).to receive(:read_file).with(root.join("Gemfile").to_s). and_return("unknown") error_msg = "There was an error parsing `Gemfile`: Undefined local variable or method `unknown' for Gemfile. Bundler cannot continue." @@ -83,13 +83,13 @@ RSpec.describe Bundler::Dsl do describe "#eval_gemfile" do it "handles syntax errors with a useful message" do - expect(Bundler).to receive(:read_file).with(bundled_app("Gemfile").to_s).and_return("}") + expect(Bundler).to receive(:read_file).with(root.join("Gemfile").to_s).and_return("}") expect { subject.eval_gemfile("Gemfile") }. to raise_error(Bundler::GemfileError, /There was an error parsing `Gemfile`: (syntax error, unexpected tSTRING_DEND|(compile error - )?syntax error, unexpected '\}'). Bundler cannot continue./) end it "distinguishes syntax errors from evaluation errors" do - expect(Bundler).to receive(:read_file).with(bundled_app("Gemfile").to_s).and_return( + expect(Bundler).to receive(:read_file).with(root.join("Gemfile").to_s).and_return( "ruby '2.1.5', :engine => 'ruby', :engine_version => '1.2.4'" ) expect { subject.eval_gemfile("Gemfile") }. diff --git a/spec/bundler/env_spec.rb b/spec/bundler/env_spec.rb index aefedebf13..8ff5ddada6 100644 --- a/spec/bundler/env_spec.rb +++ b/spec/bundler/env_spec.rb @@ -86,6 +86,8 @@ RSpec.describe Bundler::Env do BUNDLED WITH 1.10.0 L + + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) end let(:output) { described_class.report(:print_gemfile => true) } @@ -125,6 +127,8 @@ RSpec.describe Bundler::Env do File.open(bundled_app.join("foo.gemspec"), "wb") do |f| f.write(gemspec) end + + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) end it "prints the gemspec" do @@ -137,13 +141,15 @@ RSpec.describe Bundler::Env do context "when eval_gemfile is used" do it "prints all gemfiles" do - create_file "other/Gemfile-other", "gem 'rack'" - create_file "other/Gemfile", "eval_gemfile 'Gemfile-other'" - create_file "Gemfile-alt", <<-G + create_file bundled_app("other/Gemfile-other"), "gem 'rack'" + create_file bundled_app("other/Gemfile"), "eval_gemfile 'Gemfile-other'" + create_file bundled_app("Gemfile-alt"), <<-G source "#{file_uri_for(gem_repo1)}" eval_gemfile "other/Gemfile" G - gemfile "eval_gemfile #{File.expand_path("Gemfile-alt").dump}" + gemfile "eval_gemfile #{bundled_app("Gemfile-alt").to_s.dump}" + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) + allow(Bundler::SharedHelpers).to receive(:pwd).and_return(bundled_app) output = described_class.report(:print_gemspecs => true) expect(output).to include(strip_whitespace(<<-ENV)) @@ -152,7 +158,7 @@ RSpec.describe Bundler::Env do ### Gemfile ```ruby - eval_gemfile #{File.expand_path("Gemfile-alt").dump} + eval_gemfile #{bundled_app("Gemfile-alt").to_s.dump} ``` ### Gemfile-alt diff --git a/spec/bundler/gem_helper_spec.rb b/spec/bundler/gem_helper_spec.rb index b27c96b9a5..6af78e00be 100644 --- a/spec/bundler/gem_helper_spec.rb +++ b/spec/bundler/gem_helper_spec.rb @@ -178,13 +178,11 @@ RSpec.describe Bundler::GemHelper do end before do - Dir.chdir(app_path) do - `git init` - `git config user.email "you@example.com"` - `git config user.name "name"` - `git config commit.gpgsign false` - `git config push.default simple` - end + sys_exec("git init", :dir => app_path) + sys_exec("git config user.email \"you@example.com\"", :dir => app_path) + sys_exec("git config user.name \"name\"", :dir => app_path) + sys_exec("git config commit.gpgsign false", :dir => app_path) + sys_exec("git config push.default simple", :dir => app_path) # silence messages allow(Bundler.ui).to receive(:confirm) @@ -198,13 +196,13 @@ RSpec.describe Bundler::GemHelper do end it "when there are uncommitted files" do - Dir.chdir(app_path) { `git add .` } + sys_exec("git add .", :dir => app_path) expect { Rake.application["release"].invoke }. to raise_error("There are files that need to be committed first.") end it "when there is no git remote" do - Dir.chdir(app_path) { `git commit -a -m "initial commit"` } + sys_exec("git commit -a -m \"initial commit\"", :dir => app_path) expect { Rake.application["release"].invoke }.to raise_error(RuntimeError) end end @@ -213,10 +211,8 @@ RSpec.describe Bundler::GemHelper do let(:repo) { build_git("foo", :bare => true) } before do - Dir.chdir(app_path) do - sys_exec("git remote add origin #{file_uri_for(repo.path)}") - sys_exec('git commit -a -m "initial commit"') - end + sys_exec("git remote add origin #{file_uri_for(repo.path)}", :dir => app_path) + sys_exec('git commit -a -m "initial commit"', :dir => app_path) end context "on releasing" do @@ -225,7 +221,7 @@ RSpec.describe Bundler::GemHelper do mock_confirm_message "Tagged v#{app_version}." mock_confirm_message "Pushed git commits and tags." - Dir.chdir(app_path) { sys_exec("git push -u origin master") } + sys_exec("git push -u origin master", :dir => app_path) end it "calls rubygem_push with proper arguments" do @@ -246,9 +242,7 @@ RSpec.describe Bundler::GemHelper do mock_confirm_message "Tag v#{app_version} has already been created." expect(subject).to receive(:rubygem_push).with(app_gem_path.to_s) - Dir.chdir(app_path) do - `git tag -a -m \"Version #{app_version}\" v#{app_version}` - end + sys_exec("git tag -a -m \"Version #{app_version}\" v#{app_version}", :dir => app_path) Rake.application["release"].invoke end @@ -269,12 +263,10 @@ RSpec.describe Bundler::GemHelper do end before do - Dir.chdir(app_path) do - `git init` - `git config user.email "you@example.com"` - `git config user.name "name"` - `git config push.default simple` - end + sys_exec("git init", :dir => app_path) + sys_exec("git config user.email \"you@example.com\"", :dir => app_path) + sys_exec("git config user.name \"name\"", :dir => app_path) + sys_exec("git config push.gpgsign simple", :dir => app_path) # silence messages allow(Bundler.ui).to receive(:confirm) diff --git a/spec/bundler/plugin/index_spec.rb b/spec/bundler/plugin/index_spec.rb index e18e960fb8..925dc558ac 100644 --- a/spec/bundler/plugin/index_spec.rb +++ b/spec/bundler/plugin/index_spec.rb @@ -4,6 +4,7 @@ RSpec.describe Bundler::Plugin::Index do Index = Bundler::Plugin::Index before do + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) gemfile "" path = lib_path(plugin_name) index.register_plugin("new-plugin", path.to_s, [path.join("lib").to_s], commands, sources, hooks) @@ -117,11 +118,11 @@ RSpec.describe Bundler::Plugin::Index do describe "global index" do before do - Dir.chdir(tmp) do - Bundler::Plugin.reset! - path = lib_path("gplugin") - index.register_plugin("gplugin", path.to_s, [path.join("lib").to_s], [], ["glb_source"], []) - end + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(nil) + + Bundler::Plugin.reset! + path = lib_path("gplugin") + index.register_plugin("gplugin", path.to_s, [path.join("lib").to_s], [], ["glb_source"], []) end it "skips sources" do diff --git a/spec/bundler/plugin_spec.rb b/spec/bundler/plugin_spec.rb index cb6012da45..8c95723bcc 100644 --- a/spec/bundler/plugin_spec.rb +++ b/spec/bundler/plugin_spec.rb @@ -237,7 +237,7 @@ RSpec.describe Bundler::Plugin do describe "#root" do context "in app dir" do before do - gemfile "" + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) end it "returns plugin dir in app .bundle path" do @@ -246,8 +246,11 @@ RSpec.describe Bundler::Plugin do end context "outside app dir" do + before do + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(nil) + end + it "returns plugin dir in global bundle path" do - Dir.chdir tmp expect(subject.root).to eq(home.join(".bundle/plugin")) end end diff --git a/spec/bundler/settings_spec.rb b/spec/bundler/settings_spec.rb index b83d768477..116a038445 100644 --- a/spec/bundler/settings_spec.rb +++ b/spec/bundler/settings_spec.rb @@ -130,6 +130,8 @@ that would suck --ehhh=oh geez it looks like i might have broken bundler somehow describe "#temporary" do it "reset after used" do + allow(Bundler::SharedHelpers).to receive(:find_gemfile).and_return(bundled_app_gemfile) + Bundler.settings.set_command_option :no_install, true Bundler.settings.temporary(:no_install => false) do diff --git a/spec/bundler/shared_helpers_spec.rb b/spec/bundler/shared_helpers_spec.rb index 5c91cb7bd1..4b4d5f33f6 100644 --- a/spec/bundler/shared_helpers_spec.rb +++ b/spec/bundler/shared_helpers_spec.rb @@ -4,10 +4,13 @@ RSpec.describe Bundler::SharedHelpers do let(:ext_lock_double) { double(:ext_lock) } before do + pwd_stub allow(Bundler.rubygems).to receive(:ext_lock).and_return(ext_lock_double) allow(ext_lock_double).to receive(:synchronize) {|&block| block.call } end + let(:pwd_stub) { allow(subject).to receive(:pwd).and_return(bundled_app) } + subject { Bundler::SharedHelpers } describe "#default_gemfile" do @@ -77,7 +80,7 @@ RSpec.describe Bundler::SharedHelpers do let(:global_rubygems_dir) { Pathname.new(bundled_app) } before do - Dir.mkdir ".bundle" + Dir.mkdir bundled_app(".bundle") allow(Bundler.rubygems).to receive(:user_home).and_return(global_rubygems_dir) end @@ -91,7 +94,7 @@ RSpec.describe Bundler::SharedHelpers do let(:expected_bundle_dir_path) { Pathname.new("#{bundled_app}/.bundle") } before do - Dir.mkdir ".bundle" + Dir.mkdir bundled_app(".bundle") allow(Bundler.rubygems).to receive(:user_home).and_return(global_rubygems_dir) end @@ -109,8 +112,8 @@ RSpec.describe Bundler::SharedHelpers do shared_examples_for "correctly determines whether to return a Gemfile path" do context "currently in directory with a Gemfile" do - before { FileUtils.touch("Gemfile") } - after { FileUtils.rm("Gemfile") } + before { FileUtils.touch(bundled_app_gemfile) } + after { FileUtils.rm(bundled_app_gemfile) } it "returns path of the bundle Gemfile" do expect(subject.in_bundle?).to eq("#{bundled_app}/Gemfile") @@ -148,22 +151,24 @@ RSpec.describe Bundler::SharedHelpers do describe "#chdir" do let(:op_block) { proc { Dir.mkdir "nested_dir" } } - before { Dir.mkdir "chdir_test_dir" } + before { Dir.mkdir bundled_app("chdir_test_dir") } it "executes the passed block while in the specified directory" do - subject.chdir("chdir_test_dir", &op_block) - expect(Pathname.new("chdir_test_dir/nested_dir")).to exist + subject.chdir(bundled_app("chdir_test_dir"), &op_block) + expect(bundled_app("chdir_test_dir/nested_dir")).to exist end end describe "#pwd" do + let(:pwd_stub) { nil } + it "returns the current absolute path" do - expect(subject.pwd).to eq(bundled_app) + expect(subject.pwd).to eq(root) end end describe "#with_clean_git_env" do - let(:with_clean_git_env_block) { proc { Dir.mkdir "with_clean_git_env_test_dir" } } + let(:with_clean_git_env_block) { proc { Dir.mkdir bundled_app("with_clean_git_env_test_dir") } } before do ENV["GIT_DIR"] = "ORIGINAL_ENV_GIT_DIR" @@ -172,20 +177,20 @@ RSpec.describe Bundler::SharedHelpers do it "executes the passed block" do subject.with_clean_git_env(&with_clean_git_env_block) - expect(Pathname.new("with_clean_git_env_test_dir")).to exist + expect(bundled_app("with_clean_git_env_test_dir")).to exist end context "when a block is passed" do let(:with_clean_git_env_block) do 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? + Dir.mkdir bundled_app("git_dir_test_dir") unless ENV["GIT_DIR"].nil? + Dir.mkdir bundled_app("git_work_tree_test_dir") unless ENV["GIT_WORK_TREE"].nil? end end it "uses a fresh git env for execution" do 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 + expect(bundled_app("git_dir_test_dir")).to_not exist + expect(bundled_app("git_work_tree_test_dir")).to_not exist end end @@ -225,7 +230,7 @@ RSpec.describe Bundler::SharedHelpers do end shared_examples_for "ENV['PATH'] gets set correctly" do - before { Dir.mkdir ".bundle" } + before { Dir.mkdir bundled_app(".bundle") } it "ensures bundle bin path is in ENV['PATH']" do subject.set_bundle_environment @@ -245,7 +250,7 @@ RSpec.describe Bundler::SharedHelpers do let(:ruby_lib_path) { "stubbed_ruby_lib_dir" } before do - allow(Bundler::SharedHelpers).to receive(:bundler_ruby_lib).and_return(ruby_lib_path) + allow(subject).to receive(:bundler_ruby_lib).and_return(ruby_lib_path) end it "ensures bundler's ruby version lib path is in ENV['RUBYLIB']" do @@ -264,7 +269,7 @@ RSpec.describe Bundler::SharedHelpers do end it "ignores if bundler_ruby_lib is same as rubylibdir" do - allow(Bundler::SharedHelpers).to receive(:bundler_ruby_lib).and_return(RbConfig::CONFIG["rubylibdir"]) + allow(subject).to receive(:bundler_ruby_lib).and_return(RbConfig::CONFIG["rubylibdir"]) subject.set_bundle_environment |