Skip to content

Commit

Permalink
allow single quotes in preamble (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer authored Oct 6, 2023
1 parent 34c6f67 commit 9f483bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/bundler/multilock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down
24 changes: 24 additions & 0 deletions spec/bundler/multilock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9f483bf

Please sign in to comment.