Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions bundler/lib/bundler/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,21 @@ def remove_gems_from_dependencies(builder, gems, gemfile_path)
def remove_gems_from_gemfile(gems, gemfile_path)
patterns = /gem\s+(['"])#{Regexp.union(gems)}\1|gem\s*\((['"])#{Regexp.union(gems)}\2\)/

# remove lines which match the regex
new_gemfile = IO.readlines(gemfile_path).reject {|line| line.match(patterns) }
new_gemfile = []
multiline_removal = false
IO.readlines(gemfile_path).each do |line|
if line.match(patterns)
multiline_removal = line.rstrip.end_with?(",")
# skip lines which match the regex
next
end

# skip followup lines until line does not end with ','
new_gemfile << line unless multiline_removal
multiline_removal = line.rstrip.end_with?(",") if multiline_removal
end

# remove lone \n and append them with other strings
# remove line \n and append them with other strings
new_gemfile.each_with_index do |_line, index|
if new_gemfile[index + 1] == "\n"
new_gemfile[index] += new_gemfile[index + 1]
Expand Down
24 changes: 24 additions & 0 deletions bundler/spec/commands/remove_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@
source "#{file_uri_for(gem_repo1)}"
G
end

context "when gem is specified in multiple lines" do
it "shows success for removed gem" do
gemfile <<-G
source '#{file_uri_for(gem_repo1)}'

gem 'git'
gem 'rack',
git: 'https://github.com/rack/rack',
branch: 'master'
gem 'nokogiri'
G

bundle! "remove rack"

expect(out).to include("rack was removed.")
gemfile_should_be <<-G
source '#{file_uri_for(gem_repo1)}'

gem 'git'
gem 'nokogiri'
G
end
end
end

context "when gem is not present in gemfile" do
Expand Down