1require 'rubygems/installer_test_case' 2require 'rubygems/uninstaller' 3 4class TestGemUninstaller < Gem::InstallerTestCase 5 6 def setup 7 super 8 9 build_rake_in do 10 use_ui ui do 11 @installer.install 12 @spec = @installer.spec 13 14 @user_installer.install 15 @user_spec = @user_installer.spec 16 end 17 end 18 19 Gem::Specification.reset 20 end 21 22 def test_initialize_expand_path 23 uninstaller = Gem::Uninstaller.new nil, :install_dir => '/foo//bar' 24 25 assert_match %r|/foo/bar$|, uninstaller.instance_variable_get(:@gem_home) 26 end 27 28 def test_ask_if_ok 29 c = quick_spec 'c' 30 31 uninstaller = Gem::Uninstaller.new nil 32 33 ok = :junk 34 35 ui = Gem::MockGemUi.new "\n" 36 37 use_ui ui do 38 ok = uninstaller.ask_if_ok c 39 end 40 41 refute ok 42 end 43 44 def test_remove_all 45 uninstaller = Gem::Uninstaller.new nil 46 47 ui = Gem::MockGemUi.new "y\n" 48 49 use_ui ui do 50 uninstaller.remove_all [@spec] 51 end 52 53 refute_path_exists @spec.gem_dir 54 end 55 56 def test_remove_executables_force_keep 57 uninstaller = Gem::Uninstaller.new nil, :executables => false 58 59 executable = File.join Gem.bindir(@user_spec.base_dir), 'executable' 60 assert File.exist?(executable), 'executable not written' 61 62 use_ui @ui do 63 uninstaller.remove_executables @user_spec 64 end 65 66 assert File.exist? executable 67 68 assert_equal "Executables and scripts will remain installed.\n", @ui.output 69 end 70 71 def test_remove_executables_force_remove 72 uninstaller = Gem::Uninstaller.new nil, :executables => true 73 74 executable = File.join Gem.bindir(@user_spec.base_dir), 'executable' 75 assert File.exist?(executable), 'executable not written' 76 77 use_ui @ui do 78 uninstaller.remove_executables @user_spec 79 end 80 81 assert_equal "Removing executable\n", @ui.output 82 83 refute File.exist? executable 84 end 85 86 def test_remove_executables_user 87 uninstaller = Gem::Uninstaller.new nil, :executables => true 88 89 use_ui @ui do 90 uninstaller.remove_executables @user_spec 91 end 92 93 exec_path = File.join Gem.user_dir, 'bin', 'executable' 94 refute File.exist?(exec_path), 'exec still exists in user bin dir' 95 96 assert_equal "Removing executable\n", @ui.output 97 end 98 99 def test_remove_executables_user_format 100 Gem::Installer.exec_format = 'foo-%s-bar' 101 102 uninstaller = Gem::Uninstaller.new nil, :executables => true, :format_executable => true 103 104 use_ui @ui do 105 uninstaller.remove_executables @user_spec 106 end 107 108 exec_path = File.join Gem.user_dir, 'bin', 'foo-executable-bar' 109 assert_equal false, File.exist?(exec_path), 'removed exec from bin dir' 110 111 assert_equal "Removing foo-executable-bar\n", @ui.output 112 ensure 113 Gem::Installer.exec_format = nil 114 end 115 116 def test_remove_executables_user_format_disabled 117 Gem::Installer.exec_format = 'foo-%s-bar' 118 119 uninstaller = Gem::Uninstaller.new nil, :executables => true 120 121 use_ui @ui do 122 uninstaller.remove_executables @user_spec 123 end 124 125 exec_path = File.join Gem.user_dir, 'bin', 'executable' 126 refute File.exist?(exec_path), 'removed exec from bin dir' 127 128 assert_equal "Removing executable\n", @ui.output 129 ensure 130 Gem::Installer.exec_format = nil 131 end 132 133 def test_path_ok_eh 134 uninstaller = Gem::Uninstaller.new nil 135 136 assert_equal true, uninstaller.path_ok?(@gemhome, @spec) 137 end 138 139 def test_path_ok_eh_legacy 140 uninstaller = Gem::Uninstaller.new nil 141 142 @spec.loaded_from = @spec.loaded_from.gsub @spec.full_name, '\&-legacy' 143 @spec.platform = 'legacy' 144 145 assert_equal true, uninstaller.path_ok?(@gemhome, @spec) 146 end 147 148 def test_path_ok_eh_user 149 uninstaller = Gem::Uninstaller.new nil 150 151 assert_equal true, uninstaller.path_ok?(Gem.user_dir, @user_spec) 152 end 153 154 def test_uninstall 155 uninstaller = Gem::Uninstaller.new @spec.name, :executables => true 156 157 gem_dir = File.join @gemhome, 'gems', @spec.full_name 158 159 Gem.pre_uninstall do 160 assert File.exist?(gem_dir), 'gem_dir should exist' 161 end 162 163 Gem.post_uninstall do 164 refute File.exist?(gem_dir), 'gem_dir should not exist' 165 end 166 167 uninstaller.uninstall 168 169 refute File.exist?(gem_dir) 170 171 assert_same uninstaller, @pre_uninstall_hook_arg 172 assert_same uninstaller, @post_uninstall_hook_arg 173 end 174 175 def test_uninstall_default_gem 176 spec = new_default_spec 'default', '2' 177 178 install_default_gems spec 179 180 uninstaller = Gem::Uninstaller.new spec.name, :executables => true 181 182 e = assert_raises Gem::InstallError do 183 uninstaller.uninstall 184 end 185 186 assert_equal 'gem "default" cannot be uninstalled ' + 187 'because it is a default gem', 188 e.message 189 end 190 191 def test_uninstall_default_gem_with_same_version 192 default_spec = new_default_spec 'default', '2' 193 install_default_gems default_spec 194 195 spec = new_spec 'default', '2' 196 install_gem spec 197 198 Gem::Specification.reset 199 200 uninstaller = Gem::Uninstaller.new spec.name, :executables => true 201 202 uninstaller.uninstall 203 204 refute_path_exists spec.gem_dir 205 end 206 207 def test_uninstall_nonexistent 208 uninstaller = Gem::Uninstaller.new 'bogus', :executables => true 209 210 e = assert_raises Gem::InstallError do 211 uninstaller.uninstall 212 end 213 214 assert_equal 'gem "bogus" is not installed', e.message 215 end 216 217 def test_uninstall_not_ok 218 quick_gem 'z' do |s| 219 s.add_runtime_dependency @spec.name 220 end 221 222 uninstaller = Gem::Uninstaller.new @spec.name 223 224 gem_dir = File.join @gemhome, 'gems', @spec.full_name 225 executable = File.join @gemhome, 'bin', 'executable' 226 227 assert File.exist?(gem_dir), 'gem_dir must exist' 228 assert File.exist?(executable), 'executable must exist' 229 230 ui = Gem::MockGemUi.new "n\n" 231 232 assert_raises Gem::DependencyRemovalException do 233 use_ui ui do 234 uninstaller.uninstall 235 end 236 end 237 238 assert File.exist?(gem_dir), 'gem_dir must still exist' 239 assert File.exist?(executable), 'executable must still exist' 240 end 241 242 def test_uninstall_user 243 @user_spec = Gem::Specification.find_by_name 'b' 244 245 uninstaller = Gem::Uninstaller.new(@user_spec.name, 246 :executables => true, 247 :user_install => true) 248 249 gem_dir = File.join @user_spec.gem_dir 250 251 Gem.pre_uninstall do 252 assert_path_exists gem_dir 253 end 254 255 Gem.post_uninstall do 256 refute_path_exists gem_dir 257 end 258 259 uninstaller.uninstall 260 261 refute_path_exists gem_dir 262 263 assert_same uninstaller, @pre_uninstall_hook_arg 264 assert_same uninstaller, @post_uninstall_hook_arg 265 end 266 267 def test_uninstall_wrong_repo 268 Gem.use_paths "#{@gemhome}2", [@gemhome] 269 270 uninstaller = Gem::Uninstaller.new @spec.name, :executables => true 271 272 e = assert_raises Gem::InstallError do 273 uninstaller.uninstall 274 end 275 276 expected = <<-MESSAGE.strip 277#{@spec.name} is not installed in GEM_HOME, try: 278\tgem uninstall -i #{@gemhome} a 279 MESSAGE 280 281 assert_equal expected, e.message 282 end 283 284 def test_uninstall_selection_greater_than_one 285 util_make_gems 286 287 list = Gem::Specification.find_all_by_name('a') 288 289 uninstaller = Gem::Uninstaller.new('a') 290 291 use_ui Gem::MockGemUi.new("2\ny\n") do 292 uninstaller.uninstall 293 end 294 295 updated_list = Gem::Specification.find_all_by_name('a') 296 assert_equal list.length - 1, updated_list.length 297 end 298 299 def test_uninstall_prompts_about_broken_deps 300 quick_gem 'r', '1' do |s| s.add_dependency 'q', '= 1' end 301 quick_gem 'q', '1' 302 303 un = Gem::Uninstaller.new('q') 304 ui = Gem::MockGemUi.new("y\n") 305 306 use_ui ui do 307 un.uninstall 308 end 309 310 lines = ui.output.split("\n") 311 lines.shift 312 313 assert_match %r!You have requested to uninstall the gem:!, lines.shift 314 lines.shift 315 lines.shift 316 317 assert_match %r!r-1 depends on q \(= 1\)!, lines.shift 318 assert_match %r!Successfully uninstalled q-1!, lines.last 319 end 320 321 def test_uninstall_only_lists_unsatified_deps 322 quick_gem 'r', '1' do |s| s.add_dependency 'q', '~> 1.0' end 323 quick_gem 'x', '1' do |s| s.add_dependency 'q', '= 1.0' end 324 quick_gem 'q', '1.0' 325 quick_gem 'q', '1.1' 326 327 un = Gem::Uninstaller.new('q', :version => "1.0") 328 ui = Gem::MockGemUi.new("y\n") 329 330 use_ui ui do 331 un.uninstall 332 end 333 334 lines = ui.output.split("\n") 335 lines.shift 336 337 assert_match %r!You have requested to uninstall the gem:!, lines.shift 338 lines.shift 339 lines.shift 340 341 assert_match %r!x-1 depends on q \(= 1.0\)!, lines.shift 342 assert_match %r!Successfully uninstalled q-1.0!, lines.last 343 end 344 345 def test_uninstall_doesnt_prompt_when_other_gem_satifies_requirement 346 quick_gem 'r', '1' do |s| s.add_dependency 'q', '~> 1.0' end 347 quick_gem 'q', '1.0' 348 quick_gem 'q', '1.1' 349 350 un = Gem::Uninstaller.new('q', :version => "1.0") 351 ui = Gem::MockGemUi.new("y\n") 352 353 use_ui ui do 354 un.uninstall 355 end 356 357 lines = ui.output.split("\n") 358 359 assert_equal "Successfully uninstalled q-1.0", lines.shift 360 end 361 362 def test_uninstall_doesnt_prompt_when_removing_a_dev_dep 363 quick_gem 'r', '1' do |s| s.add_development_dependency 'q', '= 1.0' end 364 quick_gem 'q', '1.0' 365 366 un = Gem::Uninstaller.new('q', :version => "1.0") 367 ui = Gem::MockGemUi.new("y\n") 368 369 use_ui ui do 370 un.uninstall 371 end 372 373 lines = ui.output.split("\n") 374 375 assert_equal "Successfully uninstalled q-1.0", lines.shift 376 end 377 378 379 def test_uninstall_prompt_includes_dep_type 380 quick_gem 'r', '1' do |s| 381 s.add_development_dependency 'q', '= 1' 382 end 383 384 quick_gem 'q', '1' 385 386 un = Gem::Uninstaller.new('q', :check_dev => true) 387 ui = Gem::MockGemUi.new("y\n") 388 389 use_ui ui do 390 un.uninstall 391 end 392 393 lines = ui.output.split("\n") 394 lines.shift 395 396 assert_match %r!You have requested to uninstall the gem:!, lines.shift 397 lines.shift 398 lines.shift 399 400 assert_match %r!r-1 depends on q \(= 1, development\)!, lines.shift 401 assert_match %r!Successfully uninstalled q-1!, lines.last 402 end 403end 404