summaryrefslogtreecommitdiff
path: root/spec/bundler/install/gemfile/sources_spec.rb
blob: 26ecb840c735630fbef90cb6abdad55caf9081d8 (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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# frozen_string_literal: true

RSpec.describe "bundle install with gems on multiple sources" do
  # repo1 is built automatically before all of the specs run
  # it contains rack-obama 1.0.0 and rack 0.9.1 & 1.0.0 amongst other gems

  context "without source affinity" do
    before do
      # Oh no! Someone evil is trying to hijack rack :(
      # need this to be broken to check for correct source ordering
      build_repo gem_repo3 do
        build_gem "rack", repo3_rack_version do |s|
          s.write "lib/rack.rb", "RACK = 'FAIL'"
        end
      end
    end

    context "with multiple toplevel sources" do
      let(:repo3_rack_version) { "1.0.0" }

      before do
        gemfile <<-G
          source "https://gem.repo3"
          source "https://gem.repo1"
          gem "rack-obama"
          gem "rack"
        G
      end

      it "warns about ambiguous gems, but installs anyway, prioritizing sources last to first", :bundler => "< 3" do
        bundle :install, :artifice => "compact_index"

        expect(err).to include("Warning: the gem 'rack' was found in multiple sources.")
        expect(err).to include("Installed from: https://gem.repo1")
        expect(the_bundle).to include_gems("rack-obama 1.0.0", "rack 1.0.0", :source => "remote1")
      end

      it "fails", :bundler => "3" do
        bundle :instal, :artifice => "compact_index", :raise_on_error => false
        expect(err).to include("Each source after the first must include a block")
        expect(exitstatus).to eq(4)
      end
    end

    context "when different versions of the same gem are in multiple sources" do
      let(:repo3_rack_version) { "1.2" }

      before do
        gemfile <<-G
          source "https://gem.repo3"
          source "https://gem.repo1"
          gem "rack-obama"
          gem "rack", "1.0.0" # force it to install the working version in repo1
        G
      end

      it "warns about ambiguous gems, but installs anyway", :bundler => "< 3" do
        bundle :install, :artifice => "compact_index"
        expect(err).to include("Warning: the gem 'rack' was found in multiple sources.")
        expect(err).to include("Installed from: https://gem.repo1")
        expect(the_bundle).to include_gems("rack-obama 1.0.0", "rack 1.0.0", :source => "remote1")
      end

      it "fails", :bundler => "3" do
        bundle :install, :artifice => "compact_index", :raise_on_error => false
        expect(err).to include("Each source after the first must include a block")
        expect(exitstatus).to eq(4)
      end
    end
  end

  context "with source affinity" do
    context "with sources given by a block" do
      before do
        # Oh no! Someone evil is trying to hijack rack :(
        # need this to be broken to check for correct source ordering
        build_repo gem_repo3 do
          build_gem "rack", "1.0.0" do |s|
            s.write "lib/rack.rb", "RACK = 'FAIL'"
          end

          build_gem "rack-obama" do |s|
            s.add_dependency "rack"
          end
        end

        gemfile <<-G
          source "https://gem.repo3"
          source "https://gem.repo1" do
            gem "thin" # comes first to test name sorting
            gem "rack"
          end
          gem "rack-obama" # should come from repo3!
        G
      end

      it "installs the gems without any warning" do
        bundle :install, :artifice => "compact_index"
        expect(err).not_to include("Warning")
        expect(the_bundle).to include_gems("rack-obama 1.0.0")
        expect(the_bundle).to include_gems("rack 1.0.0", :source => "remote1")
      end

      it "can cache and deploy" do
        bundle :cache, :artifice => "compact_index"

        expect(bundled_app("vendor/cache/rack-1.0.0.gem")).to exist
        expect(bundled_app("vendor/cache/rack-obama-1.0.gem")).to exist

        bundle "config set --local deployment true"
        bundle :install, :artifice => "compact_index"

        expect(the_bundle).to include_gems("rack-obama 1.0.0", "rack 1.0.0")
      end
    end

    context "with sources set by an option" do
      before do
        # Oh no! Someone evil is trying to hijack rack :(
        # need this to be broken to check for correct source ordering
        build_repo gem_repo3 do
          build_gem "rack", "1.0.0" do |s|
            s.write "lib/rack.rb", "RACK = 'FAIL'"
          end

          build_gem "rack-obama" do |s|
            s.add_dependency "rack"
          end
        end

        install_gemfile <<-G, :artifice => "compact_index"
          source "https://gem.repo3"
          gem "rack-obama" # should come from repo3!
          gem "rack", :source => "https://gem.repo1"
        G
      end

      it "installs the gems without any warning" do
        expect(err).not_to include("Warning")
        expect(the_bundle).to include_gems("rack-obama 1.0.0", "rack 1.0.0")
      end
    end

    context "when a pinned gem has an indirect dependency in the pinned source" do
      before do
        build_repo gem_repo3 do
          build_gem "depends_on_rack", "1.0.1" do |s|
            s.add_dependency "rack"
          end
        end

        # we need a working rack gem in repo3
        update_repo gem_repo3 do
          build_gem "rack", "1.0.0"
        end

        gemfile <<-G
          source "https://gem.repo2"
          source "https://gem.repo3" do
            gem "depends_on_rack"
          end
        G
      end

      context "and not in any other sources" do
        before do
          build_repo(gem_repo2) {}
        end

        it "installs from the same source without any warning" do
          bundle :install, :artifice => "compact_index"
          expect(err).not_to include("Warning")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote3")
        end
      end

      context "and in another source" do
        before do
          # need this to be broken to check for correct source ordering
          build_repo gem_repo2 do
            build_gem "rack", "1.0.0" do |s|
              s.write "lib/rack.rb", "RACK = 'FAIL'"
            end
          end
        end

        it "installs from the same source without any warning" do
          bundle :install, :artifice => "compact_index"

          expect(err).not_to include("Warning: the gem 'rack' was found in multiple sources.")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote3")

          # In https://github.com/bundler/bundler/issues/3585 this failed
          # when there is already a lock file, and the gems are missing, so try again
          system_gems []
          bundle :install, :artifice => "compact_index"

          expect(err).not_to include("Warning: the gem 'rack' was found in multiple sources.")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote3")
        end
      end
    end

    context "when a pinned gem has an indirect dependency in a different source" do
      before do
        # In these tests, we need a working rack gem in repo2 and not repo3

        build_repo gem_repo3 do
          build_gem "depends_on_rack", "1.0.1" do |s|
            s.add_dependency "rack"
          end
        end

        build_repo gem_repo2 do
          build_gem "rack", "1.0.0"
        end
      end

      context "and not in any other sources" do
        before do
          install_gemfile <<-G, :artifice => "compact_index"
            source "https://gem.repo2"
            source "https://gem.repo3" do
              gem "depends_on_rack"
            end
          G
        end

        it "installs from the other source without any warning" do
          expect(err).not_to include("Warning")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0")
        end
      end

      context "and in yet another source" do
        before do
          gemfile <<-G
            source "https://gem.repo1"
            source "https://gem.repo2"
            source "https://gem.repo3" do
              gem "depends_on_rack"
            end
          G
        end

        it "installs from the other source and warns about ambiguous gems", :bundler => "< 3" do
          bundle :install, :artifice => "compact_index"
          expect(err).to include("Warning: the gem 'rack' was found in multiple sources.")
          expect(err).to include("Installed from: https://gem.repo2")

          expect(lockfile).to eq <<~L
            GEM
              remote: https://gem.repo1/
              remote: https://gem.repo2/
              specs:
                rack (1.0.0)

            GEM
              remote: https://gem.repo3/
              specs:
                depends_on_rack (1.0.1)
                  rack

            PLATFORMS
              #{specific_local_platform}

            DEPENDENCIES
              depends_on_rack!

            BUNDLED WITH
               #{Bundler::VERSION}
          L

          previous_lockfile = lockfile
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0")
          expect(lockfile).to eq(previous_lockfile)
        end

        it "fails", :bundler => "3" do
          bundle :install, :artifice => "compact_index", :raise_on_error => false
          expect(err).to include("Each source after the first must include a block")
          expect(exitstatus).to eq(4)
        end
      end

      context "and only the dependency is pinned" do
        before do
          # need this to be broken to check for correct source ordering
          build_repo gem_repo2 do
            build_gem "rack", "1.0.0" do |s|
              s.write "lib/rack.rb", "RACK = 'FAIL'"
            end
          end

          gemfile <<-G
            source "https://gem.repo3" # contains depends_on_rack
            source "https://gem.repo2" # contains broken rack

            gem "depends_on_rack" # installed from gem_repo3
            gem "rack", :source => "https://gem.repo1"
          G
        end

        it "installs the dependency from the pinned source without warning", :bundler => "< 3" do
          bundle :install, :artifice => "compact_index"

          expect(err).not_to include("Warning: the gem 'rack' was found in multiple sources.")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0")

          # In https://github.com/rubygems/bundler/issues/3585 this failed
          # when there is already a lock file, and the gems are missing, so try again
          system_gems []
          bundle :install, :artifice => "compact_index"

          expect(err).not_to include("Warning: the gem 'rack' was found in multiple sources.")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0")
        end

        it "fails", :bundler => "3" do
          bundle :install, :artifice => "compact_index", :raise_on_error => false
          expect(err).to include("Each source after the first must include a block")
          expect(exitstatus).to eq(4)
        end
      end
    end

    context "when a top-level gem can only be found in an scoped source" do
      before do
        build_repo2

        build_repo gem_repo3 do
          build_gem "private_gem_1", "1.0.0"
          build_gem "private_gem_2", "1.0.0"
        end

        gemfile <<-G
          source "https://gem.repo2"

          gem "private_gem_1"

          source "https://gem.repo3" do
            gem "private_gem_2"
          end
        G
      end

      it "fails" do
        bundle :install, :artifice => "compact_index", :raise_on_error => false
        expect(err).to include("Could not find gem 'private_gem_1' in rubygems repository https://gem.repo2/ or installed locally.")
      end
    end

    context "when an indirect dependency can't be found in the aggregate rubygems source", :bundler => "< 3" do
      before do
        build_repo2

        build_repo gem_repo3 do
          build_gem "depends_on_missing", "1.0.1" do |s|
            s.add_dependency "missing"
          end
        end

        gemfile <<-G
          source "https://gem.repo2"

          source "https://gem.repo3"

          gem "depends_on_missing"
        G
      end

      it "fails" do
        bundle :install, :artifice => "compact_index", :raise_on_error => false
        expect(err).to include("Could not find gem 'missing', which is required by gem 'depends_on_missing', in any of the sources.")
      end
    end

    context "when a top-level gem has an indirect dependency" do
      before do
        build_repo gem_repo2 do
          build_gem "depends_on_rack", "1.0.1" do |s|
            s.add_dependency "rack"
          end
        end

        build_repo gem_repo3 do
          build_gem "unrelated_gem", "1.0.0"
        end

        gemfile <<-G
          source "https://gem.repo2"

          gem "depends_on_rack"

          source "https://gem.repo3" do
            gem "unrelated_gem"
          end
        G
      end

      context "and the dependency is only in the top-level source" do
        before do
          update_repo gem_repo2 do
            build_gem "rack", "1.0.0"
          end
        end

        it "installs the dependency from the top-level source without warning" do
          bundle :install, :artifice => "compact_index"
          expect(err).not_to include("Warning")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", "unrelated_gem 1.0.0")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote2")
          expect(the_bundle).to include_gems("unrelated_gem 1.0.0", :source => "remote3")
        end
      end

      context "and the dependency is only in a pinned source" do
        before do
          update_repo gem_repo3 do
            build_gem "rack", "1.0.0" do |s|
              s.write "lib/rack.rb", "RACK = 'FAIL'"
            end
          end
        end

        it "does not find the dependency" do
          bundle :install, :artifice => "compact_index", :raise_on_error => false
          expect(err).to include(
            "Could not find gem 'rack', which is required by gem 'depends_on_rack', in rubygems repository https://gem.repo2/ or installed locally."
          )
        end
      end

      context "and the dependency is in both the top-level and a pinned source" do
        before do
          update_repo gem_repo2 do
            build_gem "rack", "1.0.0"
          end

          update_repo gem_repo3 do
            build_gem "rack", "1.0.0" do |s|
              s.write "lib/rack.rb", "RACK = 'FAIL'"
            end
          end
        end

        it "installs the dependency from the top-level source without warning" do
          bundle :install, :artifice => "compact_index"
          expect(err).not_to include("Warning")
          expect(run("require 'rack'; puts RACK")).to eq("1.0.0")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", "unrelated_gem 1.0.0")
          expect(the_bundle).to include_gems("depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote2")
          expect(the_bundle).to include_gems("unrelated_gem 1.0.0", :source => "remote3")
        end
      end
    end

    context "when a scoped gem has a deeply nested indirect dependency" do
      before do
        build_repo gem_repo3 do
          build_gem "depends_on_depends_on_rack", "1.0.1" do |s|
            s.add_dependency "depends_on_rack"
          end

          build_gem "depends_on_rack", "1.0.1" do |s|
            s.add_dependency "rack"
          end
        end

        gemfile <<-G
          source "https://gem.repo2"

          source "https://gem.repo3" do
            gem "depends_on_depends_on_rack"
          end
        G
      end

      context "and the dependency is only in the top-level source" do
        before do
          update_repo gem_repo2 do
            build_gem "rack", "1.0.0"
          end
        end

        it "installs the dependency from the top-level source" do
          bundle :install, :artifice => "compact_index"
          expect(the_bundle).to include_gems("depends_on_depends_on_rack 1.0.1", "depends_on_rack 1.0.1", "rack 1.0.0")
          expect(the_bundle).to include_gems("rack 1.0.0", :source => "remote2")
          expect(the_bundle).to include_gems("depends_on_depends_on_rack 1.0.1", "depends_on_rack 1.0.1", :source => "remote3")
        end
      end

      context "and the dependency is only in a pinned source" do
        before do
          build_repo2

          update_repo gem_repo3 do
            build_gem "rack", "1.0.0"
          end
        end

        it "installs the dependency from the pinned source" do
          bundle :install, :artifice => "compact_index"
          expect(the_bundle).to include_gems("depends_on_depends_on_rack 1.0.1", "depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote3")
        end
      end

      context "and the dependency is in both the top-level and a pinned source" do
        before do
          update_repo gem_repo2 do
            build_gem "rack", "1.0.0" do |s|
              s.write "lib/rack.rb", "RACK = 'FAIL'"
            end
          end

          update_repo gem_repo3 do
            build_gem "rack", "1.0.0"
          end
        end

        it "installs the dependency from the pinned source without warning" do
          bundle :install, :artifice => "compact_index"
          expect(the_bundle).to include_gems("depends_on_depends_on_rack 1.0.1", "depends_on_rack 1.0.1", "rack 1.0.0", :source => "remote3")
        end
      end
    end

    context "when the lockfile has aggregated rubygems sources and newer versions of dependencies are available" do
      before do
        build_repo gem_repo2 do
          build_gem "activesupport", "6.0.3.4" do |s|
            s.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2"
            s.add_dependency "i18n", ">= 0.7", "< 2"
            s.add_dependency "minitest", "~> 5.1"
            s.add_dependency "tzinfo", "~> 1.1"
            s.add_dependency "zeitwerk", "~> 2.2", ">= 2.2.2"
          end

          build_gem "activesupport", "6.1.2.1" do |s|
            s.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2"
            s.add_dependency "i18n", ">= 1.6", "< 2"
            s.add_dependency "minitest", ">= 5.1"
            s.add_dependency "tzinfo", "~> 2.0"
            s.add_dependency "zeitwerk", "~> 2.3"
          end

          build_gem "concurrent-ruby", "1.1.8"
          build_gem "concurrent-ruby", "1.1.9"
          build_gem "connection_pool", "2.2.3"

          build_gem "i18n", "1.8.9" do |s|
            s.add_dependency "concurrent-ruby", "~> 1.0"
          end

          build_gem "minitest", "5.14.3"
          build_gem "rack", "2.2.3"
          build_gem "redis", "4.2.5"

          build_gem "sidekiq", "6.1.3" do |s|
            s.add_dependency "connection_pool", ">= 2.2.2"
            s.add_dependency "rack", "~> 2.0"
            s.add_dependency "redis", ">= 4.2.0"
          end

          build_gem "thread_safe", "0.3.6"

          build_gem "tzinfo", "1.2.9" do |s|
            s.add_dependency "thread_safe", "~> 0.1"
          end

          build_gem "tzinfo", "2.0.4" do |s|
            s.add_dependency "concurrent-ruby", "~> 1.0"
          end

          build_gem "zeitwerk", "2.4.2"
        end

        build_repo gem_repo3 do
          build_gem "sidekiq-pro", "5.2.1" do |s|
            s.add_dependency "connection_pool", ">= 2.2.3"
            s.add_dependency "sidekiq", ">= 6.1.0"
          end
        end

        gemfile <<-G
          # frozen_string_literal: true

          source "https://gem.repo2"

          gem "activesupport"

          source "https://gem.repo3" do
            gem "sidekiq-pro"
          end
        G

        lockfile <<~L
          GEM
            remote: https://gem.repo2/
            remote: https://gem.repo3/
            specs:
              activesupport (6.0.3.4)
                concurrent-ruby (~> 1.0, >= 1.0.2)
                i18n (>= 0.7, < 2)
                minitest (~> 5.1)
                tzinfo (~> 1.1)
                zeitwerk (~> 2.2, >= 2.2.2)
              concurrent-ruby (1.1.8)
              connection_pool (2.2.3)
              i18n (1.8.9)
                concurrent-ruby (~> 1.0)
              minitest (5.14.3)
              rack (2.2.3)
              redis (4.2.5)
              sidekiq (6.1.3)
                connection_pool (>= 2.2.2)
                rack (~> 2.0)
                redis (>= 4.2.0)
              sidekiq-pro (5.2.1)
                connection_pool (>= 2.2.3)
                sidekiq (>= 6.1.0)
              thread_safe (0.3.6)
              tzinfo (1.2.9)
                thread_safe (~> 0.1)
              zeitwerk (2.4.2)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            activesupport
            sidekiq-pro!

          BUNDLED WITH
             #{Bundler::VERSION}
        L
      end

      it "does not install newer versions but updates the lockfile format when running bundle install in non frozen mode, and doesn't warn" do
        bundle :install, :artifice => "compact_index"
        expect(err).to be_empty

        expect(the_bundle).to include_gems("activesupport 6.0.3.4")
        expect(the_bundle).not_to include_gems("activesupport 6.1.2.1")
        expect(the_bundle).to include_gems("tzinfo 1.2.9")
        expect(the_bundle).not_to include_gems("tzinfo 2.0.4")
        expect(the_bundle).to include_gems("concurrent-ruby 1.1.8")
        expect(the_bundle).not_to include_gems("concurrent-ruby 1.1.9")

        expect(lockfile).to eq <<~L
          GEM
            remote: https://gem.repo2/
            specs:
              activesupport (6.0.3.4)
                concurrent-ruby (~> 1.0, >= 1.0.2)
                i18n (>= 0.7, < 2)
                minitest (~> 5.1)
                tzinfo (~> 1.1)
                zeitwerk (~> 2.2, >= 2.2.2)
              concurrent-ruby (1.1.8)
              connection_pool (2.2.3)
              i18n (1.8.9)
                concurrent-ruby (~> 1.0)
              minitest (5.14.3)
              rack (2.2.3)
              redis (4.2.5)
              sidekiq (6.1.3)
                connection_pool (>= 2.2.2)
                rack (~> 2.0)
                redis (>= 4.2.0)
              thread_safe (0.3.6)
              tzinfo (1.2.9)
                thread_safe (~> 0.1)
              zeitwerk (2.4.2)

          GEM
            remote: https://gem.repo3/
            specs:
              sidekiq-pro (5.2.1)
                connection_pool (>= 2.2.3)
                sidekiq (>= 6.1.0)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            activesupport
            sidekiq-pro!

          BUNDLED WITH
             #{Bundler::VERSION}
        L
      end

      it "does not install newer versions or generate lockfile changes when running bundle install in frozen mode, and warns", :bundler => "< 3" do
        initial_lockfile = lockfile

        bundle "config set --local frozen true"
        bundle :install, :artifice => "compact_index"

        expect(err).to include("Your lockfile contains a single rubygems source section with multiple remotes, which is insecure.")

        expect(the_bundle).to include_gems("activesupport 6.0.3.4")
        expect(the_bundle).not_to include_gems("activesupport 6.1.2.1")
        expect(the_bundle).to include_gems("tzinfo 1.2.9")
        expect(the_bundle).not_to include_gems("tzinfo 2.0.4")
        expect(the_bundle).to include_gems("concurrent-ruby 1.1.8")
        expect(the_bundle).not_to include_gems("concurrent-ruby 1.1.9")

        expect(lockfile).to eq(initial_lockfile)
      end

      it "fails when running bundle install in frozen mode", :bundler => "3" do
        initial_lockfile = lockfile

        bundle "config set --local frozen true"
        bundle :install, :artifice => "compact_index", :raise_on_error => false

        expect(err).to include("Your lockfile contains a single rubygems source section with multiple remotes, which is insecure.")

        expect(lockfile).to eq(initial_lockfile)
      end

      it "splits sections and upgrades gems when running bundle update, and doesn't warn" do
        bundle "update --all", :artifice => "compact_index"
        expect(err).to be_empty

        expect(the_bundle).not_to include_gems("activesupport 6.0.3.4")
        expect(the_bundle).to include_gems("activesupport 6.1.2.1")
        expect(the_bundle).not_to include_gems("tzinfo 1.2.9")
        expect(the_bundle).to include_gems("tzinfo 2.0.4")
        expect(the_bundle).not_to include_gems("concurrent-ruby 1.1.8")
        expect(the_bundle).to include_gems("concurrent-ruby 1.1.9")

        expect(lockfile).to eq <<~L
          GEM
            remote: https://gem.repo2/
            specs:
              activesupport (6.1.2.1)
                concurrent-ruby (~> 1.0, >= 1.0.2)
                i18n (>= 1.6, < 2)
                minitest (>= 5.1)
                tzinfo (~> 2.0)
                zeitwerk (~> 2.3)
              concurrent-ruby (1.1.9)
              connection_pool (2.2.3)
              i18n (1.8.9)
                concurrent-ruby (~> 1.0)
              minitest (5.14.3)
              rack (2.2.3)
              redis (4.2.5)
              sidekiq (6.1.3)
                connection_pool (>= 2.2.2)
                rack (~> 2.0)
                redis (>= 4.2.0)
              tzinfo (2.0.4)
                concurrent-ruby (~> 1.0)
              zeitwerk (2.4.2)

          GEM
            remote: https://gem.repo3/
            specs:
              sidekiq-pro (5.2.1)
                connection_pool (>= 2.2.3)
                sidekiq (>= 6.1.0)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            activesupport
            sidekiq-pro!

          BUNDLED WITH
             #{Bundler::VERSION}
        L
      end

      it "upgrades the lockfile format and upgrades the requested gem when running bundle update with an argument" do
        bundle "update concurrent-ruby", :artifice => "compact_index"
        expect(err).to be_empty

        expect(the_bundle).to include_gems("activesupport 6.0.3.4")
        expect(the_bundle).not_to include_gems("activesupport 6.1.2.1")
        expect(the_bundle).to include_gems("tzinfo 1.2.9")
        expect(the_bundle).not_to include_gems("tzinfo 2.0.4")
        expect(the_bundle).to include_gems("concurrent-ruby 1.1.9")
        expect(the_bundle).not_to include_gems("concurrent-ruby 1.1.8")

        expect(lockfile).to eq <<~L
          GEM
            remote: https://gem.repo2/
            specs:
              activesupport (6.0.3.4)
                concurrent-ruby (~> 1.0, >= 1.0.2)
                i18n (>= 0.7, < 2)
                minitest (~> 5.1)
                tzinfo (~> 1.1)
                zeitwerk (~> 2.2, >= 2.2.2)
              concurrent-ruby (1.1.9)
              connection_pool (2.2.3)
              i18n (1.8.9)
                concurrent-ruby (~> 1.0)
              minitest (5.14.3)
              rack (2.2.3)
              redis (4.2.5)
              sidekiq (6.1.3)
                connection_pool (>= 2.2.2)
                rack (~> 2.0)
                redis (>= 4.2.0)
              thread_safe (0.3.6)
              tzinfo (1.2.9)
                thread_safe (~> 0.1)
              zeitwerk (2.4.2)

          GEM
            remote: https://gem.repo3/
            specs:
              sidekiq-pro (5.2.1)
                connection_pool (>= 2.2.3)
                sidekiq (>= 6.1.0)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            activesupport
            sidekiq-pro!

          BUNDLED WITH
             #{Bundler::VERSION}
        L
      end
    end

    context "when a top-level gem has an indirect dependency present in the default source, but with a different version from the one resolved" do
      before do
        build_lib "activesupport", "7.0.0.alpha", :path => lib_path("rails/activesupport")
        build_lib "rails", "7.0.0.alpha", :path => lib_path("rails") do |s|
          s.add_dependency "activesupport", "= 7.0.0.alpha"
        end

        build_repo gem_repo2 do
          build_gem "activesupport", "6.1.2"

          build_gem "webpacker", "5.2.1" do |s|
            s.add_dependency "activesupport", ">= 5.2"
          end
        end

        gemfile <<-G
          source "https://gem.repo2"

          gemspec :path => "#{lib_path("rails")}"

          gem "webpacker", "~> 5.0"
        G
      end

      it "installs all gems without warning" do
        bundle :install, :artifice => "compact_index"
        expect(err).not_to include("Warning")
        expect(the_bundle).to include_gems("activesupport 7.0.0.alpha", "rails 7.0.0.alpha")
        expect(the_bundle).to include_gems("activesupport 7.0.0.alpha", :source => "path@#{lib_path("rails/activesupport")}")
        expect(the_bundle).to include_gems("rails 7.0.0.alpha", :source => "path@#{lib_path("rails")}")
      end
    end

    context "when a pinned gem has an indirect dependency with more than one level of indirection in the default source " do
      before do
        build_repo gem_repo3 do
          build_gem "handsoap", "0.2.5.5" do |s|
            s.add_dependency "nokogiri", ">= 1.2.3"
          end
        end

        update_repo gem_repo2 do
          build_gem "nokogiri", "1.11.1" do |s|
            s.add_dependency "racca", "~> 1.4"
          end

          build_gem "racca", "1.5.2"
        end

        gemfile <<-G
          source "https://gem.repo2"

          source "https://gem.repo3" do
            gem "handsoap"
          end

          gem "nokogiri"
        G
      end

      it "installs from the default source without any warnings or errors and generates a proper lockfile" do
        expected_lockfile = <<~L
          GEM
            remote: https://gem.repo2/
            specs:
              nokogiri (1.11.1)
                racca (~> 1.4)
              racca (1.5.2)

          GEM
            remote: https://gem.repo3/
            specs:
              handsoap (0.2.5.5)
                nokogiri (>= 1.2.3)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            handsoap!
            nokogiri

          BUNDLED WITH
             #{Bundler::VERSION}
        L

        bundle "install --verbose", :artifice => "compact_index"
        expect(err).not_to include("Warning")
        expect(the_bundle).to include_gems("handsoap 0.2.5.5", "nokogiri 1.11.1", "racca 1.5.2")
        expect(the_bundle).to include_gems("handsoap 0.2.5.5", :source => "remote3")
        expect(the_bundle).to include_gems("nokogiri 1.11.1", "racca 1.5.2", :source => "remote2")
        expect(lockfile).to eq(expected_lockfile)

        # Even if the gems are already installed
        FileUtils.rm bundled_app_lock
        bundle "install --verbose", :artifice => "compact_index"
        expect(err).not_to include("Warning")
        expect(the_bundle).to include_gems("handsoap 0.2.5.5", "nokogiri 1.11.1", "racca 1.5.2")
        expect(the_bundle).to include_gems("handsoap 0.2.5.5", :source => "remote3")
        expect(the_bundle).to include_gems("nokogiri 1.11.1", "racca 1.5.2", :source => "remote2")
        expect(lockfile).to eq(expected_lockfile)
      end
    end

    context "with a gem that is only found in the wrong source" do
      before do
        build_repo gem_repo3 do
          build_gem "not_in_repo1", "1.0.0"
        end

        install_gemfile <<-G, :artifice => "compact_index", :raise_on_error => false
          source "https://gem.repo3"
          gem "not_in_repo1", :source => "https://gem.repo1"
        G
      end

      it "does not install the gem" do
        expect(err).to include("Could not find gem 'not_in_repo1'")
      end
    end

    context "with an existing lockfile" do
      before do
        system_gems "rack-0.9.1", "rack-1.0.0", :path => default_bundle_path

        lockfile <<-L
          GEM
            remote: https://gem.repo1
            specs:

          GEM
            remote: https://gem.repo3
            specs:
              rack (0.9.1)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            rack!
        L

        gemfile <<-G
          source "https://gem.repo1"
          source "https://gem.repo3" do
            gem 'rack'
          end
        G
      end

      # Reproduction of https://github.com/rubygems/bundler/issues/3298
      it "does not unlock the installed gem on exec" do
        expect(the_bundle).to include_gems("rack 0.9.1")
      end
    end

    context "with a lockfile with aggregated rubygems sources" do
      let(:aggregate_gem_section_lockfile) do
        <<~L
          GEM
            remote: https://gem.repo1/
            remote: https://gem.repo3/
            specs:
              rack (0.9.1)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            rack!

          BUNDLED WITH
             #{Bundler::VERSION}
        L
      end

      let(:split_gem_section_lockfile) do
        <<~L
          GEM
            remote: https://gem.repo1/
            specs:

          GEM
            remote: https://gem.repo3/
            specs:
              rack (0.9.1)

          PLATFORMS
            #{specific_local_platform}

          DEPENDENCIES
            rack!

          BUNDLED WITH
             #{Bundler::VERSION}
        L
      end

      before do
        build_repo gem_repo3 do
          build_gem "rack", "0.9.1"
        end

        gemfile <<-G
          source "https://gem.repo1"
          source "https://gem.repo3" do
            gem 'rack'
          end
        G

        lockfile aggregate_gem_section_lockfile
      end

      it "installs the existing lockfile but prints a warning", :bundler => "< 3" do
        bundle "config set --local deployment true"

        bundle "install", :artifice => "compact_index"

        expect(lockfile).to eq(aggregate_gem_section_lockfile)
        expect(err).to include("Your lockfile contains a single rubygems source section with multiple remotes, which is insecure.")
        expect(the_bundle).to include_gems("rack 0.9.1", :source => "remote3")
      end

      it "refuses to install the existing lockfile and prints an error", :bundler => "3" do
        bundle "config set --local deployment true"

        bundle "install", :artifice => "compact_index", :raise_on_error =>false

        expect(lockfile).to eq(aggregate_gem_section_lockfile)
        expect(err).to include("Your lockfile contains a single rubygems source section with multiple remotes, which is insecure.")
        expect(out).to be_empty
      end
    end

    context "with a path gem in the same Gemfile" do
      before do
        build_lib "foo"

        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "rack", :source => "https://gem.repo1"
          gem "foo", :path => "#{lib_path("foo-1.0")}"
        G
      end

      it "does not unlock the non-path gem after install" do
        bundle :install, :artifice => "compact_index"

        bundle %(exec ruby -e 'puts "OK"')

        expect(out).to include("OK")
      end
    end
  end

  context "when an older version of the same gem also ships with Ruby" do
    before do
      system_gems "rack-0.9.1"

      install_gemfile <<-G, :artifice => "compact_index"
        source "https://gem.repo1"
        gem "rack" # should come from repo1!
      G
    end

    it "installs the gems without any warning" do
      expect(err).not_to include("Warning")
      expect(the_bundle).to include_gems("rack 1.0.0")
    end
  end

  context "when a single source contains multiple locked gems" do
    before do
      # With these gems,
      build_repo4 do
        build_gem "foo", "0.1"
        build_gem "bar", "0.1"
      end

      # Installing this gemfile...
      gemfile <<-G
        source 'https://gem.repo1'
        gem 'rack'
        gem 'foo', '~> 0.1', :source => 'https://gem.repo4'
        gem 'bar', '~> 0.1', :source => 'https://gem.repo4'
      G

      bundle "config set --local path ../gems/system"
      bundle :install, :artifice => "compact_index"

      # And then we add some new versions...
      update_repo4 do
        build_gem "foo", "0.2"
        build_gem "bar", "0.3"
      end
    end

    it "allows them to be unlocked separately" do
      # And install this gemfile, updating only foo.
      install_gemfile <<-G, :artifice => "compact_index"
        source 'https://gem.repo1'
        gem 'rack'
        gem 'foo', '~> 0.2', :source => 'https://gem.repo4'
        gem 'bar', '~> 0.1', :source => 'https://gem.repo4'
      G

      # It should update foo to 0.2, but not the (locked) bar 0.1
      expect(the_bundle).to include_gems("foo 0.2", "bar 0.1")
    end
  end

  context "re-resolving" do
    context "when there is a mix of sources in the gemfile" do
      before do
        build_repo gem_repo3 do
          build_gem "rack"
        end

        build_lib "path1"
        build_lib "path2"
        build_git "git1"
        build_git "git2"

        install_gemfile <<-G, :artifice => "compact_index"
          source "https://gem.repo1"
          gem "rails"

          source "https://gem.repo3" do
            gem "rack"
          end

          gem "path1", :path => "#{lib_path("path1-1.0")}"
          gem "path2", :path => "#{lib_path("path2-1.0")}"
          gem "git1",  :git  => "#{lib_path("git1-1.0")}"
          gem "git2",  :git  => "#{lib_path("git2-1.0")}"
        G
      end

      it "does not re-resolve" do
        bundle :install, :artifice => "compact_index", :verbose => true
        expect(out).to include("using resolution from the lockfile")
        expect(out).not_to include("re-resolving dependencies")
      end
    end
  end

  context "when a gem is installed to system gems" do
    before do
      install_gemfile <<-G, :artifice => "compact_index"
        source "https://gem.repo1"
        gem "rack"
      G
    end

    context "and the gemfile changes" do
      it "is still able to find that gem from remote sources" do
        build_repo4 do
          build_gem "rack", "2.0.1.1.forked"
          build_gem "thor", "0.19.1.1.forked"
        end

        # When this gemfile is installed...
        install_gemfile <<-G, :artifice => "compact_index"
          source "https://gem.repo1"

          source "https://gem.repo4" do
            gem "rack", "2.0.1.1.forked"
            gem "thor"
          end
          gem "rack-obama"
        G

        # Then we change the Gemfile by adding a version to thor
        gemfile <<-G
          source "https://gem.repo1"

          source "https://gem.repo4" do
            gem "rack", "2.0.1.1.forked"
            gem "thor", "0.19.1.1.forked"
          end
          gem "rack-obama"
        G

        # But we should still be able to find rack 2.0.1.1.forked and install it
        bundle :install, :artifice => "compact_index"
      end
    end
  end

  describe "source changed to one containing a higher version of a dependency" do
    before do
      install_gemfile <<-G, :artifice => "compact_index"
        source "https://gem.repo1"

        gem "rack"
      G

      build_repo2 do
        build_gem "rack", "1.2" do |s|
          s.executables = "rackup"
        end

        build_gem "bar"
      end

      build_lib("gemspec_test", :path => tmp.join("gemspec_test")) do |s|
        s.add_dependency "bar", "=1.0.0"
      end

      install_gemfile <<-G, :artifice => "compact_index"
        source "https://gem.repo2"
        gem "rack"
        gemspec :path => "#{tmp.join("gemspec_test")}"
      G
    end

    it "conservatively installs the existing locked version" do
      expect(the_bundle).to include_gems("rack 1.0.0")
    end
  end

  it "doesn't update version when a gem uses a source block but a higher version from another source is already installed locally" do
    build_repo2 do
      build_gem "example", "0.1.0"
    end

    build_repo4 do
      build_gem "example", "1.0.2"
    end

    install_gemfile <<-G, :artifice => "compact_index"
      source "https://gem.repo4"

      gem "example", :source => "https://gem.repo2"
    G

    bundle "info example"
    expect(out).to include("example (0.1.0)")

    system_gems "example-1.0.2", :path => default_bundle_path, :gem_repo => gem_repo4

    bundle "update example --verbose", :artifice => "compact_index"
    expect(out).not_to include("Using example 1.0.2")
    expect(out).to include("Using example 0.1.0")
  end

  it "fails inmmediately with a helpful error when a rubygems source does not exist and bundler/setup is required" do
    gemfile <<-G
      source "https://gem.repo1"

      source "https://gem.repo4" do
        gem "example"
      end
    G

    simulate_bundler_version_when_missing_prerelease_default_gem_activation do
      ruby <<~R, :raise_on_error => false
        require 'bundler/setup'
      R
    end

    expect(last_command).to be_failure
    expect(err).to include("Could not find gem 'example' in locally installed gems.")
  end

  it "fails inmmediately with a helpful error when a non retriable network error happens while resolving sources" do
    gemfile <<-G
      source "https://gem.repo1"

      source "https://gem.repo4" do
        gem "example"
      end
    G

    bundle "install", :artifice => nil, :raise_on_error => false

    expect(last_command).to be_failure
    expect(err).to include("Could not reach host gem.repo4. Check your network connection and try again.")
  end

  context "when an indirect dependency is available from multiple ambiguous sources", :bundler => "< 3" do
    it "succeeds but warns, suggesting a source block" do
      build_repo4 do
        build_gem "depends_on_rack" do |s|
          s.add_dependency "rack"
        end
        build_gem "rack"
      end

      install_gemfile <<-G, :artifice => "compact_index", :raise_on_error => false
        source "#{file_uri_for(gem_repo1)}"

        source "https://gem.repo4" do
          gem "depends_on_rack"
        end

        source "https://gem.repo1" do
          gem "thin"
        end
      G
      expect(err).to eq strip_whitespace(<<-EOS).strip
        Warning: The gem 'rack' was found in multiple relevant sources.
          * rubygems repository https://gem.repo1/
          * rubygems repository https://gem.repo4/
        You should add this gem to the source block for the source you wish it to be installed from.
      EOS
      expect(last_command).to be_success
      expect(the_bundle).to be_locked
    end
  end

  context "when an indirect dependency is available from multiple ambiguous sources", :bundler => "3" do
    it "raises, suggesting a source block" do
      build_repo4 do
        build_gem "depends_on_rack" do |s|
          s.add_dependency "rack"
        end
        build_gem "rack"
      end

      install_gemfile <<-G, :artifice => "compact_index", :raise_on_error => false
        source "#{file_uri_for(gem_repo1)}"
        source "https://gem.repo4" do
          gem "depends_on_rack"
        end
        source "https://gem.repo1" do
          gem "thin"
        end
      G
      expect(last_command).to be_failure
      expect(err).to eq strip_whitespace(<<-EOS).strip
        The gem 'rack' was found in multiple relevant sources.
          * rubygems repository https://gem.repo1/
          * rubygems repository https://gem.repo4/
        You must add this gem to the source block for the source you wish it to be installed from.
      EOS
      expect(the_bundle).not_to be_locked
    end
  end

  context "when upgrading a lockfile suffering from dependency confusion" do
    before do
      build_repo4 do
        build_gem "mime-types", "3.0.0"
      end

      build_repo2 do
        build_gem "capybara", "2.5.0" do |s|
          s.add_dependency "mime-types", ">= 1.16"
        end

        build_gem "mime-types", "3.3.1"
      end

      gemfile <<~G
        source "https://gem.repo2"

        gem "capybara", "~> 2.5.0"

        source "https://gem.repo4" do
          gem "mime-types", "~> 3.0"
        end
      G

      lockfile <<-L
        GEM
          remote: https://gem.repo2/
          remote: https://gem.repo4/
          specs:
            capybara (2.5.0)
              mime-types (>= 1.16)
            mime-types (3.3.1)

        PLATFORMS
          #{specific_local_platform}

        DEPENDENCIES
          capybara (~> 2.5.0)
          mime-types (~> 3.0)!
      L
    end

    it "upgrades the lockfile correctly" do
      bundle "lock --update", :artifice => "compact_index"

      expect(lockfile).to eq <<~L
        GEM
          remote: https://gem.repo2/
          specs:
            capybara (2.5.0)
              mime-types (>= 1.16)

        GEM
          remote: https://gem.repo4/
          specs:
            mime-types (3.0.0)

        PLATFORMS
          #{specific_local_platform}

        DEPENDENCIES
          capybara (~> 2.5.0)
          mime-types (~> 3.0)!

        BUNDLED WITH
           #{Bundler::VERSION}
      L
    end
  end

  context "when default source includes old gems with nil required_rubygems_version" do
    before do
      build_repo2 do
        build_gem "ruport", "1.7.0.3" do |s|
          s.add_dependency "pdf-writer", "1.1.8"
        end
      end

      build_repo gem_repo4 do
        build_gem "pdf-writer", "1.1.8"
      end

      path = "#{gem_repo4}/#{Gem::MARSHAL_SPEC_DIR}/pdf-writer-1.1.8.gemspec.rz"
      spec = Marshal.load(Bundler.rubygems.inflate(File.binread(path)))
      spec.instance_variable_set(:@required_rubygems_version, nil)
      File.open(path, "wb") do |f|
        f.write Gem.deflate(Marshal.dump(spec))
      end

      gemfile <<~G
        source "https://localgemserver.test"

        gem "ruport", "= 1.7.0.3", :source => "https://localgemserver.test/extra"
      G
    end

    it "handles that fine" do
      bundle "install", :artifice => "compact_index_extra", :env => { "BUNDLER_SPEC_GEM_REPO" => gem_repo4.to_s }

      expect(lockfile).to eq <<~L
        GEM
          remote: https://localgemserver.test/
          specs:
            pdf-writer (1.1.8)

        GEM
          remote: https://localgemserver.test/extra/
          specs:
            ruport (1.7.0.3)
              pdf-writer (= 1.1.8)

        PLATFORMS
          #{specific_local_platform}

        DEPENDENCIES
          ruport (= 1.7.0.3)!

        BUNDLED WITH
           #{Bundler::VERSION}
      L
    end
  end
end