gli 2.19.2 → 2.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +28 -0
- data/.gitignore +1 -3
- data/Gemfile +0 -6
- data/README.rdoc +2 -12
- data/Rakefile +15 -37
- data/bin/ci +29 -0
- data/bin/gli +25 -64
- data/bin/rake +29 -0
- data/bin/setup +5 -0
- data/exe/gli +68 -0
- data/gli.gemspec +19 -21
- data/gli.rdoc +2 -2
- data/lib/gli/commands/help_modules/command_help_format.rb +1 -1
- data/lib/gli/commands/help_modules/global_help_format.rb +1 -1
- data/lib/gli/commands/scaffold.rb +9 -93
- data/lib/gli/options.rb +2 -2
- data/lib/gli/version.rb +1 -1
- data/object-model.dot +29 -0
- data/object-model.png +0 -0
- data/test/apps/todo/Gemfile +1 -1
- data/test/apps/todo/bin/todo +1 -1
- data/test/integration/gli_cli_test.rb +69 -0
- data/test/integration/gli_powered_app_test.rb +52 -0
- data/test/integration/scaffold_test.rb +30 -0
- data/test/integration/test_helper.rb +52 -0
- data/test/{tc_command_finder.rb → unit/command_finder_test.rb} +6 -6
- data/test/{tc_command.rb → unit/command_test.rb} +4 -4
- data/test/unit/compound_command_test.rb +17 -0
- data/test/{tc_doc.rb → unit/doc_test.rb} +38 -51
- data/test/{tc_flag.rb → unit/flag_test.rb} +19 -25
- data/test/{tc_gli.rb → unit/gli_test.rb} +28 -47
- data/test/{tc_help.rb → unit/help_test.rb} +48 -107
- data/test/{init_simplecov.rb → unit/init_simplecov.rb} +0 -0
- data/test/{tc_options.rb → unit/options_test.rb} +4 -4
- data/test/unit/subcommand_parsing_test.rb +263 -0
- data/test/unit/subcommands_test.rb +245 -0
- data/test/{fake_std_out.rb → unit/support/fake_std_out.rb} +0 -0
- data/test/{config.yaml → unit/support/gli_test_config.yml} +0 -0
- data/test/unit/switch_test.rb +49 -0
- data/test/{tc_terminal.rb → unit/terminal_test.rb} +4 -3
- data/test/unit/test_helper.rb +13 -0
- data/test/unit/verbatim_wrapper_test.rb +24 -0
- metadata +57 -124
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/.travis.yml +0 -11
- data/ObjectModel.graffle +0 -1191
- data/bin/report_on_rake_results +0 -10
- data/bin/test_all_rubies.sh +0 -6
- data/features/gli_executable.feature +0 -90
- data/features/gli_init.feature +0 -236
- data/features/step_definitions/gli_executable_steps.rb +0 -18
- data/features/step_definitions/gli_init_steps.rb +0 -11
- data/features/step_definitions/todo_steps.rb +0 -100
- data/features/support/env.rb +0 -54
- data/features/support/hooks.rb +0 -5
- data/features/todo.feature +0 -579
- data/features/todo_legacy.feature +0 -130
- data/test/option_test_helper.rb +0 -13
- data/test/tc_compound_command.rb +0 -22
- data/test/tc_subcommand_parsing.rb +0 -280
- data/test/tc_subcommands.rb +0 -259
- data/test/tc_switch.rb +0 -55
- data/test/tc_verbatim_wrapper.rb +0 -36
- data/test/test_helper.rb +0 -21
File without changes
|
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
|
-
class
|
3
|
+
class OptiosnTest < MiniTest::Test
|
4
4
|
include TestHelper
|
5
5
|
|
6
6
|
def test_by_method
|
@@ -10,7 +10,7 @@ class TC_testOptions < Clean::Test::TestCase
|
|
10
10
|
assert_equal 'verbose', o[:name]
|
11
11
|
assert_equal 'verbose', o['name']
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def test_by_string
|
15
15
|
o = GLI::Options.new
|
16
16
|
o['name'] = 'verbose'
|
@@ -18,7 +18,7 @@ class TC_testOptions < Clean::Test::TestCase
|
|
18
18
|
assert_equal 'verbose', o[:name]
|
19
19
|
assert_equal 'verbose', o['name']
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def test_by_symbol
|
23
23
|
o = GLI::Options.new
|
24
24
|
o[:name] = 'verbose'
|
@@ -0,0 +1,263 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
require_relative "support/fake_std_out"
|
3
|
+
|
4
|
+
class SubcommandParsingTest < MiniTest::Test
|
5
|
+
include TestHelper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@fake_stdout = FakeStdOut.new
|
9
|
+
@fake_stderr = FakeStdOut.new
|
10
|
+
|
11
|
+
@original_stdout = $stdout
|
12
|
+
$stdout = @fake_stdout
|
13
|
+
@original_stderr = $stderr
|
14
|
+
$stderr = @fake_stderr
|
15
|
+
|
16
|
+
@app = CLIApp.new
|
17
|
+
@app.reset
|
18
|
+
@app.subcommand_option_handling :legacy
|
19
|
+
@app.error_device=@fake_stderr
|
20
|
+
ENV.delete('GLI_DEBUG')
|
21
|
+
|
22
|
+
@results = {}
|
23
|
+
@exit_code = 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def teardown
|
27
|
+
$stdout = @original_stdout
|
28
|
+
$stderr = @original_stderr
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_commands_options_may_clash_with_globals_and_it_gets_sorted_out
|
32
|
+
setup_app_with_subcommands_storing_results
|
33
|
+
@app.run(%w(-f global command1 -f command -s foo))
|
34
|
+
assert_equal 'command1',@results[:command_name]
|
35
|
+
assert_equal 'global', @results[:global_options][:f],'global'
|
36
|
+
assert !@results[:global_options][:s]
|
37
|
+
assert_equal 'command', @results[:command_options][:f]
|
38
|
+
assert @results[:command_options][:s]
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_in_legacy_mode_subcommand_options_all_share_a_namespace
|
42
|
+
setup_app_with_subcommands_storing_results
|
43
|
+
@app.run(%w(-f global command1 -f command -s subcommand10 -f sub))
|
44
|
+
with_clue {
|
45
|
+
assert_equal 'subcommand10',@results[:command_name]
|
46
|
+
assert_equal 'global', @results[:global_options][:f],'global'
|
47
|
+
assert !@results[:global_options][:s]
|
48
|
+
assert_equal 'sub', @results[:command_options][:f]
|
49
|
+
assert @results[:command_options][:s]
|
50
|
+
assert_nil @results[:command_options][GLI::Command::PARENT]
|
51
|
+
assert_nil @results[:command_options][GLI::Command::PARENT]
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_in_normal_mode_each_subcommand_has_its_own_namespace
|
56
|
+
setup_app_with_subcommands_storing_results :normal
|
57
|
+
@app.run(%w(-f global command1 -f command -s subcommand10 -f sub))
|
58
|
+
with_clue {
|
59
|
+
assert_equal 'subcommand10',@results[:command_name]
|
60
|
+
assert_equal 'global', @results[:global_options][:f],'global'
|
61
|
+
assert !@results[:global_options][:s]
|
62
|
+
assert_equal 'sub', @results[:command_options][:f]
|
63
|
+
assert !@results[:command_options][:s]
|
64
|
+
assert_equal 'command',@results[:command_options][GLI::Command::PARENT][:f]
|
65
|
+
assert @results[:command_options][GLI::Command::PARENT][:s]
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_in_loose_mode_with_autocomplete_false_it_doesnt_autocorrect_a_sub_command
|
70
|
+
setup_app_with_subcommand_storing_results :normal, false, :loose
|
71
|
+
@app.run(%w(-f global command -f flag -s subcomm -f subflag))
|
72
|
+
with_clue {
|
73
|
+
assert_equal "command",@results[:command_name]
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_in_strict_mode_with_autocomplete_false_it_doesnt_autocorrect_a_sub_command
|
78
|
+
setup_app_with_subcommand_storing_results :normal, false, :strict
|
79
|
+
@app.run(%w(-f global command -f flag -s subcomm -f subflag))
|
80
|
+
with_clue {
|
81
|
+
assert_equal nil,@results[:command_name]
|
82
|
+
assert @fake_stderr.contained?(/error: Too many arguments for command/)
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_in_loose_mode_argument_validation_is_ignored
|
87
|
+
setup_app_with_arguments 1, 1, false, :loose
|
88
|
+
run_app_with_X_arguments 0
|
89
|
+
with_clue {
|
90
|
+
assert_equal 0, @results[:number_of_args_give_to_action]
|
91
|
+
assert_equal 0, @exit_code
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_in_strict_mode_subcommand_option_handling_must_be_normal
|
96
|
+
setup_app_with_arguments 1, 1, false, :strict, :legacy
|
97
|
+
run_app_with_X_arguments 1
|
98
|
+
with_clue {
|
99
|
+
assert_nil @results[:number_of_args_give_to_action]
|
100
|
+
assert_equal 1, @exit_code
|
101
|
+
assert @fake_stderr.contained?(/you must enable normal subcommand_option_handling/)
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
[
|
106
|
+
[1 , 1 , false , 1 ] ,
|
107
|
+
[1 , 1 , false , 2 ] ,
|
108
|
+
[1 , 1 , true , 1 ] ,
|
109
|
+
[1 , 1 , true , 2 ] ,
|
110
|
+
[1 , 1 , true , 3 ] ,
|
111
|
+
[1 , 1 , true , 30 ] ,
|
112
|
+
[0 , 0 , false , 0 ] ,
|
113
|
+
[0 , 1 , false , 1 ] ,
|
114
|
+
[0 , 1 , false , 0 ] ,
|
115
|
+
[1 , 0 , false , 1 ] ,
|
116
|
+
[0 , 0 , true , 0 ] ,
|
117
|
+
[0 , 0 , true , 10 ]
|
118
|
+
|
119
|
+
].each do |number_required, number_optional, has_multiple, number_generated|
|
120
|
+
define_method "test_strict_options_success_cases_#{number_required}_#{number_optional}_#{has_multiple}_#{number_generated}" do
|
121
|
+
setup_app_with_arguments number_required, number_optional, has_multiple, :strict
|
122
|
+
run_app_with_X_arguments number_generated
|
123
|
+
with_clue {
|
124
|
+
assert_equal number_generated, @results[:number_of_args_give_to_action]
|
125
|
+
assert_equal 0, @exit_code
|
126
|
+
assert !@fake_stderr.contained?(/Not enough arguments for command/)
|
127
|
+
assert !@fake_stderr.contained?(/Too many arguments for command/)
|
128
|
+
}
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
[
|
133
|
+
[1 , 1 , false , 3 , :too_many] ,
|
134
|
+
[0 , 0 , false , 1 , :too_many] ,
|
135
|
+
[1 , 1 , false , 0 , :not_enough] ,
|
136
|
+
[1 , 1 , true , 0 , :not_enough] ,
|
137
|
+
[1 , 0 , false , 0 , :not_enough] ,
|
138
|
+
|
139
|
+
].each do |number_required, number_optional, has_multiple, number_generated, status|
|
140
|
+
define_method "test_strict_options_failure_cases_#{number_required}_#{number_optional}_#{has_multiple}_#{number_generated}_#{status}" do
|
141
|
+
setup_app_with_arguments number_required, number_optional, has_multiple, :strict
|
142
|
+
run_app_with_X_arguments number_generated
|
143
|
+
with_clue {
|
144
|
+
if status == :not_enough then
|
145
|
+
assert_nil @results[:number_of_args_give_to_action]
|
146
|
+
assert_equal 64, @exit_code
|
147
|
+
assert @fake_stderr.contained?(/Not enough arguments for command/)
|
148
|
+
elsif status == :too_many then
|
149
|
+
assert_nil @results[:number_of_args_give_to_action]
|
150
|
+
assert_equal 64, @exit_code
|
151
|
+
assert @fake_stderr.contained?(/Too many arguments for command/)
|
152
|
+
else
|
153
|
+
assert false
|
154
|
+
end
|
155
|
+
}
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
def with_clue(&block)
|
161
|
+
block.call
|
162
|
+
rescue Exception
|
163
|
+
dump = ""
|
164
|
+
PP.pp "\nRESULTS---#{@results}", dump unless @results.empty?
|
165
|
+
PP.pp "\nSTDERR---\n#{@fake_stderr.to_s}", dump
|
166
|
+
PP.pp "\nSTDOUT---\n#{@fake_stdout.to_s}", dump
|
167
|
+
@original_stdout.puts dump
|
168
|
+
raise
|
169
|
+
end
|
170
|
+
|
171
|
+
def setup_app_with_subcommand_storing_results(subcommand_option_handling_strategy, autocomplete, arguments_handling_strategy)
|
172
|
+
@app.subcommand_option_handling subcommand_option_handling_strategy
|
173
|
+
@app.autocomplete_commands autocomplete
|
174
|
+
@app.arguments arguments_handling_strategy
|
175
|
+
@app.flag ['f','flag']
|
176
|
+
@app.switch ['s','switch']
|
177
|
+
|
178
|
+
@app.command "command" do |c|
|
179
|
+
c.flag ['f','flag']
|
180
|
+
c.switch ['s','switch']
|
181
|
+
c.action do |global,options,args|
|
182
|
+
@results = {
|
183
|
+
:command_name => "command",
|
184
|
+
:global_options => global,
|
185
|
+
:command_options => options,
|
186
|
+
:args => args
|
187
|
+
}
|
188
|
+
end
|
189
|
+
|
190
|
+
c.command "subcommand" do |subcommand|
|
191
|
+
subcommand.flag ['f','flag']
|
192
|
+
subcommand.flag ['foo']
|
193
|
+
subcommand.switch ['s','switch']
|
194
|
+
subcommand.action do |global,options,args|
|
195
|
+
@results = {
|
196
|
+
:command_name => "subcommand",
|
197
|
+
:global_options => global,
|
198
|
+
:command_options => options,
|
199
|
+
:args => args
|
200
|
+
}
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def setup_app_with_subcommands_storing_results(subcommand_option_handling_strategy = :legacy)
|
207
|
+
@app.subcommand_option_handling subcommand_option_handling_strategy
|
208
|
+
@app.flag ['f','flag']
|
209
|
+
@app.switch ['s','switch']
|
210
|
+
|
211
|
+
2.times do |i|
|
212
|
+
@app.command "command#{i}" do |c|
|
213
|
+
c.flag ['f','flag']
|
214
|
+
c.switch ['s','switch']
|
215
|
+
c.action do |global,options,args|
|
216
|
+
@results = {
|
217
|
+
:command_name => "command#{i}",
|
218
|
+
:global_options => global,
|
219
|
+
:command_options => options,
|
220
|
+
:args => args
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
224
|
+
2.times do |j|
|
225
|
+
c.command "subcommand#{i}#{j}" do |subcommand|
|
226
|
+
subcommand.flag ['f','flag']
|
227
|
+
subcommand.flag ['foo']
|
228
|
+
subcommand.switch ['s','switch']
|
229
|
+
subcommand.action do |global,options,args|
|
230
|
+
@results = {
|
231
|
+
:command_name => "subcommand#{i}#{j}",
|
232
|
+
:global_options => global,
|
233
|
+
:command_options => options,
|
234
|
+
:args => args
|
235
|
+
}
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def setup_app_with_arguments(number_required_arguments, number_optional_arguments, has_argument_multiple, arguments_handling_strategy = :loose, subcommand_option_handling_strategy = :normal)
|
244
|
+
@app.arguments arguments_handling_strategy
|
245
|
+
@app.subcommand_option_handling subcommand_option_handling_strategy
|
246
|
+
|
247
|
+
number_required_arguments.times { |i| @app.arg("needed#{i}") }
|
248
|
+
number_optional_arguments.times { |i| @app.arg("optional#{i}", :optional) }
|
249
|
+
@app.arg :multiple, [:multiple, :optional] if has_argument_multiple
|
250
|
+
|
251
|
+
@app.command :cmd do |c|
|
252
|
+
c.action do |g,o,a|
|
253
|
+
@results = {
|
254
|
+
:number_of_args_give_to_action => a.size
|
255
|
+
}
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def run_app_with_X_arguments(number_arguments)
|
261
|
+
@exit_code = @app.run [].tap{|args| args << "cmd"; number_arguments.times {|i| args << "arg#{i}"}}
|
262
|
+
end
|
263
|
+
end
|
@@ -0,0 +1,245 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
require_relative "support/fake_std_out"
|
3
|
+
|
4
|
+
class SubcommandsTest < MiniTest::Test
|
5
|
+
include TestHelper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@fake_stdout = FakeStdOut.new
|
9
|
+
@fake_stderr = FakeStdOut.new
|
10
|
+
|
11
|
+
@original_stdout = $stdout
|
12
|
+
$stdout = @fake_stdout
|
13
|
+
@original_stderr = $stderr
|
14
|
+
$stderr = @fake_stderr
|
15
|
+
|
16
|
+
@app = CLIApp.new
|
17
|
+
@app.reset
|
18
|
+
@app.subcommand_option_handling :legacy
|
19
|
+
@app.error_device=@fake_stderr
|
20
|
+
ENV.delete('GLI_DEBUG')
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
$stdout = @original_stdout
|
25
|
+
$stderr = @original_stderr
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_we_run_add_command_using_add
|
29
|
+
we_have_a_command_with_two_subcommands
|
30
|
+
run_app('remote',"add",'-f','foo','bar')
|
31
|
+
assert_command_ran_with(:add, :command_options => {:f => true}, :args => %w(foo bar))
|
32
|
+
end
|
33
|
+
def test_we_run_add_command_using_new
|
34
|
+
we_have_a_command_with_two_subcommands
|
35
|
+
run_app('remote',"new",'-f','foo','bar')
|
36
|
+
assert_command_ran_with(:add, :command_options => {:f => true}, :args => %w(foo bar))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_subcommands_not_used_on_command_line_uses_base_action
|
40
|
+
we_have_a_command_with_two_subcommands
|
41
|
+
run_app('remote','foo','bar')
|
42
|
+
assert_command_ran_with(:base, :command_options => {:f => false}, :args => %w(foo bar))
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_switches_and_flags_on_subcommand_available
|
46
|
+
we_have_a_command_with_two_subcommands(:switches => [:addswitch], :flags => [:addflag])
|
47
|
+
run_app('remote','add','--addswitch','--addflag','foo','bar')
|
48
|
+
assert_command_ran_with(:add,:command_options => { :addswitch => true, :addflag => 'foo', :f => false },
|
49
|
+
:args => ['bar'])
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_help_flag_works_in_normal_mode
|
53
|
+
@app.subcommand_option_handling :normal
|
54
|
+
we_have_a_command_with_two_subcommands
|
55
|
+
@app.run(["remote", "add", "--help"]) rescue nil
|
56
|
+
refute_match /^error/, @fake_stderr.to_s, "should not output an error message"
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_help_flag_works_in_legacy_mode
|
60
|
+
@app.subcommand_option_handling :legacy
|
61
|
+
we_have_a_command_with_two_subcommands
|
62
|
+
@app.run(["remote", "add", "--help"]) rescue nil
|
63
|
+
refute_match /^error/, @fake_stderr.to_s, "should not output an error message"
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_we_can_reopen_commands_to_add_new_subcommands
|
67
|
+
@app.command :remote do |p|
|
68
|
+
p.command :add do |c|
|
69
|
+
c.action do |global_options,command_options,args|
|
70
|
+
@ran_command = :add
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
@app.command :remote do |p|
|
75
|
+
p.command :new do |c|
|
76
|
+
c.action do |global_options,command_options,args|
|
77
|
+
@ran_command = :new
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
run_app('remote','new')
|
82
|
+
assert_equal(@ran_command, :new)
|
83
|
+
run_app('remote', 'add')
|
84
|
+
assert_equal(@ran_command, :add)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_reopening_commands_doesnt_readd_to_output
|
88
|
+
@app.command :remote do |p|
|
89
|
+
p.command(:add) { }
|
90
|
+
end
|
91
|
+
@app.command :remote do |p|
|
92
|
+
p.command(:new) { }
|
93
|
+
end
|
94
|
+
command_names = @app.instance_variable_get("@commands_declaration_order").collect { |c| c.name }
|
95
|
+
assert_equal 1, command_names.grep(:remote).size
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
def test_we_can_reopen_commands_without_causing_conflicts
|
100
|
+
@app.command :remote do |p|
|
101
|
+
p.command :add do |c|
|
102
|
+
c.action do |global_options,command_options,args|
|
103
|
+
@ran_command = :remote_add
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
@app.command :local do |p|
|
108
|
+
p.command :add do |c|
|
109
|
+
c.action do |global_options,command_options,args|
|
110
|
+
@ran_command = :local_add
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
run_app('remote','add')
|
115
|
+
assert_equal(@ran_command, :remote_add)
|
116
|
+
run_app('local', 'add')
|
117
|
+
assert_equal(@ran_command, :local_add)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
def test_we_can_nest_subcommands_very_deep
|
122
|
+
@run_results = { :add => nil, :rename => nil, :base => nil }
|
123
|
+
@app.command :remote do |c|
|
124
|
+
|
125
|
+
c.switch :f
|
126
|
+
c.command :add do |add|
|
127
|
+
add.command :some do |some|
|
128
|
+
some.command :cmd do |cmd|
|
129
|
+
cmd.switch :s
|
130
|
+
cmd.action do |global_options,command_options,args|
|
131
|
+
@run_results[:cmd] = [global_options,command_options,args]
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
ENV['GLI_DEBUG'] = 'true'
|
138
|
+
run_app('remote','add','some','cmd','-s','blah')
|
139
|
+
assert_command_ran_with(:cmd, :command_options => {:s => true, :f => false},:args => ['blah'])
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_when_any_command_has_no_action_but_there_are_args_indicate_unknown_command
|
143
|
+
a_very_deeply_nested_command_structure
|
144
|
+
assert_raises GLI::UnknownCommand do
|
145
|
+
When run_app('remote','add','some','foo')
|
146
|
+
end
|
147
|
+
assert_match /Unknown command 'foo'/,@fake_stderr.to_s
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_when_any_command_has_no_action_but_there_are_no_args_indicate_subcommand_needed
|
151
|
+
a_very_deeply_nested_command_structure
|
152
|
+
assert_raises GLI::BadCommandLine do
|
153
|
+
When run_app('remote','add','some')
|
154
|
+
end
|
155
|
+
assert_match /Command 'some' requires a subcommand/,@fake_stderr.to_s
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
def run_app(*args)
|
161
|
+
@exit_code = @app.run(args)
|
162
|
+
end
|
163
|
+
|
164
|
+
def a_very_deeply_nested_command_structure
|
165
|
+
@run_results = { :add => nil, :rename => nil, :base => nil }
|
166
|
+
@app.command :remote do |c|
|
167
|
+
|
168
|
+
c.switch :f
|
169
|
+
c.command :add do |add|
|
170
|
+
add.command :some do |some|
|
171
|
+
some.command :cmd do |cmd|
|
172
|
+
cmd.switch :s
|
173
|
+
cmd.action do |global_options,command_options,args|
|
174
|
+
@run_results[:cmd] = [global_options,command_options,args]
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
ENV['GLI_DEBUG'] = 'true'
|
181
|
+
end
|
182
|
+
|
183
|
+
# expected_command - name of command exepcted to have been run
|
184
|
+
# options:
|
185
|
+
# - global_options => hash of expected options
|
186
|
+
# - command_options => hash of expected command options
|
187
|
+
# - args => array of expected args
|
188
|
+
def assert_command_ran_with(expected_command,options)
|
189
|
+
global_options = options[:global_options] || { :help => false }
|
190
|
+
@run_results.each do |command,results|
|
191
|
+
if command == expected_command
|
192
|
+
assert_equal(indifferent_hash(global_options),results[0])
|
193
|
+
assert_equal(indifferent_hash(options[:command_options]),results[1])
|
194
|
+
assert_equal(options[:args],results[2])
|
195
|
+
else
|
196
|
+
assert_nil results
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def indifferent_hash(possibly_nil_hash)
|
202
|
+
return {} if possibly_nil_hash.nil?
|
203
|
+
possibly_nil_hash.keys.each do |key|
|
204
|
+
if key.kind_of? Symbol
|
205
|
+
possibly_nil_hash[key.to_s] = possibly_nil_hash[key] unless possibly_nil_hash.has_key?(key.to_s)
|
206
|
+
elsif key.kind_of? String
|
207
|
+
possibly_nil_hash[key.to_sym] = possibly_nil_hash[key] unless possibly_nil_hash.has_key?(key.to_sym)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
possibly_nil_hash
|
211
|
+
end
|
212
|
+
|
213
|
+
# options -
|
214
|
+
# :flags => flags to add to :add
|
215
|
+
# :switiches => switiches to add to :add
|
216
|
+
def we_have_a_command_with_two_subcommands(options = {})
|
217
|
+
@run_results = { :add => nil, :rename => nil, :base => nil }
|
218
|
+
@app.command :remote do |c|
|
219
|
+
|
220
|
+
c.switch :f
|
221
|
+
|
222
|
+
c.desc "add a remote"
|
223
|
+
c.command [:add,:new] do |add|
|
224
|
+
|
225
|
+
Array(options[:flags]).each { |_| add.flag _ }
|
226
|
+
Array(options[:switches]).each { |_| add.switch _ }
|
227
|
+
add.action do |global_options,command_options,args|
|
228
|
+
@run_results[:add] = [global_options,command_options,args]
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
c.desc "rename a remote"
|
233
|
+
c.command :rename do |rename|
|
234
|
+
rename.action do |global_options,command_options,args|
|
235
|
+
@run_results[:rename] = [global_options,command_options,args]
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
c.action do |global_options,command_options,args|
|
240
|
+
@run_results[:base] = [global_options,command_options,args]
|
241
|
+
end
|
242
|
+
end
|
243
|
+
ENV['GLI_DEBUG'] = 'true'
|
244
|
+
end
|
245
|
+
end
|