ruby-changes:61529
From: Carsten <ko1@a...>
Date: Fri, 5 Jun 2020 07:34:04 +0900 (JST)
Subject: [ruby-changes:61529] 0ae5cd55e5 (master): [rubygems/rubygems] Remove multiline gem specifications correctly
https://git.ruby-lang.org/ruby.git/commit/?id=0ae5cd55e5 From 0ae5cd55e5c764c8fb14c63eb663cdbff355434e Mon Sep 17 00:00:00 2001 From: Carsten Wirth <carsten.wirth@h...> Date: Tue, 26 Nov 2019 15:48:55 +0100 Subject: [rubygems/rubygems] Remove multiline gem specifications correctly https://github.com/rubygems/rubygems/commit/8dca0ad56e diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb index 767c5ff..6694642 100644 --- a/lib/bundler/injector.rb +++ b/lib/bundler/injector.rb @@ -180,8 +180,19 @@ module Bundler https://github.com/ruby/ruby/blob/trunk/lib/bundler/injector.rb#L180 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 line \n and append them with other strings new_gemfile.each_with_index do |_line, index| diff --git a/spec/bundler/commands/remove_spec.rb b/spec/bundler/commands/remove_spec.rb index ef31392..3b255cd 100644 --- a/spec/bundler/commands/remove_spec.rb +++ b/spec/bundler/commands/remove_spec.rb @@ -44,6 +44,30 @@ RSpec.describe "bundle remove" do https://github.com/ruby/ruby/blob/trunk/spec/bundler/commands/remove_spec.rb#L44 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 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/