1
require_relative 'gem_installer_test_case'
1
require 'rubygems/installer_test_case'
2
2
require 'rubygems/uninstaller'
4
class TestGemUninstaller < GemInstallerTestCase
4
class TestGemUninstaller < Gem::InstallerTestCase
12
@user_spec.executables = ["my_exec"]
15
user_bin_dir = File.join Gem.user_dir, 'gems', @user_spec.full_name, 'bin'
16
FileUtils.mkdir_p user_bin_dir
17
exec_path = File.join user_bin_dir, "my_exec"
18
File.open exec_path, 'w' do |f|
19
f.puts "#!/usr/bin/ruby"
22
user_bin_dir = File.join Gem.user_dir, 'bin'
23
FileUtils.mkdir_p user_bin_dir
24
exec_path = File.join user_bin_dir, "my_exec"
25
File.open exec_path, 'w' do |f|
26
f.puts "#!/usr/bin/ruby"
9
@user_spec.executables = ["executable"]
32
14
@user_installer.install
33
Gem::Uninstaller.new(@user_spec.name, :executables => false).uninstall
16
Gem.use_paths @gemhome, Gem.user_dir
18
@spec = Gem::Specification.find_by_name 'a'
19
@user_spec = Gem::Specification.find_by_name 'b'
41
27
assert_match %r|/foo/bar$|, uninstaller.instance_variable_get(:@gem_home)
31
uninstaller = Gem::Uninstaller.new nil
33
ui = Gem::MockGemUi.new "y\n"
36
uninstaller.remove_all [@spec]
39
refute_path_exists @spec.gem_dir
44
42
def test_remove_executables_force_keep
45
43
uninstaller = Gem::Uninstaller.new nil, :executables => false
45
executable = File.join Gem.user_dir, 'bin', 'executable'
46
assert File.exist?(executable), 'executable not written'
48
uninstaller.remove_executables @spec
49
uninstaller.remove_executables @user_spec
51
assert_equal true, File.exist?(File.join(@gemhome, 'bin', 'executable'))
52
assert File.exist? executable
53
54
assert_equal "Executables and scripts will remain installed.\n", @ui.output
56
57
def test_remove_executables_force_remove
57
58
uninstaller = Gem::Uninstaller.new nil, :executables => true
60
executable = File.join Gem.user_dir, 'bin', 'executable'
61
assert File.exist?(executable), 'executable not written'
60
uninstaller.remove_executables @spec
64
uninstaller.remove_executables @user_spec
63
67
assert_equal "Removing executable\n", @ui.output
65
assert_equal false, File.exist?(File.join(@gemhome, 'bin', 'executable'))
69
refute File.exist? executable
68
72
def test_remove_executables_user
72
76
uninstaller.remove_executables @user_spec
75
exec_path = File.join Gem.user_dir, 'bin', 'my_exec'
76
assert_equal false, File.exist?(exec_path), 'removed exec from bin dir'
78
assert_equal "Removing my_exec\n", @ui.output
79
exec_path = File.join Gem.user_dir, 'bin', 'executable'
80
assert_equal false, File.exist?(exec_path), 'removed exec from bin dir'
82
assert_equal "Removing executable\n", @ui.output
85
def test_remove_executables_user_format
86
Gem::Installer.exec_format = 'foo-%s-bar'
88
uninstaller = Gem::Uninstaller.new nil, :executables => true, :format_executable => true
91
uninstaller.remove_executables @user_spec
94
exec_path = File.join Gem.user_dir, 'bin', 'foo-executable-bar'
95
assert_equal false, File.exist?(exec_path), 'removed exec from bin dir'
97
assert_equal "Removing foo-executable-bar\n", @ui.output
99
Gem::Installer.exec_format = nil
102
def test_remove_executables_user_format_disabled
103
Gem::Installer.exec_format = 'foo-%s-bar'
105
uninstaller = Gem::Uninstaller.new nil, :executables => true
108
uninstaller.remove_executables @user_spec
111
exec_path = File.join Gem.user_dir, 'bin', 'executable'
112
refute File.exist?(exec_path), 'removed exec from bin dir'
114
assert_equal "Removing executable\n", @ui.output
116
Gem::Installer.exec_format = nil
81
119
def test_path_ok_eh
87
125
def test_path_ok_eh_legacy
88
126
uninstaller = Gem::Uninstaller.new nil
90
@spec.loaded_from.gsub! @spec.full_name, '\&-legacy'
128
@spec.loaded_from = @spec.loaded_from.gsub @spec.full_name, '\&-legacy'
91
129
@spec.platform = 'legacy'
93
131
assert_equal true, uninstaller.path_ok?(@gemhome, @spec)
120
158
assert_same uninstaller, @post_uninstall_hook_arg
161
def test_uninstall_nonexistent
162
uninstaller = Gem::Uninstaller.new 'bogus', :executables => true
164
e = assert_raises Gem::InstallError do
165
uninstaller.uninstall
168
assert_equal 'gem "bogus" is not installed', e.message
171
def test_uninstall_not_ok
173
s.add_runtime_dependency @spec.name
176
uninstaller = Gem::Uninstaller.new @spec.name
178
gem_dir = File.join @gemhome, 'gems', @spec.full_name
179
executable = File.join @gemhome, 'bin', 'executable'
181
assert File.exist?(gem_dir), 'gem_dir must exist'
182
assert File.exist?(executable), 'executable must exist'
184
ui = Gem::MockGemUi.new "n\n"
186
assert_raises Gem::DependencyRemovalException do
188
uninstaller.uninstall
192
assert File.exist?(gem_dir), 'gem_dir must still exist'
193
assert File.exist?(executable), 'executable must still exist'
123
196
def test_uninstall_user
124
uninstaller = Gem::Uninstaller.new @user_spec.name, :executables => true,
125
:user_install => true
197
@user_spec = Gem::Specification.find_by_name 'b'
199
uninstaller = Gem::Uninstaller.new(@user_spec.name,
200
:executables => true,
201
:user_install => true)
127
203
gem_dir = File.join Gem.user_dir, 'gems', @user_spec.full_name
129
205
Gem.pre_uninstall do
130
assert File.exist?(gem_dir), 'gem_dir should exist'
206
assert_path_exists gem_dir
133
209
Gem.post_uninstall do
134
refute File.exist?(gem_dir), 'gem_dir should not exist'
210
refute_path_exists gem_dir
137
213
uninstaller.uninstall
139
refute File.exist?(gem_dir)
215
refute_path_exists gem_dir
141
217
assert_same uninstaller, @pre_uninstall_hook_arg
142
218
assert_same uninstaller, @post_uninstall_hook_arg
221
def test_uninstall_selection_greater_than_one
224
list = Gem::Specification.find_all_by_name('a')
226
uninstaller = Gem::Uninstaller.new('a')
228
use_ui Gem::MockGemUi.new("2\n") do
229
uninstaller.uninstall
232
updated_list = Gem::Specification.find_all_by_name('a')
233
assert_equal list.length - 1, updated_list.length