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
|
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/application/face_base'
require 'tmpdir'
class Puppet::Application::FaceBase::Basetest < Puppet::Application::FaceBase
end
describe Puppet::Application::FaceBase do
before :all do
Puppet::Face.define(:basetest, '0.0.1') do
option("--[no-]boolean")
option("--mandatory MANDATORY")
action :foo do
option("--action")
when_invoked { |*args| args.length }
end
end
end
let :app do
app = Puppet::Application::FaceBase::Basetest.new
app.command_line.stubs(:subcommand_name).returns('subcommand')
Puppet::Util::Log.stubs(:newdestination)
app
end
describe "#find_global_settings_argument" do
it "should not match --ca to --ca-location" do
option = mock('ca option', :optparse_args => ["--ca"])
Puppet.settings.expects(:each).yields(:ca, option)
app.find_global_settings_argument("--ca-location").should be_nil
end
end
describe "#parse_options" do
before :each do
app.command_line.stubs(:args).returns %w{}
end
describe "with just an action" do
before :all do
# We have to stub Signal.trap to avoid a crazy mess where we take
# over signal handling and make it impossible to cancel the test
# suite run.
#
# It would be nice to fix this elsewhere, but it is actually hard to
# capture this in rspec 2.5 and all. :( --daniel 2011-04-08
Signal.stubs(:trap)
app.command_line.stubs(:args).returns %w{foo}
app.preinit
app.parse_options
end
it "should set the face based on the type" do
app.face.name.should == :basetest
end
it "should find the action" do
app.action.should be
app.action.name.should == :foo
end
end
it "should use the default action if not given any arguments" do
app.command_line.stubs(:args).returns []
action = stub(:options => [])
Puppet::Face[:basetest, '0.0.1'].expects(:get_default_action).returns(action)
app.stubs(:main)
app.run
app.action.should == action
app.arguments.should == [ { } ]
end
it "should use the default action if not given a valid one" do
app.command_line.stubs(:args).returns %w{bar}
action = stub(:options => [])
Puppet::Face[:basetest, '0.0.1'].expects(:get_default_action).returns(action)
app.stubs(:main)
app.run
app.action.should == action
app.arguments.should == [ 'bar', { } ]
end
it "should have no action if not given a valid one and there is no default action" do
app.command_line.stubs(:args).returns %w{bar}
Puppet::Face[:basetest, '0.0.1'].expects(:get_default_action).returns(nil)
app.stubs(:main)
app.run
app.action.should be_nil
app.arguments.should == [ 'bar', { } ]
end
it "should report a sensible error when options with = fail" do
app.command_line.stubs(:args).returns %w{--action=bar foo}
expect { app.preinit; app.parse_options }.
to raise_error OptionParser::InvalidOption, /invalid option: --action/
end
it "should fail if an action option is before the action" do
app.command_line.stubs(:args).returns %w{--action foo}
expect { app.preinit; app.parse_options }.
to raise_error OptionParser::InvalidOption, /invalid option: --action/
end
it "should fail if an unknown option is before the action" do
app.command_line.stubs(:args).returns %w{--bar foo}
expect { app.preinit; app.parse_options }.
to raise_error OptionParser::InvalidOption, /invalid option: --bar/
end
it "should fail if an unknown option is after the action" do
app.command_line.stubs(:args).returns %w{foo --bar}
expect { app.preinit; app.parse_options }.
to raise_error OptionParser::InvalidOption, /invalid option: --bar/
end
it "should accept --bar as an argument to a mandatory option after action" do
app.command_line.stubs(:args).returns %w{foo --mandatory --bar}
app.preinit
app.parse_options
app.action.name.should == :foo
app.options.should == { :mandatory => "--bar" }
end
it "should accept --bar as an argument to a mandatory option before action" do
app.command_line.stubs(:args).returns %w{--mandatory --bar foo}
app.preinit
app.parse_options
app.action.name.should == :foo
app.options.should == { :mandatory => "--bar" }
end
it "should not skip when --foo=bar is given" do
app.command_line.stubs(:args).returns %w{--mandatory=bar --bar foo}
expect { app.preinit; app.parse_options }.
to raise_error OptionParser::InvalidOption, /invalid option: --bar/
end
{ "boolean options before" => %w{--trace foo},
"boolean options after" => %w{foo --trace}
}.each do |name, args|
it "should accept global boolean settings #{name} the action" do
app.command_line.stubs(:args).returns args
app.preinit
app.parse_options
Puppet[:trace].should be_true
end
end
{ "before" => %w{--syslogfacility user1 foo},
" after" => %w{foo --syslogfacility user1}
}.each do |name, args|
it "should accept global settings with arguments #{name} the action" do
app.command_line.stubs(:args).returns args
app.preinit
app.parse_options
Puppet[:syslogfacility].should == "user1"
end
end
it "should handle application-level options" do
app.command_line.stubs(:args).returns %w{help --verbose help}
app.preinit
app.parse_options
app.face.name.should == :basetest
end
end
describe "#setup" do
it "should remove the action name from the arguments" do
app.command_line.stubs(:args).returns %w{--mandatory --bar foo}
app.preinit
app.parse_options
app.setup
app.arguments.should == [{ :mandatory => "--bar" }]
end
it "should pass positional arguments" do
app.command_line.stubs(:args).returns %w{--mandatory --bar foo bar baz quux}
app.preinit
app.parse_options
app.setup
app.arguments.should == ['bar', 'baz', 'quux', { :mandatory => "--bar" }]
end
end
describe "#main" do
before :each do
app.expects(:exit).with(0)
app.face = Puppet::Face[:basetest, '0.0.1']
app.action = app.face.get_action(:foo)
app.arguments = ["myname", "myarg"]
end
it "should send the specified verb and name to the face" do
app.face.expects(:foo).with(*app.arguments)
app.main
end
it "should lookup help when it cannot do anything else" do
app.action = nil
Puppet::Face[:help, :current].expects(:help).with(:basetest, *app.arguments)
app.stubs(:puts) # meh. Don't print nil, thanks. --daniel 2011-04-12
app.main
end
it "should use its render method to render any result" do
app.expects(:render).with(app.arguments.length + 1)
app.stubs(:puts) # meh. Don't print nil, thanks. --daniel 2011-04-12
app.main
end
end
describe "#render" do
before :each do
app.face = Puppet::Face[:basetest, '0.0.1']
app.action = app.face.get_action(:foo)
end
["hello", 1, 1.0].each do |input|
it "should just return a #{input.class.name}" do
app.render(input).should == input
end
end
[[1, 2], ["one"], [{ 1 => 1 }]].each do |input|
it "should render #{input.class} using the 'pp' library" do
app.render(input).should == input.pretty_inspect
end
end
it "should render a non-trivially-keyed Hash with the 'pp' library" do
hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 }
app.render(hash).should == hash.pretty_inspect
end
it "should render a {String,Numeric}-keyed Hash into a table" do
object = Object.new
hash = { "one" => 1, "two" => [], "three" => {}, "four" => object,
5 => 5, 6.0 => 6 }
# Gotta love ASCII-betical sort order. Hope your objects are better
# structured for display than my test one is. --daniel 2011-04-18
app.render(hash).should == <<EOT
5 5
6.0 6
four #{object.pretty_inspect.chomp}
one 1
three {}
two []
EOT
end
it "should render a hash nicely with a multi-line value" do
hash = {
"number" => { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 },
"text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 }
}
app.render(hash).should == <<EOT
number {"1"=>"1111111111111111111111111111111111111111",
"2"=>"2222222222222222222222222222222222222222",
"3"=>"3333333333333333333333333333333333333333"}
text {"a"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"b"=>"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"c"=>"cccccccccccccccccccccccccccccccccccccccc"}
EOT
end
it "should invoke the action rendering hook while rendering" do
app.action.set_rendering_method_for(:for_humans, proc { |value| "bi-winning!" })
app.action.render_as = :for_humans
app.render("bi-polar?").should == "bi-winning!"
end
end
end
|