gli 2.11.0 → 2.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.circleci/config.yml +28 -0
- data/.gitignore +3 -3
- data/.tool-versions +1 -0
- data/Gemfile +0 -2
- data/README.rdoc +29 -19
- data/Rakefile +15 -37
- data/bin/ci +29 -0
- data/bin/gli +24 -54
- data/bin/rake +29 -0
- data/bin/setup +5 -0
- data/exe/gli +68 -0
- data/gli.gemspec +20 -24
- data/gli.rdoc +9 -9
- data/lib/gli/app.rb +31 -8
- data/lib/gli/app_support.rb +15 -3
- data/lib/gli/command.rb +24 -2
- data/lib/gli/command_finder.rb +42 -25
- data/lib/gli/command_support.rb +7 -6
- data/lib/gli/commands/doc.rb +9 -3
- data/lib/gli/commands/help.rb +2 -1
- data/lib/gli/commands/help_modules/arg_name_formatter.rb +2 -2
- data/lib/gli/commands/help_modules/command_help_format.rb +19 -1
- data/lib/gli/commands/help_modules/full_synopsis_formatter.rb +3 -2
- data/lib/gli/commands/help_modules/global_help_format.rb +1 -1
- data/lib/gli/commands/help_modules/options_formatter.rb +4 -6
- data/lib/gli/commands/initconfig.rb +3 -6
- data/lib/gli/commands/rdoc_document_listener.rb +2 -1
- data/lib/gli/commands/scaffold.rb +71 -142
- data/lib/gli/dsl.rb +2 -1
- data/lib/gli/flag.rb +23 -2
- data/lib/gli/gli_option_parser.rb +66 -15
- data/lib/gli/option_parser_factory.rb +9 -2
- data/lib/gli/options.rb +2 -2
- data/lib/gli/switch.rb +4 -0
- data/lib/gli/terminal.rb +6 -2
- data/lib/gli/version.rb +1 -1
- data/lib/gli.rb +1 -0
- 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 +12 -6
- data/test/apps/todo/lib/todo/commands/create.rb +42 -41
- data/test/apps/todo/lib/todo/commands/list.rb +48 -36
- data/test/apps/todo/lib/todo/commands/ls.rb +25 -24
- data/test/apps/todo/lib/todo/commands/make.rb +42 -39
- data/test/apps/todo/todo.gemspec +1 -2
- data/test/apps/todo_legacy/todo.gemspec +1 -2
- data/test/apps/todo_plugins/commands/third.rb +2 -0
- 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/unit/command_finder_test.rb +54 -0
- data/test/{tc_command.rb → unit/command_test.rb} +20 -7
- 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} +78 -50
- data/test/{tc_help.rb → unit/help_test.rb} +54 -113
- 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/{config.yaml → unit/support/gli_test_config.yml} +1 -0
- data/test/unit/switch_test.rb +49 -0
- data/test/{tc_terminal.rb → unit/terminal_test.rb} +28 -3
- data/test/unit/test_helper.rb +13 -0
- data/test/unit/verbatim_wrapper_test.rb +24 -0
- metadata +85 -141
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/.travis.yml +0 -12
- 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 -232
- 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 -55
- data/features/todo.feature +0 -546
- data/features/todo_legacy.feature +0 -128
- data/test/option_test_helper.rb +0 -13
- data/test/tc_compound_command.rb +0 -22
- data/test/tc_subcommand_parsing.rb +0 -104
- 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 -20
- /data/test/{init_simplecov.rb → unit/init_simplecov.rb} +0 -0
- /data/test/{fake_std_out.rb → unit/support/fake_std_out.rb} +0 -0
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
|
-
class
|
3
|
+
class HelpTest < MiniTest::Test
|
4
4
|
include TestHelper
|
5
5
|
|
6
6
|
def setup
|
@@ -24,86 +24,61 @@ class TC_testHelp < Clean::Test::TestCase
|
|
24
24
|
include GLI::App
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
assert @command.skips_pre
|
40
|
-
assert @command.skips_post
|
41
|
-
assert @command.skips_around
|
42
|
-
}
|
27
|
+
def test_help_command_configured_properly_when_created
|
28
|
+
app = TestApp.new
|
29
|
+
app.subcommand_option_handling :normal
|
30
|
+
@command = GLI::Commands::Help.new(app,@output,@error)
|
31
|
+
assert_equal 'help',@command.name.to_s
|
32
|
+
assert_nil @command.aliases
|
33
|
+
assert_equal 'command',@command.arguments_description
|
34
|
+
refute_nil @command.description
|
35
|
+
refute_nil @command.long_description
|
36
|
+
assert @command.skips_pre
|
37
|
+
assert @command.skips_post
|
38
|
+
assert @command.skips_around
|
43
39
|
end
|
44
40
|
|
45
|
-
|
46
|
-
Given {
|
41
|
+
def test_the_help_command_can_be_configured_to_skip_things_declaratively
|
47
42
|
app = TestApp.new
|
48
43
|
app.subcommand_option_handling :normal
|
49
44
|
@command = GLI::Commands::Help.new(app,@output,@error)
|
50
45
|
GLI::Commands::Help.skips_pre = false
|
51
46
|
GLI::Commands::Help.skips_post = false
|
52
47
|
GLI::Commands::Help.skips_around = false
|
53
|
-
}
|
54
|
-
Then {
|
55
48
|
assert !@command.skips_pre
|
56
49
|
assert !@command.skips_post
|
57
50
|
assert !@command.skips_around
|
58
|
-
}
|
59
51
|
end
|
60
52
|
|
61
|
-
|
62
|
-
Given {
|
53
|
+
def test_the_help_command_can_be_configured_to_skip_things_declaratively_regardless_of_when_the_object_was_created
|
63
54
|
GLI::Commands::Help.skips_pre = false
|
64
55
|
GLI::Commands::Help.skips_post = false
|
65
56
|
GLI::Commands::Help.skips_around = false
|
66
57
|
app = TestApp.new
|
67
58
|
app.subcommand_option_handling :normal
|
68
59
|
@command = GLI::Commands::Help.new(app,@output,@error)
|
69
|
-
}
|
70
|
-
Then {
|
71
60
|
assert !@command.skips_pre
|
72
61
|
assert !@command.skips_post
|
73
62
|
assert !@command.skips_around
|
74
|
-
}
|
75
63
|
end
|
76
64
|
|
77
|
-
|
78
|
-
|
79
|
-
And {
|
65
|
+
def test_invoking_help_with_no_arguments_results_in_listing_all_commands_and_global_options
|
66
|
+
setup_GLI_app
|
80
67
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
81
|
-
}
|
82
|
-
When {
|
83
68
|
@command.execute({},{},[])
|
84
|
-
}
|
85
|
-
Then {
|
86
69
|
assert_top_level_help_output
|
87
|
-
}
|
88
70
|
end
|
89
71
|
|
90
|
-
|
91
|
-
|
92
|
-
And {
|
72
|
+
def test_invoking_help_with_a_command_that_doesnt_exist_shows_an_error
|
73
|
+
setup_GLI_app
|
93
74
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
94
75
|
@unknown_command_name = any_command_name
|
95
|
-
}
|
96
|
-
When {
|
97
76
|
@command.execute({},{},[@unknown_command_name])
|
98
|
-
}
|
99
|
-
Then {
|
100
77
|
assert_error_contained(/error: Unknown command '#{@unknown_command_name}'./)
|
101
|
-
}
|
102
78
|
end
|
103
79
|
|
104
|
-
|
105
|
-
|
106
|
-
And {
|
80
|
+
def test_invoking_help_with_a_known_command_shows_help_for_that_command
|
81
|
+
setup_GLI_app
|
107
82
|
@command_name = cm = any_command_name
|
108
83
|
@desc = d = any_desc
|
109
84
|
@long_desc = ld = any_desc
|
@@ -127,11 +102,7 @@ class TC_testHelp < Clean::Test::TestCase
|
|
127
102
|
end
|
128
103
|
end
|
129
104
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
130
|
-
}
|
131
|
-
When {
|
132
105
|
@command.execute({},{},[@command_name])
|
133
|
-
}
|
134
|
-
Then {
|
135
106
|
assert_output_contained(@command_name,"Name of the command")
|
136
107
|
assert_output_contained(@desc,"Short description")
|
137
108
|
assert_output_contained(@long_desc,"Long description")
|
@@ -139,27 +110,19 @@ class TC_testHelp < Clean::Test::TestCase
|
|
139
110
|
assert_output_contained(@switch_desc,"switch description")
|
140
111
|
assert_output_contained("-" + @flag,"command flag")
|
141
112
|
assert_output_contained(@flag_desc,"flag description")
|
142
|
-
}
|
143
113
|
end
|
144
114
|
|
145
|
-
|
146
|
-
|
147
|
-
And {
|
115
|
+
def test_invoking_help_with_no_global_options_omits_the_global_options_placeholder_from_usage
|
116
|
+
setup_GLI_app(:no_options)
|
148
117
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
149
|
-
}
|
150
|
-
When {
|
151
118
|
@command.execute({},{},[])
|
152
|
-
}
|
153
|
-
Then {
|
154
119
|
refute_output_contained(/\[global options\] command \[command options\] \[arguments\.\.\.\]/)
|
155
120
|
refute_output_contained('GLOBAL OPTIONS')
|
156
121
|
assert_output_contained(/command \[command options\] \[arguments\.\.\.\]/)
|
157
|
-
}
|
158
122
|
end
|
159
123
|
|
160
|
-
|
161
|
-
|
162
|
-
And {
|
124
|
+
def test_invoking_help_with_a_known_command_when_no_global_options_are_present_omits_placeholder_from_the_usage_string
|
125
|
+
setup_GLI_app(:no_options)
|
163
126
|
@command_name = cm = any_command_name
|
164
127
|
@desc = d = any_desc
|
165
128
|
@long_desc = ld = any_desc
|
@@ -183,20 +146,14 @@ class TC_testHelp < Clean::Test::TestCase
|
|
183
146
|
end
|
184
147
|
end
|
185
148
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
186
|
-
}
|
187
|
-
When {
|
188
149
|
@command.execute({},{},[@command_name])
|
189
|
-
}
|
190
|
-
Then {
|
191
150
|
refute_output_contained(/\[global options\]/)
|
192
151
|
assert_output_contained(/\[command options\]/)
|
193
152
|
assert_output_contained('COMMAND OPTIONS')
|
194
|
-
}
|
195
153
|
end
|
196
154
|
|
197
|
-
|
198
|
-
|
199
|
-
And {
|
155
|
+
def test_omits_both_placeholders_when_no_options_present
|
156
|
+
setup_GLI_app(:no_options)
|
200
157
|
@command_name = cm = any_command_name
|
201
158
|
@desc = d = any_desc
|
202
159
|
@long_desc = ld = any_desc
|
@@ -209,20 +166,14 @@ class TC_testHelp < Clean::Test::TestCase
|
|
209
166
|
end
|
210
167
|
end
|
211
168
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
212
|
-
}
|
213
|
-
When {
|
214
169
|
@command.execute({},{},[@command_name])
|
215
|
-
}
|
216
|
-
Then {
|
217
170
|
refute_output_contained(/\[global options\]/)
|
218
171
|
refute_output_contained(/\[command options\]/)
|
219
172
|
refute_output_contained('COMMAND OPTIONS')
|
220
|
-
}
|
221
173
|
end
|
222
174
|
|
223
|
-
|
224
|
-
|
225
|
-
And {
|
175
|
+
def test_no_command_options_omits_command_options_placeholder
|
176
|
+
setup_GLI_app
|
226
177
|
@command_name = cm = any_command_name
|
227
178
|
@desc = d = any_desc
|
228
179
|
@long_desc = ld = any_desc
|
@@ -235,19 +186,13 @@ class TC_testHelp < Clean::Test::TestCase
|
|
235
186
|
end
|
236
187
|
end
|
237
188
|
@command = GLI::Commands::Help.new(@app,@output,@error)
|
238
|
-
}
|
239
|
-
When {
|
240
189
|
@command.execute({},{},[@command_name])
|
241
|
-
}
|
242
|
-
Then {
|
243
190
|
assert_output_contained(/\[global options\]/)
|
244
191
|
refute_output_contained(/\[command options\]/)
|
245
192
|
refute_output_contained('COMMAND OPTIONS')
|
246
|
-
}
|
247
193
|
end
|
248
194
|
|
249
|
-
|
250
|
-
Given {
|
195
|
+
def test_omitting_default_description_doesnt_blow_up
|
251
196
|
app = TestApp.new
|
252
197
|
app.instance_eval do
|
253
198
|
subcommand_option_handling :normal
|
@@ -266,23 +211,20 @@ class TC_testHelp < Clean::Test::TestCase
|
|
266
211
|
end
|
267
212
|
end
|
268
213
|
@command = GLI::Commands::Help.new(app,@output,@error)
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
assert_nothing_raised(&@code)
|
275
|
-
}
|
214
|
+
begin
|
215
|
+
@command.execute({},{},['top'])
|
216
|
+
rescue => ex
|
217
|
+
assert false, "Expected no exception, got: #{ex.message}"
|
218
|
+
end
|
276
219
|
end
|
277
220
|
|
278
221
|
private
|
279
222
|
|
280
|
-
def
|
281
|
-
lambda {
|
223
|
+
def setup_GLI_app(omit_options=false)
|
282
224
|
@program_description = program_description = any_desc
|
283
225
|
@flags = flags = [
|
284
|
-
[any_desc.strip,
|
285
|
-
[any_desc.strip,
|
226
|
+
[any_desc.strip,:foo,[any_option]],
|
227
|
+
[any_desc.strip,:bar,[any_option,any_long_option]],
|
286
228
|
]
|
287
229
|
@switches = switches = [
|
288
230
|
[any_desc.strip,[any_option]],
|
@@ -302,24 +244,23 @@ private
|
|
302
244
|
unless omit_options
|
303
245
|
flags.each do |(description,arg,flag_names)|
|
304
246
|
desc description
|
305
|
-
|
306
|
-
|
247
|
+
arg_name arg
|
248
|
+
flag flag_names
|
307
249
|
end
|
308
250
|
|
309
251
|
switches.each do |(description,switch_names)|
|
310
252
|
desc description
|
311
|
-
|
253
|
+
switch switch_names
|
312
254
|
end
|
313
255
|
end
|
314
256
|
|
315
257
|
commands.each do |(description,command_names)|
|
316
258
|
desc description
|
317
|
-
command command_names do |c|
|
318
|
-
c.action {}
|
259
|
+
command command_names do |c|
|
260
|
+
c.action {}
|
319
261
|
end
|
320
262
|
end
|
321
263
|
end
|
322
|
-
}
|
323
264
|
end
|
324
265
|
|
325
266
|
def assert_top_level_help_output
|
@@ -357,29 +298,29 @@ private
|
|
357
298
|
|
358
299
|
def refute_output_contained(string_or_regexp,desc='')
|
359
300
|
string_or_regexp = /#{string_or_regexp}/ if string_or_regexp.kind_of?(String)
|
360
|
-
|
301
|
+
refute_match string_or_regexp,@output.string,desc
|
361
302
|
end
|
362
303
|
|
363
304
|
def any_option
|
364
305
|
('a'..'z').to_a[@option_index].tap { @option_index += 1 }
|
365
306
|
end
|
366
|
-
|
367
|
-
def any_long_option
|
368
|
-
Faker::Lorem.words(10)[rand(10)]
|
369
|
-
end
|
370
307
|
|
371
|
-
def
|
372
|
-
|
308
|
+
def any_long_option
|
309
|
+
["foo","bar","blah"].sample
|
373
310
|
end
|
374
311
|
|
375
312
|
def any_desc
|
376
|
-
|
313
|
+
[
|
314
|
+
"This command does some stuff",
|
315
|
+
"Do things and whatnot",
|
316
|
+
"Behold the power of this command"
|
317
|
+
].sample
|
377
318
|
end
|
378
319
|
|
379
320
|
def any_command_name
|
380
|
-
command_name =
|
321
|
+
command_name = ["new","edit","delete","create","update"].sample
|
381
322
|
while @command_names_used.include?(command_name)
|
382
|
-
command_name =
|
323
|
+
command_name = ["new","edit","delete","create","update"].sample
|
383
324
|
end
|
384
325
|
@command_names_used << command_name
|
385
326
|
command_name
|
@@ -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
|