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

/vendor/bundle/ruby/1.9.1/gems/capistrano-2.9.0/test/cli/options_test.rb

https://github.com/bublanina/masterlanguage
Ruby | 329 lines | 275 code | 54 blank | 0 comment | 0 complexity | 88f0487407c45bab2a132d2993a8db43 MD5 | raw file
  1. require "utils"
  2. require 'capistrano/cli/options'
  3. class CLIOptionsTest < Test::Unit::TestCase
  4. class ExitException < Exception; end
  5. class MockCLI
  6. def initialize
  7. @args = []
  8. end
  9. attr_reader :args
  10. include Capistrano::CLI::Options
  11. end
  12. def setup
  13. @cli = MockCLI.new
  14. end
  15. def test_parse_options_should_require_non_empty_args_list
  16. @cli.stubs(:warn)
  17. @cli.expects(:exit).raises(ExitException)
  18. assert_raises(ExitException) { @cli.parse_options! }
  19. end
  20. def test_parse_options_with_d_should_set_debug_option
  21. @cli.args << "-d"
  22. @cli.parse_options!
  23. assert @cli.options[:debug]
  24. end
  25. def test_parse_options_with_n_should_set_dry_run_option
  26. @cli.args << "-n"
  27. @cli.parse_options!
  28. assert @cli.options[:dry_run]
  29. end
  30. def test_parse_options_with_dry_run_should_set_dry_run_option
  31. @cli.args << "--dry-run"
  32. @cli.parse_options!
  33. assert @cli.options[:dry_run]
  34. end
  35. def test_parse_options_with_e_should_set_explain_option
  36. @cli.args << "-e" << "sample"
  37. @cli.parse_options!
  38. assert_equal "sample", @cli.options[:explain]
  39. end
  40. def test_parse_options_with_f_should_add_recipe_file
  41. @cli.args << "-f" << "deploy"
  42. @cli.parse_options!
  43. assert_equal %w(deploy), @cli.options[:recipes]
  44. end
  45. def test_parse_options_with_multiple_f_should_add_each_as_recipe_file
  46. @cli.args << "-f" << "deploy" << "-f" << "monitor"
  47. @cli.parse_options!
  48. assert_equal %w(deploy monitor), @cli.options[:recipes]
  49. end
  50. def test_parse_options_with_H_should_show_verbose_help_and_exit
  51. @cli.expects(:exit).raises(ExitException)
  52. @cli.expects(:long_help)
  53. @cli.args << "-H"
  54. assert_raises(ExitException) { @cli.parse_options! }
  55. end
  56. def test_parse_options_with_h_should_show_options_and_exit
  57. @cli.expects(:puts).with(@cli.option_parser)
  58. @cli.expects(:exit).raises(ExitException)
  59. @cli.args << "-h"
  60. assert_raises(ExitException) { @cli.parse_options! }
  61. end
  62. def test_parse_options_with_p_should_prompt_for_password
  63. MockCLI.expects(:password_prompt).returns(:the_password)
  64. @cli.args << "-p"
  65. @cli.parse_options!
  66. assert_equal :the_password, @cli.options[:password]
  67. end
  68. def test_parse_options_without_p_should_set_proc_for_password
  69. @cli.args << "-e" << "sample"
  70. @cli.parse_options!
  71. assert_instance_of Proc, @cli.options[:password]
  72. end
  73. def test_parse_options_with_q_should_set_verbose_to_0
  74. @cli.args << "-q"
  75. @cli.parse_options!
  76. assert_equal 0, @cli.options[:verbose]
  77. end
  78. def test_parse_options_with_r_should_set_preserve_roles_option
  79. @cli.args << "-r"
  80. @cli.parse_options!
  81. assert @cli.options[:preserve_roles]
  82. end
  83. def test_parse_options_with_preserve_roles_should_set_preserve_roles_option
  84. @cli.args << "--preserve-roles"
  85. @cli.parse_options!
  86. assert @cli.options[:preserve_roles]
  87. end
  88. def test_parse_options_with_S_should_set_pre_vars
  89. @cli.args << "-S" << "foo=bar"
  90. @cli.parse_options!
  91. assert_equal "bar", @cli.options[:pre_vars][:foo]
  92. end
  93. def test_S_should_coerce_digits_to_integers
  94. @cli.args << "-S" << "foo=1234"
  95. @cli.parse_options!
  96. assert_equal 1234, @cli.options[:pre_vars][:foo]
  97. end
  98. def test_S_should_treat_quoted_integers_as_string
  99. @cli.args << "-S" << "foo=\"1234\""
  100. @cli.parse_options!
  101. assert_equal "1234", @cli.options[:pre_vars][:foo]
  102. end
  103. def test_S_should_treat_digits_with_dot_as_floating_point
  104. @cli.args << "-S" << "foo=3.1415"
  105. @cli.parse_options!
  106. assert_equal 3.1415, @cli.options[:pre_vars][:foo]
  107. end
  108. def test_S_should_treat_true_as_boolean_true
  109. @cli.args << "-S" << "foo=true"
  110. @cli.parse_options!
  111. assert_equal true, @cli.options[:pre_vars][:foo]
  112. end
  113. def test_S_should_treat_false_as_boolean_false
  114. @cli.args << "-S" << "foo=false"
  115. @cli.parse_options!
  116. assert_equal false, @cli.options[:pre_vars][:foo]
  117. end
  118. def test_S_should_treat_nil_as_nil
  119. @cli.args << "-S" << "foo=nil"
  120. @cli.parse_options!
  121. assert_equal nil, @cli.options[:pre_vars][:foo]
  122. end
  123. def test_parse_options_with_s_should_set_vars
  124. @cli.args << "-s" << "foo=bar"
  125. @cli.parse_options!
  126. assert_equal "bar", @cli.options[:vars][:foo]
  127. end
  128. def test_s_should_coerce_digits_to_integers
  129. @cli.args << "-s" << "foo=1234"
  130. @cli.parse_options!
  131. assert_equal 1234, @cli.options[:vars][:foo]
  132. end
  133. def test_s_should_treat_quoted_integers_as_string
  134. @cli.args << "-s" << "foo=\"1234\""
  135. @cli.parse_options!
  136. assert_equal "1234", @cli.options[:vars][:foo]
  137. end
  138. def test_s_should_treat_digits_with_dot_as_floating_point
  139. @cli.args << "-s" << "foo=3.1415"
  140. @cli.parse_options!
  141. assert_equal 3.1415, @cli.options[:vars][:foo]
  142. end
  143. def test_s_should_treat_true_as_boolean_true
  144. @cli.args << "-s" << "foo=true"
  145. @cli.parse_options!
  146. assert_equal true, @cli.options[:vars][:foo]
  147. end
  148. def test_s_should_treat_false_as_boolean_false
  149. @cli.args << "-s" << "foo=false"
  150. @cli.parse_options!
  151. assert_equal false, @cli.options[:vars][:foo]
  152. end
  153. def test_s_should_treat_nil_as_nil
  154. @cli.args << "-s" << "foo=nil"
  155. @cli.parse_options!
  156. assert_equal nil, @cli.options[:vars][:foo]
  157. end
  158. def test_parse_options_with_T_should_set_tasks_option_and_set_verbose_off
  159. @cli.args << "-T"
  160. @cli.parse_options!
  161. assert @cli.options[:tasks]
  162. assert_equal 0, @cli.options[:verbose]
  163. end
  164. def test_parse_options_with_V_should_show_version_and_exit
  165. @cli.args << "-V"
  166. @cli.expects(:puts).with { |s| s.include?(Capistrano::Version.to_s) }
  167. @cli.expects(:exit).raises(ExitException)
  168. assert_raises(ExitException) { @cli.parse_options! }
  169. end
  170. def test_parse_options_with_v_should_set_verbose_to_1
  171. @cli.args << "-v"
  172. @cli.parse_options!
  173. assert_equal 1, @cli.options[:verbose]
  174. end
  175. def test_parse_options_with_multiple_v_should_set_verbose_accordingly
  176. @cli.args << "-vvvvvvv"
  177. @cli.parse_options!
  178. assert_equal 7, @cli.options[:verbose]
  179. end
  180. def test_parse_options_without_X_should_set_sysconf
  181. @cli.args << "-v"
  182. @cli.parse_options!
  183. assert @cli.options.key?(:sysconf)
  184. end
  185. def test_parse_options_with_X_should_unset_sysconf
  186. @cli.args << "-X"
  187. @cli.parse_options!
  188. assert !@cli.options.key?(:sysconf)
  189. end
  190. def test_parse_options_without_x_should_set_dotfile
  191. @cli.args << "-v"
  192. @cli.parse_options!
  193. assert @cli.options.key?(:dotfile)
  194. end
  195. def test_parse_options_with_x_should_unset_dotfile
  196. @cli.args << "-x"
  197. @cli.parse_options!
  198. assert !@cli.options.key?(:dotfile)
  199. end
  200. def test_parse_options_without_q_or_v_should_set_verbose_to_3
  201. @cli.args << "-x"
  202. @cli.parse_options!
  203. assert_equal 3, @cli.options[:verbose]
  204. end
  205. def test_should_search_for_default_recipes_if_f_not_given
  206. @cli.expects(:look_for_default_recipe_file!)
  207. @cli.args << "-v"
  208. @cli.parse_options!
  209. end
  210. def test_should_not_search_for_default_recipes_if_f_given
  211. @cli.expects(:look_for_default_recipe_file!).never
  212. @cli.args << "-f" << "hello"
  213. @cli.parse_options!
  214. end
  215. def test_F_should_search_for_default_recipes_even_if_f_is_given
  216. @cli.expects(:look_for_default_recipe_file!)
  217. @cli.args << "-Ff" << "hello"
  218. @cli.parse_options!
  219. end
  220. def test_should_extract_env_vars_from_command_line
  221. assert_nil ENV["HELLO"]
  222. assert_nil ENV["ANOTHER"]
  223. @cli.args << "HELLO=world" << "hello" << "ANOTHER=value"
  224. @cli.parse_options!
  225. assert_equal "world", ENV["HELLO"]
  226. assert_equal "value", ENV["ANOTHER"]
  227. ensure
  228. ENV.delete("HELLO")
  229. ENV.delete("ANOTHER")
  230. end
  231. def test_remaining_args_should_be_added_to_actions_list
  232. @cli.args << "-v" << "HELLO=world" << "-f" << "foo" << "something" << "else"
  233. @cli.parse_options!
  234. assert_equal %w(something else), @cli.args
  235. ensure
  236. ENV.delete("HELLO")
  237. end
  238. def test_search_for_default_recipe_file_should_look_for_Capfile
  239. File.stubs(:file?).returns(false)
  240. File.expects(:file?).with("Capfile").returns(true)
  241. @cli.args << "-v"
  242. @cli.parse_options!
  243. assert_equal %w(Capfile), @cli.options[:recipes]
  244. end
  245. def test_search_for_default_recipe_file_should_look_for_capfile
  246. File.stubs(:file?).returns(false)
  247. File.expects(:file?).with("capfile").returns(true)
  248. @cli.args << "-v"
  249. @cli.parse_options!
  250. assert_equal %w(capfile), @cli.options[:recipes]
  251. end
  252. def test_search_for_default_recipe_should_hike_up_the_directory_tree_until_it_finds_default_recipe
  253. File.stubs(:file?).returns(false)
  254. File.expects(:file?).with("capfile").times(2).returns(false,true)
  255. Dir.expects(:pwd).times(3).returns(*%w(/bar/baz /bar/baz /bar))
  256. Dir.expects(:chdir).with("..")
  257. @cli.args << "-v"
  258. @cli.parse_options!
  259. assert_equal %w(capfile), @cli.options[:recipes]
  260. end
  261. def test_search_for_default_recipe_should_halt_at_root_directory
  262. File.stubs(:file?).returns(false)
  263. Dir.expects(:pwd).times(7).returns(*%w(/bar/baz /bar/baz /bar /bar / / /))
  264. Dir.expects(:chdir).with("..").times(3)
  265. Dir.expects(:chdir).with("/bar/baz")
  266. @cli.args << "-v"
  267. @cli.parse_options!
  268. assert @cli.options[:recipes].empty?
  269. end
  270. def test_parse_should_instantiate_new_cli_and_call_parse_options
  271. cli = mock("cli", :parse_options! => nil)
  272. MockCLI.expects(:new).with(%w(a b c)).returns(cli)
  273. assert_equal cli, MockCLI.parse(%w(a b c))
  274. end
  275. end