summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_installer.rb
blob: 988f14146d7e091f866ee99df6c326c76d740302 (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
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
# frozen_string_literal: true
require_relative 'installer_test_case'

class TestGemInstaller < Gem::InstallerTestCase
  def setup
    super
    common_installer_setup

    @config = Gem.configuration
  end

  def teardown
    common_installer_teardown

    super

    Gem.configuration = instance_variable_defined?(:@config) ? @config : nil
  end

  def test_app_script_text
    installer = setup_base_installer

    util_make_exec @spec, ''

    expected = <<-EOF
#!#{Gem.ruby}
#
# This file was generated by RubyGems.
#
# The application 'a' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

Gem.use_gemdeps

version = \">= 0.a\"

str = ARGV.first
if str
  str = str.b[/\\A_(.*)_\\z/, 1]
  if str and Gem::Version.correct?(str)
    version = str
    ARGV.shift
  end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('a', 'executable', version)
else
gem "a", version
load Gem.bin_path("a", "executable", version)
end
    EOF

    wrapper = installer.app_script_text 'executable'
    assert_equal expected, wrapper
  end

  def test_check_executable_overwrite
    installer = setup_base_installer

    installer.generate_bin

    @spec = Gem::Specification.new do |s|
      s.files = ['lib/code.rb']
      s.name = "a"
      s.version = "3"
      s.summary = "summary"
      s.description = "desc"
      s.require_path = 'lib'
    end

    util_make_exec
    installer.gem_dir = @spec.gem_dir
    installer.wrappers = true
    installer.generate_bin

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end

  def test_check_executable_overwrite_default_bin_dir
    installer = setup_base_installer(false)

    bindir(Gem.bindir) do
      util_conflict_executable false

      ui = Gem::MockGemUi.new "n\n"
      use_ui ui do
        e = assert_raise Gem::InstallError do
          installer.generate_bin
        end

        conflicted = File.join @gemhome, 'bin', 'executable'
        assert_match %r{\A"executable" from a conflicts with (?:#{Regexp.quote(conflicted)}|installed executable from conflict)\z},
                     e.message
      end
    end
  end

  def test_check_executable_overwrite_format_executable
    installer = setup_base_installer

    installer.generate_bin

    @spec = Gem::Specification.new do |s|
      s.files = ['lib/code.rb']
      s.name = "a"
      s.version = "3"
      s.summary = "summary"
      s.description = "desc"
      s.require_path = 'lib'
    end

    File.open File.join(util_inst_bindir, 'executable'), 'w' do |io|
      io.write <<-EXEC
#!/usr/local/bin/ruby
#
# This file was generated by RubyGems

gem 'other', version
     EXEC
    end

    util_make_exec
    Gem::Installer.exec_format = 'foo-%s-bar'
    installer.gem_dir = @spec.gem_dir
    installer.wrappers = true
    installer.format_executable = true

    installer.generate_bin # should not raise

    installed_exec = File.join util_inst_bindir, 'foo-executable-bar'
    assert_path_exist installed_exec

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  ensure
    Gem::Installer.exec_format = nil
  end

  def test_check_executable_overwrite_other_gem
    installer = setup_base_installer(false)

    util_conflict_executable true

    ui = Gem::MockGemUi.new "n\n"

    use_ui ui do
      e = assert_raise Gem::InstallError do
        installer.generate_bin
      end

      assert_equal '"executable" from a conflicts with installed executable from conflict',
                   e.message
    end
  end

  def test_check_executable_overwrite_other_gem_force
    installer = setup_base_installer

    util_conflict_executable true
    installer.wrappers = true
    installer.force = true

    installer.generate_bin

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end

  def test_check_executable_overwrite_other_non_gem
    installer = setup_base_installer

    util_conflict_executable false
    installer.wrappers = true

    installer.generate_bin

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end unless Gem.win_platform?

  def test_check_that_user_bin_dir_is_in_path
    installer = setup_base_installer

    bin_dir = installer.bin_dir

    if Gem.win_platform?
      bin_dir = bin_dir.downcase
    end

    orig_PATH, ENV['PATH'] =
      ENV['PATH'], [ENV['PATH'], bin_dir].join(File::PATH_SEPARATOR)

    use_ui @ui do
      installer.check_that_user_bin_dir_is_in_path
    end

    assert_empty @ui.error

    return unless win_platform?

    ENV['PATH'] = [orig_PATH, bin_dir.tr(File::SEPARATOR, File::ALT_SEPARATOR)].join(File::PATH_SEPARATOR)

    use_ui @ui do
      installer.check_that_user_bin_dir_is_in_path
    end

    assert_empty @ui.error
  ensure
    ENV['PATH'] = orig_PATH
  end

  def test_check_that_user_bin_dir_is_in_path_tilde
    pend "Tilde is PATH is not supported under MS Windows" if win_platform?

    orig_PATH, ENV['PATH'] =
      ENV['PATH'], [ENV['PATH'], '~/bin'].join(File::PATH_SEPARATOR)

    installer = setup_base_installer
    installer.bin_dir.replace File.join @userhome, 'bin'

    use_ui @ui do
      installer.check_that_user_bin_dir_is_in_path
    end

    assert_empty @ui.error
  ensure
    ENV['PATH'] = orig_PATH unless win_platform?
  end

  def test_check_that_user_bin_dir_is_in_path_not_in_path
    installer = setup_base_installer

    use_ui @ui do
      installer.check_that_user_bin_dir_is_in_path
    end

    expected = installer.bin_dir

    if Gem.win_platform?
      expected = expected.downcase
    end

    assert_match expected, @ui.error
  end

  def test_ensure_dependency
    installer = setup_base_installer

    util_spec 'a'

    dep = Gem::Dependency.new 'a', '>= 2'
    assert installer.ensure_dependency(@spec, dep)

    dep = Gem::Dependency.new 'b', '> 2'
    e = assert_raise Gem::InstallError do
      installer.ensure_dependency @spec, dep
    end

    assert_equal 'a requires b (> 2)', e.message
  end

  def test_ensure_loadable_spec
    a, a_gem = util_gem 'a', 2 do |s|
      s.add_dependency 'garbage ~> 5'
    end

    installer = Gem::Installer.at a_gem

    e = assert_raise Gem::InstallError do
      installer.ensure_loadable_spec
    end

    assert_equal "The specification for #{a.full_name} is corrupt " +
                 "(SyntaxError)", e.message
  end

  def test_ensure_loadable_spec_security_policy
    pend 'openssl is missing' unless Gem::HAVE_OPENSSL

    _, a_gem = util_gem 'a', 2 do |s|
      s.add_dependency 'garbage ~> 5'
    end

    policy = Gem::Security::HighSecurity
    installer = Gem::Installer.at a_gem, :security_policy => policy

    assert_raise Gem::Security::Exception do
      installer.ensure_loadable_spec
    end
  end

  def test_extract_files
    installer = setup_base_installer

    installer.extract_files

    assert_path_exist File.join @spec.gem_dir, 'bin/executable'
  end

  def test_generate_bin_bindir
    installer = setup_base_installer

    installer.wrappers = true

    @spec.executables = %w[executable]
    @spec.bindir = 'bin'

    exec_file = installer.formatted_program_filename 'executable'
    exec_path = File.join @spec.gem_dir, exec_file
    File.open exec_path, 'w' do |f|
      f.puts '#!/usr/bin/ruby'
    end

    installer.gem_dir = @spec.gem_dir

    installer.generate_bin

    assert_directory_exists util_inst_bindir
    installed_exec = File.join(util_inst_bindir, 'executable')
    assert_path_exist installed_exec
    assert_equal mask, File.stat(installed_exec).mode unless win_platform?

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end

  def test_generate_bin_bindir_with_user_install_warning
    bin_dir = Gem.win_platform? ? File.expand_path(ENV["WINDIR"]).upcase :
                                  "/usr/bin"

    old_path = ENV["PATH"]
    ENV["PATH"] = [ENV["PATH"], bin_dir].compact.join(File::PATH_SEPARATOR)

    options = {
      :bin_dir => bin_dir,
      :install_dir => "/non/existent",
    }

    inst = Gem::Installer.at '', options

    Gem::Installer.path_warning = false

    use_ui @ui do
      inst.check_that_user_bin_dir_is_in_path
    end

    assert_equal "", @ui.error

  ensure
    ENV["PATH"] = old_path
  end

  def test_generate_bin_script
    installer = setup_base_installer

    installer.wrappers = true
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    installer.generate_bin
    assert_directory_exists util_inst_bindir
    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec
    assert_equal mask, File.stat(installed_exec).mode unless win_platform?

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end

  def test_generate_bin_script_format
    installer = setup_base_installer

    installer.format_executable = true
    installer.wrappers = true
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    Gem::Installer.exec_format = 'foo-%s-bar'
    installer.generate_bin
    assert_directory_exists util_inst_bindir
    installed_exec = File.join util_inst_bindir, 'foo-executable-bar'
    assert_path_exist installed_exec
  ensure
    Gem::Installer.exec_format = nil
  end

  def test_generate_bin_script_format_disabled
    installer = setup_base_installer

    installer.wrappers = true
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    Gem::Installer.exec_format = 'foo-%s-bar'
    installer.generate_bin
    assert_directory_exists util_inst_bindir
    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec
  ensure
    Gem::Installer.exec_format = nil
  end

  def test_generate_bin_script_install_dir
    installer = setup_base_installer

    installer.wrappers = true

    gem_dir = File.join("#{@gemhome}2", "gems", @spec.full_name)
    gem_bindir = File.join gem_dir, 'bin'
    FileUtils.mkdir_p gem_bindir
    File.open File.join(gem_bindir, 'executable'), 'w' do |f|
      f.puts "#!/bin/ruby"
    end

    installer.gem_home = "#{@gemhome}2"
    installer.gem_dir = gem_dir
    installer.bin_dir = File.join "#{@gemhome}2", 'bin'

    installer.generate_bin

    installed_exec = File.join("#{@gemhome}2", "bin", 'executable')
    assert_path_exist installed_exec
    assert_equal mask, File.stat(installed_exec).mode unless win_platform?

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end

  def test_generate_bin_script_no_execs
    installer = setup_base_installer

    installer = util_execless

    installer.wrappers = true
    installer.generate_bin

    assert_path_not_exist util_inst_bindir, 'bin dir was created when not needed'
  end

  def test_generate_bin_script_no_perms
    installer = setup_base_installer

    installer.wrappers = true
    util_make_exec

    Dir.mkdir util_inst_bindir

    if win_platform?
      pend('test_generate_bin_script_no_perms skipped on MS Windows')
    elsif Process.uid.zero?
      pend('test_generate_bin_script_no_perms skipped in root privilege')
    else
      FileUtils.chmod 0000, util_inst_bindir

      assert_raise Gem::FilePermissionError do
        installer.generate_bin
      end
    end
  ensure
    FileUtils.chmod 0755, util_inst_bindir unless ($DEBUG or win_platform?)
  end

  def test_generate_bin_script_no_shebang
    installer = setup_base_installer

    installer.wrappers = true
    @spec.executables = %w[executable]

    gem_dir = File.join @gemhome, 'gems', @spec.full_name
    gem_bindir = File.join gem_dir, 'bin'
    FileUtils.mkdir_p gem_bindir
    File.open File.join(gem_bindir, 'executable'), 'w' do |f|
      f.puts "blah blah blah"
    end

    installer.generate_bin

    installed_exec = File.join @gemhome, 'bin', 'executable'
    assert_path_exist installed_exec
    assert_equal mask, File.stat(installed_exec).mode unless win_platform?

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
    # HACK some gems don't have #! in their executables, restore 2008/06
    #assert_no_match %r|generated by RubyGems|, wrapper
  end

  def test_generate_bin_script_wrappers
    installer = setup_base_installer

    installer.wrappers = true
    util_make_exec
    installer.gem_dir = @spec.gem_dir
    installed_exec = File.join(util_inst_bindir, 'executable')

    real_exec = File.join @spec.gem_dir, 'bin', 'executable'

    # fake --no-wrappers for previous install
    unless Gem.win_platform?
      FileUtils.mkdir_p File.dirname(installed_exec)
      FileUtils.ln_s real_exec, installed_exec
    end

    installer.generate_bin
    assert_directory_exists util_inst_bindir
    assert_path_exist installed_exec
    assert_equal mask, File.stat(installed_exec).mode unless win_platform?

    assert_match %r{generated by RubyGems}, File.read(installed_exec)

    refute_match %r{generated by RubyGems}, File.read(real_exec),
                 'real executable overwritten'
  end

  def test_generate_bin_symlink
    pend "Symlinks not supported or not enabled" unless symlink_supported?

    installer = setup_base_installer

    installer.wrappers = false
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    installer.generate_bin
    assert_directory_exists util_inst_bindir
    installed_exec = File.join util_inst_bindir, 'executable'
    assert_equal true, File.symlink?(installed_exec)
    assert_equal(File.join(@spec.gem_dir, 'bin', 'executable'),
                 File.readlink(installed_exec))
  end

  def test_generate_bin_symlink_no_execs
    installer = setup_base_installer

    installer = util_execless

    installer.wrappers = false
    installer.generate_bin

    assert_path_not_exist util_inst_bindir
  end

  def test_generate_bin_symlink_no_perms
    installer = setup_base_installer

    installer.wrappers = false
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    Dir.mkdir util_inst_bindir

    if win_platform?
      pend('test_generate_bin_symlink_no_perms skipped on MS Windows')
    elsif Process.uid.zero?
      pend('test_user_install_disabled_read_only test skipped in root privilege')
    else
      FileUtils.chmod 0000, util_inst_bindir

      assert_raise Gem::FilePermissionError do
        installer.generate_bin
      end
    end
  ensure
    FileUtils.chmod 0755, util_inst_bindir unless ($DEBUG or win_platform?)
  end

  def test_generate_bin_symlink_update_newer
    pend "Symlinks not supported or not enabled" unless symlink_supported?

    installer = setup_base_installer

    installer.wrappers = false
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    installer.generate_bin
    installed_exec = File.join(util_inst_bindir, 'executable')
    assert_equal(File.join(@spec.gem_dir, 'bin', 'executable'),
                 File.readlink(installed_exec))

    @spec = Gem::Specification.new do |s|
      s.files = ['lib/code.rb']
      s.name = "a"
      s.version = "3"
      s.summary = "summary"
      s.description = "desc"
      s.require_path = 'lib'
    end

    util_make_exec
    installer.gem_dir = @spec.gem_dir
    installer.generate_bin
    installed_exec = File.join(util_inst_bindir, 'executable')
    assert_equal(@spec.bin_file('executable'),
                 File.readlink(installed_exec),
                 "Ensure symlink moved to latest version")
  end

  def test_generate_bin_symlink_update_older
    pend "Symlinks not supported or not enabled" unless symlink_supported?

    installer = setup_base_installer

    installer.wrappers = false
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    installer.generate_bin
    installed_exec = File.join(util_inst_bindir, 'executable')
    assert_equal(File.join(@spec.gem_dir, 'bin', 'executable'),
                 File.readlink(installed_exec))

    spec = Gem::Specification.new do |s|
      s.files = ['lib/code.rb']
      s.name = "a"
      s.version = "1"
      s.summary = "summary"
      s.description = "desc"
      s.require_path = 'lib'
    end

    util_make_exec
    one = @spec.dup
    one.version = 1
    installer = Gem::Installer.for_spec spec
    installer.gem_dir = one.gem_dir

    installer.generate_bin

    installed_exec = File.join util_inst_bindir, 'executable'
    expected = File.join @spec.gem_dir, 'bin', 'executable'
    assert_equal(expected,
                 File.readlink(installed_exec),
                 "Ensure symlink not moved")
  end

  def test_generate_bin_symlink_update_remove_wrapper
    pend "Symlinks not supported or not enabled" unless symlink_supported?

    installer = setup_base_installer

    installer.wrappers = true
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    installer.generate_bin

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec

    @spec = Gem::Specification.new do |s|
      s.files = ['lib/code.rb']
      s.name = "a"
      s.version = "3"
      s.summary = "summary"
      s.description = "desc"
      s.require_path = 'lib'
    end
    util_make_exec

    util_installer @spec, @gemhome
    installer.wrappers = false
    installer.gem_dir = @spec.gem_dir

    installer.generate_bin

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_equal(@spec.bin_file('executable'),
                 File.readlink(installed_exec),
                 "Ensure symlink moved to latest version")
  end

  def test_generate_bin_symlink_win32
    old_win_platform = Gem.win_platform?
    Gem.win_platform = true
    old_alt_separator = File::ALT_SEPARATOR
    File.__send__(:remove_const, :ALT_SEPARATOR)
    File.const_set(:ALT_SEPARATOR, '\\')

    installer = setup_base_installer

    installer.wrappers = false
    util_make_exec
    installer.gem_dir = @spec.gem_dir

    use_ui @ui do
      installer.generate_bin
    end

    assert_directory_exists util_inst_bindir
    installed_exec = File.join(util_inst_bindir, 'executable')
    assert_path_exist installed_exec

    if symlink_supported?
      assert File.symlink?(installed_exec)
      return
    end

    assert_match(/Unable to use symlinks, installing wrapper/i,
                 @ui.error)

    wrapper = File.read installed_exec
    assert_match(/generated by RubyGems/, wrapper)
  ensure
    File.__send__(:remove_const, :ALT_SEPARATOR)
    File.const_set(:ALT_SEPARATOR, old_alt_separator)
    Gem.win_platform = old_win_platform
  end

  def test_generate_bin_uses_default_shebang
    pend "Symlinks not supported or not enabled" unless symlink_supported?

    installer = setup_base_installer

    installer.wrappers = true
    util_make_exec

    installer.generate_bin

    default_shebang = Gem.ruby
    shebang_line = File.open("#{@gemhome}/bin/executable") {|f| f.readlines.first }
    assert_match(/\A#!/, shebang_line)
    assert_match(/#{default_shebang}/, shebang_line)
  end

  def test_generate_bin_with_dangling_symlink
    gem_with_dangling_symlink = File.expand_path("packages/ascii_binder-0.1.10.1.gem", __dir__)

    installer = Gem::Installer.at(
      gem_with_dangling_symlink,
      :user_install => false,
      :force => true
    )

    build_rake_in do
      use_ui @ui do
        installer.install
      end
    end

    assert_match %r{bin/ascii_binder` is dangling symlink pointing to `bin/asciibinder`}, @ui.error
    assert_empty @ui.output
  end

  def test_generate_plugins
    installer = util_setup_installer do |spec|
      write_file File.join(@tempdir, 'lib', 'rubygems_plugin.rb') do |io|
        io.write "puts __FILE__"
      end

      spec.files += %w[lib/rubygems_plugin.rb]
    end

    build_rake_in do
      installer.install
    end

    plugin_path = File.join Gem.plugindir, 'a_plugin.rb'

    FileUtils.rm plugin_path

    installer.generate_plugins

    assert File.exist?(plugin_path), 'plugin not written'
  end

  def test_generate_plugins_with_install_dir
    spec = quick_gem 'a' do |s|
      write_file File.join(@tempdir, 'lib', 'rubygems_plugin.rb') do |io|
        io.write "puts __FILE__"
      end

      s.files += %w[lib/rubygems_plugin.rb]
    end

    util_build_gem spec

    plugin_path = File.join "#{@gemhome}2", 'plugins', 'a_plugin.rb'
    installer = util_installer spec, "#{@gemhome}2"

    assert_equal spec, installer.install

    assert File.exist?(plugin_path), 'plugin not written to install_dir'
  end

  def test_generate_plugins_with_user_install
    spec = quick_gem 'a' do |s|
      write_file File.join(@tempdir, 'lib', 'rubygems_plugin.rb') do |io|
        io.write "puts __FILE__"
      end

      s.files += %w[lib/rubygems_plugin.rb]
    end

    util_build_gem spec

    File.chmod(0555, Gem.plugindir)
    system_path = File.join(Gem.plugindir, 'a_plugin.rb')
    user_path = File.join(Gem.plugindir(Gem.user_dir), 'a_plugin.rb')
    installer = util_installer spec, Gem.dir, :user

    assert_equal spec, installer.install

    assert !File.exist?(system_path), 'plugin incorrectly written to system plugins_dir'
    assert File.exist?(user_path), 'plugin not written to user plugins_dir'
  end

  def test_generate_plugins_with_build_root
    spec = quick_gem 'a' do |s|
      write_file File.join(@tempdir, 'lib', 'rubygems_plugin.rb') do |io|
        io.write "puts __FILE__"
      end

      s.files += %w[lib/rubygems_plugin.rb]
    end

    util_build_gem spec

    File.chmod(0555, Gem.plugindir)
    system_path = File.join(Gem.plugindir, 'a_plugin.rb')

    build_root = File.join(@tempdir, 'build_root')
    build_root_path = File.join(build_root, Gem.plugindir.gsub(/^[a-zA-Z]:/, ''), 'a_plugin.rb')

    installer = Gem::Installer.at spec.cache_file, :build_root => build_root

    assert_equal spec, installer.install

    assert !File.exist?(system_path), 'plugin written incorrect written to system plugins_dir'
    assert File.exist?(build_root_path), 'plugin not written to build_root'

    refute_includes File.read(build_root_path), build_root
  end

  def test_keeps_plugins_up_to_date
    # NOTE: version a-2 is already installed by setup hooks

    write_file File.join(@tempdir, 'lib', 'rubygems_plugin.rb') do |io|
      io.write "puts __FILE__"
    end

    build_rake_in do
      util_setup_installer do |spec|
        spec.version = '1'
        spec.files += %w[lib/rubygems_plugin.rb]
      end.install

      plugin_path = File.join Gem.plugindir, 'a_plugin.rb'
      refute File.exist?(plugin_path), 'old version installed while newer version without plugin also installed, but plugin written'

      util_setup_installer do |spec|
        spec.version = '2'
        spec.files += %w[lib/rubygems_plugin.rb]
      end.install

      plugin_path = File.join Gem.plugindir, 'a_plugin.rb'
      assert File.exist?(plugin_path), 'latest version reinstalled, but plugin not written'
      assert_match %r{\Arequire.*a-2/lib/rubygems_plugin\.rb}, File.read(plugin_path), 'written plugin has incorrect content'

      util_setup_installer do |spec|
        spec.version = '3'
        spec.files += %w[lib/rubygems_plugin.rb]
      end.install

      plugin_path = File.join Gem.plugindir, 'a_plugin.rb'
      assert File.exist?(plugin_path), 'latest version installed, but plugin removed'
      assert_match %r{\Arequire.*a-3/lib/rubygems_plugin\.rb}, File.read(plugin_path), 'written plugin has incorrect content'

      util_setup_installer do |spec|
        spec.version = '4'
      end.install

      refute File.exist?(plugin_path), 'new version installed without a plugin while older version with a plugin installed, but plugin not removed'
    end
  end

  def test_generates_plugins_dir_under_install_dir_if_not_there
    Gem.use_paths "#{@gemhome}2" # Set GEM_HOME to an uninitialized repo

    @spec = util_spec 'a'

    path = Gem::Package.build @spec

    installer = Gem::Installer.at path, :install_dir => "#{@gemhome}3"
    assert_equal @spec, installer.install
  end

  def test_initialize
    spec = util_spec 'a' do |s|
      s.platform = Gem::Platform.new 'mswin32'
    end

    gem = File.join @tempdir, spec.file_name

    Dir.mkdir util_inst_bindir
    util_build_gem spec
    FileUtils.mv spec.cache_file, @tempdir

    installer = Gem::Installer.at gem

    assert_equal File.join(@gemhome, 'gems', spec.full_name), installer.gem_dir
    assert_equal File.join(@gemhome, 'bin'), installer.bin_dir
  end

  def test_initialize_user_install
    @gem = setup_base_gem

    installer = Gem::Installer.at @gem, :user_install => true

    assert_equal File.join(Gem.user_dir, 'gems', @spec.full_name),
                 installer.gem_dir
    assert_equal Gem.bindir(Gem.user_dir), installer.bin_dir
  end

  def test_initialize_user_install_bin_dir
    @gem = setup_base_gem

    installer =
      Gem::Installer.at @gem, :user_install => true, :bin_dir => @tempdir

    assert_equal File.join(Gem.user_dir, 'gems', @spec.full_name),
                 installer.gem_dir
    assert_equal @tempdir, installer.bin_dir
  end

  def test_install
    installer = util_setup_installer

    gemdir     = File.join @gemhome, 'gems', @spec.full_name
    cache_file = File.join @gemhome, 'cache', @spec.file_name
    stub_exe   = File.join @gemhome, 'bin', 'executable'
    rakefile   = File.join gemdir, 'ext', 'a', 'Rakefile'
    spec_file  = File.join @gemhome, 'specifications', @spec.spec_name

    Gem.pre_install do
      assert_path_not_exist cache_file, 'cache file must not exist yet'
      assert_path_not_exist spec_file,  'spec file must not exist yet'
      true
    end

    Gem.post_build do
      assert_path_exist gemdir, 'gem install dir must exist'
      assert_path_exist rakefile, 'gem executable must exist'
      assert_path_not_exist stub_exe, 'gem executable must not exist'
      assert_path_not_exist spec_file, 'spec file must not exist yet'
      true
    end

    Gem.post_install do
      assert_path_exist cache_file, 'cache file must exist'
      assert_path_exist spec_file,  'spec file must exist'
    end

    @newspec = nil
    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    assert_equal @spec, @newspec
    assert_path_exist gemdir
    assert_path_exist stub_exe, 'gem executable must exist'

    exe = File.join gemdir, 'bin', 'executable'
    assert_path_exist exe

    exe_mode = File.stat(exe).mode & 0111
    assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform?

    assert_path_exist File.join gemdir, 'lib', 'code.rb'

    assert_path_exist rakefile

    assert_equal spec_file, @newspec.loaded_from
    assert_path_exist spec_file

    assert_same installer, @post_build_hook_arg
    assert_same installer, @post_install_hook_arg
    assert_same installer, @pre_install_hook_arg
  end

  def test_install_creates_working_binstub
    installer = util_setup_installer

    installer.wrappers = true

    gemdir = File.join @gemhome, 'gems', @spec.full_name

    @newspec = nil
    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    exe = File.join gemdir, 'bin', 'executable'

    e = assert_raise RuntimeError do
      instance_eval File.read(exe)
    end

    assert_match(/ran executable/, e.message)
  end

  def test_conflicting_binstubs
    @gem = setup_base_gem

    # build old version that has a bin file
    installer = util_setup_gem do |spec|
      File.open File.join('bin', 'executable'), 'w' do |f|
        f.puts "require 'code'"
      end
      File.open File.join('lib', 'code.rb'), 'w' do |f|
        f.puts 'raise "I have an executable"'
      end
    end

    installer.wrappers = true
    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    old_bin_file = File.join installer.bin_dir, 'executable'

    # build new version that doesn't have a bin file
    installer = util_setup_gem do |spec|
      FileUtils.rm File.join('bin', 'executable')
      spec.files.delete File.join('bin', 'executable')
      spec.executables.delete 'executable'
      spec.version = @spec.version.bump
      File.open File.join('lib', 'code.rb'), 'w' do |f|
        f.puts 'raise "I do not have an executable"'
      end
    end

    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    e = assert_raise RuntimeError do
      instance_eval File.read(old_bin_file)
    end

    # We expect the bin stub to activate the version that actually contains
    # the binstub.
    assert_match('I have an executable', e.message)
  end

  def test_install_creates_binstub_that_understand_version
    installer = util_setup_installer

    installer.wrappers = true

    @newspec = nil
    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    exe = File.join @gemhome, 'bin', 'executable'

    ARGV.unshift "_3.0_"

    begin
      Gem::Specification.reset

      e = assert_raise Gem::GemNotFoundException do
        instance_eval File.read(exe)
      end
    ensure
      ARGV.shift if ARGV.first == "_3.0_"
    end

    assert_includes(e.message, "can't find gem a (= 3.0)")
  end

  def test_install_creates_binstub_that_prefers_user_installed_gem_to_default
    default_spec = new_default_spec('default', '2', nil, 'exe/executable')
    default_spec.executables = 'executable'
    install_default_gems default_spec

    exe = File.join @gemhome, 'bin', 'executable'

    assert_path_exist exe, "default gem's executable not installed"

    installer = util_setup_installer do |spec|
      spec.name = 'default'
      spec.version = '2'
    end

    util_clear_gems

    installer.wrappers = true

    @newspec = nil
    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    e = assert_raise RuntimeError do
      instance_eval File.read(exe)
    end

    assert_equal(e.message, "ran executable")
  end

  def test_install_creates_binstub_that_dont_trust_encoding
    installer = util_setup_installer

    installer.wrappers = true

    @newspec = nil
    build_rake_in do
      use_ui @ui do
        @newspec = installer.install
      end
    end

    exe = File.join @gemhome, 'bin', 'executable'

    extra_arg = "\xE4pfel".dup.force_encoding("UTF-8")
    ARGV.unshift extra_arg

    begin
      Gem::Specification.reset

      e = assert_raise RuntimeError do
        instance_eval File.read(exe)
      end
    ensure
      ARGV.shift if ARGV.first == extra_arg
    end

    assert_match(/ran executable/, e.message)
  end

  def test_install_with_no_prior_files
    installer = util_setup_installer

    build_rake_in do
      use_ui @ui do
        assert_equal @spec, installer.install
      end
    end

    gemdir = File.join(@gemhome, 'gems', @spec.full_name)
    assert_path_exist File.join gemdir, 'lib', 'code.rb'

    installer = util_setup_installer

    # Morph spec to have lib/other.rb instead of code.rb and recreate
    @spec.files = File.join('lib', 'other.rb')
    Dir.chdir @tempdir do
      File.open File.join('lib', 'other.rb'), 'w' do |f|
        f.puts '1'
      end

      use_ui ui do
        FileUtils.rm @gem
        Gem::Package.build @spec
      end
    end
    installer = Gem::Installer.at @gem, :force => true
    build_rake_in do
      use_ui @ui do
        assert_equal @spec, installer.install
      end
    end

    assert_path_exist File.join gemdir, 'lib', 'other.rb'
    assert_path_not_exist File.join gemdir, 'lib', 'code.rb',
           "code.rb from prior install of same gem shouldn't remain here"
  end

  def test_install_force
    _, missing_dep_gem = util_gem 'missing_dep', '1' do |s|
      s.add_dependency 'doesnt_exist', '1'
    end

    use_ui @ui do
      installer = Gem::Installer.at missing_dep_gem, :force => true
      installer.install
    end

    gem_dir = File.join(@gemhome, 'gems', 'missing_dep-1')
    assert_path_exist gem_dir
  end

  def test_install_build_root
    build_root = File.join(@tempdir, 'build_root')

    @gem = setup_base_gem
    installer = Gem::Installer.at @gem, :build_root => build_root

    assert_equal @spec, installer.install
  end

  def test_install_missing_dirs
    installer = setup_base_installer

    FileUtils.rm_f File.join(Gem.dir, 'cache')
    FileUtils.rm_f File.join(Gem.dir, 'doc')
    FileUtils.rm_f File.join(Gem.dir, 'specifications')

    use_ui @ui do
      installer.install
    end

    assert_directory_exists File.join(Gem.dir, 'cache')
    assert_directory_exists File.join(Gem.dir, 'doc')
    assert_directory_exists File.join(Gem.dir, 'specifications')

    assert_path_exist File.join @gemhome, 'cache', @spec.file_name
    assert_path_exist File.join @gemhome, 'specifications', @spec.spec_name
  end

  def test_install_post_build_false
    installer = setup_base_installer

    Gem.post_build do
      false
    end

    use_ui @ui do
      e = assert_raise Gem::InstallError do
        installer.install
      end

      location = "#{__FILE__}:#{__LINE__ - 9}"

      assert_equal "post-build hook at #{location} failed for a-2", e.message
    end

    spec_file = File.join @gemhome, 'specifications', @spec.spec_name
    assert_path_not_exist spec_file

    gem_dir = File.join @gemhome, 'gems', @spec.full_name
    assert_path_not_exist gem_dir
  end

  def test_install_post_build_nil
    installer = setup_base_installer

    Gem.post_build do
      nil
    end

    use_ui @ui do
      installer.install
    end

    spec_file = File.join @gemhome, 'specifications', @spec.spec_name
    assert_path_exist spec_file

    gem_dir = File.join @gemhome, 'gems', @spec.full_name
    assert_path_exist gem_dir
  end

  def test_install_pre_install_false
    installer = setup_base_installer

    Gem.pre_install do
      false
    end

    use_ui @ui do
      e = assert_raise Gem::InstallError do
        installer.install
      end

      location = "#{__FILE__}:#{__LINE__ - 9}"

      assert_equal "pre-install hook at #{location} failed for a-2", e.message
    end

    spec_file = File.join @gemhome, 'specifications', @spec.spec_name
    assert_path_not_exist spec_file
  end

  def test_install_pre_install_nil
    installer = setup_base_installer

    Gem.pre_install do
      nil
    end

    use_ui @ui do
      installer.install
    end

    spec_file = File.join @gemhome, 'specifications', @spec.spec_name
    assert_path_exist spec_file
  end

  def test_install_with_message
    @spec = setup_base_spec
    @spec.post_install_message = 'I am a shiny gem!'

    use_ui @ui do
      path = Gem::Package.build @spec

      installer = Gem::Installer.at path
      installer.install
    end

    assert_match %r{I am a shiny gem!}, @ui.output
  end

  def test_install_with_skipped_message
    @spec = setup_base_spec
    @spec.post_install_message = 'I am a shiny gem!'

    use_ui @ui do
      path = Gem::Package.build @spec

      installer = Gem::Installer.at path, :post_install_message => false
      installer.install
    end

    refute_match %r{I am a shiny gem!}, @ui.output
  end

  def test_install_extension_dir
    gemhome2 = "#{@gemhome}2"

    @spec = setup_base_spec
    @spec.extensions << "extconf.rb"
    write_file File.join(@tempdir, "extconf.rb") do |io|
      io.write <<-RUBY
        require "mkmf"
        create_makefile("#{@spec.name}")
      RUBY
    end

    @spec.files += %w[extconf.rb]

    use_ui @ui do
      path = Gem::Package.build @spec

      installer = Gem::Installer.at path, :install_dir => gemhome2
      installer.install
    end

    expected_makefile = File.join gemhome2, 'gems', @spec.full_name, 'Makefile'

    assert_path_exist expected_makefile
  end

  def test_install_extension_dir_is_removed_on_reinstall
    @spec = setup_base_spec

    @spec.extensions << "extconf.rb"
    write_file File.join(@tempdir, "extconf.rb") do |io|
      io.write <<-RUBY
        require "mkmf"
        create_makefile("#{@spec.name}")
      RUBY
    end

    @spec.files += %w[extconf.rb]

    path = Gem::Package.build @spec

    # Install a gem with an extension
    use_ui @ui do
      installer = Gem::Installer.at path
      installer.install
    end

    # pretend that a binary file was created as part of the build
    should_be_removed = File.join(@spec.extension_dir, "#{@spec.name}.so")
    write_file should_be_removed do |io|
      io.write "DELETE ME ON REINSTALL"
    end
    assert_path_exist should_be_removed

    # reinstall the gem, this is also the same as pristine
    use_ui @ui do
      installer = Gem::Installer.at path, :force => true
      installer.install
    end

    assert_path_not_exist should_be_removed
  end

  def test_install_user_extension_dir
    @spec = setup_base_spec
    @spec.extensions << "extconf.rb"
    write_file File.join(@tempdir, "extconf.rb") do |io|
      io.write <<-RUBY
        require "mkmf"
        create_makefile("#{@spec.name}")
      RUBY
    end

    @spec.files += %w[extconf.rb]

    # Create the non-user ext dir
    expected_extension_dir = @spec.extension_dir.dup
    FileUtils.mkdir_p expected_extension_dir

    use_ui @ui do
      path = Gem::Package.build @spec

      installer = Gem::Installer.at path, :user_install => true
      installer.install
    end

    expected_makefile = File.join Gem.user_dir, 'gems', @spec.full_name, 'Makefile'

    assert_path_exist expected_makefile
    assert_path_exist expected_extension_dir
    assert_path_not_exist File.join expected_extension_dir, 'gem_make.out'
  end

  def test_find_lib_file_after_install
    pend "extensions don't quite work on jruby" if Gem.java_platform?

    @spec = setup_base_spec
    @spec.extensions << "extconf.rb"
    write_file File.join(@tempdir, "extconf.rb") do |io|
      io.write <<-RUBY
        require "mkmf"

        CONFIG['CC'] = '$(TOUCH) $@ ||'
        CONFIG['LDSHARED'] = '$(TOUCH) $@ ||'
        $ruby = '#{Gem.ruby}'

        create_makefile("#{@spec.name}")
      RUBY
    end

    write_file File.join(@tempdir, "depend")

    write_file File.join(@tempdir, "a.c") do |io|
      io.write <<-C
        #include <ruby.h>
        void Init_a() { }
      C
    end

    Dir.mkdir File.join(@tempdir, "lib")
    write_file File.join(@tempdir, 'lib', "b.rb") do |io|
      io.write "# b.rb"
    end

    @spec.files += %w[extconf.rb lib/b.rb depend a.c]

    use_ui @ui do
      path = Gem::Package.build @spec

      installer = Gem::Installer.at path
      installer.install
    end

    expected = File.join @spec.full_require_paths.find {|path|
      File.exist? File.join path, 'b.rb'
    }, 'b.rb'
    assert_equal expected, @spec.matches_for_glob('b.rb').first
  end

  def test_install_extension_and_script
    pend "Makefile creation crashes on jruby" if Gem.java_platform?
    pend if /mswin/ =~ RUBY_PLATFORM && ENV.key?('GITHUB_ACTIONS') # not working from the beginning

    @spec = setup_base_spec
    @spec.extensions << "extconf.rb"
    write_file File.join(@tempdir, "extconf.rb") do |io|
      io.write <<-RUBY
        require "mkmf"
        create_makefile("#{@spec.name}")
      RUBY
    end

    rb = File.join("lib", "#{@spec.name}.rb")
    @spec.files += [rb]
    write_file File.join(@tempdir, rb) do |io|
      io.write <<-RUBY
        # #{@spec.name}.rb
      RUBY
    end

    Dir.mkdir(File.join("lib", @spec.name))
    rb2 = File.join("lib", @spec.name, "#{@spec.name}.rb")
    @spec.files << rb2
    write_file File.join(@tempdir, rb2) do |io|
      io.write <<-RUBY
        # #{@spec.name}/#{@spec.name}.rb
      RUBY
    end

    assert_path_not_exist File.join @spec.gem_dir, rb
    assert_path_not_exist File.join @spec.gem_dir, rb2
    use_ui @ui do
      path = Gem::Package.build @spec

      installer = Gem::Installer.at path
      installer.install
    end
    assert_path_exist File.join @spec.gem_dir, rb
    assert_path_exist File.join @spec.gem_dir, rb2
  end

  def test_install_extension_flat
    pend "extensions don't quite work on jruby" if Gem.java_platform?

    begin
      @spec = setup_base_spec
      @spec.require_paths = ["."]

      @spec.extensions << "extconf.rb"

      write_file File.join(@tempdir, "extconf.rb") do |io|
        io.write <<-RUBY
          require "mkmf"

          CONFIG['CC'] = '$(TOUCH) $@ ||'
          CONFIG['LDSHARED'] = '$(TOUCH) $@ ||'
          $ruby = '#{Gem.ruby}'

          create_makefile("#{@spec.name}")
        RUBY
      end

      # empty depend file for no auto dependencies
      @spec.files += %W[depend #{@spec.name}.c].each do |file|
        write_file File.join(@tempdir, file)
      end

      so = File.join(@spec.gem_dir, "#{@spec.name}.#{RbConfig::CONFIG["DLEXT"]}")
      assert_path_not_exist so
      use_ui @ui do
        path = Gem::Package.build @spec

        installer = Gem::Installer.at path
        installer.install
      end
      assert_path_exist so
    rescue
      puts '-' * 78
      puts File.read File.join(@gemhome, 'gems', 'a-2', 'Makefile')
      puts '-' * 78

      path = File.join(@gemhome, 'gems', 'a-2', 'gem_make.out')

      if File.exist?(path)
        puts File.read(path)
        puts '-' * 78
      end

      raise
    end
  end

  def test_installation_satisfies_dependency_eh
    installer = setup_base_installer

    util_spec 'a'

    dep = Gem::Dependency.new 'a', '>= 2'
    assert installer.installation_satisfies_dependency?(dep)

    dep = Gem::Dependency.new 'a', '> 2'
    refute installer.installation_satisfies_dependency?(dep)
  end

  def test_installation_satisfies_dependency_eh_development
    installer = setup_base_installer
    installer.options[:development] = true
    installer.options[:dev_shallow] = true

    util_spec 'a'

    dep = Gem::Dependency.new 'a', :development
    assert installer.installation_satisfies_dependency?(dep)
  end

  def test_pre_install_checks_dependencies
    installer = setup_base_installer
    @spec.add_dependency 'b', '> 5'
    installer = util_setup_gem
    installer.force = false

    use_ui @ui do
      assert_raise Gem::InstallError do
        installer.install
      end
    end
  end

  def test_pre_install_checks_dependencies_ignore
    installer = util_setup_installer
    @spec.add_dependency 'b', '> 5'
    installer.ignore_dependencies = true

    build_rake_in do
      use_ui @ui do
        assert installer.pre_install_checks
      end
    end
  end

  def test_pre_install_checks_dependencies_install_dir
    gemhome2 = "#{@gemhome}2"

    @gem = setup_base_gem
    @spec.add_dependency 'd'

    quick_gem 'd', 2

    gem = File.join @gemhome, @spec.file_name

    FileUtils.mv @gemhome, gemhome2
    FileUtils.mkdir @gemhome

    FileUtils.mv File.join(gemhome2, 'cache', @spec.file_name), gem

    # Don't leak any already activated gems into the installer, require
    # that it work everything out on it's own.
    Gem::Specification.reset

    installer = Gem::Installer.at gem, :install_dir => gemhome2

    build_rake_in do
      use_ui @ui do
        assert installer.pre_install_checks
      end
    end
  end

  def test_pre_install_checks_malicious_name
    spec = util_spec '../malicious', '1'
    def spec.full_name # so the spec is buildable
      "malicious-1"
    end
    def spec.validate(packaging, strict); end

    util_build_gem spec

    gem = File.join(@gemhome, 'cache', spec.file_name)

    use_ui @ui do
      installer = Gem::Installer.at gem
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end
      assert_equal '#<Gem::Specification name=../malicious version=1> has an invalid name', e.message
    end
  end

  def test_pre_install_checks_malicious_name_before_eval
    spec = util_spec "malicious\n::Object.const_set(:FROM_EVAL, true)#", '1'
    def spec.full_name # so the spec is buildable
      "malicious-1"
    end
    def spec.validate(*args); end

    util_build_gem spec

    gem = File.join(@gemhome, 'cache', spec.file_name)

    use_ui @ui do
      installer = Gem::Installer.at gem
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end
      assert_equal "#<Gem::Specification name=malicious\n::Object.const_set(:FROM_EVAL, true)# version=1> has an invalid name", e.message
    end
    refute defined?(::Object::FROM_EVAL)
  end

  def test_pre_install_checks_malicious_require_paths_before_eval
    spec = util_spec "malicious", '1'
    def spec.full_name # so the spec is buildable
      "malicious-1"
    end
    def spec.validate(*args); end
    spec.require_paths = ["malicious\n``"]

    util_build_gem spec

    gem = File.join(@gemhome, 'cache', spec.file_name)

    use_ui @ui do
      installer = Gem::Installer.at gem
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end
      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid require_paths", e.message
    end
  end

  def test_pre_install_checks_malicious_extensions_before_eval
    pend "mswin environment disallow to create file contained the carriage return code." if Gem.win_platform?

    spec = util_spec "malicious", '1'
    def spec.full_name # so the spec is buildable
      "malicious-1"
    end
    def spec.validate(*args); end
    spec.extensions = ["malicious\n``"]

    util_build_gem spec

    gem = File.join(@gemhome, 'cache', spec.file_name)

    use_ui @ui do
      installer = Gem::Installer.at gem
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end
      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid extensions", e.message
    end
  end

  def test_pre_install_checks_malicious_specification_version_before_eval
    spec = util_spec "malicious", '1'
    def spec.full_name # so the spec is buildable
      "malicious-1"
    end
    def spec.validate(*args); end
    spec.specification_version = "malicious\n``"

    util_build_gem spec

    gem = File.join(@gemhome, 'cache', spec.file_name)

    use_ui @ui do
      installer = Gem::Installer.at gem
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end
      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid specification_version", e.message
    end
  end

  def test_pre_install_checks_malicious_dependencies_before_eval
    spec = util_spec "malicious", '1'
    def spec.full_name # so the spec is buildable
      "malicious-1"
    end
    def spec.validate(*args); end
    spec.add_dependency "b\nfoo", '> 5'

    util_build_gem spec

    gem = File.join(@gemhome, 'cache', spec.file_name)

    use_ui @ui do
      installer = Gem::Installer.at gem
      installer.ignore_dependencies = true
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end
      assert_equal "#<Gem::Specification name=malicious version=1> has an invalid dependencies", e.message
    end
  end

  def test_pre_install_checks_malicious_platform_before_eval
    gem_with_ill_formated_platform = File.expand_path("packages/ill-formatted-platform-1.0.0.10.gem", __dir__)

    installer = Gem::Installer.at(
      gem_with_ill_formated_platform,
      :install_dir => @gem_home,
      :user_install => false,
      :force => true
    )

    use_ui @ui do
      e = assert_raise Gem::InstallError do
        installer.pre_install_checks
      end

      assert_equal "x86-mswin32\n system('id > /tmp/nyangawa')# is an invalid platform", e.message
      assert_empty @ui.output
    end
  end

  def test_shebang
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/ruby"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby}", shebang
  end

  def test_process_options
    installer = setup_base_installer

    assert_nil installer.build_root
    assert_equal File.join(@gemhome, 'bin'), installer.bin_dir
    assert_equal @gemhome, installer.gem_home
  end

  def test_process_options_build_root
    build_root = File.join @tempdir, 'build_root'
    bin_dir = File.join(build_root, @gemhome.gsub(/^[a-zA-Z]:/, ''), 'bin')
    gem_home = File.join(build_root, @gemhome.gsub(/^[a-zA-Z]:/, ''))
    plugins_dir = File.join(build_root, @gemhome.gsub(/^[a-zA-Z]:/, ''), 'plugins')

    @gem = setup_base_gem
    installer = use_ui(@ui) { Gem::Installer.at @gem, :build_root => build_root }

    assert_equal build_root, installer.build_root
    assert_equal bin_dir, installer.bin_dir
    assert_equal gem_home, installer.gem_home

    errors = @ui.error.split("\n")

    assert_equal "WARNING:  You build with buildroot.", errors.shift
    assert_equal "  Build root: #{build_root}", errors.shift
    assert_equal "  Bin dir: #{bin_dir}", errors.shift
    assert_equal "  Gem home: #{gem_home}", errors.shift
    assert_equal "  Plugins dir: #{plugins_dir}", errors.shift
  end

  def test_shebang_arguments
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/ruby -ws"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby} -ws", shebang
  end

  def test_shebang_empty
    installer = setup_base_installer

    util_make_exec @spec, ''

    shebang = installer.shebang 'executable'
    assert_equal "#!#{Gem.ruby}", shebang
  end

  def test_shebang_env
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/env ruby"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby}", shebang
  end

  def test_shebang_env_arguments
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/env ruby -ws"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby} -ws", shebang
  end

  def test_shebang_env_shebang
    installer = setup_base_installer

    util_make_exec @spec, ''
    installer.env_shebang = true

    shebang = installer.shebang 'executable'

    bin_env = get_bin_env

    assert_equal("#!#{bin_env} #{RbConfig::CONFIG['ruby_install_name']}",
                 shebang)
  end

  def test_shebang_nested
    installer = setup_base_installer

    util_make_exec @spec, "#!/opt/local/ruby/bin/ruby"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby}", shebang
  end

  def test_shebang_nested_arguments
    installer = setup_base_installer

    util_make_exec @spec, "#!/opt/local/ruby/bin/ruby -ws"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby} -ws", shebang
  end

  def test_shebang_version
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/ruby18"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby}", shebang
  end

  def test_shebang_version_arguments
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/ruby18 -ws"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby} -ws", shebang
  end

  def test_shebang_version_env
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/env ruby18"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby}", shebang
  end

  def test_shebang_version_env_arguments
    installer = setup_base_installer

    util_make_exec @spec, "#!/usr/bin/env ruby18 -ws"

    shebang = installer.shebang 'executable'

    assert_equal "#!#{Gem.ruby} -ws", shebang
  end

  def test_shebang_custom
    installer = setup_base_installer

    conf = Gem::ConfigFile.new []
    conf[:custom_shebang] = 'test'

    Gem.configuration = conf

    util_make_exec @spec, "#!/usr/bin/ruby"

    shebang = installer.shebang 'executable'

    assert_equal "#!test", shebang
  end

  def get_bin_env
    if win_platform?
      ""
    else
      %w[/usr/bin/env /bin/env].find {|f| File.executable?(f) }
    end
  end

  def test_shebang_custom_with_expands
    installer = setup_base_installer

    bin_env = get_bin_env
    conf = Gem::ConfigFile.new []
    conf[:custom_shebang] = '1 $env 2 $ruby 3 $exec 4 $name'

    Gem.configuration = conf

    util_make_exec @spec, "#!/usr/bin/ruby"

    shebang = installer.shebang 'executable'

    assert_equal "#!1 #{bin_env} 2 #{Gem.ruby} 3 executable 4 a", shebang
  end

  def test_shebang_custom_with_expands_and_arguments
    installer = setup_base_installer

    bin_env = get_bin_env
    conf = Gem::ConfigFile.new []
    conf[:custom_shebang] = '1 $env 2 $ruby 3 $exec'

    Gem.configuration = conf

    util_make_exec @spec, "#!/usr/bin/ruby -ws"

    shebang = installer.shebang 'executable'

    assert_equal "#!1 #{bin_env} 2 #{Gem.ruby} -ws 3 executable", shebang
  end

  def test_unpack
    installer = util_setup_installer

    dest = File.join @gemhome, 'gems', @spec.full_name

    Gem::Deprecate.skip_during do
      installer.unpack dest
    end

    assert_path_exist File.join dest, 'lib', 'code.rb'
    assert_path_exist File.join dest, 'bin', 'executable'
  end

  def test_write_build_info_file
    installer = setup_base_installer

    assert_path_not_exist @spec.build_info_file

    installer.build_args = %w[
      --with-libyaml-dir /usr/local/Cellar/libyaml/0.1.4
    ]

    installer.write_build_info_file

    assert_path_exist @spec.build_info_file

    expected = "--with-libyaml-dir\n/usr/local/Cellar/libyaml/0.1.4\n"

    assert_equal expected, File.read(@spec.build_info_file)
  end

  def test_write_build_info_file_empty
    installer = setup_base_installer

    assert_path_not_exist @spec.build_info_file

    installer.write_build_info_file

    assert_path_not_exist @spec.build_info_file
  end

  def test_write_build_info_file_install_dir
    @gem = setup_base_gem
    installer = Gem::Installer.at @gem, :install_dir => "#{@gemhome}2"

    installer.build_args = %w[
      --with-libyaml-dir /usr/local/Cellar/libyaml/0.1.4
    ]

    installer.write_build_info_file

    assert_path_not_exist @spec.build_info_file
    assert_path_exist \
      File.join("#{@gemhome}2", 'build_info', "#{@spec.full_name}.info")
  end

  def test_write_cache_file
    @gem = setup_base_gem
    cache_file = File.join @gemhome, 'cache', @spec.file_name
    gem = File.join @gemhome, @spec.file_name

    FileUtils.mv cache_file, gem
    assert_path_not_exist cache_file

    installer = Gem::Installer.at gem
    installer.gem_home = @gemhome

    installer.write_cache_file

    assert_path_exist cache_file
  end

  def test_write_spec
    @spec = setup_base_spec
    FileUtils.rm @spec.spec_file
    assert_path_not_exist @spec.spec_file

    installer = Gem::Installer.for_spec @spec
    installer.gem_home = @gemhome

    installer.write_spec

    assert_path_exist @spec.spec_file

    loaded = Gem::Specification.load @spec.spec_file

    assert_equal @spec, loaded

    assert_equal Gem.rubygems_version, @spec.installed_by_version
  end

  def test_write_spec_writes_cached_spec
    @spec = setup_base_spec
    FileUtils.rm @spec.spec_file
    assert_path_not_exist @spec.spec_file

    @spec.files = %w[a.rb b.rb c.rb]

    installer = Gem::Installer.for_spec @spec
    installer.gem_home = @gemhome

    installer.write_spec

    # cached specs have no file manifest:
    @spec.files = []

    assert_equal @spec, eval(File.read(@spec.spec_file))
  end

  def test_dir
    installer = setup_base_installer

    assert_match %r{/gemhome/gems/a-2$}, installer.dir
  end

  def test_default_gem_loaded_from
    spec = util_spec 'a'
    installer = Gem::Installer.for_spec spec, :install_as_default => true
    installer.install
    assert_predicate spec, :default_gem?
  end

  def test_default_gem_without_wrappers
    installer = setup_base_installer

    FileUtils.rm_f File.join(Gem.dir, 'specifications')

    installer.wrappers = false
    installer.options[:install_as_default] = true
    installer.gem_dir = @spec.gem_dir

    use_ui @ui do
      installer.install
    end

    assert_directory_exists File.join(@spec.gem_dir, 'bin')
    installed_exec = File.join @spec.gem_dir, 'bin', 'executable'
    assert_path_exist installed_exec

    assert_directory_exists File.join(Gem.default_dir, 'specifications')
    assert_directory_exists File.join(Gem.default_dir, 'specifications', 'default')

    default_spec = eval File.read File.join(Gem.default_dir, 'specifications', 'default', 'a-2.gemspec')
    assert_equal Gem::Version.new("2"), default_spec.version
    assert_equal ['bin/executable'], default_spec.files

    assert_directory_exists util_inst_bindir

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec

    wrapper = File.read installed_exec

    if symlink_supported?
      refute_match %r{generated by RubyGems}, wrapper
    else # when symlink not supported, it warns and fallbacks back to installing wrapper
      assert_match %r{Unable to use symlinks, installing wrapper}, @ui.error
      assert_match %r{generated by RubyGems}, wrapper
    end
  end

  def test_default_gem_with_wrappers
    installer = setup_base_installer

    installer.wrappers = true
    installer.options[:install_as_default] = true
    installer.gem_dir = @spec.gem_dir

    use_ui @ui do
      installer.install
    end

    assert_directory_exists util_inst_bindir

    installed_exec = File.join util_inst_bindir, 'executable'
    assert_path_exist installed_exec

    wrapper = File.read installed_exec
    assert_match %r{generated by RubyGems}, wrapper
  end

  def test_default_gem_with_exe_as_bindir
    @spec = quick_gem 'c' do |spec|
      util_make_exec spec, '#!/usr/bin/ruby', 'exe'
    end

    util_build_gem @spec

    @spec.cache_file

    installer = util_installer @spec, @gemhome

    installer.options[:install_as_default] = true
    installer.gem_dir = @spec.gem_dir

    use_ui @ui do
      installer.install
    end

    assert_directory_exists File.join(@spec.gem_dir, 'exe')
    installed_exec = File.join @spec.gem_dir, 'exe', 'executable'
    assert_path_exist installed_exec

    assert_directory_exists File.join(Gem.default_dir, 'specifications')
    assert_directory_exists File.join(Gem.default_dir, 'specifications', 'default')

    default_spec = eval File.read File.join(Gem.default_dir, 'specifications', 'default', 'c-2.gemspec')
    assert_equal Gem::Version.new("2"), default_spec.version
    assert_equal ['exe/executable'], default_spec.files
  end

  def test_default_gem_to_specific_install_dir
    @gem = setup_base_gem
    installer = util_installer @spec, "#{@gemhome}2"
    installer.options[:install_as_default] = true

    use_ui @ui do
      installer.install
    end

    assert_directory_exists File.join("#{@gemhome}2", 'specifications')
    assert_directory_exists File.join("#{@gemhome}2", 'specifications', 'default')

    default_spec = eval File.read File.join("#{@gemhome}2", 'specifications', 'default', 'a-2.gemspec')
    assert_equal Gem::Version.new("2"), default_spec.version
    assert_equal ['bin/executable'], default_spec.files
  end

  def test_package_attribute
    gem = quick_gem 'c' do |spec|
      util_make_exec spec, '#!/usr/bin/ruby', 'exe'
    end

    installer = util_installer(gem, @gemhome)
    assert_respond_to(installer, :package)
    assert_kind_of(Gem::Package, installer.package)
  end

  def test_gem_attribute
    gem = quick_gem 'c' do |spec|
      util_make_exec spec, '#!/usr/bin/ruby', 'exe'
    end

    installer = util_installer(gem, @gemhome)
    assert_respond_to(installer, :gem)
    assert_kind_of(String, installer.gem)
  end

  def util_execless
    @spec = util_spec 'z'
    util_build_gem @spec

    util_installer @spec, @gemhome
  end

  def util_conflict_executable(wrappers)
    conflict = quick_gem 'conflict' do |spec|
      util_make_exec spec
    end

    util_build_gem conflict

    installer = util_installer conflict, @gemhome
    installer.wrappers = wrappers
    installer.generate_bin
  end

  def mask
    0100755
  end
end