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
|
require 'rubygems/installer_test_case'
class TestGemInstaller < Gem::InstallerTestCase
def util_setup_install
@gemhome = @installer_tmp
Gem.use_paths @installer_tmp
@spec = Gem::Specification.find_by_name 'a'
@user_spec = Gem::Specification.find_by_name 'b'
@installer.spec = @spec
@installer.gem_home = @installer_tmp
@installer.gem_dir = @spec.gem_dir
@user_installer.spec = @user_spec
@user_installer.gem_home = @installer_tmp
end
def test_app_script_text
util_setup_install
@spec.version = 2
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'
version = \">= 0\"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\\A_(.*)_\\z/
version = $1
ARGV.shift
end
end
gem 'a', version
load Gem.bin_path('a', 'executable', version)
EOF
wrapper = @installer.app_script_text 'executable'
assert_equal expected, wrapper
end
def test_build_extensions_none
util_setup_install
use_ui @ui do
@installer.build_extensions
end
assert_equal '', @ui.output
assert_equal '', @ui.error
refute File.exist?('gem_make.out')
end
def test_build_extensions_extconf_bad
util_setup_install
@spec.extensions << 'extconf.rb'
e = assert_raises Gem::Installer::ExtensionBuildError do
use_ui @ui do
@installer.build_extensions
end
end
assert_match(/\AERROR: Failed to build gem native extension.$/, e.message)
assert_equal "Building native extensions. This could take a while...\n",
@ui.output
assert_equal '', @ui.error
gem_make_out = File.join @gemhome, 'gems', @spec.full_name, 'gem_make.out'
assert_match %r%#{Regexp.escape Gem.ruby} extconf\.rb%,
File.read(gem_make_out)
assert_match %r%#{Regexp.escape Gem.ruby}: No such file%,
File.read(gem_make_out)
end
def test_build_extensions_unsupported
util_setup_install
gem_make_out = File.join @gemhome, 'gems', @spec.full_name, 'gem_make.out'
@spec.extensions << nil
e = assert_raises Gem::Installer::ExtensionBuildError do
use_ui @ui do
@installer.build_extensions
end
end
assert_match(/^\s*No builder for extension ''$/, e.message)
assert_equal "Building native extensions. This could take a while...\n",
@ui.output
assert_equal '', @ui.error
assert_equal "No builder for extension ''\n", File.read(gem_make_out)
ensure
FileUtils.rm_f gem_make_out
end
def test_ensure_dependency
util_setup_install
dep = Gem::Dependency.new 'a', '>= 2'
assert @installer.ensure_dependency(@spec, dep)
dep = Gem::Dependency.new 'b', '> 2'
e = assert_raises Gem::InstallError do
@installer.ensure_dependency @spec, dep
end
assert_equal 'a requires b (> 2)', e.message
end
def test_extract_files
util_setup_install
format = Object.new
def format.file_entries
[[{'size' => 7, 'mode' => 0400, 'path' => 'thefile'}, 'content']]
end
@installer.format = format
@installer.extract_files
thefile_path = File.join(util_gem_dir, 'thefile')
assert_equal 'content', File.read(thefile_path)
unless Gem.win_platform? then
assert_equal 0400, File.stat(thefile_path).mode & 0777
end
end
def test_extract_files_bad_dest
util_setup_install
@installer.gem_dir = 'somedir'
@installer.format = nil
e = assert_raises ArgumentError do
@installer.extract_files
end
assert_equal 'format required to extract from', e.message
end
def test_extract_files_relative
util_setup_install
format = Object.new
def format.file_entries
[[{'size' => 10, 'mode' => 0644, 'path' => '../thefile'}, '../thefile']]
end
@installer.format = format
e = assert_raises Gem::InstallError do
@installer.extract_files
end
dir = util_gem_dir
expected = "attempt to install file into \"../thefile\" under #{dir}"
assert_equal expected, e.message
assert_equal false, File.file?(File.join(@tempdir, '../thefile')),
"You may need to remove this file if you broke the test once"
end
def test_extract_files_absolute
util_setup_install
format = Object.new
def format.file_entries
[[{'size' => 8, 'mode' => 0644, 'path' => '/thefile'}, '/thefile']]
end
@installer.format = format
e = assert_raises Gem::InstallError do
@installer.extract_files
end
assert_equal 'attempt to install file into /thefile', e.message
assert_equal false, File.file?(File.join('/thefile')),
"You may need to remove this file if you broke the test once"
end
def test_generate_bin_bindir
util_setup_install
@installer.wrappers = true
@spec.executables = %w[executable]
@spec.bindir = '.'
exec_file = @installer.formatted_program_filename 'executable'
exec_path = File.join util_gem_dir(@spec), exec_file
File.open exec_path, 'w' do |f|
f.puts '#!/usr/bin/ruby'
end
@installer.gem_dir = util_gem_dir
@installer.generate_bin
assert_equal true, File.directory?(util_inst_bindir)
installed_exec = File.join(util_inst_bindir, 'executable')
assert_equal true, File.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
util_setup_install
bin_dir = Gem.win_platform? ? File.expand_path(ENV["WINDIR"]) : "/usr/bin"
options = {
:bin_dir => bin_dir,
:install_dir => "/non/existant"
}
inst = Gem::Installer.new nil, options
Gem::Installer.path_warning = false
use_ui @ui do
inst.check_that_user_bin_dir_is_in_path
end
assert_equal "", @ui.error
end
def test_generate_bin_script
util_setup_install
@installer.wrappers = true
util_make_exec
@installer.gem_dir = util_gem_dir
@installer.generate_bin
assert_equal true, File.directory?(util_inst_bindir)
installed_exec = File.join(util_inst_bindir, 'executable')
assert_equal true, File.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
util_setup_install
@installer.format_executable = true
@installer.wrappers = true
util_make_exec
@installer.gem_dir = util_gem_dir
Gem::Installer.exec_format = 'foo-%s-bar'
@installer.generate_bin
assert_equal true, File.directory?(util_inst_bindir)
installed_exec = File.join util_inst_bindir, 'foo-executable-bar'
assert_equal true, File.exist?(installed_exec)
ensure
Gem::Installer.exec_format = nil
end
def test_generate_bin_script_format_disabled
util_setup_install
@installer.wrappers = true
util_make_exec
@installer.gem_dir = util_gem_dir
Gem::Installer.exec_format = 'foo-%s-bar'
@installer.generate_bin
assert_equal true, File.directory?(util_inst_bindir)
installed_exec = File.join util_inst_bindir, 'executable'
assert_equal true, File.exist?(installed_exec)
ensure
Gem::Installer.exec_format = nil
end
def test_generate_bin_script_install_dir
util_setup_install
@installer.wrappers = true
@spec.executables = %w[executable]
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.generate_bin
installed_exec = File.join("#{@gemhome}2", "bin", 'executable')
assert_equal true, File.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
util_setup_install
util_execless
@installer.wrappers = true
@installer.generate_bin
refute File.exist?(util_inst_bindir), 'bin dir was created when not needed'
end
def test_generate_bin_script_no_perms
util_setup_install
@installer.wrappers = true
util_make_exec
Dir.mkdir util_inst_bindir
if win_platform?
skip('test_generate_bin_script_no_perms skipped on MS Windows')
else
FileUtils.chmod 0000, util_inst_bindir
assert_raises 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
util_setup_install
@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_equal true, File.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
util_setup_install
@installer.wrappers = true
util_make_exec
@installer.gem_dir = util_gem_dir
installed_exec = File.join(util_inst_bindir, 'executable')
real_exec = File.join util_gem_dir, 'bin', 'executable'
# fake --no-wrappers for previous install
unless Gem.win_platform? then
FileUtils.mkdir_p File.dirname(installed_exec)
FileUtils.ln_s real_exec, installed_exec
end
@installer.generate_bin
assert_equal true, File.directory?(util_inst_bindir)
assert_equal true, File.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
util_setup_install
return if win_platform? #Windows FS do not support symlinks
@installer.wrappers = false
util_make_exec
@installer.gem_dir = util_gem_dir
@installer.generate_bin
assert_equal true, File.directory?(util_inst_bindir)
installed_exec = File.join util_inst_bindir, 'executable'
assert_equal true, File.symlink?(installed_exec)
assert_equal(File.join(util_gem_dir, 'bin', 'executable'),
File.readlink(installed_exec))
end
def test_generate_bin_symlink_no_execs
util_setup_install
util_execless
@installer.wrappers = false
@installer.generate_bin
refute File.exist?(util_inst_bindir)
end
def test_generate_bin_symlink_no_perms
util_setup_install
@installer.wrappers = false
util_make_exec
@installer.gem_dir = util_gem_dir
Dir.mkdir util_inst_bindir
if win_platform?
skip('test_generate_bin_symlink_no_perms skipped on MS Windows')
else
FileUtils.chmod 0000, util_inst_bindir
assert_raises 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
util_setup_install
return if win_platform? #Windows FS do not support symlinks
@installer.wrappers = false
util_make_exec
@installer.gem_dir = util_gem_dir
@installer.generate_bin
installed_exec = File.join(util_inst_bindir, 'executable')
assert_equal(File.join(util_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
@spec.version = 3
util_make_exec
@installer.gem_dir = util_gem_dir @spec
@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
util_setup_install
return if win_platform? #Windows FS do not support symlinks
@installer.wrappers = false
util_make_exec
@installer.gem_dir = util_gem_dir
@installer.generate_bin
installed_exec = File.join(util_inst_bindir, 'executable')
assert_equal(File.join(util_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_dir = util_gem_dir one
@installer.spec = spec
@installer.generate_bin
installed_exec = File.join util_inst_bindir, 'executable'
expected = File.join util_gem_dir, 'bin', 'executable'
assert_equal(expected,
File.readlink(installed_exec),
"Ensure symlink not moved")
end
def test_generate_bin_symlink_update_remove_wrapper
util_setup_install
return if win_platform? #Windows FS do not support symlinks
@installer.wrappers = true
util_make_exec
@installer.gem_dir = util_gem_dir
@installer.generate_bin
installed_exec = File.join util_inst_bindir, 'executable'
assert_equal true, File.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
@installer.wrappers = false
@spec.version = 3
util_make_exec
@installer.gem_dir = util_gem_dir
@installer.generate_bin
installed_exec = File.join(util_inst_bindir, 'executable')
assert_equal(File.join(util_gem_dir, 'bin', 'executable'),
File.readlink(installed_exec),
"Ensure symlink moved to latest version")
end
def test_generate_bin_symlink_win32
util_setup_install
old_win_platform = Gem.win_platform?
Gem.win_platform = true
@installer.wrappers = false
util_make_exec
@installer.gem_dir = util_gem_dir
use_ui @ui do
@installer.generate_bin
end
assert_equal true, File.directory?(util_inst_bindir)
installed_exec = File.join(util_inst_bindir, 'executable')
assert_equal true, File.exist?(installed_exec)
assert_match(/Unable to use symlinks on Windows, installing wrapper/i,
@ui.error)
wrapper = File.read installed_exec
assert_match(/generated by RubyGems/, wrapper)
ensure
Gem.win_platform = old_win_platform
end
def test_generate_bin_uses_default_shebang
util_setup_install
return if win_platform? #Windows FS do not support symlinks
@installer.wrappers = true
util_make_exec
@installer.generate_bin
default_shebang = Gem.ruby
shebang_line = open("#{@gemhome}/bin/executable") { |f| f.readlines.first }
assert_match(/\A#!/, shebang_line)
assert_match(/#{default_shebang}/, shebang_line)
end
def test_initialize
spec = quick_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.new gem
assert_equal File.join(@gemhome, 'gems', spec.full_name), installer.gem_dir
end
def test_install
Dir.mkdir util_inst_bindir
util_setup_gem
util_clear_gems
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'
Gem.pre_install do |installer|
refute File.exist?(cache_file), 'cache file must not exist yet'
true
end
Gem.post_build do |installer|
assert File.exist?(gemdir), 'gem install dir must exist'
assert File.exist?(rakefile), 'gem executable must exist'
refute File.exist?(stub_exe), 'gem executable must not exist'
true
end
Gem.post_install do |installer|
assert File.exist?(cache_file), 'cache file must exist'
end
@newspec = nil
build_rake_in do
use_ui @ui do
@newspec = @installer.install
end
end
assert_equal @spec, @newspec
assert File.exist? gemdir
assert File.exist?(stub_exe), 'gem executable must exist'
exe = File.join gemdir, 'bin', 'executable'
assert File.exist? exe
exe_mode = File.stat(exe).mode & 0111
assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform?
assert File.exist?(File.join(gemdir, 'lib', 'code.rb'))
assert File.exist? rakefile
spec_file = File.join(@gemhome, 'specifications', @spec.spec_name)
assert_equal spec_file, @newspec.loaded_from
assert File.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
Dir.mkdir util_inst_bindir
util_setup_gem
util_clear_gems
@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_raises RuntimeError do
instance_eval File.read(exe)
end
assert_match(/ran executable/, e.message)
end
def test_install_creates_binstub_that_understand_version
Dir.mkdir util_inst_bindir
util_setup_gem
util_clear_gems
@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_raises Gem::LoadError do
instance_eval File.read(exe)
end
ensure
ARGV.shift if ARGV.first == "_3.0_"
end
assert_match(/\(= 3\.0\)/, e.message)
end
def test_install_creates_binstub_that_dont_trust_encoding
skip unless "".respond_to?(:force_encoding)
Dir.mkdir util_inst_bindir
util_setup_gem
util_clear_gems
@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".force_encoding("UTF-8")
ARGV.unshift extra_arg
begin
Gem::Specification.reset
e = assert_raises 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
Dir.mkdir util_inst_bindir
util_clear_gems
util_setup_gem
build_rake_in do
use_ui @ui do
assert_equal @spec, @installer.install
end
end
gemdir = File.join(@gemhome, 'gems', @spec.full_name)
assert File.exist?(File.join(gemdir, 'lib', 'code.rb'))
util_setup_gem
# 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::Builder.new(@spec).build
end
end
@installer = Gem::Installer.new @gem
build_rake_in do
use_ui @ui do
assert_equal @spec, @installer.install
end
end
assert File.exist?(File.join(gemdir, 'lib', 'other.rb'))
refute(File.exist?(File.join(gemdir, 'lib', 'code.rb')),
"code.rb from prior install of same gem shouldn't remain here")
end
def test_install_bad_gem
gem = nil
use_ui @ui do
Dir.chdir @tempdir do Gem::Builder.new(@spec).build end
gem = File.join @tempdir, @spec.file_name
end
gem_data = File.open gem, 'rb' do |fp| fp.read 1024 end
File.open gem, 'wb' do |fp| fp.write gem_data end
e = assert_raises Gem::InstallError do
use_ui @ui do
@installer = Gem::Installer.new gem
@installer.install
end
end
assert_equal "invalid gem format for #{gem}", e.message
end
def test_install_check_dependencies
@spec.add_dependency 'b', '> 5'
util_setup_gem
use_ui @ui do
assert_raises Gem::InstallError do
@installer.install
end
end
end
def test_install_check_dependencies_install_dir
gemhome2 = "#{@gemhome}2"
@spec.add_dependency 'b'
quick_gem 'b', 2
FileUtils.mv @gemhome, gemhome2
Gem::Specification.dirs = [gemhome2] # TODO: switch all dirs= to use_paths
util_setup_gem
@installer = Gem::Installer.new @gem, :install_dir => gemhome2
gem_home = Gem.dir
build_rake_in do
use_ui @ui do
@installer.install
end
end
assert File.exist?(File.join(gemhome2, 'gems', @spec.full_name))
assert_equal gem_home, Gem.dir
end
def test_install_force
use_ui @ui do
installer = Gem::Installer.new old_ruby_required, :force => true
installer.install
end
gem_dir = File.join(@gemhome, 'gems', 'old_ruby_required-1')
assert File.exist?(gem_dir)
end
def test_install_ignore_dependencies
Dir.mkdir util_inst_bindir
@spec.add_dependency 'b', '> 5'
util_setup_gem
@installer.ignore_dependencies = true
build_rake_in do
use_ui @ui do
assert_equal @spec, @installer.install
end
end
gemdir = File.join @gemhome, 'gems', @spec.full_name
assert File.exist?(gemdir)
exe = File.join(gemdir, 'bin', 'executable')
assert File.exist?(exe)
exe_mode = File.stat(exe).mode & 0111
assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform?
assert File.exist?(File.join(gemdir, 'lib', 'code.rb'))
assert File.exist?(File.join(@gemhome, 'specifications', @spec.spec_name))
end
def test_install_missing_dirs
FileUtils.rm_f File.join(Gem.dir, 'cache')
FileUtils.rm_f File.join(Gem.dir, 'docs')
FileUtils.rm_f File.join(Gem.dir, 'specifications')
use_ui @ui do
Dir.chdir @tempdir do Gem::Builder.new(@spec).build end
@installer.install
end
File.directory? File.join(Gem.dir, 'cache')
File.directory? File.join(Gem.dir, 'docs')
File.directory? File.join(Gem.dir, 'specifications')
assert File.exist?(File.join(@gemhome, 'cache', @spec.file_name))
assert File.exist?(File.join(@gemhome, 'specifications', @spec.spec_name))
end
def test_install_post_build_false
util_clear_gems
Gem.post_build do
false
end
use_ui @ui do
e = assert_raises 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
refute File.exist? spec_file
gem_dir = File.join @gemhome, 'gems', @spec.full_name
refute File.exist? gem_dir
end
def test_install_post_build_nil
util_clear_gems
Gem.post_build do
nil
end
use_ui @ui do
@installer.install
end
spec_file = File.join @gemhome, 'specifications', @spec.spec_name
assert File.exist? spec_file
gem_dir = File.join @gemhome, 'gems', @spec.full_name
assert File.exist? gem_dir
end
def test_install_pre_install_false
util_clear_gems
Gem.pre_install do
false
end
use_ui @ui do
e = assert_raises 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
refute File.exist? spec_file
end
def test_install_pre_install_nil
util_clear_gems
Gem.pre_install do
nil
end
use_ui @ui do
@installer.install
end
spec_file = File.join @gemhome, 'specifications', @spec.spec_name
assert File.exist? spec_file
end
def test_install_with_message
@spec.post_install_message = 'I am a shiny gem!'
use_ui @ui do
path = Gem::Builder.new(@spec).build
@installer = Gem::Installer.new path
@installer.install
end
assert_match %r|I am a shiny gem!|, @ui.output
end
def test_install_wrong_ruby_version
use_ui @ui do
installer = Gem::Installer.new old_ruby_required
e = assert_raises Gem::InstallError do
installer.install
end
assert_equal 'old_ruby_required requires Ruby version = 1.4.6.',
e.message
end
end
def test_install_wrong_rubygems_version
spec = quick_spec 'old_rubygems_required', '1' do |s|
s.required_rubygems_version = '< 0'
end
util_build_gem spec
gem = File.join(@gemhome, 'cache', spec.file_name)
use_ui @ui do
@installer = Gem::Installer.new gem
e = assert_raises Gem::InstallError do
@installer.install
end
assert_equal 'old_rubygems_required requires RubyGems version < 0. ' +
"Try 'gem update --system' to update RubyGems itself.", e.message
end
end
def test_installation_satisfies_dependency_eh
util_setup_install
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_shebang
util_setup_install
util_make_exec @spec, "#!/usr/bin/ruby"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby}", shebang
end
def test_shebang_arguments
util_setup_install
util_make_exec @spec, "#!/usr/bin/ruby -ws"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby} -ws", shebang
end
def test_shebang_empty
util_setup_install
util_make_exec @spec, ''
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby}", shebang
end
def test_shebang_env
util_setup_install
util_make_exec @spec, "#!/usr/bin/env ruby"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby}", shebang
end
def test_shebang_env_arguments
util_setup_install
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
util_setup_install
util_make_exec @spec, ''
@installer.env_shebang = true
shebang = @installer.shebang 'executable'
env_shebang = "/usr/bin/env" unless Gem.win_platform?
assert_equal("#!#{env_shebang} #{Gem::ConfigMap[:ruby_install_name]}",
shebang)
end
def test_shebang_nested
util_setup_install
util_make_exec @spec, "#!/opt/local/ruby/bin/ruby"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby}", shebang
end
def test_shebang_nested_arguments
util_setup_install
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
util_setup_install
util_make_exec @spec, "#!/usr/bin/ruby18"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby}", shebang
end
def test_shebang_version_arguments
util_setup_install
util_make_exec @spec, "#!/usr/bin/ruby18 -ws"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby} -ws", shebang
end
def test_shebang_version_env
util_setup_install
util_make_exec @spec, "#!/usr/bin/env ruby18"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby}", shebang
end
def test_shebang_version_env_arguments
util_setup_install
util_make_exec @spec, "#!/usr/bin/env ruby18 -ws"
shebang = @installer.shebang 'executable'
assert_equal "#!#{Gem.ruby} -ws", shebang
end
def test_unpack
util_setup_install
util_setup_gem
dest = File.join @gemhome, 'gems', @spec.full_name
@installer.unpack dest
assert File.exist?(File.join(dest, 'lib', 'code.rb'))
assert File.exist?(File.join(dest, 'bin', 'executable'))
end
def test_write_spec
util_setup_install
spec_dir = File.join @gemhome, 'specifications'
spec_file = File.join spec_dir, @spec.spec_name
FileUtils.rm spec_file
refute File.exist?(spec_file)
@installer.spec = @spec
@installer.gem_home = @gemhome
@installer.write_spec
assert File.exist?(spec_file)
assert_equal @spec, eval(File.read(spec_file))
end
def test_write_spec_writes_cached_spec
util_setup_install
spec_dir = File.join @gemhome, 'specifications'
spec_file = File.join spec_dir, @spec.spec_name
FileUtils.rm spec_file
refute File.exist?(spec_file)
@spec.files = %w[a.rb b.rb c.rb]
@installer.spec = @spec
@installer.gem_home = @gemhome
@installer.write_spec
# cached specs have no file manifest:
@spec.files = []
assert_equal @spec, eval(File.read(spec_file))
end
def test_dir
util_setup_install
assert_match @installer.dir, %r!/installer/gems/a-2$!
end
def old_ruby_required
spec = quick_spec 'old_ruby_required', '1' do |s|
s.required_ruby_version = '= 1.4.6'
end
util_build_gem spec
spec.cache_file
end
def util_execless
@spec = quick_spec 'z'
util_build_gem @spec
@installer = util_installer @spec, @gemhome
end
def mask
0100755 & (~File.umask)
end
end
|