jamis / capistrano

Remote multi-server automation tool. This repository is no longer being actively maintained. Please ask on the mailing list to find someone who has a well-maintained fork. Thanks!

This URL has Read+Write access

rmm5t (author)
Sat May 10 16:07:44 -0700 2008
commit  192d8e41e47841f6037fef73929b1f2cbfc3e7a4
tree    ffd74a0847cb41e7b5ea3f36916037c8c50d7226
parent  1114c00039b55b65b8a527c3ddbcb861d742a99a
capistrano / test / cli / options_test.rb
100644 233 lines (194 sloc) 6.754 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require "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_d_should_set_debug_option
    @cli.args << "-d"
    @cli.parse_options!
    assert @cli.options[:debug]
  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
 
  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
 
  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
 
  def test_parse_options_with_T_should_set_tasks_option_and_set_verbose_off
    @cli.args << "-T"
    @cli.parse_options!
    assert @cli.options[:tasks]
    assert_equal 0, @cli.options[:verbose]
  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
    @cli.args << "-x"
    @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
 
  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
 
  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
 
  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
 
  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
end