PageRenderTime 65ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rubygems/test_gem_commands_query_command.rb

https://github.com/wanabe/ruby
Ruby | 857 lines | 614 code | 240 blank | 3 comment | 1 complexity | 461cb817b38fd02cf2dfdc6860194ca9 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, 0BSD, Unlicense, GPL-2.0, BSD-3-Clause
  1. # frozen_string_literal: true
  2. require_relative 'helper'
  3. require 'rubygems/commands/query_command'
  4. module TestGemCommandsQueryCommandSetup
  5. def setup
  6. super
  7. @cmd = Gem::Commands::QueryCommand.new
  8. @specs = add_gems_to_fetcher
  9. @stub_ui = Gem::MockGemUi.new
  10. @stub_fetcher = Gem::FakeFetcher.new
  11. @stub_fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] = proc do
  12. raise Gem::RemoteFetcher::FetchError
  13. end
  14. end
  15. end
  16. class TestGemCommandsQueryCommandWithInstalledGems < Gem::TestCase
  17. include TestGemCommandsQueryCommandSetup
  18. def test_execute
  19. spec_fetcher do |fetcher|
  20. fetcher.legacy_platform
  21. end
  22. @cmd.handle_options %w[-r]
  23. use_ui @stub_ui do
  24. @cmd.execute
  25. end
  26. expected = <<-EOF
  27. *** REMOTE GEMS ***
  28. a (2)
  29. pl (1 i386-linux)
  30. EOF
  31. assert_equal expected, @stub_ui.output
  32. assert_equal '', @stub_ui.error
  33. end
  34. def test_execute_all
  35. spec_fetcher do |fetcher|
  36. fetcher.legacy_platform
  37. end
  38. @cmd.handle_options %w[-r --all]
  39. use_ui @stub_ui do
  40. @cmd.execute
  41. end
  42. expected = <<-EOF
  43. *** REMOTE GEMS ***
  44. a (2, 1)
  45. pl (1 i386-linux)
  46. EOF
  47. assert_equal expected, @stub_ui.output
  48. assert_equal '', @stub_ui.error
  49. end
  50. def test_execute_all_prerelease
  51. spec_fetcher do |fetcher|
  52. fetcher.legacy_platform
  53. end
  54. @cmd.handle_options %w[-r --all --prerelease]
  55. use_ui @stub_ui do
  56. @cmd.execute
  57. end
  58. expected = <<-EOF
  59. *** REMOTE GEMS ***
  60. a (3.a, 2, 1)
  61. pl (1 i386-linux)
  62. EOF
  63. assert_equal expected, @stub_ui.output
  64. assert_equal '', @stub_ui.error
  65. end
  66. def test_execute_details
  67. spec_fetcher do |fetcher|
  68. fetcher.spec 'a', 2 do |s|
  69. s.summary = 'This is a lot of text. ' * 4
  70. s.authors = ['Abraham Lincoln', 'Hirohito']
  71. s.homepage = 'http://a.example.com/'
  72. end
  73. fetcher.legacy_platform
  74. end
  75. @cmd.handle_options %w[-r -d]
  76. use_ui @stub_ui do
  77. @cmd.execute
  78. end
  79. expected = <<-EOF
  80. *** REMOTE GEMS ***
  81. a (2)
  82. Authors: Abraham Lincoln, Hirohito
  83. Homepage: http://a.example.com/
  84. This is a lot of text. This is a lot of text. This is a lot of text.
  85. This is a lot of text.
  86. pl (1)
  87. Platform: i386-linux
  88. Author: A User
  89. Homepage: http://example.com
  90. this is a summary
  91. EOF
  92. assert_equal expected, @stub_ui.output
  93. assert_equal '', @stub_ui.error
  94. end
  95. def test_execute_details_cleans_text
  96. spec_fetcher do |fetcher|
  97. fetcher.spec 'a', 2 do |s|
  98. s.summary = 'This is a lot of text. ' * 4
  99. s.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
  100. s.homepage = "http://a.example.com/\x03"
  101. end
  102. fetcher.legacy_platform
  103. end
  104. @cmd.handle_options %w[-r -d]
  105. use_ui @stub_ui do
  106. @cmd.execute
  107. end
  108. expected = <<-EOF
  109. *** REMOTE GEMS ***
  110. a (2)
  111. Authors: Abraham Lincoln ., . Hirohito
  112. Homepage: http://a.example.com/.
  113. This is a lot of text. This is a lot of text. This is a lot of text.
  114. This is a lot of text.
  115. pl (1)
  116. Platform: i386-linux
  117. Author: A User
  118. Homepage: http://example.com
  119. this is a summary
  120. EOF
  121. assert_equal expected, @stub_ui.output
  122. assert_equal '', @stub_ui.error
  123. end
  124. def test_execute_details_truncates_summary
  125. spec_fetcher do |fetcher|
  126. fetcher.spec 'a', 2 do |s|
  127. s.summary = 'This is a lot of text. ' * 10_000
  128. s.authors = ["Abraham Lincoln \x01", "\x02 Hirohito"]
  129. s.homepage = "http://a.example.com/\x03"
  130. end
  131. fetcher.legacy_platform
  132. end
  133. @cmd.handle_options %w[-r -d]
  134. use_ui @stub_ui do
  135. @cmd.execute
  136. end
  137. expected = <<-EOF
  138. *** REMOTE GEMS ***
  139. a (2)
  140. Authors: Abraham Lincoln ., . Hirohito
  141. Homepage: http://a.example.com/.
  142. Truncating the summary for a-2 to 100,000 characters:
  143. #{" This is a lot of text. This is a lot of text. This is a lot of text.\n" * 1449} This is a lot of te
  144. pl (1)
  145. Platform: i386-linux
  146. Author: A User
  147. Homepage: http://example.com
  148. this is a summary
  149. EOF
  150. assert_equal expected, @stub_ui.output
  151. assert_equal '', @stub_ui.error
  152. end
  153. def test_execute_installed
  154. @cmd.handle_options %w[-n a --installed]
  155. assert_raise Gem::MockGemUi::SystemExitException do
  156. use_ui @stub_ui do
  157. @cmd.execute
  158. end
  159. end
  160. assert_equal "true\n", @stub_ui.output
  161. assert_equal '', @stub_ui.error
  162. end
  163. def test_execute_installed_inverse
  164. @cmd.handle_options %w[-n a --no-installed]
  165. e = assert_raise Gem::MockGemUi::TermError do
  166. use_ui @stub_ui do
  167. @cmd.execute
  168. end
  169. end
  170. assert_equal "false\n", @stub_ui.output
  171. assert_equal '', @stub_ui.error
  172. assert_equal 1, e.exit_code
  173. end
  174. def test_execute_installed_inverse_not_installed
  175. @cmd.handle_options %w[-n not_installed --no-installed]
  176. assert_raise Gem::MockGemUi::SystemExitException do
  177. use_ui @stub_ui do
  178. @cmd.execute
  179. end
  180. end
  181. assert_equal "true\n", @stub_ui.output
  182. assert_equal '', @stub_ui.error
  183. end
  184. def test_execute_installed_no_name
  185. @cmd.handle_options %w[--installed]
  186. e = assert_raise Gem::MockGemUi::TermError do
  187. use_ui @stub_ui do
  188. @cmd.execute
  189. end
  190. end
  191. assert_equal '', @stub_ui.output
  192. assert_equal "ERROR: You must specify a gem name\n", @stub_ui.error
  193. assert_equal 4, e.exit_code
  194. end
  195. def test_execute_installed_not_installed
  196. @cmd.handle_options %w[-n not_installed --installed]
  197. e = assert_raise Gem::MockGemUi::TermError do
  198. use_ui @stub_ui do
  199. @cmd.execute
  200. end
  201. end
  202. assert_equal "false\n", @stub_ui.output
  203. assert_equal '', @stub_ui.error
  204. assert_equal 1, e.exit_code
  205. end
  206. def test_execute_installed_version
  207. @cmd.handle_options %w[-n a --installed --version 2]
  208. assert_raise Gem::MockGemUi::SystemExitException do
  209. use_ui @stub_ui do
  210. @cmd.execute
  211. end
  212. end
  213. assert_equal "true\n", @stub_ui.output
  214. assert_equal '', @stub_ui.error
  215. end
  216. def test_execute_installed_version_not_installed
  217. @cmd.handle_options %w[-n c --installed --version 2]
  218. e = assert_raise Gem::MockGemUi::TermError do
  219. use_ui @stub_ui do
  220. @cmd.execute
  221. end
  222. end
  223. assert_equal "false\n", @stub_ui.output
  224. assert_equal '', @stub_ui.error
  225. assert_equal 1, e.exit_code
  226. end
  227. def test_execute_local
  228. spec_fetcher do |fetcher|
  229. fetcher.legacy_platform
  230. end
  231. @cmd.options[:domain] = :local
  232. use_ui @stub_ui do
  233. @cmd.execute
  234. end
  235. expected = <<-EOF
  236. *** LOCAL GEMS ***
  237. a (3.a, 2, 1)
  238. pl (1 i386-linux)
  239. EOF
  240. assert_equal expected, @stub_ui.output
  241. assert_equal '', @stub_ui.error
  242. end
  243. def test_execute_local_notty
  244. spec_fetcher do |fetcher|
  245. fetcher.legacy_platform
  246. end
  247. @cmd.handle_options %w[]
  248. @stub_ui.outs.tty = false
  249. use_ui @stub_ui do
  250. @cmd.execute
  251. end
  252. expected = <<-EOF
  253. a (3.a, 2, 1)
  254. pl (1 i386-linux)
  255. EOF
  256. assert_equal expected, @stub_ui.output
  257. assert_equal '', @stub_ui.error
  258. end
  259. def test_execute_local_quiet
  260. spec_fetcher do |fetcher|
  261. fetcher.legacy_platform
  262. end
  263. @cmd.options[:domain] = :local
  264. Gem.configuration.verbose = false
  265. use_ui @stub_ui do
  266. @cmd.execute
  267. end
  268. expected = <<-EOF
  269. a (3.a, 2, 1)
  270. pl (1 i386-linux)
  271. EOF
  272. assert_equal expected, @stub_ui.output
  273. assert_equal '', @stub_ui.error
  274. end
  275. def test_execute_no_versions
  276. spec_fetcher do |fetcher|
  277. fetcher.legacy_platform
  278. end
  279. @cmd.handle_options %w[-r --no-versions]
  280. use_ui @stub_ui do
  281. @cmd.execute
  282. end
  283. expected = <<-EOF
  284. *** REMOTE GEMS ***
  285. a
  286. pl
  287. EOF
  288. assert_equal expected, @stub_ui.output
  289. assert_equal '', @stub_ui.error
  290. end
  291. def test_execute_notty
  292. spec_fetcher do |fetcher|
  293. fetcher.legacy_platform
  294. end
  295. @cmd.handle_options %w[-r]
  296. @stub_ui.outs.tty = false
  297. use_ui @stub_ui do
  298. @cmd.execute
  299. end
  300. expected = <<-EOF
  301. a (2)
  302. pl (1 i386-linux)
  303. EOF
  304. assert_equal expected, @stub_ui.output
  305. assert_equal '', @stub_ui.error
  306. end
  307. def test_execute_prerelease
  308. @cmd.handle_options %w[-r --prerelease]
  309. use_ui @stub_ui do
  310. @cmd.execute
  311. end
  312. expected = <<-EOF
  313. *** REMOTE GEMS ***
  314. a (3.a)
  315. EOF
  316. assert_equal expected, @stub_ui.output
  317. assert_equal '', @stub_ui.error
  318. end
  319. def test_execute_prerelease_local
  320. spec_fetcher do |fetcher|
  321. fetcher.legacy_platform
  322. end
  323. @cmd.handle_options %w[-l --prerelease]
  324. use_ui @stub_ui do
  325. @cmd.execute
  326. end
  327. expected = <<-EOF
  328. *** LOCAL GEMS ***
  329. a (3.a, 2, 1)
  330. pl (1 i386-linux)
  331. EOF
  332. assert_equal expected, @stub_ui.output
  333. end
  334. def test_execute_no_prerelease_local
  335. spec_fetcher do |fetcher|
  336. fetcher.legacy_platform
  337. end
  338. @cmd.handle_options %w[-l --no-prerelease]
  339. use_ui @stub_ui do
  340. @cmd.execute
  341. end
  342. expected = <<-EOF
  343. *** LOCAL GEMS ***
  344. a (2, 1)
  345. pl (1 i386-linux)
  346. EOF
  347. assert_equal expected, @stub_ui.output
  348. end
  349. def test_execute_remote
  350. spec_fetcher do |fetcher|
  351. fetcher.legacy_platform
  352. end
  353. @cmd.options[:domain] = :remote
  354. use_ui @stub_ui do
  355. @cmd.execute
  356. end
  357. expected = <<-EOF
  358. *** REMOTE GEMS ***
  359. a (2)
  360. pl (1 i386-linux)
  361. EOF
  362. assert_equal expected, @stub_ui.output
  363. assert_equal '', @stub_ui.error
  364. end
  365. def test_execute_remote_notty
  366. spec_fetcher do |fetcher|
  367. fetcher.legacy_platform
  368. end
  369. @cmd.handle_options %w[]
  370. @stub_ui.outs.tty = false
  371. use_ui @stub_ui do
  372. @cmd.execute
  373. end
  374. expected = <<-EOF
  375. a (3.a, 2, 1)
  376. pl (1 i386-linux)
  377. EOF
  378. assert_equal expected, @stub_ui.output
  379. assert_equal '', @stub_ui.error
  380. end
  381. def test_execute_remote_quiet
  382. spec_fetcher do |fetcher|
  383. fetcher.legacy_platform
  384. end
  385. @cmd.options[:domain] = :remote
  386. Gem.configuration.verbose = false
  387. use_ui @stub_ui do
  388. @cmd.execute
  389. end
  390. expected = <<-EOF
  391. a (2)
  392. pl (1 i386-linux)
  393. EOF
  394. assert_equal expected, @stub_ui.output
  395. assert_equal '', @stub_ui.error
  396. end
  397. def test_make_entry
  398. a_2_name = @specs['a-2'].original_name
  399. @stub_fetcher.data.delete \
  400. "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{a_2_name}.gemspec.rz"
  401. a2 = @specs['a-2']
  402. entry_tuples = [
  403. [Gem::NameTuple.new(a2.name, a2.version, a2.platform),
  404. Gem.sources.first],
  405. ]
  406. platforms = { a2.version => [a2.platform] }
  407. entry = @cmd.send :make_entry, entry_tuples, platforms
  408. assert_equal 'a (2)', entry
  409. end
  410. # Test for multiple args handling!
  411. def test_execute_multiple_args
  412. spec_fetcher do |fetcher|
  413. fetcher.legacy_platform
  414. end
  415. @cmd.handle_options %w[a pl]
  416. use_ui @stub_ui do
  417. @cmd.execute
  418. end
  419. assert_match %r{^a }, @stub_ui.output
  420. assert_match %r{^pl }, @stub_ui.output
  421. assert_equal '', @stub_ui.error
  422. end
  423. def test_show_gems
  424. @cmd.options[:name] = //
  425. @cmd.options[:domain] = :remote
  426. use_ui @stub_ui do
  427. @cmd.send :show_gems, /a/i
  428. end
  429. assert_match %r{^a }, @stub_ui.output
  430. refute_match %r{^pl }, @stub_ui.output
  431. assert_empty @stub_ui.error
  432. end
  433. private
  434. def add_gems_to_fetcher
  435. spec_fetcher do |fetcher|
  436. fetcher.spec 'a', 1
  437. fetcher.spec 'a', 2
  438. fetcher.spec 'a', '3.a'
  439. end
  440. end
  441. end
  442. class TestGemCommandsQueryCommandWithoutInstalledGems < Gem::TestCase
  443. include TestGemCommandsQueryCommandSetup
  444. def test_execute_platform
  445. spec_fetcher do |fetcher|
  446. fetcher.spec 'a', 1
  447. fetcher.spec 'a', 1 do |s|
  448. s.platform = 'x86-linux'
  449. end
  450. fetcher.spec 'a', 2 do |s|
  451. s.platform = 'universal-darwin'
  452. end
  453. end
  454. @cmd.handle_options %w[-r -a]
  455. use_ui @stub_ui do
  456. @cmd.execute
  457. end
  458. expected = <<-EOF
  459. *** REMOTE GEMS ***
  460. a (2 universal-darwin, 1 ruby x86-linux)
  461. EOF
  462. assert_equal expected, @stub_ui.output
  463. assert_equal '', @stub_ui.error
  464. end
  465. def test_execute_show_default_gems
  466. spec_fetcher {|fetcher| fetcher.spec 'a', 2 }
  467. a1 = new_default_spec 'a', 1
  468. install_default_gems a1
  469. use_ui @stub_ui do
  470. @cmd.execute
  471. end
  472. expected = <<-EOF
  473. *** LOCAL GEMS ***
  474. a (2, default: 1)
  475. EOF
  476. assert_equal expected, @stub_ui.output
  477. end
  478. def test_execute_show_default_gems_with_platform
  479. a1 = new_default_spec 'a', 1
  480. a1.platform = 'java'
  481. install_default_gems a1
  482. use_ui @stub_ui do
  483. @cmd.execute
  484. end
  485. expected = <<-EOF
  486. *** LOCAL GEMS ***
  487. a (default: 1 java)
  488. EOF
  489. assert_equal expected, @stub_ui.output
  490. end
  491. def test_execute_default_details
  492. spec_fetcher do |fetcher|
  493. fetcher.spec 'a', 2
  494. end
  495. a1 = new_default_spec 'a', 1
  496. install_default_gems a1
  497. @cmd.handle_options %w[-l -d]
  498. use_ui @stub_ui do
  499. @cmd.execute
  500. end
  501. expected = <<-EOF
  502. *** LOCAL GEMS ***
  503. a (2, 1)
  504. Author: A User
  505. Homepage: http://example.com
  506. Installed at (2): #{@gemhome}
  507. (1, default): #{a1.base_dir}
  508. this is a summary
  509. EOF
  510. assert_equal expected, @stub_ui.output
  511. end
  512. def test_execute_local_details
  513. spec_fetcher do |fetcher|
  514. fetcher.spec 'a', 1 do |s|
  515. s.platform = 'x86-linux'
  516. end
  517. fetcher.spec 'a', 2 do |s|
  518. s.summary = 'This is a lot of text. ' * 4
  519. s.authors = ['Abraham Lincoln', 'Hirohito']
  520. s.homepage = 'http://a.example.com/'
  521. s.platform = 'universal-darwin'
  522. end
  523. fetcher.legacy_platform
  524. end
  525. @cmd.handle_options %w[-l -d]
  526. use_ui @stub_ui do
  527. @cmd.execute
  528. end
  529. str = @stub_ui.output
  530. str.gsub!(/\(\d\): [^\n]*/, "-")
  531. str.gsub!(/at: [^\n]*/, "at: -")
  532. expected = <<-EOF
  533. *** LOCAL GEMS ***
  534. a (2, 1)
  535. Platforms:
  536. 1: x86-linux
  537. 2: universal-darwin
  538. Authors: Abraham Lincoln, Hirohito
  539. Homepage: http://a.example.com/
  540. Installed at -
  541. -
  542. This is a lot of text. This is a lot of text. This is a lot of text.
  543. This is a lot of text.
  544. pl (1)
  545. Platform: i386-linux
  546. Author: A User
  547. Homepage: http://example.com
  548. Installed at: -
  549. this is a summary
  550. EOF
  551. assert_equal expected, @stub_ui.output
  552. end
  553. def test_execute_exact_remote
  554. spec_fetcher do |fetcher|
  555. fetcher.spec 'coolgem-omg', 3
  556. fetcher.spec 'coolgem', '4.2.1'
  557. fetcher.spec 'wow_coolgem', 1
  558. end
  559. @cmd.handle_options %w[--remote --exact coolgem]
  560. use_ui @stub_ui do
  561. @cmd.execute
  562. end
  563. expected = <<-EOF
  564. *** REMOTE GEMS ***
  565. coolgem (4.2.1)
  566. EOF
  567. assert_equal expected, @stub_ui.output
  568. end
  569. def test_execute_exact_local
  570. spec_fetcher do |fetcher|
  571. fetcher.spec 'coolgem-omg', 3
  572. fetcher.spec 'coolgem', '4.2.1'
  573. fetcher.spec 'wow_coolgem', 1
  574. end
  575. @cmd.handle_options %w[--exact coolgem]
  576. use_ui @stub_ui do
  577. @cmd.execute
  578. end
  579. expected = <<-EOF
  580. *** LOCAL GEMS ***
  581. coolgem (4.2.1)
  582. EOF
  583. assert_equal expected, @stub_ui.output
  584. end
  585. def test_execute_exact_multiple
  586. spec_fetcher do |fetcher|
  587. fetcher.spec 'coolgem-omg', 3
  588. fetcher.spec 'coolgem', '4.2.1'
  589. fetcher.spec 'wow_coolgem', 1
  590. fetcher.spec 'othergem-omg', 3
  591. fetcher.spec 'othergem', '1.2.3'
  592. fetcher.spec 'wow_othergem', 1
  593. end
  594. @cmd.handle_options %w[--exact coolgem othergem]
  595. use_ui @stub_ui do
  596. @cmd.execute
  597. end
  598. expected = <<-EOF
  599. *** LOCAL GEMS ***
  600. coolgem (4.2.1)
  601. *** LOCAL GEMS ***
  602. othergem (1.2.3)
  603. EOF
  604. assert_equal expected, @stub_ui.output
  605. end
  606. def test_depprecated
  607. assert @cmd.deprecated?
  608. end
  609. private
  610. def add_gems_to_fetcher
  611. spec_fetcher do |fetcher|
  612. fetcher.download 'a', 1
  613. fetcher.download 'a', 2
  614. fetcher.download 'a', '3.a'
  615. end
  616. end
  617. end