PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rubygems/test_gem_command_manager.rb

https://github.com/wanabe/ruby
Ruby | 316 lines | 228 code | 57 blank | 31 comment | 3 complexity | 9ed5207bea84757f033fd95a1016e4bc 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/command_manager'
  4. class TestGemCommandManager < Gem::TestCase
  5. PROJECT_DIR = File.expand_path('../../..', __FILE__).tap(&Gem::UNTAINT)
  6. def setup
  7. super
  8. @command_manager = Gem::CommandManager.new
  9. end
  10. def test_find_command
  11. command = @command_manager.find_command 'install'
  12. assert_kind_of Gem::Commands::InstallCommand, command
  13. command = @command_manager.find_command 'ins'
  14. assert_kind_of Gem::Commands::InstallCommand, command
  15. end
  16. def test_find_command_ambiguous
  17. e = assert_raise Gem::CommandLineError do
  18. @command_manager.find_command 'u'
  19. end
  20. assert_equal 'Ambiguous command u matches [uninstall, unpack, update]',
  21. e.message
  22. end
  23. def test_find_alias_command
  24. command = @command_manager.find_command 'i'
  25. assert_kind_of Gem::Commands::InstallCommand, command
  26. end
  27. def test_find_command_ambiguous_exact
  28. ins_command = Class.new
  29. Gem::Commands.send :const_set, :InsCommand, ins_command
  30. @command_manager.register_command :ins
  31. command = @command_manager.find_command 'ins'
  32. assert_kind_of ins_command, command
  33. ensure
  34. Gem::Commands.send :remove_const, :InsCommand
  35. end
  36. def test_find_command_unknown
  37. e = assert_raise Gem::UnknownCommandError do
  38. @command_manager.find_command 'xyz'
  39. end
  40. assert_equal 'Unknown command xyz', e.message
  41. end
  42. def test_find_command_unknown_suggestions
  43. e = assert_raise Gem::UnknownCommandError do
  44. @command_manager.find_command 'pish'
  45. end
  46. message = 'Unknown command pish'.dup
  47. if RUBY_VERSION >= "2.4" && defined?(DidYouMean::SPELL_CHECKERS) && defined?(DidYouMean::Correctable)
  48. message << "\nDid you mean? \"push\""
  49. end
  50. assert_equal message, e.message
  51. end
  52. def test_run_interrupt
  53. old_load_path = $:.dup
  54. $: << File.expand_path("test/rubygems", PROJECT_DIR)
  55. Gem.load_env_plugins
  56. @command_manager.register_command :interrupt
  57. use_ui @ui do
  58. assert_raise Gem::MockGemUi::TermError do
  59. @command_manager.run %w[interrupt]
  60. end
  61. assert_equal '', ui.output
  62. assert_equal "ERROR: Interrupted\n", ui.error
  63. end
  64. ensure
  65. $:.replace old_load_path
  66. Gem::CommandManager.reset
  67. end
  68. def test_run_crash_command
  69. old_load_path = $:.dup
  70. $: << File.expand_path("test/rubygems", PROJECT_DIR)
  71. @command_manager.register_command :crash
  72. use_ui @ui do
  73. assert_raise Gem::MockGemUi::TermError do
  74. @command_manager.run %w[crash]
  75. end
  76. assert_equal '', ui.output
  77. err = ui.error.split("\n").first
  78. assert_equal "ERROR: Loading command: crash (RuntimeError)", err
  79. end
  80. ensure
  81. $:.replace old_load_path
  82. @command_manager.unregister_command :crash
  83. end
  84. def test_process_args_bad_arg
  85. use_ui @ui do
  86. assert_raise Gem::MockGemUi::TermError do
  87. @command_manager.process_args %w[--bad-arg]
  88. end
  89. end
  90. assert_match(/invalid option: --bad-arg/i, @ui.error)
  91. end
  92. # HACK move to install command test
  93. def test_process_args_install
  94. #capture all install options
  95. use_ui @ui do
  96. check_options = nil
  97. @command_manager['install'].when_invoked do |options|
  98. check_options = options
  99. true
  100. end
  101. #check defaults
  102. @command_manager.process_args %w[install]
  103. assert_equal %w[ri], check_options[:document].sort
  104. assert_equal false, check_options[:force]
  105. assert_equal :both, check_options[:domain]
  106. assert_equal true, check_options[:wrappers]
  107. assert_equal Gem::Requirement.default, check_options[:version]
  108. assert_nil check_options[:install_dir]
  109. assert_nil check_options[:bin_dir]
  110. #check settings
  111. check_options = nil
  112. @command_manager.process_args %w[
  113. install --force --local --document=ri,rdoc --install-dir .
  114. --version 3.0 --no-wrapper --bindir .
  115. ]
  116. assert_equal %w[rdoc ri], check_options[:document].sort
  117. assert_equal true, check_options[:force]
  118. assert_equal :local, check_options[:domain]
  119. assert_equal false, check_options[:wrappers]
  120. assert_equal Gem::Requirement.new('3.0'), check_options[:version]
  121. assert_equal Dir.pwd, check_options[:install_dir]
  122. assert_equal Dir.pwd, check_options[:bin_dir]
  123. #check remote domain
  124. check_options = nil
  125. @command_manager.process_args %w[install --remote]
  126. assert_equal :remote, check_options[:domain]
  127. #check both domain
  128. check_options = nil
  129. @command_manager.process_args %w[install --both]
  130. assert_equal :both, check_options[:domain]
  131. #check both domain
  132. check_options = nil
  133. @command_manager.process_args %w[install --both]
  134. assert_equal :both, check_options[:domain]
  135. end
  136. end
  137. # HACK move to uninstall command test
  138. def test_process_args_uninstall
  139. #capture all uninstall options
  140. check_options = nil
  141. @command_manager['uninstall'].when_invoked do |options|
  142. check_options = options
  143. true
  144. end
  145. #check defaults
  146. @command_manager.process_args %w[uninstall]
  147. assert_equal Gem::Requirement.default, check_options[:version]
  148. #check settings
  149. check_options = nil
  150. @command_manager.process_args %w[uninstall foobar --version 3.0]
  151. assert_equal "foobar", check_options[:args].first
  152. assert_equal Gem::Requirement.new('3.0'), check_options[:version]
  153. end
  154. # HACK move to check command test
  155. def test_process_args_check
  156. #capture all check options
  157. check_options = nil
  158. @command_manager['check'].when_invoked do |options|
  159. check_options = options
  160. true
  161. end
  162. #check defaults
  163. @command_manager.process_args %w[check]
  164. assert_equal true, check_options[:alien]
  165. #check settings
  166. check_options = nil
  167. @command_manager.process_args %w[check foobar --alien]
  168. assert_equal true, check_options[:alien]
  169. end
  170. # HACK move to build command test
  171. def test_process_args_build
  172. #capture all build options
  173. check_options = nil
  174. @command_manager['build'].when_invoked do |options|
  175. check_options = options
  176. true
  177. end
  178. #check defaults
  179. @command_manager.process_args %w[build]
  180. #NOTE: Currently no defaults
  181. #check settings
  182. check_options = nil
  183. @command_manager.process_args %w[build foobar.rb]
  184. assert_equal 'foobar.rb', check_options[:args].first
  185. end
  186. # HACK move to query command test
  187. def test_process_args_query
  188. #capture all query options
  189. check_options = nil
  190. @command_manager['query'].when_invoked do |options|
  191. check_options = options
  192. true
  193. end
  194. #check defaults
  195. Gem::Deprecate.skip_during do
  196. @command_manager.process_args %w[query]
  197. end
  198. assert_equal(//, check_options[:name])
  199. assert_equal :local, check_options[:domain]
  200. assert_equal false, check_options[:details]
  201. #check settings
  202. check_options = nil
  203. Gem::Deprecate.skip_during do
  204. @command_manager.process_args %w[query --name foobar --local --details]
  205. end
  206. assert_equal(/foobar/i, check_options[:name])
  207. assert_equal :local, check_options[:domain]
  208. assert_equal true, check_options[:details]
  209. #remote domain
  210. check_options = nil
  211. Gem::Deprecate.skip_during do
  212. @command_manager.process_args %w[query --remote]
  213. end
  214. assert_equal :remote, check_options[:domain]
  215. #both (local/remote) domains
  216. check_options = nil
  217. Gem::Deprecate.skip_during do
  218. @command_manager.process_args %w[query --both]
  219. end
  220. assert_equal :both, check_options[:domain]
  221. end
  222. # HACK move to update command test
  223. def test_process_args_update
  224. #capture all update options
  225. check_options = nil
  226. @command_manager['update'].when_invoked do |options|
  227. check_options = options
  228. true
  229. end
  230. #check defaults
  231. @command_manager.process_args %w[update]
  232. assert_includes check_options[:document], 'ri'
  233. #check settings
  234. check_options = nil
  235. @command_manager.process_args %w[update --force --document=ri --install-dir .]
  236. assert_includes check_options[:document], 'ri'
  237. assert_equal true, check_options[:force]
  238. assert_equal Dir.pwd, check_options[:install_dir]
  239. end
  240. def test_deprecated_command
  241. require 'rubygems/command'
  242. foo_command = Class.new(Gem::Command) do
  243. extend Gem::Deprecate
  244. rubygems_deprecate_command
  245. def execute
  246. say "pew pew!"
  247. end
  248. end
  249. Gem::Commands.send(:const_set, :FooCommand, foo_command)
  250. @command_manager.register_command(:foo, foo_command.new("foo"))
  251. use_ui @ui do
  252. @command_manager.process_args(%w[foo])
  253. end
  254. assert_equal "pew pew!\n", @ui.output
  255. assert_match(/WARNING: foo command is deprecated. It will be removed in Rubygems [0-9]+/, @ui.error)
  256. ensure
  257. Gem::Commands.send(:remove_const, :FooCommand)
  258. end
  259. end