~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/rubygems/test_gem_uninstaller.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require_relative 'gem_installer_test_case'
 
1
require 'rubygems/installer_test_case'
2
2
require 'rubygems/uninstaller'
3
3
 
4
 
class TestGemUninstaller < GemInstallerTestCase
 
4
class TestGemUninstaller < Gem::InstallerTestCase
5
5
 
6
6
  def setup
7
7
    super
8
8
 
9
 
    ui = MockGemUi.new
10
 
    util_setup_gem ui
11
 
 
12
 
    @user_spec.executables = ["my_exec"]
13
 
 
14
 
    # HACK util_make_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"
20
 
    end
21
 
 
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"
27
 
    end
 
9
    @user_spec.executables = ["executable"]
28
10
 
29
11
    build_rake_in do
30
12
      use_ui ui do
31
13
        @installer.install
32
14
        @user_installer.install
33
 
        Gem::Uninstaller.new(@user_spec.name, :executables => false).uninstall
 
15
 
 
16
        Gem.use_paths @gemhome, Gem.user_dir
 
17
 
 
18
        @spec      = Gem::Specification.find_by_name 'a'
 
19
        @user_spec = Gem::Specification.find_by_name 'b'
34
20
      end
35
21
    end
36
22
  end
41
27
    assert_match %r|/foo/bar$|, uninstaller.instance_variable_get(:@gem_home)
42
28
  end
43
29
 
 
30
  def test_remove_all
 
31
    uninstaller = Gem::Uninstaller.new nil
 
32
 
 
33
    ui = Gem::MockGemUi.new "y\n"
 
34
 
 
35
    use_ui ui do
 
36
      uninstaller.remove_all [@spec]
 
37
    end
 
38
 
 
39
    refute_path_exists @spec.gem_dir
 
40
  end
 
41
 
44
42
  def test_remove_executables_force_keep
45
43
    uninstaller = Gem::Uninstaller.new nil, :executables => false
46
44
 
 
45
    executable = File.join Gem.user_dir, 'bin', 'executable'
 
46
    assert File.exist?(executable), 'executable not written'
 
47
 
47
48
    use_ui @ui do
48
 
      uninstaller.remove_executables @spec
 
49
      uninstaller.remove_executables @user_spec
49
50
    end
50
51
 
51
 
    assert_equal true, File.exist?(File.join(@gemhome, 'bin', 'executable'))
 
52
    assert File.exist? executable
52
53
 
53
54
    assert_equal "Executables and scripts will remain installed.\n", @ui.output
54
55
  end
56
57
  def test_remove_executables_force_remove
57
58
    uninstaller = Gem::Uninstaller.new nil, :executables => true
58
59
 
 
60
    executable = File.join Gem.user_dir, 'bin', 'executable'
 
61
    assert File.exist?(executable), 'executable not written'
 
62
 
59
63
    use_ui @ui do
60
 
      uninstaller.remove_executables @spec
 
64
      uninstaller.remove_executables @user_spec
61
65
    end
62
66
 
63
67
    assert_equal "Removing executable\n", @ui.output
64
68
 
65
 
    assert_equal false, File.exist?(File.join(@gemhome, 'bin', 'executable'))
 
69
    refute File.exist? executable
66
70
  end
67
71
 
68
72
  def test_remove_executables_user
72
76
      uninstaller.remove_executables @user_spec
73
77
    end
74
78
 
75
 
    exec_path = File.join Gem.user_dir, 'bin', 'my_exec'
76
 
    assert_equal false, File.exist?(exec_path), 'removed exec from bin dir'
77
 
 
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'
 
81
 
 
82
    assert_equal "Removing executable\n", @ui.output
 
83
  end
 
84
 
 
85
  def test_remove_executables_user_format
 
86
    Gem::Installer.exec_format = 'foo-%s-bar'
 
87
 
 
88
    uninstaller = Gem::Uninstaller.new nil, :executables => true, :format_executable => true
 
89
 
 
90
    use_ui @ui do
 
91
      uninstaller.remove_executables @user_spec
 
92
    end
 
93
 
 
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'
 
96
 
 
97
    assert_equal "Removing foo-executable-bar\n", @ui.output
 
98
  ensure
 
99
    Gem::Installer.exec_format = nil
 
100
  end
 
101
 
 
102
  def test_remove_executables_user_format_disabled
 
103
    Gem::Installer.exec_format = 'foo-%s-bar'
 
104
 
 
105
    uninstaller = Gem::Uninstaller.new nil, :executables => true
 
106
 
 
107
    use_ui @ui do
 
108
      uninstaller.remove_executables @user_spec
 
109
    end
 
110
 
 
111
    exec_path = File.join Gem.user_dir, 'bin', 'executable'
 
112
    refute File.exist?(exec_path), 'removed exec from bin dir'
 
113
 
 
114
    assert_equal "Removing executable\n", @ui.output
 
115
  ensure
 
116
    Gem::Installer.exec_format = nil
79
117
  end
80
118
 
81
119
  def test_path_ok_eh
87
125
  def test_path_ok_eh_legacy
88
126
    uninstaller = Gem::Uninstaller.new nil
89
127
 
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'
92
130
 
93
131
    assert_equal true, uninstaller.path_ok?(@gemhome, @spec)
120
158
    assert_same uninstaller, @post_uninstall_hook_arg
121
159
  end
122
160
 
 
161
  def test_uninstall_nonexistent
 
162
    uninstaller = Gem::Uninstaller.new 'bogus', :executables => true
 
163
 
 
164
    e = assert_raises Gem::InstallError do
 
165
      uninstaller.uninstall
 
166
    end
 
167
 
 
168
    assert_equal 'gem "bogus" is not installed', e.message
 
169
  end
 
170
 
 
171
  def test_uninstall_not_ok
 
172
    quick_gem 'z' do |s|
 
173
      s.add_runtime_dependency @spec.name
 
174
    end
 
175
 
 
176
    uninstaller = Gem::Uninstaller.new @spec.name
 
177
 
 
178
    gem_dir = File.join @gemhome, 'gems', @spec.full_name
 
179
    executable = File.join @gemhome, 'bin', 'executable'
 
180
 
 
181
    assert File.exist?(gem_dir),    'gem_dir must exist'
 
182
    assert File.exist?(executable), 'executable must exist'
 
183
 
 
184
    ui = Gem::MockGemUi.new "n\n"
 
185
 
 
186
    assert_raises Gem::DependencyRemovalException do
 
187
      use_ui ui do
 
188
        uninstaller.uninstall
 
189
      end
 
190
    end
 
191
 
 
192
    assert File.exist?(gem_dir),    'gem_dir must still exist'
 
193
    assert File.exist?(executable), 'executable must still exist'
 
194
  end
 
195
 
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'
 
198
 
 
199
    uninstaller = Gem::Uninstaller.new(@user_spec.name,
 
200
                                       :executables  => true,
 
201
                                       :user_install => true)
126
202
 
127
203
    gem_dir = File.join Gem.user_dir, 'gems', @user_spec.full_name
128
204
 
129
205
    Gem.pre_uninstall do
130
 
      assert File.exist?(gem_dir), 'gem_dir should exist'
 
206
      assert_path_exists gem_dir
131
207
    end
132
208
 
133
209
    Gem.post_uninstall do
134
 
      refute File.exist?(gem_dir), 'gem_dir should not exist'
 
210
      refute_path_exists gem_dir
135
211
    end
136
212
 
137
213
    uninstaller.uninstall
138
214
 
139
 
    refute File.exist?(gem_dir)
 
215
    refute_path_exists gem_dir
140
216
 
141
217
    assert_same uninstaller, @pre_uninstall_hook_arg
142
218
    assert_same uninstaller, @post_uninstall_hook_arg
143
219
  end
144
220
 
 
221
  def test_uninstall_selection_greater_than_one
 
222
    util_make_gems
 
223
    
 
224
    list = Gem::Specification.find_all_by_name('a')
 
225
 
 
226
    uninstaller = Gem::Uninstaller.new('a')
 
227
 
 
228
    use_ui Gem::MockGemUi.new("2\n") do
 
229
      uninstaller.uninstall
 
230
    end
 
231
 
 
232
    updated_list = Gem::Specification.find_all_by_name('a')
 
233
    assert_equal list.length - 1, updated_list.length
 
234
  end
145
235
end
146