summaryrefslogtreecommitdiff
path: root/spec/bundler/commands/add_spec.rb
blob: 36e286793b6d736264d67df947f4b2b095bc31c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# 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"
      build_gem "lemur", "3.1.1.pre.2023.1.1"
    end

    build_git "foo", "2.0"

    gemfile <<-G
      source "#{file_uri_for(gem_repo2)}"
      gem "weakling", "~> 0.0.1"
    G
  end

  context "when no gems are specified" do
    it "shows error" do
      bundle "add", raise_on_error: false

      expect(err).to include("Please specify gems to add")
    end
  end

  describe "without version specified" do
    it "version requirement becomes ~> major.minor.patch when resolved version is < 1.0" do
      bundle "add 'bar'"
      expect(bundled_app_gemfile.read).to match(/gem "bar", "~> 0.12.3"/)
      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'"
      expect(bundled_app_gemfile.read).to match(/gem "baz", "~> 1.2"/)
      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'"
      expect(bundled_app_gemfile.read).to match(/gem "cat", "~> 0.12.3.pre"/)
      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'"
      expect(bundled_app_gemfile.read).to match(/gem "dog", "~> 1.1.pre"/)
      expect(the_bundle).to include_gems "dog 1.1.3.pre"
    end

    it "version requirement becomes ~> major.minor.pre.tail when resolved version has a very long tail pre version" do
      bundle "add 'lemur'"
      # the trailing pre purposely matches the release version to ensure that subbing the release doesn't change the pre.version"
      expect(bundled_app_gemfile.read).to match(/gem "lemur", "~> 3.1.pre.2023.1.1"/)
      expect(the_bundle).to include_gems "lemur 3.1.1.pre.2023.1.1"
    end
  end

  describe "with --version" do
    it "adds dependency of specified version and runs install" do
      bundle "add 'foo' --version='~> 1.0'"
      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 1.0"/)
      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(", ")}'"
      expect(bundled_app_gemfile.read).to match(/gem "foo", #{Gem::Requirement.new(requirements).as_list.map(&:dump).join(", ")}/)
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --require" do
    it "adds the require param for the gem" do
      bundle "add 'foo' --require=foo/engine"
      expect(bundled_app_gemfile.read).to match(%r{gem "foo",(?: .*,) :require => "foo\/engine"})
    end

    it "converts false to a boolean" do
      bundle "add 'foo' --require=false"
      expect(bundled_app_gemfile.read).to match(/gem "foo",(?: .*,) :require => false/)
    end
  end

  describe "with --group" do
    it "adds dependency for the specified group" do
      bundle "add 'foo' --group='development'"
      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2.0", :group => :development/)
      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'"
      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2.0", :groups => \[:development, :test\]/)
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --source" do
    it "adds dependency with specified source" do
      bundle "add 'foo' --source='#{file_uri_for(gem_repo2)}'"

      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2.0", :source => "#{file_uri_for(gem_repo2)}"/)
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --path" do
    it "adds dependency with specified path" do
      bundle "add 'foo' --path='#{lib_path("foo-2.0")}'"

      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2.0", :path => "#{lib_path("foo-2.0")}"/)
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --git" do
    it "adds dependency with specified git source" do
      bundle "add foo --git=#{lib_path("foo-2.0")}"

      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2.0", :git => "#{lib_path("foo-2.0")}"/)
      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 git source and branch" do
      bundle "add foo --git=#{lib_path("foo-2.0")} --branch=test"

      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2.0", :git => "#{lib_path("foo-2.0")}", :branch => "test"/)
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --git and --ref" do
    it "adds dependency with specified git source and branch" do
      bundle "add foo --git=#{lib_path("foo-2.0")} --ref=#{revision_for(lib_path("foo-2.0"))}"

      expect(bundled_app_gemfile.read).to match(/gem "foo", "~> 2\.0", :git => "#{lib_path("foo-2.0")}", :ref => "#{revision_for(lib_path("foo-2.0"))}"/)
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --github" do
    it "adds dependency with specified github source", :realworld do
      bundle "add rake --github=ruby/rake"

      expect(bundled_app_gemfile.read).to match(%r{gem "rake", "~> 13\.\d+", :github => "ruby\/rake"})
    end
  end

  describe "with --github and --branch" do
    it "adds dependency with specified github source and branch", :realworld do
      bundle "add rake --github=ruby/rake --branch=master"

      expect(bundled_app_gemfile.read).to match(%r{gem "rake", "~> 13\.\d+", :github => "ruby\/rake", :branch => "master"})
    end
  end

  describe "with --github and --ref" do
    it "adds dependency with specified github source and ref", :realworld do
      bundle "add rake --github=ruby/rake --ref=5c60da8"

      expect(bundled_app_gemfile.read).to match(%r{gem "rake", "~> 13\.\d+", :github => "ruby\/rake", :ref => "5c60da8"})
    end
  end

  describe "with --git and --glob" do
    it "adds dependency with specified git source" do
      bundle "add foo --git=#{lib_path("foo-2.0")} --glob='./*.gemspec'"

      expect(bundled_app_gemfile.read).to match(%r{gem "foo", "~> 2.0", :git => "#{lib_path("foo-2.0")}", :glob => "\./\*\.gemspec"})
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --git and --branch and --glob" do
    before do
      update_git "foo", "2.0", branch: "test"
    end

    it "adds dependency with specified git source and branch" do
      bundle "add foo --git=#{lib_path("foo-2.0")} --branch=test --glob='./*.gemspec'"

      expect(bundled_app_gemfile.read).to match(%r{gem "foo", "~> 2.0", :git => "#{lib_path("foo-2.0")}", :branch => "test", :glob => "\./\*\.gemspec"})
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --git and --ref and --glob" do
    it "adds dependency with specified git source and branch" do
      bundle "add foo --git=#{lib_path("foo-2.0")} --ref=#{revision_for(lib_path("foo-2.0"))} --glob='./*.gemspec'"

      expect(bundled_app_gemfile.read).to match(%r{gem "foo", "~> 2\.0", :git => "#{lib_path("foo-2.0")}", :ref => "#{revision_for(lib_path("foo-2.0"))}", :glob => "\./\*\.gemspec"})
      expect(the_bundle).to include_gems "foo 2.0"
    end
  end

  describe "with --github and --glob" do
    it "adds dependency with specified github source", :realworld do
      bundle "add rake --github=ruby/rake --glob='./*.gemspec'"

      expect(bundled_app_gemfile.read).to match(%r{gem "rake", "~> 13\.\d+", :github => "ruby\/rake", :glob => "\.\/\*\.gemspec"})
    end
  end

  describe "with --github and --branch --and glob" do
    it "adds dependency with specified github source and branch", :realworld do
      bundle "add rake --github=ruby/rake --branch=master --glob='./*.gemspec'"

      expect(bundled_app_gemfile.read).to match(%r{gem "rake", "~> 13\.\d+", :github => "ruby\/rake", :branch => "master", :glob => "\.\/\*\.gemspec"})
    end
  end

  describe "with --github and --ref and --glob" do
    it "adds dependency with specified github source and ref", :realworld do
      bundle "add rake --github=ruby/rake --ref=5c60da8 --glob='./*.gemspec'"

      expect(bundled_app_gemfile.read).to match(%r{gem "rake", "~> 13\.\d+", :github => "ruby\/rake", :ref => "5c60da8", :glob => "\.\/\*\.gemspec"})
    end
  end

  describe "with --skip-install" do
    it "adds gem to Gemfile but is not installed" do
      bundle "add foo --skip-install --version=2.0"

      expect(bundled_app_gemfile.read).to match(/gem "foo", "= 2.0"/)
      expect(the_bundle).to_not include_gems "foo 2.0"
    end
  end

  it "using combination of short form options works like long form" do
    bundle "add 'foo' -s='#{file_uri_for(gem_repo2)}' -g='development' -v='~>1.0'"
    expect(bundled_app_gemfile.read).to include %(gem "foo", "~> 1.0", :group => :development, :source => "#{file_uri_for(gem_repo2)}")
    expect(the_bundle).to include_gems "foo 1.1"
  end

  it "shows error message when version is not formatted correctly" do
    bundle "add 'foo' -v='~>1 . 0'", raise_on_error: false
    expect(err).to match("Invalid gem requirement pattern '~>1 . 0'")
  end

  it "shows error message when gem cannot be found" do
    bundle "config set force_ruby_platform true"
    bundle "add 'werk_it'", raise_on_error: false
    expect(err).to match("Could not find gem 'werk_it' in")

    bundle "add 'werk_it' -s='#{file_uri_for(gem_repo2)}'", raise_on_error: false
    expect(err).to match("Could not find gem 'werk_it' in rubygems repository")
  end

  it "shows error message when source cannot be reached" do
    bundle "add 'baz' --source='http://badhostasdf'", raise_on_error: false
    expect(err).to include("Could not reach host badhostasdf. Check your network connection and try again.")

    bundle "add 'baz' --source='file://does/not/exist'", raise_on_error: false
    expect(err).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

  describe "with --optimistic and --strict" do
    it "throws error" do
      bundle "add 'foo' --strict --optimistic", raise_on_error: false

      expect(err).to include("You can not specify `--strict` and `--optimistic` at the same time")
    end
  end

  context "multiple gems" do
    it "adds multiple gems to gemfile" do
      bundle "add bar baz"

      expect(bundled_app_gemfile.read).to match(/gem "bar", "~> 0.12.3"/)
      expect(bundled_app_gemfile.read).to match(/gem "baz", "~> 1.2"/)
    end

    it "throws error if any of the specified gems are present in the gemfile with different version" do
      bundle "add weakling bar", raise_on_error: false

      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).")
    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
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo2)}"
        gem "rack", "1.0"
      G

      bundle "add 'rack' --version=1.1", raise_on_error: false

      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")
    end

    it "shows error when added without version requirements" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo2)}"
        gem "rack", "1.0"
      G

      bundle "add 'rack'", raise_on_error: false

      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")
    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
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo2)}"
        gem "rack"
      G

      bundle "add 'rack' --version=1.1", raise_on_error: false

      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")
    end
  end

  describe "when a gem is added and cache exists" do
    it "caches all new dependencies added for the specified gem" do
      bundle :cache

      bundle "add 'rack' --version=1.0.0"
      expect(bundled_app("vendor/cache/rack-1.0.0.gem")).to exist
    end
  end
end