Skip to content

Commit

Permalink
Move stub_bundle_with_env inside Bundler.with_unbundled_env (Shop…
Browse files Browse the repository at this point in the history
…ify#2562)

If the developer has bundler related envs in the shell,
`Bundler.with_unbundled_env` will clear them out. And if
`stub_bundle_with_env` is called outside of `Bundler.with_unbundled_env`,
it will contain those envs, and causes a mismatch.

Co-authored-by: @vinistock <[email protected]>
  • Loading branch information
st0012 and vinistock authored Sep 17, 2024
1 parent 0f190ab commit a4f5a2d
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def test_changing_lockfile_causes_custom_bundle_to_be_rebuilt
# ruby-lsp is a part of the custom lockfile and would try to run `bundle update ruby-lsp`, which would fail. If
# we evaluate lazily, then we only find dependencies after the lockfile was copied, and then run bundle install
# instead, which re-locks and adds the ruby-lsp
stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
Bundler.with_unbundled_env do
stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
run_script(dir)
end
end
Expand Down Expand Up @@ -147,14 +147,14 @@ def test_does_not_copy_gemfile_lock_when_not_modified
FileUtils.touch("Gemfile.lock", mtime: Time.now + 10 * 60)

capture_subprocess_io do
stub_bundle_with_env(
bundle_env(dir, ".ruby-lsp/Gemfile"),
"((bundle check && bundle update ruby-lsp debug) || bundle install) 1>&2",
)
Bundler.with_unbundled_env do
stub_bundle_with_env(
bundle_env(dir, ".ruby-lsp/Gemfile"),
"((bundle check && bundle update ruby-lsp debug) || bundle install) 1>&2",
)

FileUtils.expects(:cp).never
FileUtils.expects(:cp).never

Bundler.with_unbundled_env do
# Run the script again without having the lockfile modified
run_script(dir)
end
Expand Down Expand Up @@ -184,9 +184,8 @@ def test_does_only_updates_every_4_hours
File.write(File.join(dir, ".ruby-lsp", "last_updated"), (Time.now - 30 * 60).iso8601)

capture_subprocess_io do
stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))

Bundler.with_unbundled_env do
stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
# Run the script again without having the lockfile modified
run_script(dir)
end
Expand All @@ -213,9 +212,9 @@ def test_creates_custom_bundle_if_no_gemfile
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
bundle_gemfile = Pathname.new(".ruby-lsp").expand_path(dir) + "Gemfile"
stub_bundle_with_env(bundle_env(dir, bundle_gemfile.to_s))

Bundler.with_unbundled_env do
stub_bundle_with_env(bundle_env(dir, bundle_gemfile.to_s))
run_script(dir)
end

Expand Down Expand Up @@ -289,8 +288,8 @@ def test_creates_custom_bundle_with_specified_branch
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
bundle_gemfile = Pathname.new(".ruby-lsp").expand_path(Dir.pwd) + "Gemfile"
stub_bundle_with_env(bundle_env(dir, bundle_gemfile.to_s))
Bundler.with_unbundled_env do
stub_bundle_with_env(bundle_env(dir, bundle_gemfile.to_s))
run_script(File.realpath(dir), branch: "test-branch")
end

Expand Down Expand Up @@ -321,12 +320,12 @@ def test_install_prerelease_versions_if_experimental_is_true
end

capture_subprocess_io do
stub_bundle_with_env(
bundle_env(dir, ".ruby-lsp/Gemfile"),
"((bundle check && bundle update ruby-lsp debug --pre) || bundle install) 1>&2",
)

Bundler.with_unbundled_env do
stub_bundle_with_env(
bundle_env(dir, ".ruby-lsp/Gemfile"),
"((bundle check && bundle update ruby-lsp debug --pre) || bundle install) 1>&2",
)

run_script(dir, experimental: true)
end
end
Expand Down Expand Up @@ -466,8 +465,8 @@ def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
end
end

stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
Bundler.with_unbundled_env do
stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
run_script(dir)
end

Expand Down Expand Up @@ -495,8 +494,8 @@ def test_ruby_lsp_rails_detection_handles_lang_from_environment
end
end

stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
Bundler.with_unbundled_env do
stub_bundle_with_env(bundle_env(dir, ".ruby-lsp/Gemfile"))
run_script(dir)
end

Expand Down Expand Up @@ -638,6 +637,8 @@ def run_script(path = Dir.pwd, expected_path: nil, **options)
assert_empty(stdout)
end

# This method needs to be called inside the `Bundler.with_unbundled_env` block IF the command you want to test is
# inside it.
def stub_bundle_with_env(env, command = "(bundle check || bundle install) 1>&2")
Object.any_instance.expects(:system).with do |actual_env, actual_command|
actual_env.delete_if { |k, _v| k.start_with?("BUNDLE_PKGS") }
Expand Down

0 comments on commit a4f5a2d

Please sign in to comment.