Permalink
Browse files

inject command with specs

  • Loading branch information...
1 parent 7a61d7b commit 2a3d10b87f9a64538c8e39a72d8a27c886939be8 @indirect indirect committed Aug 26, 2012
Showing with 75 additions and 0 deletions.
  1. +22 −0 lib/bundler/cli.rb
  2. +53 −0 spec/integration/inject.rb
View
@@ -664,6 +664,28 @@ def platform
Bundler.ui.info output.join("\n\n")
end
+ desc "add GEM VERSION ...", "Add the named gem(s), with version requirements, to the resolved Gemfile"
+ def inject(name, version, *gems)
+ # The required arguments allow Thor to give useful feedback when the arguments
+ # are incorrect. This adds those first two arguments onto the list as a whole.
+ gems.unshift(version).unshift(name)
+
+ # Build an array of Dependency objects out of the arguments
+ deps = []
+ gems.each_slice(2) do |name, version|
+ deps << Bundler::Dependency.new(name, version)
+ end
+
+ added = Injector.inject(deps)
+
+ if added.any?
+ Bundler.ui.confirm "Added to Gemfile:"
+ Bundler.ui.confirm added.map{ |g| " #{g}" }.join("\n")
+ else
+ Bundler.ui.confirm "All injected gems were already present in the Gemfile"
+ end
+ end
+
private
def setup_cache_all
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe "bundle inject" do
+ context "with a lockfile" do
+ before :each do
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack"
+ G
+ end
+
+ it "adds the injected gems to the gemfile" do
+ bundled_app("Gemfile").read.should_not match(/rack-obama/)
+ bundle "inject 'rack-obama' '> 0'"
+ bundled_app("Gemfile").read.should match(/rack-obama/)
+ end
+
+ it "locks with the injected gems" do
+ bundled_app("Gemfile.lock").read.should_not match(/rack-obama/)
+ bundle "inject 'rack-obama' '> 0'"
+ bundled_app("Gemfile.lock").read.should match(/rack-obama/)
+ end
+ end
+
+ context "without a lockfile" do
+ before :each do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack"
+ G
+ end
+
+ it "locks with the injected gems" do
+ bundled_app("Gemfile.lock").should_not exist
+ bundle "inject 'rack-obama' '> 0'"
+ bundled_app("Gemfile.lock").read.should match(/rack-obama/)
+ end
+ end
+
+ context "injected gems already in the Gemfile" do
+ before :each do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack"
+ G
+ end
+
+ it "doesn't add existing gems" do
+ bundle "inject 'rack' '> 0'"
+ out.should match(/cannot specify the same gem twice/i)
+ end
+ end
+end

0 comments on commit 2a3d10b

Please sign in to comment.