summaryrefslogtreecommitdiff
path: root/spec/bundler/commands/remove_spec.rb
blob: 70dc09c9b67144ecd146e4d48a729a3232e61519 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# frozen_string_literal: true

RSpec.describe "bundle remove" do
  context "when no gems are specified" do
    it "throws error" do
      gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
      G

      bundle "remove", :raise_on_error => false

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

  context "when --install flag is specified", :bundler => "< 3" do
    it "removes gems from .bundle" do
      gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"

        gem "rack"
      G

      bundle "remove rack --install"

      expect(out).to include("rack was removed.")
      expect(the_bundle).to_not include_gems "rack"
    end
  end

  describe "remove single gem from gemfile" do
    context "when gem is present in gemfile" do
      it "shows success for removed gem" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          gem "rack"
        G

        bundle "remove rack"

        expect(out).to include("rack was removed.")
        expect(the_bundle).to_not include_gems "rack"
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end

      context "when gem is specified in multiple lines" do
        it "shows success for removed gem" do
          gemfile <<-G
            source '#{file_uri_for(gem_repo1)}'

            gem 'git'
            gem 'rack',
                git: 'https://github.com/rack/rack',
                branch: 'master'
            gem 'nokogiri'
          G

          bundle "remove rack"

          expect(out).to include("rack was removed.")
          expect(gemfile).to eq <<~G
            source '#{file_uri_for(gem_repo1)}'

            gem 'git'
            gem 'nokogiri'
          G
        end
      end
    end

    context "when gem is not present in gemfile" do
      it "shows warning for gem that could not be removed" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
        G

        bundle "remove rack", :raise_on_error => false

        expect(err).to include("`rack` is not specified in #{bundled_app_gemfile} so it could not be removed.")
      end
    end
  end

  describe "remove multiple gems from gemfile" do
    context "when all gems are present in gemfile" do
      it "shows success fir all removed gems" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          gem "rack"
          gem "rails"
        G

        bundle "remove rack rails"

        expect(out).to include("rack was removed.")
        expect(out).to include("rails was removed.")
        expect(gemfile).to eq <<~G
        source "#{file_uri_for(gem_repo1)}"
        G
      end
    end

    context "when some gems are not present in the gemfile" do
      it "shows warning for those not present and success for those that can be removed" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          gem "rails"
          gem "minitest"
          gem "rspec"
        G

        bundle "remove rails rack minitest", :raise_on_error => false

        expect(err).to include("`rack` is not specified in #{bundled_app_gemfile} so it could not be removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          gem "rails"
          gem "minitest"
          gem "rspec"
        G
      end
    end
  end

  context "with inline groups" do
    it "removes the specified gem" do
      gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"

        gem "rack", :group => [:dev]
      G

      bundle "remove rack"

      expect(out).to include("rack was removed.")
      expect(gemfile).to eq <<~G
        source "#{file_uri_for(gem_repo1)}"
      G
    end
  end

  describe "with group blocks" do
    context "when single group block with gem to be removed is present" do
      it "removes the group block" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            gem "rspec"
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end
    end

    context "when gem to be removed is outside block" do
      it "does not modify group" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          gem "rack"
          group :test do
            gem "coffee-script-source"
          end
        G

        bundle "remove rack"

        expect(out).to include("rack was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            gem "coffee-script-source"
          end
        G
      end
    end

    context "when an empty block is also present" do
      it "removes all empty blocks" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            gem "rspec"
          end

          group :dev do
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end
    end

    context "when the gem belongs to multiple groups" do
      it "removes the groups" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :test, :serioustest do
            gem "rspec"
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end
    end

    context "when the gem is present in multiple groups" do
      it "removes all empty blocks" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :one do
            gem "rspec"
          end

          group :two do
            gem "rspec"
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end
    end
  end

  describe "nested group blocks" do
    context "when all the groups will be empty after removal" do
      it "removes the empty nested blocks" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            group :serioustest do
              gem "rspec"
            end
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end
    end

    context "when outer group will not be empty after removal" do
      it "removes only empty blocks" do
        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            gem "rack-test"

            group :serioustest do
              gem "rspec"
            end
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            gem "rack-test"

          end
        G
      end
    end

    context "when inner group will not be empty after removal" do
      it "removes only empty blocks" do
        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            group :serioustest do
              gem "rspec"
              gem "rack-test"
            end
          end
        G

        bundle "remove rspec"

        expect(out).to include("rspec was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          group :test do
            group :serioustest do
              gem "rack-test"
            end
          end
        G
      end
    end
  end

  describe "arbitrary gemfile" do
    context "when multiple gems are present in same line" do
      it "shows warning for gems not removed" do
        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "rack"; gem "rails"
        G

        bundle "remove rails", :raise_on_error => false

        expect(err).to include("Gems could not be removed. rack (>= 0) would also have been removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
          gem "rack"; gem "rails"
        G
      end
    end

    context "when some gems could not be removed" do
      it "shows warning for gems not removed and success for those removed" do
        install_gemfile <<-G, :raise_on_error => false
          source "#{file_uri_for(gem_repo1)}"
          gem"rack"
          gem"rspec"
          gem "rails"
          gem "minitest"
        G

        bundle "remove rails rack rspec minitest"

        expect(out).to include("rails was removed.")
        expect(out).to include("minitest was removed.")
        expect(out).to include("rack, rspec could not be removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
          gem"rack"
          gem"rspec"
        G
      end
    end
  end

  context "with sources" do
    before do
      build_repo gem_repo3 do
        build_gem "rspec"
      end
    end

    it "removes gems and empty source blocks" do
      gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"

        gem "rack"

        source "#{file_uri_for(gem_repo3)}" do
          gem "rspec"
        end
      G

      bundle "install"

      bundle "remove rspec"

      expect(out).to include("rspec was removed.")
      expect(gemfile).to eq <<~G
        source "#{file_uri_for(gem_repo1)}"

        gem "rack"
      G
    end
  end

  describe "with eval_gemfile" do
    context "when gems are present in both gemfiles" do
      it "removes the gems" do
        create_file "Gemfile-other", <<-G
          gem "rack"
        G

        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"

          gem "rack"
        G

        bundle "remove rack"

        expect(out).to include("rack was removed.")
      end
    end

    context "when gems are present in other gemfile" do
      it "removes the gems" do
        create_file "Gemfile-other", <<-G
          gem "rack"
        G

        install_gemfile <<-G
          source "#{file_uri_for(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.")
      end
    end

    context "when gems to be removed are not specified in any of the gemfiles" do
      it "throws error for the 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_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
        G

        bundle "remove rack", :raise_on_error => false

        expect(err).to include("`rack` is not specified in #{bundled_app_gemfile} so it could not be removed.")
      end
    end

    context "when the gem is present in parent file but not in gemfile specified by eval_gemfile" do
      it "removes the gem" do
        create_file "Gemfile-other", <<-G
          gem "rails"
        G

        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
          gem "rack"
        G

        bundle "remove rack", :raise_on_error => false

        expect(out).to include("rack was removed.")
        expect(err).to include("`rack` is not specified in #{bundled_app("Gemfile-other")} so it could not be removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
        G
      end
    end

    context "when gems can not be removed from other gemfile" do
      it "shows error" do
        create_file "Gemfile-other", <<-G
          gem "rails"; gem "rack"
        G

        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
          gem "rack"
        G

        bundle "remove rack", :raise_on_error => false

        expect(out).to include("rack was removed.")
        expect(err).to include("Gems could not be removed. rails (>= 0) would also have been removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
        G
      end
    end

    context "when gems could not be removed from parent gemfile" do
      it "shows error" do
        create_file "Gemfile-other", <<-G
          gem "rack"
        G

        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
          gem "rails"; gem "rack"
        G

        bundle "remove rack", :raise_on_error => false

        expect(err).to include("Gems could not be removed. rails (>= 0) would also have been removed.")
        expect(bundled_app("Gemfile-other").read).to include("gem \"rack\"")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
          gem "rails"; gem "rack"
        G
      end
    end

    context "when gem present in gemfiles but could not be removed from one from one of them" do
      it "removes gem which can be removed and shows warning for file from which it can not be removed" do
        create_file "Gemfile-other", <<-G
          gem "rack"
        G

        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          eval_gemfile "Gemfile-other"
          gem"rack"
        G

        bundle "remove rack"

        expect(out).to include("rack was removed.")
        expect(bundled_app("Gemfile-other").read).to_not include("gem \"rack\"")
      end
    end
  end

  context "with install_if" do
    it "removes gems inside blocks and empty blocks" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"

        install_if(lambda { false }) do
          gem "rack"
        end
      G

      bundle "remove rack"

      expect(out).to include("rack was removed.")
      expect(gemfile).to eq <<~G
        source "#{file_uri_for(gem_repo1)}"
      G
    end
  end

  context "with env" do
    it "removes gems inside blocks and empty blocks" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"

        env "BUNDLER_TEST" do
          gem "rack"
        end
      G

      bundle "remove rack"

      expect(out).to include("rack was removed.")
      expect(gemfile).to eq <<~G
        source "#{file_uri_for(gem_repo1)}"
      G
    end
  end

  context "with gemspec" do
    it "should not remove the gem" do
      build_lib("foo", :path => tmp.join("foo")) do |s|
        s.write("foo.gemspec", "")
        s.add_dependency "rack"
      end

      install_gemfile(<<-G)
        source "#{file_uri_for(gem_repo1)}"
        gemspec :path => '#{tmp.join("foo")}', :name => 'foo'
      G

      bundle "remove foo"

      expect(out).to include("foo could not be removed.")
    end
  end

  describe "with comments that mention gems" do
    context "when comment is a separate line comment" do
      it "does not remove the line comment" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          # gem "rack" might be used in the future
          gem "rack"
        G

        bundle "remove rack"

        expect(out).to include("rack was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          # gem "rack" might be used in the future
        G
      end
    end

    context "when gem specified for removal has an inline comment" do
      it "removes the inline comment" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"

          gem "rack" # this can be removed
        G

        bundle "remove rack"

        expect(out).to include("rack was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
        G
      end
    end

    context "when gem specified for removal is mentioned in other gem's comment" do
      it "does not remove other gem" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "puma" # implements interface provided by gem "rack"

          gem "rack"
        G

        bundle "remove rack"

        expect(out).to_not include("puma was removed.")
        expect(out).to include("rack was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"
          gem "puma" # implements interface provided by gem "rack"
        G
      end
    end

    context "when gem specified for removal has a comment that mentions other gem" do
      it "does not remove other gem" do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "puma" # implements interface provided by gem "rack"

          gem "rack"
        G

        bundle "remove puma"

        expect(out).to include("puma was removed.")
        expect(out).to_not include("rack was removed.")
        expect(gemfile).to eq <<~G
          source "#{file_uri_for(gem_repo1)}"

          gem "rack"
        G
      end
    end
  end
end