summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAgrim Mittal <agrimmittal97@gmail.com>2018-05-19 00:36:08 +0530
committerAgrim Mittal <agrimmittal97@gmail.com>2018-06-28 15:35:16 +0530
commitd4bd77c32502945374d0de33e6442acb15f0bbb0 (patch)
treea1af3bcdfcf3adeb180750d32181ac01f60b2b4a
parentff0a776f55b35b4402fb646be2fa9dfebc2b0499 (diff)
downloadbundler-d4bd77c32502945374d0de33e6442acb15f0bbb0.tar.gz
Handle eval_gemfile
-rw-r--r--lib/bundler/cli/remove.rb5
-rw-r--r--lib/bundler/injector.rb51
-rw-r--r--lib/bundler/shared_helpers.rb3
-rw-r--r--spec/commands/remove_spec.rb106
4 files changed, 114 insertions, 51 deletions
diff --git a/lib/bundler/cli/remove.rb b/lib/bundler/cli/remove.rb
index 4090d81a6e..289971d243 100644
--- a/lib/bundler/cli/remove.rb
+++ b/lib/bundler/cli/remove.rb
@@ -12,13 +12,10 @@ module Bundler
raise InvalidOption, "Please specify gems to remove." if @gems.empty?
# remove requested gems
- removed_deps = Injector.remove(@gems, {})
+ Injector.remove(@gems, {})
# Remove gems from .bundle if install flag is specified
Installer.install(Bundler.root, Bundler.definition) if @options["install"]
-
- # print success for removed gems
- removed_deps.each {|dep| Bundler.ui.confirm "#{SharedHelpers.pretty_dependency(dep)} was removed." }
end
end
end
diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb
index 6cb30b0eda..5c96884201 100644
--- a/lib/bundler/injector.rb
+++ b/lib/bundler/injector.rb
@@ -61,27 +61,16 @@ module Bundler
# @param [Pathname] lockfile_path The lockfile from which to remove dependencies.
# @return [Array]
def remove(gemfile_path, lockfile_path)
- # get initial gemfile snap shot
- initial_gemfile = IO.readlines(gemfile_path)
-
- # evaluate the Gemfile we have
+ # evaluate the main Gemfile
builder = Dsl.new
builder.eval_gemfile(Bundler.default_gemfile)
- # remove gems from dependencies
- removed_deps = remove_gems_from_dependencies(builder, @deps)
+ definition = builder.to_definition(lockfile_path, {})
- # gemfile after removing requested gems
- cleaned_gemfile = remove_gems_from_gemfile(@deps, gemfile_path)
-
- # write the new gemfile
- write_to_gemfile(gemfile_path, cleaned_gemfile)
-
- # check for errors
- # including extra gems being removed
- # or some gems not being removed
- # and return the actual removed deps
- cross_check_for_errors(gemfile_path, builder.dependencies, removed_deps, initial_gemfile)
+ definition.gemfiles.each do |g|
+ # print success for removed gems
+ evaluate_gemfile(g).each {|dep| Bundler.ui.confirm "#{SharedHelpers.pretty_dependency(dep, false, true)} was removed." }
+ end
end
private
@@ -133,6 +122,34 @@ module Bundler
end
end
+ # evalutes a gemfile to remove the specified gem
+ # from it.
+ def evaluate_gemfile(gemfile_path)
+ # get initial gemfile snap shot
+ initial_gemfile = IO.readlines(gemfile_path)
+
+ Bundler.ui.info "Removing gems from #{gemfile_path}"
+
+ # evaluate the Gemfile we have
+ builder = Dsl.new
+ builder.eval_gemfile(Bundler.default_gemfile)
+
+ # remove gems from dependencies
+ removed_deps = remove_gems_from_dependencies(builder, @deps)
+
+ # gemfile after removing requested gems
+ cleaned_gemfile = remove_gems_from_gemfile(@deps, gemfile_path)
+
+ # write the new gemfile
+ write_to_gemfile(gemfile_path, cleaned_gemfile)
+
+ # check for errors
+ # including extra gems being removed
+ # or some gems not being removed
+ # and return the actual removed deps
+ cross_check_for_errors(gemfile_path, builder.dependencies, removed_deps, initial_gemfile)
+ end
+
# @param [Dsl] builder Dsl object of current Gemfile.
# @param [Array] gems Array of names of gems to be removed.
# @return [Array] removed_deps Array of removed dependencies.
diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb
index 2b9fb8d6a7..e0376eb00b 100644
--- a/lib/bundler/shared_helpers.rb
+++ b/lib/bundler/shared_helpers.rb
@@ -195,13 +195,14 @@ module Bundler
"\nEither installing with `--full-index` or running `bundle update #{spec.name}` should fix the problem."
end
- def pretty_dependency(dep, print_source = false)
+ def pretty_dependency(dep, print_source = false, print_requirements = false)
msg = String.new(dep.name)
msg << " (#{dep.requirement})" unless dep.requirement == Gem::Requirement.default
if dep.is_a?(Bundler::Dependency)
platform_string = dep.platforms.join(", ")
msg << " " << platform_string if !platform_string.empty? && platform_string != Gem::Platform::RUBY
end
+ msg << " (#{dep.requirement})" if print_requirements && dep.requirement
msg << " from the `#{dep.source}` source" if print_source && dep.source
msg
end
diff --git a/spec/commands/remove_spec.rb b/spec/commands/remove_spec.rb
index 3262636efc..6eeec24c3b 100644
--- a/spec/commands/remove_spec.rb
+++ b/spec/commands/remove_spec.rb
@@ -23,7 +23,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rack --install"
- expect(out).to include("rack was removed.")
+ expect(out).to include("rack (>= 0) was removed.")
expect(the_bundle).to_not include_gems "rack"
end
end
@@ -39,7 +39,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rack"
- expect(out).to include("rack was removed.")
+ expect(out).to include("rack (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -68,8 +68,8 @@ RSpec.describe "bundle remove" do
bundle! "remove rack rails"
- expect(out).to include("rack was removed.")
- expect(out).to include("rails was removed.")
+ expect(out).to include("rack (>= 0) was removed.")
+ expect(out).to include("rails (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -100,7 +100,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rack"
- expect(out).to include("rack was removed.")
+ expect(out).to include("rack (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -119,7 +119,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -139,7 +139,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -158,7 +158,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -179,7 +179,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -200,7 +200,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -221,7 +221,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
@@ -246,7 +246,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
@@ -290,8 +290,8 @@ RSpec.describe "bundle remove" do
bundle! "remove rails rack rspec minitest"
- expect(out).to include("rails was removed.")
- expect(out).to include("minitest was removed.")
+ expect(out).to include("rails (>= 0) was removed.")
+ expect(out).to include("minitest (>= 0) was removed.")
expect(out).to include("rack, rspec could not be removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
@@ -324,7 +324,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rspec"
- expect(out).to include("rspec was removed.")
+ expect(out).to include("rspec (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
@@ -333,22 +333,70 @@ RSpec.describe "bundle remove" do
end
end
- context "with eval_gemfile" do
- it "removes gems" do
- create_file "Gemfile-other", <<-G
- gem "rack"
- G
+ describe "with eval_gemfile" do
+ context "when gems are present in gemfile" do
+ it "removes gems" do
+ create_file "Gemfile-other", <<-G
+ gem "rack"
+ G
- install_gemfile <<-G
- source "file://#{gem_repo1}"
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
- eval_gemfile "Gemfile-other"
- G
+ eval_gemfile "Gemfile-other"
+ G
- bundle! "remove rack"
+ bundle! "remove rack"
+
+ expect(bundled_app("Gemfile-other").read).to_not include("gem \"rack\"")
+ expect(out).to include("rack (>= 0) was removed.")
+ end
+ end
+
+ context "when gem to removed is not specified in the gemfile" do
+ it "throws error for gems not present" do
+ # an empty gemfile
+ # indicating the gem is not present in the gemfile
+ create_file "Gemfile-other", <<-G
+ G
+
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+
+ eval_gemfile "Gemfile-other"
+ G
+
+ bundle "remove rack"
- expect(bundled_app("Gemfile-other").read).to_not include("gem \"rack\"")
- expect(out).to include("rack was removed.")
+ expect(out).to include("You cannot remove a gem which not specified in Gemfile.")
+ expect(out).to include("`rack` is not specified in Gemfile so not removed.")
+ end
+ end
+
+ context "when the gem is present in parent file but not in gemfile specified by eval_gemfile" do
+ it "removes gem" do
+ create_file "Gemfile-other", <<-G
+ gem "rails"
+ G
+
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+
+ eval_gemfile "Gemfile-other"
+ gem "rack"
+ G
+
+ bundle "remove rack"
+
+ expect(out).to include("rack (>= 0) was removed.")
+ expect(out).to include("You cannot remove a gem which not specified in Gemfile.")
+ expect(out).to include("`rack` is not specified in Gemfile so not removed.")
+ gemfile_should_be <<-G
+ source "file://#{gem_repo1}"
+
+ eval_gemfile "Gemfile-other"
+ G
+ end
end
end
@@ -364,7 +412,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rack"
- expect(out).to include("rack was removed.")
+ expect(out).to include("rack (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G
@@ -383,7 +431,7 @@ RSpec.describe "bundle remove" do
bundle! "remove rack"
- expect(out).to include("rack was removed.")
+ expect(out).to include("rack (>= 0) was removed.")
gemfile_should_be <<-G
source "file://#{gem_repo1}"
G