summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAgrim Mittal <agrimmittal97@gmail.com>2018-05-27 18:00:00 +0530
committerAgrim Mittal <agrimmittal97@gmail.com>2018-05-28 16:03:03 +0530
commitec36d0790a1975613aba9b336059ecc209c57071 (patch)
tree5d4bf3d2ad7cb0c57f5f90a82d4d5c6a46b40155
parentc793c38a55559677573d28d4bfadd811e8508b48 (diff)
downloadbundler-ec36d0790a1975613aba9b336059ecc209c57071.tar.gz
add version prefix
-rw-r--r--lib/bundler/cli.rb2
-rw-r--r--lib/bundler/cli/add.rb5
-rw-r--r--lib/bundler/injector.rb12
-rw-r--r--spec/commands/add_spec.rb24
4 files changed, 41 insertions, 2 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index f3340f7cf7..cd382f7a16 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -335,6 +335,8 @@ module Bundler
method_option "source", :aliases => "-s", :type => :string
method_option "skip-install", :type => :boolean, :banner =>
"Adds gem to the Gemfile but does not install it"
+ method_option "optimistic", :type => :boolean
+ method_option "strict", :type => :boolean
def add(gem_name)
require "bundler/cli/add"
Add.new(options.dup, gem_name).run
diff --git a/lib/bundler/cli/add.rb b/lib/bundler/cli/add.rb
index e1a662161e..524156165a 100644
--- a/lib/bundler/cli/add.rb
+++ b/lib/bundler/cli/add.rb
@@ -18,7 +18,10 @@ module Bundler
end
dependency = Bundler::Dependency.new(@gem_name, version, @options)
- Injector.inject([dependency], :conservative_versioning => @options[:version].nil?) # Perform conservative versioning only when version is not specified
+ Injector.inject([dependency],
+ :conservative_versioning => @options[:version].nil?, # Perform conservative versioning only when version is not specified
+ :optimistic => @options[:optimistic],
+ :strict => @options[:strict])
Installer.install(Bundler.root, Bundler.definition) unless @options["skip-install"]
end
end
diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb
index 9c67a80777..b62279b94c 100644
--- a/lib/bundler/injector.rb
+++ b/lib/bundler/injector.rb
@@ -61,7 +61,17 @@ module Bundler
seg_end_index = version >= Gem::Version.new("1.0") ? 1 : 2
prerelease_suffix = version.to_s.gsub(version.release.to_s, "") if version.prerelease?
- "~> #{segments[0..seg_end_index].join(".")}#{prerelease_suffix}"
+ "#{version_prefix}#{segments[0..seg_end_index].join(".")}#{prerelease_suffix}"
+ end
+
+ def version_prefix
+ if @options[:strict]
+ "= "
+ elsif @options[:optimistic]
+ ">= "
+ else
+ "~> "
+ end
end
def build_gem_lines(conservative_versioning)
diff --git a/spec/commands/add_spec.rb b/spec/commands/add_spec.rb
index 7299938caa..a695d628f5 100644
--- a/spec/commands/add_spec.rb
+++ b/spec/commands/add_spec.rb
@@ -116,4 +116,28 @@ RSpec.describe "bundle add" do
bundle "add 'baz' --source='file://does/not/exist'"
expect(out).to include("Could not fetch specs from file://does/not/exist/")
end
+
+ describe "with --optimistic" do
+ it "adds optimistic version" do
+ bundle! "add 'foo' --optimistic"
+ expect(bundled_app("Gemfile").read).to include %(gem "foo", ">= 2.0")
+ expect(the_bundle).to include_gems "foo 2.0"
+ end
+ end
+
+ describe "with --strict option" do
+ it "adds strict version" do
+ bundle! "add 'foo' --strict"
+ expect(bundled_app("Gemfile").read).to include %(gem "foo", "= 2.0")
+ expect(the_bundle).to include_gems "foo 2.0"
+ end
+ end
+
+ describe "with no option" do
+ it "adds pessimistic version" do
+ bundle! "add 'foo'"
+ expect(bundled_app("Gemfile").read).to include %(gem "foo", "~> 2.0")
+ expect(the_bundle).to include_gems "foo 2.0"
+ end
+ end
end