/
runner_test.rb
277 lines (222 loc) · 5.97 KB
/
runner_test.rb
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require File.dirname(__FILE__) + '/test_helper'
require 'shellwords'
# hack required to re-add default_commands_runner methods
$".delete_if {|e| e[%r{boson/runner.rb$}] }
Boson.send(:remove_const, :Runner)
Boson.send(:remove_const, :DefaultCommandsRunner)
require 'boson/runner'
# remove side effects from other tests
Boson::Runner::GLOBAL_OPTIONS.delete_if {|k,v| k != :help }
class MyRunner < Boson::Runner
desc "This is a small"
def small(*args)
p args
end
option :tags, :type => :array
option :blurg, :type => :boolean
desc 'This is splot'
def splot(*args)
p args
end
option :spicy, type: :boolean, desc: 'hot'
desc "This is a medium"
def medium(arg=nil, opts={})
p [arg, opts]
end
desc "This is a mini"
def mini(me)
end
def quiet
end
def boom
nil.boom
end
def broken
raise ArgumentError
end
def test
puts "TEST"
end
private
def no_run
end
end
class ExtendedRunner < Boson::Runner
def self.execute(command, args, options)
options[:version] ? puts("Version 1000.0") : super
end
def self.display_command_help(cmd)
super
puts "And don't forget to eat BAACCCONN"
end
end
describe "Runner" do
before_all { reset }
def my_command(cmd='')
$0 = 'my_command'
capture_stdout do
MyRunner.start Shellwords.split(cmd)
end
end
def extended_command(cmd='')
$0 = 'extended_command'
capture_stdout do
ExtendedRunner.start Shellwords.split(cmd)
end
end
def default_usage
<<-STR
Usage: my_command [OPTIONS] COMMAND [ARGS]
Available commands:
boom
broken
help Displays help for a command
medium This is a medium
mini This is a mini
quiet
small This is a small
splot This is splot
test
Options:
-h, --help Displays this help message
STR
end
it "prints sorted commands by default" do
my_command.should == default_usage
end
it "prints default usage for -h and --help" do
my_command('-h').should == default_usage
my_command('--help').should == default_usage
end
describe "for help COMMAND" do
it 'prints help for valid command' do
my_command('help quiet').should ==<<-STR
Usage: my_command quiet
Description:
TODO
STR
end
it 'prints error for invalid command' do
Boson::DefaultCommandsRunner.expects(:abort).
with("Could not find command \"invalid\"")
my_command('help invalid')
end
end
describe "for COMMAND -h" do
it "prints help for descriptionless command" do
my_command('quiet -h').should == <<-STR
Usage: my_command quiet
Description:
TODO
STR
end
it "prints help for optionless command with splat args" do
my_command('small -h').should == <<-STR
Usage: my_command small *ARGS
Description:
This is a small
STR
end
it "prints help for optionless command with required args" do
my_command('mini -h').should == <<-STR
Usage: my_command mini ME
Description:
This is a mini
STR
end
it "prints help for command with options and optional args" do
my_command('medium -h').should == <<-STR
Usage: my_command medium [ARG]
Options:
-s, --spicy hot
Description:
This is a medium
STR
end
end
it "handles command with default arguments correctly" do
my_command('medium').chomp.should == '[nil, {}]'
end
it "calls command with options correctly" do
my_command('medium 1 --spicy').chomp.should == '["1", {:spicy=>true}]'
end
it "calls command with quoted arguments correctly" do
my_command("medium '1 2'").chomp.should == '["1 2", {}]'
end
it "calls optionless command correctly" do
my_command('small 1 2').chomp.should == '["1", "2"]'
end
it "calls command with too many args" do
MyRunner.expects(:abort).with <<-STR.chomp
'medium' was called incorrectly.
Usage: medium [ARG]
STR
my_command('medium 1 2 3')
end
it "calls command with splat args and multiple options correctly" do
Boson.in_shell = true
my_command('splot 1 2 -b --tags=1,2').chomp.should ==
'["1", "2", {:blurg=>true, :tags=>["1", "2"]}]'
Boson.in_shell = nil
end
it "executes custom global option" do
# setup goes here to avoid coupling to other runner
ExtendedRunner::GLOBAL_OPTIONS[:version] = {
type: :boolean, :desc => 'Print version'
}
extended_command('-v').chomp.should == 'Version 1000.0'
end
it "allows Kernel-method command names" do
my_command('test').chomp.should == 'TEST'
end
it "prints error message for internal public method" do
MyRunner.expects(:abort).with %[Could not find command "to_s"]
my_command('to_s').should == ''
end
it "prints error message for nonexistant command" do
MyRunner.expects(:abort).with %[Could not find command "blarg"]
my_command('blarg').should == ''
end
it "allows no method error in command" do
assert_error(NoMethodError) { my_command('boom') }
end
it "allows no method error in command" do
assert_error(ArgumentError) { my_command('broken') }
end
it "prints error message for private method" do
MyRunner.expects(:abort).with %[Could not find command "no_run"]
my_command('no_run').should == ''
end
describe "$BOSONRC" do
before { ENV.delete('BOSONRC') }
it "is not loaded by default" do
MyRunner.expects(:load).never
my_command('quiet').should == ''
end
it "is loaded if set" do
ENV['BOSONRC'] = 'whoop'
File.expects(:exists?).returns(true)
MyRunner.expects(:load).with('whoop')
my_command('quiet')
end
after_all { ENV['BOSONRC'] = File.dirname(__FILE__) + '/.bosonrc' }
end
describe "extend Runner" do
it "can extend help" do
extended_command('help help').should == <<-STR
Usage: extended_command help CMD
Description:
Displays help for a command
And don't forget to eat BAACCCONN
STR
end
it "can extend a command's --help" do
extended_command('help -h').should == <<-STR
Usage: extended_command help CMD
Description:
Displays help for a command
And don't forget to eat BAACCCONN
STR
end
end
end