1require 'rubygems/test_case'
2require 'rubygems/commands/query_command'
3
4class TestGemCommandsQueryCommand < Gem::TestCase
5
6  def setup
7    super
8
9    @cmd = Gem::Commands::QueryCommand.new
10
11    util_setup_fake_fetcher
12    util_clear_gems
13    util_setup_spec_fetcher @a1, @a2, @pl1, @a3a
14
15    @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] = proc do
16      raise Gem::RemoteFetcher::FetchError
17    end
18  end
19
20  def test_execute
21    @cmd.handle_options %w[-r]
22
23    use_ui @ui do
24      @cmd.execute
25    end
26
27    expected = <<-EOF
28
29*** REMOTE GEMS ***
30
31a (2)
32pl (1 i386-linux)
33    EOF
34
35    assert_equal expected, @ui.output
36    assert_equal '', @ui.error
37  end
38
39  def test_execute_platform
40    @a1r = @a1.dup
41
42    @a1.platform = 'x86-linux'
43    @a2.platform = 'universal-darwin'
44
45    util_clear_gems
46    util_setup_spec_fetcher @a1, @a1r, @a2, @b2, @pl1
47
48    @cmd.handle_options %w[-r -a]
49
50    use_ui @ui do
51      @cmd.execute
52    end
53
54    expected = <<-EOF
55
56*** REMOTE GEMS ***
57
58a (2 universal-darwin, 1 ruby x86-linux)
59b (2)
60pl (1 i386-linux)
61    EOF
62
63    assert_equal expected, @ui.output
64    assert_equal '', @ui.error
65  end
66
67  def test_execute_all
68    @cmd.handle_options %w[-r --all]
69
70    use_ui @ui do
71      @cmd.execute
72    end
73
74    expected = <<-EOF
75
76*** REMOTE GEMS ***
77
78a (2, 1)
79pl (1 i386-linux)
80    EOF
81
82    assert_equal expected, @ui.output
83    assert_equal '', @ui.error
84  end
85
86  def test_execute_all_prerelease
87    @cmd.handle_options %w[-r --all --prerelease]
88
89    use_ui @ui do
90      @cmd.execute
91    end
92
93    expected = <<-EOF
94
95*** REMOTE GEMS ***
96
97a (3.a, 2, 1)
98pl (1 i386-linux)
99    EOF
100
101    assert_equal expected, @ui.output
102    assert_equal '', @ui.error
103  end
104
105  def test_execute_details
106    @a2.summary = 'This is a lot of text. ' * 4
107    @a2.authors = ['Abraham Lincoln', 'Hirohito']
108    @a2.homepage = 'http://a.example.com/'
109
110    util_clear_gems
111    util_setup_spec_fetcher @a1, @a2, @pl1
112
113    @cmd.handle_options %w[-r -d]
114
115    use_ui @ui do
116      @cmd.execute
117    end
118
119    expected = <<-EOF
120
121*** REMOTE GEMS ***
122
123a (2)
124    Authors: Abraham Lincoln, Hirohito
125    Homepage: http://a.example.com/
126
127    This is a lot of text. This is a lot of text. This is a lot of text.
128    This is a lot of text.
129
130pl (1)
131    Platform: i386-linux
132    Author: A User
133    Homepage: http://example.com
134
135    this is a summary
136    EOF
137
138    assert_equal expected, @ui.output
139    assert_equal '', @ui.error
140  end
141
142  def test_execute_details_platform
143    @a1.platform = 'x86-linux'
144
145    @a2.summary = 'This is a lot of text. ' * 4
146    @a2.authors = ['Abraham Lincoln', 'Hirohito']
147    @a2.homepage = 'http://a.example.com/'
148    @a2.platform = 'universal-darwin'
149
150    util_clear_gems
151    util_setup_spec_fetcher @a1, @a2, @pl1
152
153    @cmd.handle_options %w[-r -d]
154
155    use_ui @ui do
156      @cmd.execute
157    end
158
159    expected = <<-EOF
160
161*** REMOTE GEMS ***
162
163a (2, 1)
164    Platforms:
165        1: x86-linux
166        2: universal-darwin
167    Authors: Abraham Lincoln, Hirohito
168    Homepage: http://a.example.com/
169
170    This is a lot of text. This is a lot of text. This is a lot of text.
171    This is a lot of text.
172
173pl (1)
174    Platform: i386-linux
175    Author: A User
176    Homepage: http://example.com
177
178    this is a summary
179    EOF
180
181    assert_equal expected, @ui.output
182    assert_equal '', @ui.error
183  end
184
185  def test_execute_installed
186    @cmd.handle_options %w[-n a --installed]
187
188    assert_raises Gem::MockGemUi::SystemExitException do
189      use_ui @ui do
190        @cmd.execute
191      end
192    end
193
194    assert_equal "true\n", @ui.output
195    assert_equal '', @ui.error
196  end
197
198  def test_execute_installed_inverse
199    @cmd.handle_options %w[-n a --no-installed]
200
201    e = assert_raises Gem::MockGemUi::TermError do
202      use_ui @ui do
203        @cmd.execute
204      end
205    end
206
207    assert_equal "false\n", @ui.output
208    assert_equal '', @ui.error
209
210    assert_equal 1, e.exit_code
211  end
212
213  def test_execute_installed_inverse_not_installed
214    @cmd.handle_options %w[-n not_installed --no-installed]
215
216    assert_raises Gem::MockGemUi::SystemExitException do
217      use_ui @ui do
218        @cmd.execute
219      end
220    end
221
222    assert_equal "true\n", @ui.output
223    assert_equal '', @ui.error
224  end
225
226  def test_execute_installed_no_name
227    @cmd.handle_options %w[--installed]
228
229    e = assert_raises Gem::MockGemUi::TermError do
230      use_ui @ui do
231        @cmd.execute
232      end
233    end
234
235    assert_equal '', @ui.output
236    assert_equal "ERROR:  You must specify a gem name\n", @ui.error
237
238    assert_equal 4, e.exit_code
239  end
240
241  def test_execute_installed_not_installed
242    @cmd.handle_options %w[-n not_installed --installed]
243
244    e = assert_raises Gem::MockGemUi::TermError do
245      use_ui @ui do
246        @cmd.execute
247      end
248    end
249
250    assert_equal "false\n", @ui.output
251    assert_equal '', @ui.error
252
253    assert_equal 1, e.exit_code
254  end
255
256  def test_execute_installed_version
257    @cmd.handle_options %w[-n a --installed --version 2]
258
259    assert_raises Gem::MockGemUi::SystemExitException do
260      use_ui @ui do
261        @cmd.execute
262      end
263    end
264
265    assert_equal "true\n", @ui.output
266    assert_equal '', @ui.error
267  end
268
269  def test_execute_installed_version_not_installed
270    @cmd.handle_options %w[-n c --installed --version 2]
271
272    e = assert_raises Gem::MockGemUi::TermError do
273      use_ui @ui do
274        @cmd.execute
275      end
276    end
277
278    assert_equal "false\n", @ui.output
279    assert_equal '', @ui.error
280
281    assert_equal 1, e.exit_code
282  end
283
284  def test_execute_local_notty
285    @cmd.handle_options %w[]
286
287    @ui.outs.tty = false
288
289    use_ui @ui do
290      @cmd.execute
291    end
292
293    expected = <<-EOF
294a (3.a, 2, 1)
295pl (1 i386-linux)
296    EOF
297
298    assert_equal expected, @ui.output
299    assert_equal '', @ui.error
300  end
301
302  def test_execute_no_versions
303    @cmd.handle_options %w[-r --no-versions]
304
305    use_ui @ui do
306      @cmd.execute
307    end
308
309    expected = <<-EOF
310
311*** REMOTE GEMS ***
312
313a
314pl
315    EOF
316
317    assert_equal expected, @ui.output
318    assert_equal '', @ui.error
319  end
320
321  def test_execute_notty
322    @cmd.handle_options %w[-r]
323
324    @ui.outs.tty = false
325
326    use_ui @ui do
327      @cmd.execute
328    end
329
330    expected = <<-EOF
331a (2)
332pl (1 i386-linux)
333    EOF
334
335    assert_equal expected, @ui.output
336    assert_equal '', @ui.error
337  end
338
339  def test_execute_prerelease
340    @cmd.handle_options %w[-r --prerelease]
341
342    use_ui @ui do
343      @cmd.execute
344    end
345
346    expected = <<-EOF
347
348*** REMOTE GEMS ***
349
350a (3.a)
351    EOF
352
353    assert_equal expected, @ui.output
354    assert_equal '', @ui.error
355  end
356
357  def test_execute_prerelease_local
358    @cmd.handle_options %w[-l --prerelease]
359
360    use_ui @ui do
361      @cmd.execute
362    end
363
364    expected = <<-EOF
365
366*** LOCAL GEMS ***
367
368a (3.a, 2, 1)
369pl (1 i386-linux)
370    EOF
371
372    assert_equal expected, @ui.output
373    assert_equal "WARNING:  prereleases are always shown locally\n", @ui.error
374  end
375
376  def test_execute_local_details
377    @a1.platform = 'x86-linux'
378
379    @a2.summary = 'This is a lot of text. ' * 4
380    @a2.authors = ['Abraham Lincoln', 'Hirohito']
381    @a2.homepage = 'http://a.example.com/'
382    @a2.platform = 'universal-darwin'
383
384    util_clear_gems
385    util_setup_spec_fetcher @a1, @a2, @pl1
386
387    @cmd.handle_options %w[-l -d]
388
389    use_ui @ui do
390      @cmd.execute
391    end
392
393    str = @ui.output
394
395    str.gsub!(/\(\d\): [^\n]*/, "-")
396    str.gsub!(/at: [^\n]*/, "at: -")
397
398    expected = <<-EOF
399*** LOCAL GEMS ***
400
401a (2, 1)
402    Platforms:
403        1: x86-linux
404        2: universal-darwin
405    Authors: Abraham Lincoln, Hirohito
406    Homepage: http://a.example.com/
407    Installed at -
408                 -
409
410    This is a lot of text. This is a lot of text. This is a lot of text.
411    This is a lot of text.
412
413pl \(1\)
414    Platform: i386-linux
415    Author: A User
416    Homepage: http://example.com
417    Installed at: -
418
419    this is a summary
420    EOF
421
422    assert_match expected, @ui.output
423  end
424
425  def test_execute_default_details
426    default_gem_dir = Gem::Specification.default_specifications_dir
427    @a1.loaded_from =
428      File.join default_gem_dir, @a1.spec_name
429
430    @cmd.handle_options %w[-l -d]
431
432    use_ui @ui do
433      @cmd.execute
434    end
435
436    expected = <<-EOF
437
438*** LOCAL GEMS ***
439
440a (3.a, 2, 1)
441    Author: A User
442    Homepage: http://example.com
443    Installed at (3.a): #{@gemhome}
444                 (2): #{@gemhome}
445                 (1, default): #{@a1.base_dir}
446
447    this is a summary
448
449pl \(1\)
450    Platform: i386-linux
451    Author: A User
452    Homepage: http://example.com
453    Installed at: #{@gemhome}
454
455    this is a summary
456    EOF
457
458    assert_equal expected, @ui.output
459  end
460
461  def test_make_entry
462    @fetcher.data.delete \
463      "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{@a2.original_name}.gemspec.rz"
464
465    entry_tuples = [
466      [Gem::NameTuple.new(@a2.name, @a2.version, @a2.platform),
467       Gem.sources.first],
468    ]
469
470    platforms = { @a2.version => [@a2.platform] }
471
472    entry = @cmd.send :make_entry, entry_tuples, platforms
473
474    assert_equal 'a (2)', entry
475  end
476
477end
478
479