2007-03-04 13:19:26 -05:00
|
|
|
require "#{File.dirname(__FILE__)}/../utils"
|
|
|
|
require 'capistrano/cli/options'
|
|
|
|
|
|
|
|
class CLIOptionsTest < Test::Unit::TestCase
|
|
|
|
class ExitException < Exception; end
|
|
|
|
|
|
|
|
class MockCLI
|
|
|
|
def initialize
|
|
|
|
@args = []
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :args
|
|
|
|
|
|
|
|
include Capistrano::CLI::Options
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@cli = MockCLI.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_should_require_non_empty_args_list
|
|
|
|
@cli.stubs(:warn)
|
|
|
|
@cli.expects(:exit).raises(ExitException)
|
|
|
|
assert_raises(ExitException) { @cli.parse_options! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_e_should_set_explain_option
|
|
|
|
@cli.args << "-e" << "sample"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal "sample", @cli.options[:explain]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_f_should_add_recipe_file
|
|
|
|
@cli.args << "-f" << "deploy"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal %w(deploy), @cli.options[:recipes]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_multiple_f_should_add_each_as_recipe_file
|
|
|
|
@cli.args << "-f" << "deploy" << "-f" << "monitor"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal %w(deploy monitor), @cli.options[:recipes]
|
|
|
|
end
|
|
|
|
|
2007-03-29 17:31:20 -04:00
|
|
|
def test_parse_options_with_H_should_show_verbose_help_and_exit
|
|
|
|
@cli.expects(:exit).raises(ExitException)
|
|
|
|
@cli.expects(:long_help)
|
|
|
|
@cli.args << "-H"
|
|
|
|
assert_raises(ExitException) { @cli.parse_options! }
|
|
|
|
end
|
|
|
|
|
2007-03-04 13:19:26 -05:00
|
|
|
def test_parse_options_with_h_should_show_options_and_exit
|
|
|
|
@cli.expects(:puts).with(@cli.option_parser)
|
|
|
|
@cli.expects(:exit).raises(ExitException)
|
|
|
|
@cli.args << "-h"
|
|
|
|
assert_raises(ExitException) { @cli.parse_options! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_p_should_prompt_for_password
|
|
|
|
MockCLI.expects(:password_prompt).returns(:the_password)
|
|
|
|
@cli.args << "-p"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal :the_password, @cli.options[:password]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_without_p_should_set_proc_for_password
|
|
|
|
@cli.args << "-e" << "sample"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_instance_of Proc, @cli.options[:password]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_q_should_set_verbose_to_0
|
|
|
|
@cli.args << "-q"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal 0, @cli.options[:verbose]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_S_should_set_pre_vars
|
|
|
|
@cli.args << "-S" << "foo=bar"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal "bar", @cli.options[:pre_vars][:foo]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_s_should_set_vars
|
|
|
|
@cli.args << "-s" << "foo=bar"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal "bar", @cli.options[:vars][:foo]
|
|
|
|
end
|
|
|
|
|
2007-03-13 11:09:54 -04:00
|
|
|
def test_parse_options_with_T_should_set_tasks_option_and_set_verbose_off
|
2007-03-04 13:19:26 -05:00
|
|
|
@cli.args << "-T"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert @cli.options[:tasks]
|
2007-03-13 11:09:54 -04:00
|
|
|
assert_equal 0, @cli.options[:verbose]
|
2007-03-04 13:19:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_V_should_show_version_and_exit
|
|
|
|
@cli.args << "-V"
|
|
|
|
@cli.expects(:puts).with { |s| s.include?(Capistrano::Version::STRING) }
|
|
|
|
@cli.expects(:exit).raises(ExitException)
|
|
|
|
assert_raises(ExitException) { @cli.parse_options! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_v_should_set_verbose_to_1
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal 1, @cli.options[:verbose]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_multiple_v_should_set_verbose_accordingly
|
|
|
|
@cli.args << "-vvvvvvv"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal 7, @cli.options[:verbose]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_without_X_should_set_sysconf
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert @cli.options.key?(:sysconf)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_X_should_unset_sysconf
|
|
|
|
@cli.args << "-X"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert !@cli.options.key?(:sysconf)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_without_x_should_set_dotfile
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert @cli.options.key?(:dotfile)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_with_x_should_unset_dotfile
|
|
|
|
@cli.args << "-x"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert !@cli.options.key?(:dotfile)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_options_without_q_or_v_should_set_verbose_to_3
|
2007-03-13 11:09:54 -04:00
|
|
|
@cli.args << "-x"
|
2007-03-04 13:19:26 -05:00
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal 3, @cli.options[:verbose]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_search_for_default_recipes_if_f_not_given
|
|
|
|
@cli.expects(:look_for_default_recipe_file!)
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_search_for_default_recipes_if_f_given
|
|
|
|
@cli.expects(:look_for_default_recipe_file!).never
|
|
|
|
@cli.args << "-f" << "hello"
|
|
|
|
@cli.parse_options!
|
|
|
|
end
|
|
|
|
|
2007-03-27 10:17:01 -04:00
|
|
|
def test_F_should_search_for_default_recipes_even_if_f_is_given
|
|
|
|
@cli.expects(:look_for_default_recipe_file!)
|
|
|
|
@cli.args << "-Ff" << "hello"
|
|
|
|
@cli.parse_options!
|
|
|
|
end
|
|
|
|
|
2007-03-04 13:19:26 -05:00
|
|
|
def test_should_extract_env_vars_from_command_line
|
|
|
|
assert_nil ENV["HELLO"]
|
|
|
|
assert_nil ENV["ANOTHER"]
|
|
|
|
|
|
|
|
@cli.args << "HELLO=world" << "hello" << "ANOTHER=value"
|
|
|
|
@cli.parse_options!
|
|
|
|
|
|
|
|
assert_equal "world", ENV["HELLO"]
|
|
|
|
assert_equal "value", ENV["ANOTHER"]
|
|
|
|
ensure
|
|
|
|
ENV["HELLO"] = ENV["ANOTHER"] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_remaining_args_should_be_added_to_actions_list
|
|
|
|
@cli.args << "-v" << "HELLO=world" << "-f" << "foo" << "something" << "else"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal %w(something else), @cli.args
|
|
|
|
ensure
|
|
|
|
ENV["HELLO"] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_search_for_default_recipe_file_should_look_for_Capfile
|
|
|
|
File.stubs(:file?).returns(false)
|
|
|
|
File.expects(:file?).with("Capfile").returns(true)
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal %w(Capfile), @cli.options[:recipes]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_search_for_default_recipe_file_should_look_for_capfile
|
|
|
|
File.stubs(:file?).returns(false)
|
|
|
|
File.expects(:file?).with("capfile").returns(true)
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal %w(capfile), @cli.options[:recipes]
|
|
|
|
end
|
2007-03-04 16:09:37 -05:00
|
|
|
|
2007-03-24 17:12:36 -04:00
|
|
|
def test_search_for_default_recipe_should_hike_up_the_directory_tree_until_it_finds_default_recipe
|
|
|
|
File.stubs(:file?).returns(false)
|
|
|
|
File.expects(:file?).with("capfile").times(2).returns(false,true)
|
|
|
|
Dir.expects(:pwd).times(3).returns(*%w(/bar/baz /bar/baz /bar))
|
|
|
|
Dir.expects(:chdir).with("..")
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert_equal %w(capfile), @cli.options[:recipes]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_search_for_default_recipe_should_halt_at_root_directory
|
|
|
|
File.stubs(:file?).returns(false)
|
|
|
|
Dir.expects(:pwd).times(7).returns(*%w(/bar/baz /bar/baz /bar /bar / / /))
|
|
|
|
Dir.expects(:chdir).with("..").times(3)
|
|
|
|
Dir.expects(:chdir).with("/bar/baz")
|
|
|
|
@cli.args << "-v"
|
|
|
|
@cli.parse_options!
|
|
|
|
assert @cli.options[:recipes].empty?
|
|
|
|
end
|
|
|
|
|
2007-03-04 16:09:37 -05:00
|
|
|
def test_parse_should_instantiate_new_cli_and_call_parse_options
|
|
|
|
cli = mock("cli", :parse_options! => nil)
|
|
|
|
MockCLI.expects(:new).with(%w(a b c)).returns(cli)
|
|
|
|
assert_equal cli, MockCLI.parse(%w(a b c))
|
|
|
|
end
|
2007-03-04 13:19:26 -05:00
|
|
|
end
|