1require 'rubygems/test_case'
2require 'rubygems/commands/update_command'
3
4begin
5  gem "rdoc"
6rescue Gem::LoadError
7  # ignore
8end
9
10class TestGemCommandsUpdateCommand < Gem::TestCase
11
12  def setup
13    super
14
15    @cmd = Gem::Commands::UpdateCommand.new
16
17    @cmd.options[:document] = []
18
19    util_setup_fake_fetcher(true)
20    util_clear_gems
21    util_setup_spec_fetcher @a1, @a2, @a3a
22
23    @a1_path = @a1.cache_file
24    @a2_path = @a2.cache_file
25    @a3a_path = @a3a.cache_file
26
27    @fetcher.data["#{@gem_repo}gems/#{File.basename @a1_path}"] =
28      read_binary @a1_path
29    @fetcher.data["#{@gem_repo}gems/#{File.basename @a2_path}"] =
30      read_binary @a2_path
31    @fetcher.data["#{@gem_repo}gems/#{File.basename @a3a_path}"] =
32      read_binary @a3a_path
33  end
34
35  def test_execute
36    util_clear_gems
37
38    Gem::Installer.new(@a1_path).install
39
40    @cmd.options[:args] = []
41
42    use_ui @ui do
43      @cmd.execute
44    end
45
46    out = @ui.output.split "\n"
47    assert_equal "Updating installed gems", out.shift
48    assert_equal "Updating #{@a2.name}", out.shift
49    assert_equal "Gems updated: #{@a2.name}", out.shift
50    assert_empty out
51  end
52
53  def util_setup_rubygem version
54    gem = quick_spec('rubygems-update', version.to_s) do |s|
55      s.files = %w[setup.rb]
56    end
57    write_file File.join(*%W[gems #{gem.original_name} setup.rb])
58    util_build_gem gem
59    util_setup_spec_fetcher gem
60    gem
61  end
62
63  def util_setup_rubygem8
64    @rubygem8 = util_setup_rubygem 8
65  end
66
67  def util_setup_rubygem9
68    @rubygem9 = util_setup_rubygem 9
69  end
70
71  def util_setup_rubygem_current
72    @rubygem_current = util_setup_rubygem Gem::VERSION
73  end
74
75  def util_add_to_fetcher *specs
76    specs.each do |spec|
77      gem_file = spec.cache_file
78      file_name = File.basename gem_file
79
80      @fetcher.data["http://gems.example.com/gems/#{file_name}"] =
81        Gem.read_binary gem_file
82    end
83  end
84
85  def test_execute_system
86    util_clear_gems
87    util_setup_rubygem9
88    util_setup_spec_fetcher @rubygem9
89    util_add_to_fetcher @rubygem9
90
91    @cmd.options[:args]          = []
92    @cmd.options[:system]        = true
93
94    use_ui @ui do
95      @cmd.execute
96    end
97
98    out = @ui.output.split "\n"
99    assert_equal "Updating rubygems-update", out.shift
100    assert_equal "Installing RubyGems 9", out.shift
101    assert_equal "RubyGems system software updated", out.shift
102
103    assert_empty out
104  end
105
106  def test_execute_system_at_latest
107    util_clear_gems
108    util_setup_rubygem_current
109    util_setup_spec_fetcher @rubygem_current
110    util_add_to_fetcher @rubygem_current
111
112    @cmd.options[:args]          = []
113    @cmd.options[:system]        = true
114
115    assert_raises Gem::MockGemUi::SystemExitException do
116      use_ui @ui do
117        @cmd.execute
118      end
119    end
120
121    out = @ui.output.split "\n"
122    assert_equal "Latest version currently installed. Aborting.", out.shift
123    assert_empty out
124  end
125
126  def test_execute_system_multiple
127    util_clear_gems
128    util_setup_rubygem9
129    util_setup_rubygem8
130    util_setup_spec_fetcher @rubygem8, @rubygem9
131    util_add_to_fetcher @rubygem8, @rubygem9
132
133    @cmd.options[:args]          = []
134    @cmd.options[:system]        = true
135
136    use_ui @ui do
137      @cmd.execute
138    end
139
140    out = @ui.output.split "\n"
141    assert_equal "Updating rubygems-update", out.shift
142    assert_equal "Installing RubyGems 9", out.shift
143    assert_equal "RubyGems system software updated", out.shift
144
145    assert_empty out
146  end
147
148  def test_execute_system_specific
149    util_clear_gems
150    util_setup_rubygem9
151    util_setup_rubygem8
152    util_setup_spec_fetcher @rubygem8, @rubygem9
153    util_add_to_fetcher @rubygem8, @rubygem9
154
155    @cmd.options[:args]          = []
156    @cmd.options[:system]        = "8"
157
158    use_ui @ui do
159      @cmd.execute
160    end
161
162    out = @ui.output.split "\n"
163    assert_equal "Updating rubygems-update", out.shift
164    assert_equal "Installing RubyGems 8", out.shift
165    assert_equal "RubyGems system software updated", out.shift
166
167    assert_empty out
168  end
169
170  def test_execute_system_specifically_to_latest_version
171    util_clear_gems
172    util_setup_rubygem9
173    util_setup_rubygem8
174    util_setup_spec_fetcher @rubygem8, @rubygem9
175    util_add_to_fetcher @rubygem8, @rubygem9
176
177    @cmd.options[:args]          = []
178    @cmd.options[:system]        = "9"
179
180    use_ui @ui do
181      @cmd.execute
182    end
183
184    out = @ui.output.split "\n"
185    assert_equal "Updating rubygems-update", out.shift
186    assert_equal "Installing RubyGems 9", out.shift
187    assert_equal "RubyGems system software updated", out.shift
188
189    assert_empty out
190  end
191
192  def test_execute_system_with_gems
193    @cmd.options[:args]          = %w[gem]
194    @cmd.options[:system]        = true
195
196    assert_raises Gem::MockGemUi::TermError do
197      use_ui @ui do
198        @cmd.execute
199      end
200    end
201
202    assert_empty @ui.output
203    assert_equal "ERROR:  Gem names are not allowed with the --system option\n",
204                 @ui.error
205  end
206
207  # before:
208  #   a1 -> c1.2
209  # after:
210  #   a2 -> b2 # new dependency
211  #   a2 -> c2
212
213  def test_execute_dependencies
214    @a1.add_dependency 'c', '1.2'
215
216    @c2 = quick_spec 'c', '2' do |s|
217      s.files = %w[lib/code.rb]
218      s.require_paths = %w[lib]
219    end
220
221    @a2.add_dependency 'c', '2'
222    @a2.add_dependency 'b', '2'
223
224    @b2_path   = @b2.cache_file
225    @c1_2_path = @c1_2.cache_file
226    @c2_path   = @c2.cache_file
227
228    install_specs @a1, @a2, @b2, @c1_2, @c2
229
230    util_build_gem @a1
231    util_build_gem @a2
232    util_build_gem @c2
233
234    @fetcher.data["#{@gem_repo}gems/#{@a1.file_name}"] = read_binary @a1_path
235    @fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] = read_binary @a2_path
236    @fetcher.data["#{@gem_repo}gems/#{@b2.file_name}"] = read_binary @b2_path
237    @fetcher.data["#{@gem_repo}gems/#{@c1_2.file_name}"] = read_binary @c1_2_path
238    @fetcher.data["#{@gem_repo}gems/#{@c2.file_name}"] = read_binary @c2_path
239
240    util_setup_spec_fetcher @a1, @a2, @b2, @c1_2, @c2
241
242    Gem::Installer.new(@c1_2_path).install
243    Gem::Installer.new(@a1_path).install
244
245    Gem::Specification.reset
246
247    @cmd.options[:args] = []
248
249    use_ui @ui do
250      @cmd.execute
251    end
252
253    out = @ui.output.split "\n"
254    assert_equal "Updating installed gems", out.shift
255    assert_equal "Updating #{@a2.name}", out.shift
256    assert_equal "Gems updated: #{@c2.name} #{@b2.name} #{@a2.name}",
257                 out.shift
258
259    assert_empty out
260  end
261
262  def test_execute_rdoc
263    Gem.done_installing(&Gem::RDoc.method(:generation_hook))
264
265    @cmd.options[:document] = %w[rdoc ri]
266
267    util_clear_gems
268
269    Gem::Installer.new(@a1_path).install
270
271    @cmd.options[:args] = [@a1.name]
272
273    use_ui @ui do
274      @cmd.execute
275    end
276
277    wait_for_child_process_to_exit
278
279    assert_path_exists File.join(@a2.doc_dir, 'ri')
280    assert_path_exists File.join(@a2.doc_dir, 'rdoc')
281  end
282
283  def test_execute_named
284    util_clear_gems
285
286    Gem::Installer.new(@a1_path).install
287
288    @cmd.options[:args] = [@a1.name]
289
290    use_ui @ui do
291      @cmd.execute
292    end
293
294    out = @ui.output.split "\n"
295    assert_equal "Updating installed gems", out.shift
296    assert_equal "Updating #{@a2.name}", out.shift
297    assert_equal "Gems updated: #{@a2.name}", out.shift
298
299    assert_empty out
300  end
301
302  def test_execute_named_up_to_date
303    util_clear_gems
304
305    Gem::Installer.new(@a2_path).install
306
307    @cmd.options[:args] = [@a2.name]
308
309    use_ui @ui do
310      @cmd.execute
311    end
312
313    out = @ui.output.split "\n"
314    assert_equal "Updating installed gems", out.shift
315    assert_equal "Nothing to update", out.shift
316
317    assert_empty out
318  end
319
320  def test_execute_named_up_to_date_prerelease
321    util_clear_gems
322
323    Gem::Installer.new(@a2_path).install
324
325    @cmd.options[:args] = [@a2.name]
326    @cmd.options[:prerelease] = true
327
328    use_ui @ui do
329      @cmd.execute
330    end
331
332    out = @ui.output.split "\n"
333    assert_equal "Updating installed gems", out.shift
334    assert_equal "Updating #{@a3a.name}", out.shift
335    assert_equal "Gems updated: #{@a3a.name}", out.shift
336
337    assert_empty out
338  end
339
340  def test_execute_up_to_date
341    util_clear_gems
342
343    Gem::Installer.new(@a2_path).install
344
345    @cmd.options[:args] = []
346
347    use_ui @ui do
348      @cmd.execute
349    end
350
351    out = @ui.output.split "\n"
352    assert_equal "Updating installed gems", out.shift
353    assert_equal "Nothing to update", out.shift
354
355    assert_empty out
356  end
357
358  def test_execute_user_install
359    util_clear_gems
360
361    Gem::Installer.new(@a1_path).install
362
363    @cmd.handle_options %w[--user-install]
364
365    use_ui @ui do
366      @cmd.execute
367    end
368
369    installer = @cmd.installer
370    user_install = installer.instance_variable_get :@user_install
371
372    assert user_install, 'user_install must be set on the installer'
373  end
374
375  def test_handle_options_system
376    @cmd.handle_options %w[--system]
377
378    expected = {
379      :args     => [],
380      :document => %w[rdoc ri],
381      :force    => false,
382      :system   => true,
383    }
384
385    assert_equal expected, @cmd.options
386  end
387
388  def test_handle_options_system_non_version
389    assert_raises ArgumentError do
390      @cmd.handle_options %w[--system non-version]
391    end
392  end
393
394  def test_handle_options_system_specific
395    @cmd.handle_options %w[--system 1.3.7]
396
397    expected = {
398      :args     => [],
399      :document => %w[rdoc ri],
400      :force    => false,
401      :system   => "1.3.7",
402    }
403
404    assert_equal expected, @cmd.options
405  end
406
407end
408