diff --git a/lib/bootboot/bundler_patch.rb b/lib/bootboot/bundler_patch.rb index 16b2192..6887301 100644 --- a/lib/bootboot/bundler_patch.rb +++ b/lib/bootboot/bundler_patch.rb @@ -14,6 +14,17 @@ def initialize(wrong_lock, *args) end end +module DefinitionUnlockPatch + def initialize(lockfile, dependencies, sources, unlock, *args) + @bootboot_unlock = unlock.is_a?(Hash) ? unlock.transform_values { |v| v.dup.freeze }.freeze : unlock + super + end + + def bootboot_unlock + @bootboot_unlock.is_a?(Hash) ? @bootboot_unlock.transform_values(&:dup) : @bootboot_unlock + end +end + module RubyVersionPatch def system # Only monkey-patch if we're updating the alternate file @@ -48,6 +59,7 @@ def default_lockfile end Bundler::Definition.prepend(DefinitionSourceRequirementsPatch) +Bundler::Definition.prepend(DefinitionUnlockPatch) Bundler::RubyVersion.singleton_class.prepend(RubyVersionPatch) Bundler::Dsl.class_eval do diff --git a/lib/bootboot/gemfile_next_auto_sync.rb b/lib/bootboot/gemfile_next_auto_sync.rb index 10dab77..0bed65b 100644 --- a/lib/bootboot/gemfile_next_auto_sync.rb +++ b/lib/bootboot/gemfile_next_auto_sync.rb @@ -51,8 +51,7 @@ def update!(current_definition) ENV[env] = "1" ENV["BOOTBOOT_UPDATING_ALTERNATE_LOCKFILE"] = "1" - unlock = current_definition.instance_variable_get(:@unlock) - definition = Bundler::Definition.build(GEMFILE, lock, unlock) + definition = Bundler::Definition.build(GEMFILE, lock, current_definition.bootboot_unlock) definition.resolve_remotely! definition.lock(lock) ensure diff --git a/test/bootboot_test.rb b/test/bootboot_test.rb index 83f3e2d..eece807 100644 --- a/test/bootboot_test.rb +++ b/test/bootboot_test.rb @@ -124,6 +124,41 @@ def test_sync_the_gemfile_next_after_update_of_gem end end + def test_sync_the_gemfile_next_after_update_of_gem_without_gemfile_change + write_gemfile do |file, _dir| + FileUtils.cp("#{file.path}.lock", gemfile_next(file)) + File.write(file, 'gem "rake", "10.5.0"', mode: "a") + run_bundle_command("install", file.path) + File.write(file, file.read.gsub(', "10.5.0"', "")) + run_bundle_command("install", file.path) + + assert_equal("10.5.0", locked_version("rake", "#{file.path}.lock")) + assert_equal("10.5.0", locked_version("rake", gemfile_next(file))) + + run_bundle_command("update rake", file.path) + + refute_equal("10.5.0", locked_version("rake", "#{file.path}.lock")) + assert_equal(locked_version("rake", "#{file.path}.lock"), locked_version("rake", gemfile_next(file))) + end + end + + def test_sync_the_gemfile_next_after_update_of_all_gems + write_gemfile do |file, _dir| + FileUtils.cp("#{file.path}.lock", gemfile_next(file)) + File.write(file, 'gem "rake", "10.5.0"', mode: "a") + run_bundle_command("install", file.path) + File.write(file, file.read.gsub(', "10.5.0"', "")) + run_bundle_command("install", file.path) + + assert_equal("10.5.0", locked_version("rake", gemfile_next(file))) + + run_bundle_command("update --all", file.path) + + refute_equal("10.5.0", locked_version("rake", "#{file.path}.lock")) + assert_equal(locked_version("rake", "#{file.path}.lock"), locked_version("rake", gemfile_next(file))) + end + end + def test_sync_the_gemfile_next_when_gemfile_contain_if_else_statement write_gemfile do |file, _dir| FileUtils.cp("#{file.path}.lock", gemfile_next(file)) @@ -311,6 +346,10 @@ def gemfile_next(gemfile) "#{gemfile.path}_next.lock" end + def locked_version(gem_name, lockfile) + Bundler::LockfileParser.new(File.read(lockfile)).specs.find { |spec| spec.name == gem_name }.version.to_s + end + def write_gemfile(content = nil) dir = Dir.mktmpdir file = Tempfile.new("Gemfile", dir).tap do |f|