gli 2.19.1 → 2.21.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/CONTRIBUTING.md +1 -3
- data/Gemfile +0 -6
- data/README.rdoc +7 -17
- data/Rakefile +22 -38
- 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/app_support.rb +3 -1
- data/lib/gli/command_support.rb +2 -6
- 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/dsl.rb +5 -0
- data/lib/gli/gli_option_parser.rb +19 -4
- 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} +58 -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 +73 -140
- 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
data/test/option_test_helper.rb
DELETED
data/test/tc_compound_command.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'tempfile'
|
3
|
-
|
4
|
-
class TC_testCompounCommand < Clean::Test::TestCase
|
5
|
-
include TestHelper
|
6
|
-
|
7
|
-
test_that "when we create a CompoundCommand where some commands are missing, we get an exception" do
|
8
|
-
Given {
|
9
|
-
@name = any_string
|
10
|
-
@unknown_name = any_string
|
11
|
-
@existing_command = OpenStruct.new(:name => @name)
|
12
|
-
@base = OpenStruct.new( :commands => { @name => @existing_command })
|
13
|
-
}
|
14
|
-
When {
|
15
|
-
@code = lambda { GLI::Commands::CompoundCommand.new(@base,{:foo => [@name,@unknown_name]}) }
|
16
|
-
}
|
17
|
-
Then {
|
18
|
-
ex = assert_raises(RuntimeError,&@code)
|
19
|
-
assert_match /#{@unknown_name}/,ex.message
|
20
|
-
}
|
21
|
-
end
|
22
|
-
end
|
@@ -1,280 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'pp'
|
3
|
-
|
4
|
-
class TC_testSubCommandParsing < Clean::Test::TestCase
|
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
|
-
test_that "commands options may clash with globals and it gets sorted out" do
|
32
|
-
Given :app_with_subcommands_storing_results
|
33
|
-
When {
|
34
|
-
@app.run(%w(-f global command1 -f command -s foo))
|
35
|
-
}
|
36
|
-
Then {
|
37
|
-
assert_equal 'command1',@results[:command_name]
|
38
|
-
assert_equal 'global', @results[:global_options][:f],'global'
|
39
|
-
assert !@results[:global_options][:s]
|
40
|
-
assert_equal 'command', @results[:command_options][:f]
|
41
|
-
assert @results[:command_options][:s]
|
42
|
-
}
|
43
|
-
end
|
44
|
-
|
45
|
-
test_that "in legacy mode, subcommand options all share a namespace" do
|
46
|
-
Given :app_with_subcommands_storing_results
|
47
|
-
When {
|
48
|
-
@app.run(%w(-f global command1 -f command -s subcommand10 -f sub))
|
49
|
-
}
|
50
|
-
Then {
|
51
|
-
with_clue {
|
52
|
-
assert_equal 'subcommand10',@results[:command_name]
|
53
|
-
assert_equal 'global', @results[:global_options][:f],'global'
|
54
|
-
assert !@results[:global_options][:s]
|
55
|
-
assert_equal 'sub', @results[:command_options][:f]
|
56
|
-
assert @results[:command_options][:s]
|
57
|
-
assert_nil @results[:command_options][GLI::Command::PARENT]
|
58
|
-
assert_nil @results[:command_options][GLI::Command::PARENT]
|
59
|
-
}
|
60
|
-
}
|
61
|
-
end
|
62
|
-
|
63
|
-
test_that "in normal mode, each subcommand has its own namespace" do
|
64
|
-
Given :app_with_subcommands_storing_results, :normal
|
65
|
-
When {
|
66
|
-
@app.run(%w(-f global command1 -f command -s subcommand10 -f sub))
|
67
|
-
}
|
68
|
-
Then {
|
69
|
-
with_clue {
|
70
|
-
assert_equal 'subcommand10',@results[:command_name]
|
71
|
-
assert_equal 'global', @results[:global_options][:f],'global'
|
72
|
-
assert !@results[:global_options][:s]
|
73
|
-
assert_equal 'sub', @results[:command_options][:f]
|
74
|
-
assert !@results[:command_options][:s]
|
75
|
-
assert_equal 'command',@results[:command_options][GLI::Command::PARENT][:f]
|
76
|
-
assert @results[:command_options][GLI::Command::PARENT][:s]
|
77
|
-
}
|
78
|
-
}
|
79
|
-
end
|
80
|
-
|
81
|
-
test_that "in loose mode with autocomplete false, it doesn't autocorrect a sub command" do
|
82
|
-
Given :app_with_subcommand_storing_results, :normal, false, :loose
|
83
|
-
When {
|
84
|
-
@app.run(%w(-f global command -f flag -s subcomm -f subflag))
|
85
|
-
}
|
86
|
-
Then {
|
87
|
-
with_clue {
|
88
|
-
assert_equal "command",@results[:command_name]
|
89
|
-
}
|
90
|
-
}
|
91
|
-
end
|
92
|
-
|
93
|
-
test_that "in strict mode with autocomplete false, it doesn't autocorrect a sub command" do
|
94
|
-
Given :app_with_subcommand_storing_results, :normal, false, :strict
|
95
|
-
When {
|
96
|
-
@app.run(%w(-f global command -f flag -s subcomm -f subflag))
|
97
|
-
}
|
98
|
-
Then {
|
99
|
-
with_clue {
|
100
|
-
assert_equal nil,@results[:command_name]
|
101
|
-
assert @fake_stderr.contained?(/error: Too many arguments for command/)
|
102
|
-
}
|
103
|
-
}
|
104
|
-
end
|
105
|
-
|
106
|
-
test_that "in loose mode, argument validation is ignored" do
|
107
|
-
Given :app_with_arguments, 1, 1, false, :loose
|
108
|
-
When :run_app_with_X_arguments, 0
|
109
|
-
Then {
|
110
|
-
with_clue {
|
111
|
-
assert_equal 0, @results[:number_of_args_give_to_action]
|
112
|
-
assert_equal 0, @exit_code
|
113
|
-
}
|
114
|
-
}
|
115
|
-
end
|
116
|
-
|
117
|
-
test_that "in strict mode, subcommand_option_handling must be normal" do
|
118
|
-
Given :app_with_arguments, 1, 1, false, :strict, :legacy
|
119
|
-
When :run_app_with_X_arguments, 1
|
120
|
-
Then {
|
121
|
-
with_clue {
|
122
|
-
assert_nil @results[:number_of_args_give_to_action]
|
123
|
-
assert_equal 1, @exit_code
|
124
|
-
assert @fake_stderr.contained?(/you must enable normal subcommand_option_handling/)
|
125
|
-
}
|
126
|
-
}
|
127
|
-
end
|
128
|
-
|
129
|
-
ix = -1
|
130
|
-
[
|
131
|
-
[1 , 1 , false , 0 , :not_enough] ,
|
132
|
-
[1 , 1 , false , 1 , :success] ,
|
133
|
-
[1 , 1 , false , 2 , :success] ,
|
134
|
-
[1 , 1 , false , 3 , :too_many] ,
|
135
|
-
[1 , 1 , true , 0 , :not_enough] ,
|
136
|
-
[1 , 1 , true , 1 , :success] ,
|
137
|
-
[1 , 1 , true , 2 , :success] ,
|
138
|
-
[1 , 1 , true , 3 , :success] ,
|
139
|
-
[1 , 1 , true , 30 , :success] ,
|
140
|
-
[0 , 0 , false , 0 , :success] ,
|
141
|
-
[0 , 0 , false , 1 , :too_many] ,
|
142
|
-
[0 , 1 , false , 1 , :success] ,
|
143
|
-
[0 , 1 , false , 0 , :success] ,
|
144
|
-
[1 , 0 , false , 1 , :success] ,
|
145
|
-
[1 , 0 , false , 0 , :not_enough] ,
|
146
|
-
[0 , 0 , true , 0 , :success] ,
|
147
|
-
[0 , 0 , true , 10 , :success]
|
148
|
-
|
149
|
-
].each do |number_required, number_optional, has_multiple, number_generated, status|
|
150
|
-
ix = ix + 1
|
151
|
-
test_that "in strict mode, with #{number_required} required, #{number_optional} optional, #{ has_multiple ? 'multiple' : 'not multiple' } and #{number_generated} generated, it should be #{status}" do
|
152
|
-
Given :app_with_arguments, number_required, number_optional, has_multiple, :strict
|
153
|
-
When :run_app_with_X_arguments, number_generated
|
154
|
-
Then {
|
155
|
-
with_clue {
|
156
|
-
if status == :success then
|
157
|
-
assert_equal number_generated, @results[:number_of_args_give_to_action]
|
158
|
-
assert_equal 0, @exit_code
|
159
|
-
assert !@fake_stderr.contained?(/Not enough arguments for command/)
|
160
|
-
assert !@fake_stderr.contained?(/Too many arguments for command/)
|
161
|
-
elsif status == :not_enough then
|
162
|
-
assert_equal nil, @results[:number_of_args_give_to_action]
|
163
|
-
assert_equal 64, @exit_code
|
164
|
-
assert @fake_stderr.contained?(/Not enough arguments for command/)
|
165
|
-
elsif status == :too_many then
|
166
|
-
assert_equal nil, @results[:number_of_args_give_to_action]
|
167
|
-
assert_equal 64, @exit_code
|
168
|
-
assert @fake_stderr.contained?(/Too many arguments for command/)
|
169
|
-
else
|
170
|
-
assert false
|
171
|
-
end
|
172
|
-
}
|
173
|
-
}
|
174
|
-
end
|
175
|
-
end
|
176
|
-
private
|
177
|
-
def with_clue(&block)
|
178
|
-
block.call
|
179
|
-
rescue Exception
|
180
|
-
dump = ""
|
181
|
-
PP.pp "\nRESULTS---#{@results}", dump unless @results.empty?
|
182
|
-
PP.pp "\nSTDERR---\n#{@fake_stderr.to_s}", dump
|
183
|
-
PP.pp "\nSTDOUT---\n#{@fake_stdout.to_s}", dump
|
184
|
-
@original_stdout.puts dump
|
185
|
-
raise
|
186
|
-
end
|
187
|
-
|
188
|
-
def app_with_subcommand_storing_results(subcommand_option_handling_strategy, autocomplete, arguments_handling_strategy)
|
189
|
-
@app.subcommand_option_handling subcommand_option_handling_strategy
|
190
|
-
@app.autocomplete_commands autocomplete
|
191
|
-
@app.arguments arguments_handling_strategy
|
192
|
-
@app.flag ['f','flag']
|
193
|
-
@app.switch ['s','switch']
|
194
|
-
|
195
|
-
@app.command "command" do |c|
|
196
|
-
c.flag ['f','flag']
|
197
|
-
c.switch ['s','switch']
|
198
|
-
c.action do |global,options,args|
|
199
|
-
@results = {
|
200
|
-
:command_name => "command",
|
201
|
-
:global_options => global,
|
202
|
-
:command_options => options,
|
203
|
-
:args => args
|
204
|
-
}
|
205
|
-
end
|
206
|
-
|
207
|
-
c.command "subcommand" do |subcommand|
|
208
|
-
subcommand.flag ['f','flag']
|
209
|
-
subcommand.flag ['foo']
|
210
|
-
subcommand.switch ['s','switch']
|
211
|
-
subcommand.action do |global,options,args|
|
212
|
-
@results = {
|
213
|
-
:command_name => "subcommand",
|
214
|
-
:global_options => global,
|
215
|
-
:command_options => options,
|
216
|
-
:args => args
|
217
|
-
}
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
def app_with_subcommands_storing_results(subcommand_option_handling_strategy = :legacy)
|
224
|
-
@app.subcommand_option_handling subcommand_option_handling_strategy
|
225
|
-
@app.flag ['f','flag']
|
226
|
-
@app.switch ['s','switch']
|
227
|
-
|
228
|
-
2.times do |i|
|
229
|
-
@app.command "command#{i}" do |c|
|
230
|
-
c.flag ['f','flag']
|
231
|
-
c.switch ['s','switch']
|
232
|
-
c.action do |global,options,args|
|
233
|
-
@results = {
|
234
|
-
:command_name => "command#{i}",
|
235
|
-
:global_options => global,
|
236
|
-
:command_options => options,
|
237
|
-
:args => args
|
238
|
-
}
|
239
|
-
end
|
240
|
-
|
241
|
-
2.times do |j|
|
242
|
-
c.command "subcommand#{i}#{j}" do |subcommand|
|
243
|
-
subcommand.flag ['f','flag']
|
244
|
-
subcommand.flag ['foo']
|
245
|
-
subcommand.switch ['s','switch']
|
246
|
-
subcommand.action do |global,options,args|
|
247
|
-
@results = {
|
248
|
-
:command_name => "subcommand#{i}#{j}",
|
249
|
-
:global_options => global,
|
250
|
-
:command_options => options,
|
251
|
-
:args => args
|
252
|
-
}
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
def app_with_arguments(number_required_arguments, number_optional_arguments, has_argument_multiple, arguments_handling_strategy = :loose, subcommand_option_handling_strategy = :normal)
|
261
|
-
@app.arguments arguments_handling_strategy
|
262
|
-
@app.subcommand_option_handling subcommand_option_handling_strategy
|
263
|
-
|
264
|
-
number_required_arguments.times { |i| @app.arg("needed#{i}") }
|
265
|
-
number_optional_arguments.times { |i| @app.arg("optional#{i}", :optional) }
|
266
|
-
@app.arg :multiple, [:multiple, :optional] if has_argument_multiple
|
267
|
-
|
268
|
-
@app.command :cmd do |c|
|
269
|
-
c.action do |g,o,a|
|
270
|
-
@results = {
|
271
|
-
:number_of_args_give_to_action => a.size
|
272
|
-
}
|
273
|
-
end
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
def run_app_with_X_arguments(number_arguments)
|
278
|
-
@exit_code = @app.run [].tap{|args| args << "cmd"; number_arguments.times {|i| args << "arg#{i}"}}
|
279
|
-
end
|
280
|
-
end
|
data/test/tc_subcommands.rb
DELETED
@@ -1,259 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TC_testSubCommand < Clean::Test::TestCase
|
4
|
-
include TestHelper
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@fake_stdout = FakeStdOut.new
|
8
|
-
@fake_stderr = FakeStdOut.new
|
9
|
-
|
10
|
-
@original_stdout = $stdout
|
11
|
-
$stdout = @fake_stdout
|
12
|
-
@original_stderr = $stderr
|
13
|
-
$stderr = @fake_stderr
|
14
|
-
|
15
|
-
@app = CLIApp.new
|
16
|
-
@app.reset
|
17
|
-
@app.subcommand_option_handling :legacy
|
18
|
-
@app.error_device=@fake_stderr
|
19
|
-
ENV.delete('GLI_DEBUG')
|
20
|
-
end
|
21
|
-
|
22
|
-
def teardown
|
23
|
-
$stdout = @original_stdout
|
24
|
-
$stderr = @original_stderr
|
25
|
-
end
|
26
|
-
|
27
|
-
['add','new'].each do |name|
|
28
|
-
test_that "We run the 'add' subcommand using '#{name}'" do
|
29
|
-
Given we_have_a_command_with_two_subcommands
|
30
|
-
When run_app('remote',name,'-f','foo','bar')
|
31
|
-
Then assert_command_ran_with(:add, :command_options => {:f => true}, :args => %w(foo bar))
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
test_that "with subcommands, but not using one on the command-line, we run the base action" do
|
36
|
-
Given we_have_a_command_with_two_subcommands
|
37
|
-
When run_app('remote','foo','bar')
|
38
|
-
Then assert_command_ran_with(:base, :command_options => {:f => false}, :args => %w(foo bar))
|
39
|
-
end
|
40
|
-
|
41
|
-
test_that "switches and flags defined on a subcommand are available" do
|
42
|
-
Given we_have_a_command_with_two_subcommands(:switches => [:addswitch], :flags => [:addflag])
|
43
|
-
When run_app('remote','add','--addswitch','--addflag','foo','bar')
|
44
|
-
Then assert_command_ran_with(:add,:command_options => { :addswitch => true, :addflag => 'foo', :f => false },
|
45
|
-
:args => ['bar'])
|
46
|
-
end
|
47
|
-
|
48
|
-
test_that "--help works for subcommands in :normal handling mode" do
|
49
|
-
Given { @app.subcommand_option_handling :normal }
|
50
|
-
And we_have_a_command_with_two_subcommands
|
51
|
-
When { @app.run(["remote", "add", "--help"]) rescue nil }
|
52
|
-
Then { assert_no_match /^error/, @fake_stderr.to_s, "should not output an error message" }
|
53
|
-
end
|
54
|
-
|
55
|
-
test_that "--help works for subcommands in :legacy handling mode" do
|
56
|
-
Given { @app.subcommand_option_handling :legacy }
|
57
|
-
And we_have_a_command_with_two_subcommands
|
58
|
-
When { @app.run(["remote", "add", "--help"]) rescue nil }
|
59
|
-
Then { assert_no_match /^error/, @fake_stderr.to_s, "should not output an error message" }
|
60
|
-
end
|
61
|
-
|
62
|
-
test_that "we can reopen commands to add new subcommands" do
|
63
|
-
Given {
|
64
|
-
@app.command :remote do |p|
|
65
|
-
p.command :add do |c|
|
66
|
-
c.action do |global_options,command_options,args|
|
67
|
-
@ran_command = :add
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
@app.command :remote do |p|
|
72
|
-
p.command :new do |c|
|
73
|
-
c.action do |global_options,command_options,args|
|
74
|
-
@ran_command = :new
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
}
|
79
|
-
When run_app('remote','new')
|
80
|
-
Then { assert_equal(@ran_command, :new) }
|
81
|
-
When run_app('remote', 'add')
|
82
|
-
Then { assert_equal(@ran_command, :add) }
|
83
|
-
end
|
84
|
-
|
85
|
-
test_that "reopening commands doesn't re-add them to the output" do
|
86
|
-
Given {
|
87
|
-
@app.command :remote do |p|
|
88
|
-
p.command(:add) { }
|
89
|
-
end
|
90
|
-
@app.command :remote do |p|
|
91
|
-
p.command(:new) { }
|
92
|
-
end
|
93
|
-
}
|
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
|
-
test_that "we can reopen commands doesn't cause conflicts" do
|
100
|
-
Given {
|
101
|
-
@app.command :remote do |p|
|
102
|
-
p.command :add do |c|
|
103
|
-
c.action do |global_options,command_options,args|
|
104
|
-
@ran_command = :remote_add
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
@app.command :local do |p|
|
109
|
-
p.command :add do |c|
|
110
|
-
c.action do |global_options,command_options,args|
|
111
|
-
@ran_command = :local_add
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
}
|
116
|
-
When run_app('remote','add')
|
117
|
-
Then { assert_equal(@ran_command, :remote_add) }
|
118
|
-
When run_app('local', 'add')
|
119
|
-
Then { assert_equal(@ran_command, :local_add) }
|
120
|
-
end
|
121
|
-
|
122
|
-
|
123
|
-
test_that "we can nest subcommands very deep" do
|
124
|
-
Given {
|
125
|
-
@run_results = { :add => nil, :rename => nil, :base => nil }
|
126
|
-
@app.command :remote do |c|
|
127
|
-
|
128
|
-
c.switch :f
|
129
|
-
c.command :add do |add|
|
130
|
-
add.command :some do |some|
|
131
|
-
some.command :cmd do |cmd|
|
132
|
-
cmd.switch :s
|
133
|
-
cmd.action do |global_options,command_options,args|
|
134
|
-
@run_results[:cmd] = [global_options,command_options,args]
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
ENV['GLI_DEBUG'] = 'true'
|
141
|
-
}
|
142
|
-
When run_app('remote','add','some','cmd','-s','blah')
|
143
|
-
Then assert_command_ran_with(:cmd, :command_options => {:s => true, :f => false},:args => ['blah'])
|
144
|
-
end
|
145
|
-
|
146
|
-
test_that "when any command in the chain has no action, but there's still arguments, indicate we have an unknown command" do
|
147
|
-
Given a_very_deeply_nested_command_structure
|
148
|
-
Then {
|
149
|
-
assert_raises GLI::UnknownCommand do
|
150
|
-
When run_app('remote','add','some','foo')
|
151
|
-
end
|
152
|
-
assert_match /Unknown command 'foo'/,@fake_stderr.to_s
|
153
|
-
}
|
154
|
-
end
|
155
|
-
|
156
|
-
test_that "when a command in the chain has no action, but there's NO additional arguments, indicate we need a subcommand" do
|
157
|
-
Given a_very_deeply_nested_command_structure
|
158
|
-
Then {
|
159
|
-
assert_raises GLI::BadCommandLine do
|
160
|
-
When run_app('remote','add','some')
|
161
|
-
end
|
162
|
-
assert_match /Command 'some' requires a subcommand/,@fake_stderr.to_s
|
163
|
-
}
|
164
|
-
end
|
165
|
-
|
166
|
-
private
|
167
|
-
|
168
|
-
def run_app(*args)
|
169
|
-
lambda { @exit_code = @app.run(args) }
|
170
|
-
end
|
171
|
-
|
172
|
-
def a_very_deeply_nested_command_structure
|
173
|
-
lambda {
|
174
|
-
@run_results = { :add => nil, :rename => nil, :base => nil }
|
175
|
-
@app.command :remote do |c|
|
176
|
-
|
177
|
-
c.switch :f
|
178
|
-
c.command :add do |add|
|
179
|
-
add.command :some do |some|
|
180
|
-
some.command :cmd do |cmd|
|
181
|
-
cmd.switch :s
|
182
|
-
cmd.action do |global_options,command_options,args|
|
183
|
-
@run_results[:cmd] = [global_options,command_options,args]
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
ENV['GLI_DEBUG'] = 'true'
|
190
|
-
}
|
191
|
-
end
|
192
|
-
|
193
|
-
# expected_command - name of command exepcted to have been run
|
194
|
-
# options:
|
195
|
-
# - global_options => hash of expected options
|
196
|
-
# - command_options => hash of expected command options
|
197
|
-
# - args => array of expected args
|
198
|
-
def assert_command_ran_with(expected_command,options)
|
199
|
-
lambda {
|
200
|
-
global_options = options[:global_options] || { :help => false }
|
201
|
-
@run_results.each do |command,results|
|
202
|
-
if command == expected_command
|
203
|
-
assert_equal(indifferent_hash(global_options),results[0])
|
204
|
-
assert_equal(indifferent_hash(options[:command_options]),results[1])
|
205
|
-
assert_equal(options[:args],results[2])
|
206
|
-
else
|
207
|
-
assert_nil results
|
208
|
-
end
|
209
|
-
end
|
210
|
-
}
|
211
|
-
end
|
212
|
-
|
213
|
-
def indifferent_hash(possibly_nil_hash)
|
214
|
-
return {} if possibly_nil_hash.nil?
|
215
|
-
possibly_nil_hash.keys.each do |key|
|
216
|
-
if key.kind_of? Symbol
|
217
|
-
possibly_nil_hash[key.to_s] = possibly_nil_hash[key] unless possibly_nil_hash.has_key?(key.to_s)
|
218
|
-
elsif key.kind_of? String
|
219
|
-
possibly_nil_hash[key.to_sym] = possibly_nil_hash[key] unless possibly_nil_hash.has_key?(key.to_sym)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
possibly_nil_hash
|
223
|
-
end
|
224
|
-
|
225
|
-
# options -
|
226
|
-
# :flags => flags to add to :add
|
227
|
-
# :switiches => switiches to add to :add
|
228
|
-
def we_have_a_command_with_two_subcommands(options = {})
|
229
|
-
lambda {
|
230
|
-
@run_results = { :add => nil, :rename => nil, :base => nil }
|
231
|
-
@app.command :remote do |c|
|
232
|
-
|
233
|
-
c.switch :f
|
234
|
-
|
235
|
-
c.desc "add a remote"
|
236
|
-
c.command [:add,:new] do |add|
|
237
|
-
|
238
|
-
Array(options[:flags]).each { |_| add.flag _ }
|
239
|
-
Array(options[:switches]).each { |_| add.switch _ }
|
240
|
-
add.action do |global_options,command_options,args|
|
241
|
-
@run_results[:add] = [global_options,command_options,args]
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
c.desc "rename a remote"
|
246
|
-
c.command :rename do |rename|
|
247
|
-
rename.action do |global_options,command_options,args|
|
248
|
-
@run_results[:rename] = [global_options,command_options,args]
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
c.action do |global_options,command_options,args|
|
253
|
-
@run_results[:base] = [global_options,command_options,args]
|
254
|
-
end
|
255
|
-
end
|
256
|
-
ENV['GLI_DEBUG'] = 'true'
|
257
|
-
}
|
258
|
-
end
|
259
|
-
end
|
data/test/tc_switch.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TC_testSwitch < Clean::Test::TestCase
|
4
|
-
include TestHelper
|
5
|
-
|
6
|
-
def test_basics_simple
|
7
|
-
Given switch_with_names(:filename)
|
8
|
-
Then attributes_should_be_set
|
9
|
-
And name_should_be(:filename)
|
10
|
-
And aliases_should_be(nil)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_basics_kinda_complex
|
14
|
-
Given switch_with_names([:f])
|
15
|
-
Then attributes_should_be_set
|
16
|
-
And name_should_be(:f)
|
17
|
-
And aliases_should_be(nil)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_basics_complex
|
21
|
-
Given switch_with_names([:f,:file,:filename])
|
22
|
-
Then attributes_should_be_set
|
23
|
-
And name_should_be(:f)
|
24
|
-
And aliases_should_be([:file,:filename])
|
25
|
-
And {
|
26
|
-
assert_equal ["-f","--[no-]file","--[no-]filename"],@switch.arguments_for_option_parser
|
27
|
-
}
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_includes_negatable
|
31
|
-
assert_equal '-a',GLI::Switch.name_as_string('a')
|
32
|
-
assert_equal '--[no-]foo',GLI::Switch.name_as_string('foo')
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def switch_with_names(names)
|
38
|
-
lambda do
|
39
|
-
@options = {
|
40
|
-
:desc => 'Filename',
|
41
|
-
:long_desc => 'The Filename',
|
42
|
-
}
|
43
|
-
@switch = GLI::Switch.new(names,@options)
|
44
|
-
@cli_option = @switch
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def attributes_should_be_set
|
49
|
-
lambda {
|
50
|
-
assert_equal(@options[:desc],@switch.description)
|
51
|
-
assert_equal(@options[:long_desc],@switch.long_description)
|
52
|
-
}
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
data/test/tc_verbatim_wrapper.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TC_testVerbatimWrapper < Clean::Test::TestCase
|
4
|
-
include TestHelper
|
5
|
-
|
6
|
-
test_that "verbatim wrapper handles nil" do
|
7
|
-
Given {
|
8
|
-
@wrapper = GLI::Commands::HelpModules::VerbatimWrapper.new(any_int,any_int)
|
9
|
-
}
|
10
|
-
When {
|
11
|
-
@result = @wrapper.wrap(nil)
|
12
|
-
}
|
13
|
-
Then {
|
14
|
-
assert_equal '',@result
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
test_that "verbatim wrapper doesn't touch the input" do
|
19
|
-
Given {
|
20
|
-
@wrapper = GLI::Commands::HelpModules::VerbatimWrapper.new(any_int,any_int)
|
21
|
-
@input = <<EOS
|
22
|
-
|This is|an ASCII|table|
|
23
|
-
+-------+--------+-----+
|
24
|
-
| foo | bar | baz |
|
25
|
-
+-------+--------+-----+
|
26
|
-
EOS
|
27
|
-
}
|
28
|
-
When {
|
29
|
-
@result = @wrapper.wrap(@input)
|
30
|
-
}
|
31
|
-
Then {
|
32
|
-
assert_equal @input,@result
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'gli.rb'
|
3
|
-
require 'test/unit'
|
4
|
-
require 'clean_test/test_case'
|
5
|
-
require 'fake_std_out'
|
6
|
-
require 'option_test_helper'
|
7
|
-
|
8
|
-
module TestHelper
|
9
|
-
include OptionTestHelper
|
10
|
-
class CLIApp
|
11
|
-
include GLI::App
|
12
|
-
|
13
|
-
def reset
|
14
|
-
super
|
15
|
-
@subcommand_option_handling_strategy = :normal
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
Faker::Config.locale = :en
|
21
|
-
I18n.enforce_available_locales = false
|