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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/application'
require 'puppet'
require 'getoptlong'
describe Puppet::Application do
before :each do
@app = Puppet::Application.new(:test)
end
it "should have a run entry-point" do
@app.should respond_to(:run)
end
it "should have a read accessor to options" do
@app.should respond_to(:options)
end
it "should create a default run_setup method" do
@app.should respond_to(:run_setup)
end
it "should create a default run_preinit method" do
@app.should respond_to(:run_preinit)
end
it "should create a default get_command method" do
@app.should respond_to(:get_command)
end
it "should return :main as default get_command" do
@app.get_command.should == :main
end
describe "when parsing command-line options" do
before :each do
@argv_bak = ARGV.dup
ARGV.clear
Puppet.settings.stubs(:optparse_addargs).returns([])
@app = Puppet::Application.new(:test)
end
after :each do
ARGV.clear
ARGV << @argv_bak
end
it "should get options from Puppet.settings.optparse_addargs" do
Puppet.settings.expects(:optparse_addargs).returns([])
@app.parse_options
end
it "should add Puppet.settings options to OptionParser" do
Puppet.settings.stubs(:optparse_addargs).returns( [["--option","-o", "Funny Option"]])
@app.opt_parser.expects(:on).with { |*arg| arg == ["--option","-o", "Funny Option"] }
@app.parse_options
end
it "should ask OptionParser to parse the command-line argument" do
@app.opt_parser.expects(:parse!)
@app.parse_options
end
describe "when using --help" do
confine "rdoc" => Puppet.features.usage?
it "should call RDoc::usage and exit" do
@app.expects(:exit)
RDoc.expects(:usage).returns(true)
@app.handle_help(nil)
end
end
describe "when using --version" do
it "should declare a version option" do
@app.should respond_to(:handle_version)
end
it "should exit after printing the version" do
@app.stubs(:puts)
lambda { @app.handle_version(nil) }.should raise_error(SystemExit)
end
end
describe "when dealing with an argument not declared directly by the application" do
it "should pass it to handle_unknown if this method exists" do
Puppet.settings.stubs(:optparse_addargs).returns([["--not-handled"]])
@app.opt_parser.stubs(:on).yields("value")
@app.expects(:handle_unknown).with("--not-handled", "value").returns(true)
@app.parse_options
end
it "should pass it to Puppet.settings if handle_unknown says so" do
Puppet.settings.stubs(:optparse_addargs).returns([["--topuppet"]])
@app.opt_parser.stubs(:on).yields("value")
@app.stubs(:handle_unknown).with("--topuppet", "value").returns(false)
Puppet.settings.expects(:handlearg).with("--topuppet", "value")
@app.parse_options
end
it "should pass it to Puppet.settings if there is no handle_unknown method" do
Puppet.settings.stubs(:optparse_addargs).returns([["--topuppet"]])
@app.opt_parser.stubs(:on).yields("value")
@app.stubs(:respond_to?).returns(false)
Puppet.settings.expects(:handlearg).with("--topuppet", "value")
@app.parse_options
end
it "should transform boolean false value to string for Puppet.settings" do
Puppet.settings.expects(:handlearg).with("--option", "false")
@app.handlearg("--option", false)
end
it "should transform boolean true value to string for Puppet.settings" do
Puppet.settings.expects(:handlearg).with("--option", "true")
@app.handlearg("--option", true)
end
it "should transform boolean option to normal form for Puppet.settings" do
Puppet.settings.expects(:handlearg).with("--option", "true")
@app.handlearg("--[no-]option", true)
end
it "should transform boolean option to no- form for Puppet.settings" do
Puppet.settings.expects(:handlearg).with("--no-option", "false")
@app.handlearg("--[no-]option", false)
end
end
it "should exit if OptionParser raises an error" do
$stderr.stubs(:puts)
@app.opt_parser.stubs(:parse!).raises(OptionParser::ParseError.new("blah blah"))
@app.expects(:exit)
lambda { @app.parse_options }.should_not raise_error
end
end
describe "when calling default setup" do
before :each do
@app = Puppet::Application.new(:test)
@app.stubs(:should_parse_config?).returns(false)
@app.options.stubs(:[])
end
[ :debug, :verbose ].each do |level|
it "should honor option #{level}" do
@app.options.stubs(:[]).with(level).returns(true)
Puppet::Util::Log.stubs(:newdestination)
Puppet::Util::Log.expects(:level=).with(level == :verbose ? :info : :debug)
@app.run_setup
end
end
it "should honor setdest option" do
@app.options.stubs(:[]).with(:setdest).returns(false)
Puppet::Util::Log.expects(:newdestination).with(:syslog)
@app.run_setup
end
end
describe "when running" do
before :each do
@app = Puppet::Application.new(:test)
@app.stubs(:run_preinit)
@app.stubs(:run_setup)
@app.stubs(:parse_options)
end
it "should call run_preinit" do
@app.stubs(:run_command)
@app.expects(:run_preinit)
@app.run
end
it "should call parse_options" do
@app.stubs(:run_command)
@app.expects(:parse_options)
@app.run
end
it "should call run_command" do
@app.expects(:run_command)
@app.run
end
it "should parse Puppet configuration if should_parse_config is called" do
@app.stubs(:run_command)
@app.should_parse_config
Puppet.expects(:parse_config)
@app.run
end
it "should not parse_option if should_not_parse_config is called" do
@app.stubs(:run_command)
@app.should_not_parse_config
Puppet.expects(:parse_config).never
@app.run
end
it "should parse Puppet configuration if needed" do
@app.stubs(:run_command)
@app.stubs(:should_parse_config?).returns(true)
Puppet.expects(:parse_config)
@app.run
end
it "should call the action matching what returned command" do
@app.stubs(:get_command).returns(:backup)
@app.stubs(:respond_to?).with(:backup).returns(true)
@app.expects(:backup)
@app.run
end
it "should call main as the default command" do
@app.expects(:main)
@app.run
end
it "should raise an error if no command can be called" do
lambda { @app.run }.should raise_error(NotImplementedError)
end
it "should raise an error if dispatch returns no command" do
@app.stubs(:get_command).returns(nil)
lambda { @app.run }.should raise_error(NotImplementedError)
end
it "should raise an error if dispatch returns an invalid command" do
@app.stubs(:get_command).returns(:this_function_doesnt_exist)
lambda { @app.run }.should raise_error(NotImplementedError)
end
end
describe "when metaprogramming" do
before :each do
@app = Puppet::Application.new(:test)
end
it "should create a new method with command" do
@app.command(:test) do
end
@app.should respond_to(:test)
end
describe "when calling attr_accessor" do
it "should create a reader method" do
@app.attr_accessor(:attribute)
@app.should respond_to(:attribute)
end
it "should create a reader that delegates to instance_variable_get" do
@app.attr_accessor(:attribute)
@app.expects(:instance_variable_get).with(:@attribute)
@app.attribute
end
it "should create a writer method" do
@app.attr_accessor(:attribute)
@app.should respond_to(:attribute=)
end
it "should create a writer that delegates to instance_variable_set" do
@app.attr_accessor(:attribute)
@app.expects(:instance_variable_set).with(:@attribute, 1234)
@app.attribute=1234
end
end
describe "when calling option" do
it "should create a new method named after the option" do
@app.option("--test1","-t") do
end
@app.should respond_to(:handle_test1)
end
it "should transpose in option name any '-' into '_'" do
@app.option("--test-dashes-again","-t") do
end
@app.should respond_to(:handle_test_dashes_again)
end
it "should create a new method called handle_test2 with option(\"--[no-]test2\")" do
@app.option("--[no-]test2","-t") do
end
@app.should respond_to(:handle_test2)
end
describe "when a block is passed" do
it "should create a new method with it" do
@app.option("--[no-]test2","-t") do
raise "I can't believe it, it works!"
end
lambda { @app.handle_test2 }.should raise_error
end
it "should declare the option to OptionParser" do
@app.opt_parser.expects(:on).with { |*arg| arg[0] == "--[no-]test3" }
@app.option("--[no-]test3","-t") do
end
end
it "should pass a block that calls our defined method" do
@app.opt_parser.stubs(:on).yields(nil)
@app.expects(:send).with(:handle_test4, nil)
@app.option("--test4","-t") do
end
end
end
describe "when no block is given" do
it "should declare the option to OptionParser" do
@app.opt_parser.expects(:on).with("--test4","-t")
@app.option("--test4","-t")
end
it "should give to OptionParser a block that adds the the value to the options array" do
@app.opt_parser.stubs(:on).with("--test4","-t").yields(nil)
@app.options.expects(:[]=).with(:test4,nil)
@app.option("--test4","-t")
end
end
end
it "should create a method called run_setup with setup" do
@app.setup do
end
@app.should respond_to(:run_setup)
end
it "should create a method called run_preinit with preinit" do
@app.preinit do
end
@app.should respond_to(:run_preinit)
end
it "should create a method called handle_unknown with unknown" do
@app.unknown do
end
@app.should respond_to(:handle_unknown)
end
it "should create a method called get_command with dispatch" do
@app.dispatch do
end
@app.should respond_to(:get_command)
end
end
end
|