2018-11-02 19:07:56 -04:00
# frozen_string_literal: true
RSpec . describe " bundle add " do
before :each do
build_repo2 do
build_gem " foo " , " 1.1 "
build_gem " foo " , " 2.0 "
build_gem " baz " , " 1.2.3 "
build_gem " bar " , " 0.12.3 "
build_gem " cat " , " 0.12.3.pre "
build_gem " dog " , " 1.1.3.pre "
end
2019-06-01 05:49:40 -04:00
build_git " foo " , " 2.0 "
2020-06-07 08:59:32 -04:00
gemfile <<-G
2019-05-06 12:06:21 -04:00
source " #{ file_uri_for ( gem_repo2 ) } "
2018-11-02 19:07:56 -04:00
gem " weakling " , " ~> 0.0.1 "
G
end
context " when no gems are specified " do
it " shows error " do
2020-06-03 12:43:17 -04:00
bundle " add " , :raise_on_error = > false
2018-11-02 19:07:56 -04:00
2019-06-01 05:49:40 -04:00
expect ( err ) . to include ( " Please specify gems to add " )
2018-11-02 19:07:56 -04:00
end
end
describe " without version specified " do
it " version requirement becomes ~> major.minor.patch when resolved version is < 1.0 " do
bundle " add 'bar' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "bar", "~> 0.12.3" / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " bar 0.12.3 "
end
it " version requirement becomes ~> major.minor when resolved version is > 1.0 " do
bundle " add 'baz' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "baz", "~> 1.2" / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " baz 1.2.3 "
end
it " version requirement becomes ~> major.minor.patch.pre when resolved version is < 1.0 " do
bundle " add 'cat' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "cat", "~> 0.12.3.pre" / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " cat 0.12.3.pre "
end
it " version requirement becomes ~> major.minor.pre when resolved version is > 1.0.pre " do
bundle " add 'dog' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "dog", "~> 1.1.pre" / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " dog 1.1.3.pre "
end
end
describe " with --version " do
it " adds dependency of specified version and runs install " do
bundle " add 'foo' --version='~> 1.0' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "~> 1.0" / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 1.1 "
end
it " adds multiple version constraints when specified " do
requirements = [ " < 3.0 " , " > 1.0 " ]
bundle " add 'foo' --version=' #{ requirements . join ( " , " ) } ' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", #{ Gem :: Requirement . new ( requirements ) . as_list . map ( & :dump ) . join ( ', ' ) } / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
describe " with --group " do
it " adds dependency for the specified group " do
bundle " add 'foo' --group='development' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "~> 2.0", :group => :development / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
it " adds dependency to more than one group " do
bundle " add 'foo' --group='development, test' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "~> 2.0", :groups => \ [:development, :test \ ] / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
describe " with --source " do
it " adds dependency with specified source " do
2019-05-06 12:06:21 -04:00
bundle " add 'foo' --source=' #{ file_uri_for ( gem_repo2 ) } ' "
2018-11-02 19:07:56 -04:00
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "~> 2.0", :source => " #{ file_uri_for ( gem_repo2 ) } " / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
2019-06-01 05:49:40 -04:00
describe " with --git " do
it " adds dependency with specified github source " do
bundle " add foo --git= #{ lib_path ( " foo-2.0 " ) } "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "~> 2.0", :git => " #{ lib_path ( " foo-2.0 " ) } " / )
2019-06-01 05:49:40 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
describe " with --git and --branch " do
before do
update_git " foo " , " 2.0 " , :branch = > " test "
end
it " adds dependency with specified github source and branch " do
bundle " add foo --git= #{ lib_path ( " foo-2.0 " ) } --branch=test "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "~> 2.0", :git => " #{ lib_path ( " foo-2.0 " ) } ", :branch => "test" / )
2019-06-01 05:49:40 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
2018-11-02 19:07:56 -04:00
describe " with --skip-install " do
it " adds gem to Gemfile but is not installed " do
bundle " add foo --skip-install --version=2.0 "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "foo", "= 2.0" / )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to_not include_gems " foo 2.0 "
end
end
it " using combination of short form options works like long form " do
2019-05-06 12:06:21 -04:00
bundle " add 'foo' -s=' #{ file_uri_for ( gem_repo2 ) } ' -g='development' -v='~>1.0' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to include %( gem "foo", "~> 1.0", :group => :development, :source => " #{ file_uri_for ( gem_repo2 ) } " )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 1.1 "
end
it " shows error message when version is not formatted correctly " do
2020-06-03 12:43:17 -04:00
bundle " add 'foo' -v='~>1 . 0' " , :raise_on_error = > false
2019-04-14 02:01:35 -04:00
expect ( err ) . to match ( " Invalid gem requirement pattern '~>1 . 0' " )
2018-11-02 19:07:56 -04:00
end
it " shows error message when gem cannot be found " do
2019-07-23 07:11:33 -04:00
bundle " config set force_ruby_platform true "
2020-06-03 12:43:17 -04:00
bundle " add 'werk_it' " , :raise_on_error = > false
2019-04-14 02:01:35 -04:00
expect ( err ) . to match ( " Could not find gem 'werk_it' in " )
2018-11-02 19:07:56 -04:00
2020-06-03 12:43:17 -04:00
bundle " add 'werk_it' -s=' #{ file_uri_for ( gem_repo2 ) } ' " , :raise_on_error = > false
2019-04-14 02:01:35 -04:00
expect ( err ) . to match ( " Could not find gem 'werk_it' in rubygems repository " )
2018-11-02 19:07:56 -04:00
end
it " shows error message when source cannot be reached " do
2020-06-03 12:43:17 -04:00
bundle " add 'baz' --source='http://badhostasdf' " , :raise_on_error = > false
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " Could not reach host badhostasdf. Check your network connection and try again. " )
2018-11-02 19:07:56 -04:00
2020-06-03 12:43:17 -04:00
bundle " add 'baz' --source='file://does/not/exist' " , :raise_on_error = > false
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " Could not fetch specs from file://does/not/exist/ " )
2018-11-02 19:07:56 -04:00
end
describe " with --optimistic " do
it " adds optimistic version " do
2020-06-03 14:45:36 -04:00
bundle " add 'foo' --optimistic "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to include %( gem "foo", ">= 2.0" )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
describe " with --strict option " do
it " adds strict version " do
2020-06-03 14:45:36 -04:00
bundle " add 'foo' --strict "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to include %( gem "foo", "= 2.0" )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
describe " with no option " do
it " adds pessimistic version " do
2020-06-03 14:45:36 -04:00
bundle " add 'foo' "
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to include %( gem "foo", "~> 2.0" )
2018-11-02 19:07:56 -04:00
expect ( the_bundle ) . to include_gems " foo 2.0 "
end
end
describe " with --optimistic and --strict " do
it " throws error " do
2020-06-03 12:43:17 -04:00
bundle " add 'foo' --strict --optimistic " , :raise_on_error = > false
2018-11-02 19:07:56 -04:00
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " You can not specify `--strict` and `--optimistic` at the same time " )
2018-11-02 19:07:56 -04:00
end
end
context " multiple gems " do
it " adds multiple gems to gemfile " do
2020-06-03 14:45:36 -04:00
bundle " add bar baz "
2018-11-02 19:07:56 -04:00
2020-05-08 01:19:04 -04:00
expect ( bundled_app_gemfile . read ) . to match ( / gem "bar", "~> 0.12.3" / )
expect ( bundled_app_gemfile . read ) . to match ( / gem "baz", "~> 1.2" / )
2018-11-02 19:07:56 -04:00
end
it " throws error if any of the specified gems are present in the gemfile with different version " do
2020-06-03 12:43:17 -04:00
bundle " add weakling bar " , :raise_on_error = > false
2018-11-02 19:07:56 -04:00
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " You cannot specify the same gem twice with different version requirements " )
expect ( err ) . to include ( " You specified: weakling (~> 0.0.1) and weakling (>= 0). " )
2018-11-02 19:07:56 -04:00
end
end
describe " when a gem is added which is already specified in Gemfile with version " do
it " shows an error when added with different version requirement " do
2020-06-03 14:46:03 -04:00
install_gemfile <<-G
2019-05-06 12:06:21 -04:00
source " #{ file_uri_for ( gem_repo2 ) } "
2018-11-02 19:07:56 -04:00
gem " rack " , " 1.0 "
G
2020-06-03 12:43:17 -04:00
bundle " add 'rack' --version=1.1 " , :raise_on_error = > false
2018-11-02 19:07:56 -04:00
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " You cannot specify the same gem twice with different version requirements " )
expect ( err ) . to include ( " If you want to update the gem version, run `bundle update rack`. You may also need to change the version requirement specified in the Gemfile if it's too restrictive " )
2018-11-02 19:07:56 -04:00
end
it " shows error when added without version requirements " do
2020-06-03 14:46:03 -04:00
install_gemfile <<-G
2019-05-06 12:06:21 -04:00
source " #{ file_uri_for ( gem_repo2 ) } "
2018-11-02 19:07:56 -04:00
gem " rack " , " 1.0 "
G
2020-06-03 12:43:17 -04:00
bundle " add 'rack' " , :raise_on_error = > false
2018-11-02 19:07:56 -04:00
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " Gem already added. " )
expect ( err ) . to include ( " You cannot specify the same gem twice with different version requirements " )
expect ( err ) . not_to include ( " If you want to update the gem version, run `bundle update rack`. You may also need to change the version requirement specified in the Gemfile if it's too restrictive " )
2018-11-02 19:07:56 -04:00
end
end
describe " when a gem is added which is already specified in Gemfile without version " do
it " shows an error when added with different version requirement " do
2020-06-03 14:46:03 -04:00
install_gemfile <<-G
2019-05-06 12:06:21 -04:00
source " #{ file_uri_for ( gem_repo2 ) } "
2018-11-02 19:07:56 -04:00
gem " rack "
G
2020-06-03 12:43:17 -04:00
bundle " add 'rack' --version=1.1 " , :raise_on_error = > false
2018-11-02 19:07:56 -04:00
2019-04-14 02:01:35 -04:00
expect ( err ) . to include ( " You cannot specify the same gem twice with different version requirements " )
expect ( err ) . to include ( " If you want to update the gem version, run `bundle update rack`. " )
expect ( err ) . not_to include ( " You may also need to change the version requirement specified in the Gemfile if it's too restrictive " )
2018-11-02 19:07:56 -04:00
end
end
2019-11-11 03:57:45 -05:00
describe " when a gem is added and cache exists " do
it " caches all new dependencies added for the specified gem " do
2020-06-03 14:45:36 -04:00
bundle :cache
2019-11-11 03:57:45 -05:00
bundle " add 'rack' --version=1.0.0 "
expect ( bundled_app ( " vendor/cache/rack-1.0.0.gem " ) ) . to exist
end
end
2018-11-02 19:07:56 -04:00
end