From 9f483bf2d9ee30b466b9f89d5ce55adc1e9f6b8e Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 6 Oct 2023 11:03:29 -0600 Subject: [PATCH] allow single quotes in preamble (#8) --- lib/bundler/multilock.rb | 8 +++++--- spec/bundler/multilock_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/bundler/multilock.rb b/lib/bundler/multilock.rb index c93d762..22a0c5b 100644 --- a/lib/bundler/multilock.rb +++ b/lib/bundler/multilock.rb @@ -329,7 +329,7 @@ def inject_preamble Bundler.ui.debug("Injecting multilock preamble") minor_version = Gem::Version.new(::Bundler::Multilock::VERSION).segments[0..1].join(".") - bundle_preamble1_match = %(plugin "bundler-multilock") + bundle_preamble1_match = /plugin ["']bundler-multilock["']/ bundle_preamble1 = <<~RUBY plugin "bundler-multilock", "~> #{minor_version}" RUBY @@ -384,8 +384,10 @@ def reset! private - def inject_specific_preamble(gemfile, gemfiles, injection_point, preamble, add_newline:, match: preamble) - return false if gemfiles.any? { |g| g.include?(match) } + def inject_specific_preamble(gemfile, gemfiles, injection_point, preamble, add_newline:, match: nil) + # allow either type of quotes + match ||= Regexp.new(Regexp.escape(preamble).gsub('"', %(["']))) + return false if gemfiles.any? { |g| match.match?(g) } add_newline = false unless gemfile[injection_point - 1] == "\n" diff --git a/spec/bundler/multilock_spec.rb b/spec/bundler/multilock_spec.rb index a9a5c86..96f3a03 100644 --- a/spec/bundler/multilock_spec.rb +++ b/spec/bundler/multilock_spec.rb @@ -46,6 +46,30 @@ end end + it "does not inject duplicate plugin load commands when you prefer single quotes" do + gemfile = <<~RUBY + # frozen_string_literal: true + + source 'https://rubygems.org' + + plugin 'bundler-multilock', '~> 1.0' + return unless Plugin.installed?('bundler-multilock') + + Plugin.send(:load_plugin, 'bundler-multilock') + + gem 'concurrent-ruby', '1.2.2' + RUBY + + with_gemfile("") do + File.write("Gemfile", gemfile) + + local_git = Shellwords.escape(File.expand_path("../..", __dir__)) + invoke_bundler("plugin install bundler-multilock --local_git=#{local_git}") + + expect(File.read("Gemfile")).to eq gemfile + end + end + it "does not inject when a secondary Gemfile has the necessary commands" do with_gemfile("") do File.write("Gemfile", <<~RUBY)