PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test/rubygems/test_gem_commands_install_command.rb

https://github.com/wanabe/ruby
Ruby | 1504 lines | 1276 code | 217 blank | 11 comment | 6 complexity | d4ae757980abbe30955fc9b20bd05c8e 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/install_command'
  4. require 'rubygems/request_set'
  5. require 'rubygems/rdoc'
  6. class TestGemCommandsInstallCommand < Gem::TestCase
  7. def setup
  8. super
  9. common_installer_setup
  10. @cmd = Gem::Commands::InstallCommand.new
  11. @cmd.options[:document] = []
  12. @gemdeps = "tmp_install_gemdeps"
  13. @orig_args = Gem::Command.build_args
  14. common_installer_setup
  15. end
  16. def teardown
  17. super
  18. common_installer_teardown
  19. Gem::Command.build_args = @orig_args
  20. File.unlink @gemdeps if File.file? @gemdeps
  21. File.unlink "#{@gemdeps}.lock" if File.file? "#{@gemdeps}.lock"
  22. end
  23. def test_execute_exclude_prerelease
  24. spec_fetcher do |fetcher|
  25. fetcher.gem 'a', 2
  26. fetcher.gem 'a', '2.pre'
  27. end
  28. @cmd.options[:args] = %w[a]
  29. use_ui @ui do
  30. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  31. @cmd.execute
  32. end
  33. end
  34. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  35. end
  36. def test_execute_explicit_version_includes_prerelease
  37. specs = spec_fetcher do |fetcher|
  38. fetcher.gem 'a', 2
  39. fetcher.gem 'a', '2.a'
  40. end
  41. a2_pre = specs['a-2.a']
  42. @cmd.handle_options [a2_pre.name, '--version', a2_pre.version.to_s,
  43. "--no-document"]
  44. assert @cmd.options[:prerelease]
  45. assert @cmd.options[:version].satisfied_by?(a2_pre.version)
  46. use_ui @ui do
  47. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  48. @cmd.execute
  49. end
  50. end
  51. assert_equal %w[a-2.a], @cmd.installed_specs.map {|spec| spec.full_name }
  52. end
  53. def test_execute_local
  54. specs = spec_fetcher do |fetcher|
  55. fetcher.gem 'a', 2
  56. end
  57. @cmd.options[:domain] = :local
  58. FileUtils.mv specs['a-2'].cache_file, @tempdir
  59. @cmd.options[:args] = %w[a]
  60. use_ui @ui do
  61. orig_dir = Dir.pwd
  62. begin
  63. Dir.chdir @tempdir
  64. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  65. @cmd.execute
  66. end
  67. ensure
  68. Dir.chdir orig_dir
  69. end
  70. end
  71. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  72. assert_match "1 gem installed", @ui.output
  73. end
  74. def test_execute_local_dependency_nonexistent
  75. specs = spec_fetcher do |fetcher|
  76. fetcher.gem 'foo', 2, 'bar' => '0.5'
  77. end
  78. @cmd.options[:domain] = :local
  79. FileUtils.mv specs['foo-2'].cache_file, @tempdir
  80. @cmd.options[:args] = ['foo']
  81. use_ui @ui do
  82. orig_dir = Dir.pwd
  83. begin
  84. Dir.chdir @tempdir
  85. e = assert_raise Gem::MockGemUi::TermError do
  86. @cmd.execute
  87. end
  88. assert_equal 2, e.exit_code
  89. ensure
  90. Dir.chdir orig_dir
  91. end
  92. end
  93. expected = <<-EXPECTED
  94. ERROR: Could not find a valid gem 'bar' (= 0.5) (required by 'foo' (>= 0)) in any repository
  95. EXPECTED
  96. assert_equal expected, @ui.error
  97. end
  98. def test_execute_local_dependency_nonexistent_ignore_dependencies
  99. specs = spec_fetcher do |fetcher|
  100. fetcher.gem 'foo', 2, 'bar' => '0.5'
  101. end
  102. @cmd.options[:domain] = :local
  103. @cmd.options[:ignore_dependencies] = true
  104. FileUtils.mv specs['foo-2'].cache_file, @tempdir
  105. @cmd.options[:args] = ['foo']
  106. use_ui @ui do
  107. orig_dir = Dir.pwd
  108. begin
  109. Dir.chdir orig_dir
  110. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  111. @cmd.execute
  112. end
  113. ensure
  114. Dir.chdir orig_dir
  115. end
  116. end
  117. assert_match "1 gem installed", @ui.output
  118. end
  119. def test_execute_local_transitive_prerelease
  120. specs = spec_fetcher do |fetcher|
  121. fetcher.download 'a', 2, 'b' => "2.a", 'c' => '3'
  122. fetcher.download 'b', '2.a'
  123. fetcher.download 'c', '3'
  124. end
  125. @cmd.options[:domain] = :local
  126. FileUtils.mv specs['a-2'].cache_file, @tempdir
  127. FileUtils.mv specs['b-2.a'].cache_file, @tempdir
  128. FileUtils.mv specs['c-3'].cache_file, @tempdir
  129. @cmd.options[:args] = %w[a]
  130. use_ui @ui do
  131. orig_dir = Dir.pwd
  132. begin
  133. Dir.chdir @tempdir
  134. FileUtils.rm_r [@gemhome, "gems"]
  135. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  136. @cmd.execute
  137. end
  138. ensure
  139. Dir.chdir orig_dir
  140. end
  141. end
  142. assert_equal %w[a-2 b-2.a c-3], @cmd.installed_specs.map {|spec| spec.full_name }.sort
  143. assert_match "3 gems installed", @ui.output
  144. end
  145. def test_execute_no_user_install
  146. pend 'skipped on MS Windows (chmod has no effect)' if win_platform?
  147. pend 'skipped in root privilege' if Process.uid.zero?
  148. specs = spec_fetcher do |fetcher|
  149. fetcher.gem 'a', 2
  150. end
  151. @cmd.options[:user_install] = false
  152. FileUtils.mv specs['a-2'].cache_file, @tempdir
  153. @cmd.options[:args] = %w[a]
  154. use_ui @ui do
  155. orig_dir = Dir.pwd
  156. begin
  157. FileUtils.chmod 0755, @userhome
  158. FileUtils.chmod 0555, @gemhome
  159. Dir.chdir @tempdir
  160. assert_raise Gem::FilePermissionError do
  161. @cmd.execute
  162. end
  163. ensure
  164. Dir.chdir orig_dir
  165. FileUtils.chmod 0755, @gemhome
  166. end
  167. end
  168. end
  169. def test_execute_local_missing
  170. spec_fetcher
  171. @cmd.options[:domain] = :local
  172. @cmd.options[:args] = %w[no_such_gem]
  173. use_ui @ui do
  174. e = assert_raise Gem::MockGemUi::TermError do
  175. @cmd.execute
  176. end
  177. assert_equal 2, e.exit_code
  178. end
  179. # HACK no repository was checked
  180. assert_match(/ould not find a valid gem 'no_such_gem'/, @ui.error)
  181. end
  182. def test_execute_local_missing_ignore_dependencies
  183. spec_fetcher
  184. @cmd.options[:domain] = :local
  185. @cmd.options[:ignore_dependencies] = true
  186. @cmd.options[:args] = %w[no_such_gem]
  187. use_ui @ui do
  188. e = assert_raise Gem::MockGemUi::TermError do
  189. @cmd.execute
  190. end
  191. assert_equal 2, e.exit_code
  192. end
  193. # HACK no repository was checked
  194. assert_match(/ould not find a valid gem 'no_such_gem'/, @ui.error)
  195. end
  196. def test_execute_no_gem
  197. @cmd.options[:args] = %w[]
  198. assert_raise Gem::CommandLineError do
  199. @cmd.execute
  200. end
  201. end
  202. def test_execute_nonexistent
  203. spec_fetcher
  204. @cmd.options[:args] = %w[nonexistent]
  205. use_ui @ui do
  206. e = assert_raise Gem::MockGemUi::TermError do
  207. @cmd.execute
  208. end
  209. assert_equal 2, e.exit_code
  210. end
  211. assert_match(/ould not find a valid gem 'nonexistent'/, @ui.error)
  212. end
  213. def test_execute_dependency_nonexistent
  214. spec_fetcher do |fetcher|
  215. fetcher.spec 'foo', 2, 'bar' => '0.5'
  216. end
  217. @cmd.options[:args] = ['foo']
  218. use_ui @ui do
  219. e = assert_raise Gem::MockGemUi::TermError do
  220. @cmd.execute
  221. end
  222. assert_equal 2, e.exit_code
  223. end
  224. expected = <<-EXPECTED
  225. ERROR: Could not find a valid gem 'bar' (= 0.5) (required by 'foo' (>= 0)) in any repository
  226. EXPECTED
  227. assert_equal expected, @ui.error
  228. end
  229. def test_execute_http_proxy
  230. use_ui @ui do
  231. e = assert_raise ArgumentError, @ui.error do
  232. @cmd.handle_options %w[-p=foo.bar.com]
  233. end
  234. assert_match "Invalid uri scheme for =foo.bar.com\nPreface URLs with one of [\"http://\", \"https://\", \"file://\", \"s3://\"]", e.message
  235. end
  236. end
  237. def test_execute_bad_source
  238. spec_fetcher
  239. # This is needed because we need to exercise the cache path
  240. # within SpecFetcher
  241. path = File.join Gem.spec_cache_dir, "not-there.nothing%80", "latest_specs.4.8"
  242. FileUtils.mkdir_p File.dirname(path)
  243. File.open path, "w" do |f|
  244. f.write Marshal.dump([])
  245. end
  246. Gem.sources.replace ["http://not-there.nothing"]
  247. @cmd.options[:args] = %w[nonexistent]
  248. use_ui @ui do
  249. e = assert_raise Gem::MockGemUi::TermError do
  250. @cmd.execute
  251. end
  252. assert_equal 2, e.exit_code
  253. end
  254. errs = @ui.error.split("\n")
  255. assert_match(/ould not find a valid gem 'nonexistent'/, errs.shift)
  256. assert_match(%r{Unable to download data from http://not-there.nothing}, errs.shift)
  257. end
  258. def test_execute_nonexistent_hint_disabled
  259. misspelled = "nonexistent_with_hint"
  260. correctly_spelled = "non_existent_with_hint"
  261. spec_fetcher do |fetcher|
  262. fetcher.spec correctly_spelled, 2
  263. end
  264. @cmd.options[:args] = [misspelled]
  265. @cmd.options[:suggest_alternate] = false
  266. use_ui @ui do
  267. e = assert_raise Gem::MockGemUi::TermError do
  268. @cmd.execute
  269. end
  270. assert_equal 2, e.exit_code
  271. end
  272. expected = <<-EXPECTED
  273. ERROR: Could not find a valid gem 'nonexistent_with_hint' (>= 0) in any repository
  274. EXPECTED
  275. assert_equal expected, @ui.error
  276. end
  277. def test_execute_nonexistent_with_hint
  278. misspelled = "nonexistent_with_hint"
  279. correctly_spelled = "non_existent_with_hint"
  280. spec_fetcher do |fetcher|
  281. fetcher.spec correctly_spelled, 2
  282. end
  283. @cmd.options[:args] = [misspelled]
  284. use_ui @ui do
  285. e = assert_raise Gem::MockGemUi::TermError do
  286. @cmd.execute
  287. end
  288. assert_equal 2, e.exit_code
  289. end
  290. expected = "ERROR: Could not find a valid gem 'nonexistent_with_hint' (>= 0) in any repository
  291. ERROR: Possible alternatives: non_existent_with_hint
  292. "
  293. assert_equal expected, @ui.error
  294. end
  295. def test_execute_nonexistent_with_dashes
  296. misspelled = "non-existent_with-hint"
  297. correctly_spelled = "nonexistent-with_hint"
  298. spec_fetcher do |fetcher|
  299. fetcher.download correctly_spelled, 2
  300. end
  301. @cmd.options[:args] = [misspelled]
  302. use_ui @ui do
  303. e = assert_raise Gem::MockGemUi::TermError do
  304. @cmd.execute
  305. end
  306. assert_equal 2, e.exit_code
  307. end
  308. expected = [
  309. "ERROR: Could not find a valid gem 'non-existent_with-hint' (>= 0) in any repository",
  310. "ERROR: Possible alternatives: nonexistent-with_hint",
  311. ]
  312. output = @ui.error.split "\n"
  313. assert_equal expected, output
  314. end
  315. def test_execute_conflicting_install_options
  316. @cmd.options[:user_install] = true
  317. @cmd.options[:install_dir] = "whatever"
  318. use_ui @ui do
  319. assert_raise Gem::MockGemUi::TermError do
  320. @cmd.execute
  321. end
  322. end
  323. expected = "ERROR: Use --install-dir or --user-install but not both\n"
  324. assert_equal expected, @ui.error
  325. end
  326. def test_execute_prerelease_skipped_when_no_flag_set
  327. spec_fetcher do |fetcher|
  328. fetcher.gem 'a', 1
  329. fetcher.gem 'a', '3.a'
  330. end
  331. @cmd.options[:prerelease] = false
  332. @cmd.options[:args] = %w[a]
  333. use_ui @ui do
  334. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  335. @cmd.execute
  336. end
  337. end
  338. assert_equal %w[a-1], @cmd.installed_specs.map {|spec| spec.full_name }
  339. end
  340. def test_execute_prerelease_wins_over_previous_ver
  341. spec_fetcher do |fetcher|
  342. fetcher.download 'a', 1
  343. fetcher.download 'a', '2.a'
  344. end
  345. @cmd.options[:prerelease] = true
  346. @cmd.options[:args] = %w[a]
  347. use_ui @ui do
  348. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  349. @cmd.execute
  350. end
  351. end
  352. assert_equal %w[a-2.a], @cmd.installed_specs.map {|spec| spec.full_name }
  353. end
  354. def test_execute_with_version_specified_by_colon
  355. spec_fetcher do |fetcher|
  356. fetcher.download 'a', 1
  357. fetcher.download 'a', 2
  358. end
  359. @cmd.options[:args] = %w[a:1]
  360. use_ui @ui do
  361. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  362. @cmd.execute
  363. end
  364. end
  365. assert_equal %w[a-1], @cmd.installed_specs.map {|spec| spec.full_name }
  366. end
  367. def test_execute_prerelease_skipped_when_non_pre_available
  368. spec_fetcher do |fetcher|
  369. fetcher.gem 'a', '2.pre'
  370. fetcher.gem 'a', 2
  371. end
  372. @cmd.options[:prerelease] = true
  373. @cmd.options[:args] = %w[a]
  374. use_ui @ui do
  375. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  376. @cmd.execute
  377. end
  378. end
  379. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  380. end
  381. def test_execute_required_ruby_version
  382. next_ruby = Gem.ruby_version.segments.map.with_index{|n, i| i == 1 ? n + 1 : n }.join(".")
  383. local = Gem::Platform.local
  384. spec_fetcher do |fetcher|
  385. fetcher.download 'a', 2
  386. fetcher.download 'a', 2 do |s|
  387. s.required_ruby_version = "< #{RUBY_VERSION}.a"
  388. s.platform = local
  389. end
  390. fetcher.download 'a', 3 do |s|
  391. s.required_ruby_version = ">= #{next_ruby}"
  392. end
  393. fetcher.download 'a', 3 do |s|
  394. s.required_ruby_version = ">= #{next_ruby}"
  395. s.platform = local
  396. end
  397. end
  398. @cmd.options[:args] = %w[a]
  399. use_ui @ui do
  400. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  401. @cmd.execute
  402. end
  403. end
  404. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  405. end
  406. def test_execute_required_ruby_version_upper_bound
  407. local = Gem::Platform.local
  408. spec_fetcher do |fetcher|
  409. fetcher.gem 'a', 2.0
  410. fetcher.gem 'a', 2.0 do |s|
  411. s.required_ruby_version = "< #{RUBY_VERSION}.a"
  412. s.platform = local
  413. end
  414. end
  415. @cmd.options[:args] = %w[a]
  416. use_ui @ui do
  417. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  418. @cmd.execute
  419. end
  420. end
  421. assert_equal %w[a-2.0], @cmd.installed_specs.map {|spec| spec.full_name }
  422. end
  423. def test_execute_required_ruby_version_specific_not_met
  424. spec_fetcher do |fetcher|
  425. fetcher.gem 'a', '1.0' do |s|
  426. s.required_ruby_version = '= 1.4.6'
  427. end
  428. end
  429. @cmd.options[:args] = %w[a]
  430. use_ui @ui do
  431. assert_raise Gem::MockGemUi::TermError do
  432. @cmd.execute
  433. end
  434. end
  435. errs = @ui.error.split("\n")
  436. assert_equal "ERROR: Error installing a:", errs.shift
  437. assert_equal "\ta-1.0 requires Ruby version = 1.4.6. The current ruby version is #{Gem.ruby_version}.", errs.shift
  438. end
  439. def test_execute_required_ruby_version_specific_prerelease_met
  440. spec_fetcher do |fetcher|
  441. fetcher.gem 'a', '1.0' do |s|
  442. s.required_ruby_version = '>= 1.4.6.preview2'
  443. end
  444. end
  445. @cmd.options[:args] = %w[a]
  446. use_ui @ui do
  447. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  448. @cmd.execute
  449. end
  450. end
  451. assert_equal %w[a-1.0], @cmd.installed_specs.map {|spec| spec.full_name }
  452. end
  453. def test_execute_required_ruby_version_specific_prerelease_not_met
  454. next_ruby_pre = Gem.ruby_version.segments.map.with_index{|n, i| i == 1 ? n + 1 : n }.join(".") + ".a"
  455. spec_fetcher do |fetcher|
  456. fetcher.gem 'a', '1.0' do |s|
  457. s.required_ruby_version = "> #{next_ruby_pre}"
  458. end
  459. end
  460. @cmd.options[:args] = %w[a]
  461. use_ui @ui do
  462. assert_raise Gem::MockGemUi::TermError do
  463. @cmd.execute
  464. end
  465. end
  466. errs = @ui.error.split("\n")
  467. assert_equal "ERROR: Error installing a:", errs.shift
  468. assert_equal "\ta-1.0 requires Ruby version > #{next_ruby_pre}. The current ruby version is #{Gem.ruby_version}.", errs.shift
  469. end
  470. def test_execute_required_rubygems_version_wrong
  471. spec_fetcher do |fetcher|
  472. fetcher.gem 'a', '1.0' do |s|
  473. s.required_rubygems_version = '< 0'
  474. end
  475. end
  476. @cmd.options[:args] = %w[a]
  477. use_ui @ui do
  478. assert_raise Gem::MockGemUi::TermError do
  479. @cmd.execute
  480. end
  481. end
  482. errs = @ui.error.split("\n")
  483. assert_equal "ERROR: Error installing a:", errs.shift
  484. assert_equal "\ta-1.0 requires RubyGems version < 0. The current RubyGems version is #{Gem.rubygems_version}. Try 'gem update --system' to update RubyGems itself.", errs.shift
  485. end
  486. def test_execute_rdoc
  487. specs = spec_fetcher do |fetcher|
  488. fetcher.gem 'a', 2
  489. end
  490. Gem.done_installing(&Gem::RDoc.method(:generation_hook))
  491. @cmd.options[:document] = %w[rdoc ri]
  492. @cmd.options[:domain] = :local
  493. a2 = specs['a-2']
  494. FileUtils.mv a2.cache_file, @tempdir
  495. @cmd.options[:args] = %w[a]
  496. use_ui @ui do
  497. # Don't use Dir.chdir with a block, it warnings a lot because
  498. # of a downstream Dir.chdir with a block
  499. old = Dir.getwd
  500. begin
  501. Dir.chdir @tempdir
  502. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  503. @cmd.execute
  504. end
  505. ensure
  506. Dir.chdir old
  507. end
  508. end
  509. wait_for_child_process_to_exit
  510. assert_path_exist File.join(a2.doc_dir, 'ri')
  511. assert_path_exist File.join(a2.doc_dir, 'rdoc')
  512. end
  513. def test_execute_rdoc_with_path
  514. specs = spec_fetcher do |fetcher|
  515. fetcher.gem 'a', 2
  516. end
  517. Gem.done_installing(&Gem::RDoc.method(:generation_hook))
  518. @cmd.options[:document] = %w[rdoc ri]
  519. @cmd.options[:domain] = :local
  520. @cmd.options[:install_dir] = 'whatever'
  521. a2 = specs['a-2']
  522. FileUtils.mv a2.cache_file, @tempdir
  523. @cmd.options[:args] = %w[a]
  524. use_ui @ui do
  525. # Don't use Dir.chdir with a block, it warnings a lot because
  526. # of a downstream Dir.chdir with a block
  527. old = Dir.getwd
  528. begin
  529. Dir.chdir @tempdir
  530. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  531. @cmd.execute
  532. end
  533. ensure
  534. Dir.chdir old
  535. end
  536. end
  537. wait_for_child_process_to_exit
  538. assert_path_exist 'whatever/doc/a-2', 'documentation not installed'
  539. end
  540. def test_execute_saves_build_args
  541. specs = spec_fetcher do |fetcher|
  542. fetcher.gem 'a', 2
  543. end
  544. args = %w[--with-awesome=true --more-awesome=yes]
  545. Gem::Command.build_args = args
  546. a2 = specs['a-2']
  547. FileUtils.mv a2.cache_file, @tempdir
  548. @cmd.options[:domain] = :local
  549. @cmd.options[:args] = %w[a]
  550. use_ui @ui do
  551. # Don't use Dir.chdir with a block, it warnings a lot because
  552. # of a downstream Dir.chdir with a block
  553. old = Dir.getwd
  554. begin
  555. Dir.chdir @tempdir
  556. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  557. @cmd.execute
  558. end
  559. ensure
  560. Dir.chdir old
  561. end
  562. end
  563. path = a2.build_info_file
  564. assert_path_exist path
  565. assert_equal args, a2.build_args
  566. end
  567. def test_execute_remote
  568. spec_fetcher do |fetcher|
  569. fetcher.gem 'a', 2
  570. end
  571. @cmd.options[:args] = %w[a]
  572. use_ui @ui do
  573. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  574. @cmd.execute
  575. end
  576. end
  577. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  578. assert_match "1 gem installed", @ui.output
  579. end
  580. def test_execute_with_invalid_gem_file
  581. FileUtils.touch("a.gem")
  582. spec_fetcher do |fetcher|
  583. fetcher.gem 'a', 2
  584. end
  585. @cmd.options[:args] = %w[a]
  586. use_ui @ui do
  587. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  588. @cmd.execute
  589. end
  590. end
  591. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  592. assert_match "1 gem installed", @ui.output
  593. end
  594. def test_execute_remote_ignores_files
  595. specs = spec_fetcher do |fetcher|
  596. fetcher.gem 'a', 1
  597. fetcher.gem 'a', 2
  598. end
  599. @cmd.options[:domain] = :remote
  600. a1 = specs['a-1']
  601. a2 = specs['a-2']
  602. FileUtils.mv a2.cache_file, @tempdir
  603. @fetcher.data["#{@gem_repo}gems/#{a2.file_name}"] =
  604. read_binary(a1.cache_file)
  605. @cmd.options[:args] = [a2.name]
  606. gemdir = File.join @gemhome, 'specifications'
  607. a2_gemspec = File.join(gemdir, "a-2.gemspec")
  608. a1_gemspec = File.join(gemdir, "a-1.gemspec")
  609. FileUtils.rm_rf a1_gemspec
  610. FileUtils.rm_rf a2_gemspec
  611. start = Dir["#{gemdir}/*"]
  612. use_ui @ui do
  613. Dir.chdir @tempdir do
  614. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  615. @cmd.execute
  616. end
  617. end
  618. end
  619. assert_equal %w[a-1], @cmd.installed_specs.map {|spec| spec.full_name }
  620. assert_match "1 gem installed", @ui.output
  621. fin = Dir["#{gemdir}/*"]
  622. assert_equal [a1_gemspec], fin - start
  623. end
  624. def test_execute_two
  625. specs = spec_fetcher do |fetcher|
  626. fetcher.gem 'a', 2
  627. fetcher.gem 'b', 2
  628. end
  629. FileUtils.mv specs['a-2'].cache_file, @tempdir
  630. FileUtils.mv specs['b-2'].cache_file, @tempdir
  631. @cmd.options[:domain] = :local
  632. @cmd.options[:args] = %w[a b]
  633. use_ui @ui do
  634. orig_dir = Dir.pwd
  635. begin
  636. Dir.chdir @tempdir
  637. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  638. @cmd.execute
  639. end
  640. ensure
  641. Dir.chdir orig_dir
  642. end
  643. end
  644. assert_equal %w[a-2 b-2], @cmd.installed_specs.map {|spec| spec.full_name }
  645. assert_match "2 gems installed", @ui.output
  646. end
  647. def test_execute_two_version
  648. @cmd.options[:args] = %w[a b]
  649. @cmd.options[:version] = Gem::Requirement.new("> 1")
  650. use_ui @ui do
  651. e = assert_raise Gem::MockGemUi::TermError do
  652. @cmd.execute
  653. end
  654. assert_equal 1, e.exit_code
  655. end
  656. assert_empty @cmd.installed_specs
  657. msg = "ERROR: Can't use --version with multiple gems. You can specify multiple gems with" \
  658. " version requirements using `gem install 'my_gem:1.0.0' 'my_other_gem:~>2.0.0'`"
  659. assert_empty @ui.output
  660. assert_equal msg, @ui.error.chomp
  661. end
  662. def test_execute_two_version_specified_by_colon
  663. spec_fetcher do |fetcher|
  664. fetcher.gem 'a', 1
  665. fetcher.gem 'a', 2
  666. fetcher.gem 'b', 1
  667. fetcher.gem 'b', 2
  668. end
  669. @cmd.options[:args] = %w[a:1 b:1]
  670. use_ui @ui do
  671. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  672. @cmd.execute
  673. end
  674. end
  675. assert_equal %w[a-1 b-1], @cmd.installed_specs.map {|spec| spec.full_name }
  676. end
  677. def test_execute_conservative
  678. spec_fetcher do |fetcher|
  679. fetcher.download 'b', 2
  680. fetcher.gem 'a', 2
  681. end
  682. @cmd.options[:conservative] = true
  683. @cmd.options[:args] = %w[a b]
  684. use_ui @ui do
  685. orig_dir = Dir.pwd
  686. begin
  687. Dir.chdir @tempdir
  688. assert_raise Gem::MockGemUi::SystemExitException do
  689. @cmd.execute
  690. end
  691. ensure
  692. Dir.chdir orig_dir
  693. end
  694. end
  695. assert_equal %w[b-2], @cmd.installed_specs.map {|spec| spec.full_name }
  696. assert_equal "", @ui.error
  697. assert_match "1 gem installed", @ui.output
  698. end
  699. def test_install_gem_ignore_dependencies_both
  700. done_installing = false
  701. Gem.done_installing do
  702. done_installing = true
  703. end
  704. spec = util_spec 'a', 2
  705. util_build_gem spec
  706. FileUtils.mv spec.cache_file, @tempdir
  707. @cmd.options[:ignore_dependencies] = true
  708. @cmd.install_gem 'a', '>= 0'
  709. assert_equal %w[a-2], @cmd.installed_specs.map {|s| s.full_name }
  710. assert done_installing, 'documentation was not generated'
  711. end
  712. def test_install_gem_ignore_dependencies_remote
  713. spec_fetcher do |fetcher|
  714. fetcher.gem 'a', 2
  715. end
  716. @cmd.options[:ignore_dependencies] = true
  717. @cmd.install_gem 'a', '>= 0'
  718. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  719. end
  720. def test_install_gem_ignore_dependencies_remote_platform_local
  721. local = Gem::Platform.local
  722. spec_fetcher do |fetcher|
  723. fetcher.gem 'a', 3
  724. fetcher.gem 'a', 3 do |s|
  725. s.platform = local
  726. end
  727. end
  728. @cmd.options[:ignore_dependencies] = true
  729. @cmd.install_gem 'a', '>= 0'
  730. assert_equal %W[a-3-#{local}], @cmd.installed_specs.map {|spec| spec.full_name }
  731. end
  732. def test_install_gem_ignore_dependencies_specific_file
  733. spec = util_spec 'a', 2
  734. util_build_gem spec
  735. FileUtils.mv spec.cache_file, @tempdir
  736. @cmd.options[:ignore_dependencies] = true
  737. @cmd.install_gem File.join(@tempdir, spec.file_name), nil
  738. assert_equal %w[a-2], @cmd.installed_specs.map {|s| s.full_name }
  739. end
  740. def test_parses_requirement_from_gemname
  741. spec_fetcher do |fetcher|
  742. fetcher.gem 'a', 2
  743. fetcher.gem 'b', 2
  744. end
  745. @cmd.options[:domain] = :local
  746. req = "a:10.0"
  747. @cmd.options[:args] = [req]
  748. e = nil
  749. use_ui @ui do
  750. orig_dir = Dir.pwd
  751. begin
  752. Dir.chdir @tempdir
  753. e = assert_raise Gem::MockGemUi::TermError do
  754. @cmd.execute
  755. end
  756. ensure
  757. Dir.chdir orig_dir
  758. end
  759. end
  760. assert_equal 2, e.exit_code
  761. assert_match %r{Could not find a valid gem 'a' \(= 10.0\)}, @ui.error
  762. end
  763. def test_show_errors_on_failure
  764. Gem.sources.replace ["http://not-there.nothing"]
  765. @cmd.options[:args] = ["blah"]
  766. e = nil
  767. use_ui @ui do
  768. orig_dir = Dir.pwd
  769. begin
  770. Dir.chdir @tempdir
  771. e = assert_raise Gem::MockGemUi::TermError do
  772. @cmd.execute
  773. end
  774. ensure
  775. Dir.chdir orig_dir
  776. end
  777. end
  778. assert_equal 2, e.exit_code
  779. assert_match 'Unable to download data', @ui.error
  780. end
  781. def test_show_source_problems_even_on_success
  782. spec_fetcher do |fetcher|
  783. fetcher.download 'a', 2
  784. end
  785. Gem.sources << "http://nonexistent.example"
  786. @cmd.options[:args] = %w[a]
  787. use_ui @ui do
  788. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  789. @cmd.execute
  790. end
  791. end
  792. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  793. assert_match "1 gem installed", @ui.output
  794. e = @ui.error
  795. x = "WARNING: Unable to pull data from 'http://nonexistent.example': no data for http://nonexistent.example/specs.4.8.gz (http://nonexistent.example/specs.4.8.gz)\n"
  796. assert_equal x, e
  797. end
  798. def test_redact_credentials_from_uri_on_warning
  799. spec_fetcher do |fetcher|
  800. fetcher.download 'a', 2
  801. end
  802. Gem.sources << "http://username:SECURE_TOKEN@nonexistent.example"
  803. @cmd.options[:args] = %w[a]
  804. use_ui @ui do
  805. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  806. @cmd.execute
  807. end
  808. end
  809. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  810. assert_match "1 gem installed", @ui.output
  811. e = @ui.error
  812. x = "WARNING: Unable to pull data from 'http://username:REDACTED@nonexistent.example': no data for http://username:REDACTED@nonexistent.example/specs.4.8.gz (http://username:REDACTED@nonexistent.example/specs.4.8.gz)\n"
  813. assert_equal x, e
  814. end
  815. def test_execute_uses_from_a_gemdeps
  816. spec_fetcher do |fetcher|
  817. fetcher.gem 'a', 2
  818. end
  819. File.open @gemdeps, "w" do |f|
  820. f << "gem 'a'"
  821. end
  822. @cmd.options[:gemdeps] = @gemdeps
  823. use_ui @ui do
  824. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  825. @cmd.execute
  826. end
  827. end
  828. assert_equal %w[], @cmd.installed_specs.map {|spec| spec.full_name }
  829. assert_match "Using a (2)", @ui.output
  830. assert File.exist?("#{@gemdeps}.lock")
  831. end
  832. def test_execute_uses_from_a_gemdeps_with_no_lock
  833. spec_fetcher do |fetcher|
  834. fetcher.gem 'a', 2
  835. end
  836. File.open @gemdeps, "w" do |f|
  837. f << "gem 'a'"
  838. end
  839. @cmd.handle_options %w[--no-lock]
  840. @cmd.options[:gemdeps] = @gemdeps
  841. use_ui @ui do
  842. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  843. @cmd.execute
  844. end
  845. end
  846. assert_equal %w[], @cmd.installed_specs.map {|spec| spec.full_name }
  847. assert_match "Using a (2)", @ui.output
  848. assert !File.exist?("#{@gemdeps}.lock")
  849. end
  850. def test_execute_installs_from_a_gemdeps_with_conservative
  851. spec_fetcher do |fetcher|
  852. fetcher.download 'a', 2
  853. fetcher.gem 'a', 1
  854. end
  855. File.open @gemdeps, "w" do |f|
  856. f << "gem 'a'"
  857. end
  858. @cmd.handle_options %w[--conservative]
  859. @cmd.options[:gemdeps] = @gemdeps
  860. use_ui @ui do
  861. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  862. @cmd.execute
  863. end
  864. end
  865. assert_equal %w[], @cmd.installed_specs.map {|spec| spec.full_name }
  866. assert_match "Using a (1)", @ui.output
  867. end
  868. def test_execute_installs_from_a_gemdeps
  869. spec_fetcher do |fetcher|
  870. fetcher.download 'a', 2
  871. end
  872. File.open @gemdeps, "w" do |f|
  873. f << "gem 'a'"
  874. end
  875. @cmd.options[:gemdeps] = @gemdeps
  876. use_ui @ui do
  877. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  878. @cmd.execute
  879. end
  880. end
  881. assert_equal %w[a-2], @cmd.installed_specs.map {|spec| spec.full_name }
  882. assert_match "Installing a (2)", @ui.output
  883. end
  884. def test_execute_installs_deps_a_gemdeps
  885. spec_fetcher do |fetcher|
  886. fetcher.download 'q', '1.0'
  887. fetcher.download 'r', '2.0', 'q' => nil
  888. end
  889. File.open @gemdeps, "w" do |f|
  890. f << "gem 'r'"
  891. end
  892. @cmd.options[:gemdeps] = @gemdeps
  893. use_ui @ui do
  894. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  895. @cmd.execute
  896. end
  897. end
  898. names = @cmd.installed_specs.map {|spec| spec.full_name }
  899. assert_equal %w[q-1.0 r-2.0], names
  900. assert_match "Installing q (1.0)", @ui.output
  901. assert_match "Installing r (2.0)", @ui.output
  902. end
  903. def test_execute_uses_deps_a_gemdeps
  904. spec_fetcher do |fetcher|
  905. fetcher.download 'r', '2.0', 'q' => nil
  906. fetcher.spec 'q', '1.0'
  907. end
  908. File.open @gemdeps, "w" do |f|
  909. f << "gem 'r'"
  910. end
  911. @cmd.options[:gemdeps] = @gemdeps
  912. use_ui @ui do
  913. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  914. @cmd.execute
  915. end
  916. end
  917. names = @cmd.installed_specs.map {|spec| spec.full_name }
  918. assert_equal %w[r-2.0], names
  919. assert_match "Using q (1.0)", @ui.output
  920. assert_match "Installing r (2.0)", @ui.output
  921. end
  922. def test_execute_installs_deps_a_gemdeps_into_a_path
  923. spec_fetcher do |fetcher|
  924. fetcher.download 'q', '1.0'
  925. fetcher.download 'r', '2.0', 'q' => nil
  926. end
  927. File.open @gemdeps, "w" do |f|
  928. f << "gem 'r'"
  929. end
  930. @cmd.options[:install_dir] = "gf-path"
  931. @cmd.options[:gemdeps] = @gemdeps
  932. use_ui @ui do
  933. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  934. @cmd.execute
  935. end
  936. end
  937. names = @cmd.installed_specs.map {|spec| spec.full_name }
  938. assert_equal %w[q-1.0 r-2.0], names
  939. assert_match "Installing q (1.0)", @ui.output
  940. assert_match "Installing r (2.0)", @ui.output
  941. assert File.file?("gf-path/specifications/q-1.0.gemspec"), "not installed"
  942. assert File.file?("gf-path/specifications/r-2.0.gemspec"), "not installed"
  943. end
  944. def test_execute_with_gemdeps_path_ignores_system
  945. specs = spec_fetcher do |fetcher|
  946. fetcher.download 'q', '1.0'
  947. fetcher.download 'r', '2.0', 'q' => nil
  948. end
  949. install_specs specs['q-1.0']
  950. File.open @gemdeps, "w" do |f|
  951. f << "gem 'r'"
  952. end
  953. @cmd.options[:install_dir] = "gf-path"
  954. @cmd.options[:gemdeps] = @gemdeps
  955. use_ui @ui do
  956. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  957. @cmd.execute
  958. end
  959. end
  960. names = @cmd.installed_specs.map {|spec| spec.full_name }
  961. assert_equal %w[q-1.0 r-2.0], names
  962. assert_match "Installing q (1.0)", @ui.output
  963. assert_match "Installing r (2.0)", @ui.output
  964. assert File.file?("gf-path/specifications/q-1.0.gemspec"), "not installed"
  965. assert File.file?("gf-path/specifications/r-2.0.gemspec"), "not installed"
  966. end
  967. def test_execute_uses_deps_a_gemdeps_with_a_path
  968. specs = spec_fetcher do |fetcher|
  969. fetcher.gem 'q', '1.0'
  970. fetcher.gem 'r', '2.0', 'q' => nil
  971. end
  972. i = Gem::Installer.at specs['q-1.0'].cache_file, :install_dir => "gf-path"
  973. i.install
  974. assert File.file?("gf-path/specifications/q-1.0.gemspec"), "not installed"
  975. File.open @gemdeps, "w" do |f|
  976. f << "gem 'r'"
  977. end
  978. @cmd.options[:install_dir] = "gf-path"
  979. @cmd.options[:gemdeps] = @gemdeps
  980. use_ui @ui do
  981. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  982. @cmd.execute
  983. end
  984. end
  985. names = @cmd.installed_specs.map {|spec| spec.full_name }
  986. assert_equal %w[r-2.0], names
  987. assert_match "Using q (1.0)", @ui.output
  988. assert_match "Installing r (2.0)", @ui.output
  989. end
  990. def test_handle_options_file
  991. FileUtils.touch 'Gemfile'
  992. @cmd.handle_options %w[-g Gemfile]
  993. assert_equal 'Gemfile', @cmd.options[:gemdeps]
  994. FileUtils.rm 'Gemfile'
  995. FileUtils.touch 'gem.deps.rb'
  996. @cmd.handle_options %w[--file gem.deps.rb]
  997. assert_equal 'gem.deps.rb', @cmd.options[:gemdeps]
  998. FileUtils.rm 'gem.deps.rb'
  999. FileUtils.touch 'Isolate'
  1000. @cmd.handle_options %w[-g]
  1001. assert_equal 'Isolate', @cmd.options[:gemdeps]
  1002. FileUtils.touch 'Gemfile'
  1003. @cmd.handle_options %w[-g]
  1004. assert_equal 'Gemfile', @cmd.options[:gemdeps]
  1005. FileUtils.touch 'gem.deps.rb'
  1006. @cmd.handle_options %w[-g]
  1007. assert_equal 'gem.deps.rb', @cmd.options[:gemdeps]
  1008. end
  1009. def test_handle_options_suggest
  1010. assert @cmd.options[:suggest_alternate]
  1011. @cmd.handle_options %w[--no-suggestions]
  1012. refute @cmd.options[:suggest_alternate]
  1013. @cmd.handle_options %w[--suggestions]
  1014. assert @cmd.options[:suggest_alternate]
  1015. end
  1016. def test_handle_options_without
  1017. @cmd.handle_options %w[--without test]
  1018. assert_equal [:test], @cmd.options[:without_groups]
  1019. @cmd.handle_options %w[--without test,development]
  1020. assert_equal [:test, :development], @cmd.options[:without_groups]
  1021. end
  1022. def test_explain_platform_local
  1023. local = Gem::Platform.local
  1024. spec_fetcher do |fetcher|
  1025. fetcher.spec 'a', 2
  1026. fetcher.spec 'a', 2 do |s|
  1027. s.platform = local
  1028. end
  1029. end
  1030. @cmd.options[:explain] = true
  1031. @cmd.options[:args] = %w[a]
  1032. use_ui @ui do
  1033. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  1034. @cmd.execute
  1035. end
  1036. end
  1037. out = @ui.output.split "\n"
  1038. assert_equal "Gems to install:", out.shift
  1039. assert_equal " a-2-#{local}", out.shift
  1040. assert_empty out
  1041. end
  1042. def test_explain_platform_local_ignore_dependencies
  1043. local = Gem::Platform.local
  1044. spec_fetcher do |fetcher|
  1045. fetcher.spec 'a', 3
  1046. fetcher.spec 'a', 3 do |s|
  1047. s.platform = local
  1048. end
  1049. end
  1050. @cmd.options[:ignore_dependencies] = true
  1051. @cmd.options[:explain] = true
  1052. @cmd.options[:args] = %w[a]
  1053. use_ui @ui do
  1054. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  1055. @cmd.execute
  1056. end
  1057. end
  1058. out = @ui.output.split "\n"
  1059. assert_equal "Gems to install:", out.shift
  1060. assert_equal " a-3-#{local}", out.shift
  1061. assert_empty out
  1062. end
  1063. def test_explain_platform_ruby
  1064. local = Gem::Platform.local
  1065. spec_fetcher do |fetcher|
  1066. fetcher.spec 'a', 2
  1067. fetcher.spec 'a', 2 do |s|
  1068. s.platform = local
  1069. end
  1070. end
  1071. # equivalent to --platform=ruby
  1072. Gem.platforms = [Gem::Platform::RUBY]
  1073. @cmd.options[:explain] = true
  1074. @cmd.options[:args] = %w[a]
  1075. use_ui @ui do
  1076. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  1077. @cmd.execute
  1078. end
  1079. end
  1080. out = @ui.output.split "\n"
  1081. assert_equal "Gems to install:", out.shift
  1082. assert_equal " a-2", out.shift
  1083. assert_empty out
  1084. end
  1085. def test_explain_platform_ruby_ignore_dependencies
  1086. local = Gem::Platform.local
  1087. spec_fetcher do |fetcher|
  1088. fetcher.spec 'a', 3
  1089. fetcher.spec 'a', 3 do |s|
  1090. s.platform = local
  1091. end
  1092. end
  1093. # equivalent to --platform=ruby
  1094. Gem.platforms = [Gem::Platform::RUBY]
  1095. @cmd.options[:ignore_dependencies] = true
  1096. @cmd.options[:explain] = true
  1097. @cmd.options[:args] = %w[a]
  1098. use_ui @ui do
  1099. assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
  1100. @cmd.execute
  1101. end
  1102. end
  1103. out = @ui.output.split "\n"
  1104. assert_equal "Gems to install:", out.shift
  1105. assert_equal " a-3", out.shift
  1106. assert_empty out
  1107. end
  1108. end