3
require File.dirname(__FILE__) + '/../../spec_helper'
5
require 'puppet/application/puppetdoc'
7
describe "puppetdoc" do
9
@puppetdoc = Puppet::Application[:puppetdoc]
10
@puppetdoc.stubs(:puts)
11
@puppetdoc.run_preinit
12
Puppet::Util::Log.stubs(:newdestination)
13
Puppet::Util::Log.stubs(:level=)
16
it "should ask Puppet::Application to not parse Puppet configuration file" do
17
@puppetdoc.should_parse_config?.should be_false
20
it "should declare a other command" do
21
@puppetdoc.should respond_to(:other)
24
it "should declare a rdoc command" do
25
@puppetdoc.should respond_to(:rdoc)
28
it "should declare a trac command" do
29
@puppetdoc.should respond_to(:trac)
32
it "should declare a fallback for unknown options" do
33
@puppetdoc.should respond_to(:handle_unknown)
36
it "should declare a preinit block" do
37
@puppetdoc.should respond_to(:run_preinit)
40
describe "in preinit" do
41
it "should set references to []" do
42
@puppetdoc.run_preinit
44
@puppetdoc.options[:references].should == []
47
it "should init mode to text" do
48
@puppetdoc.run_preinit
50
@puppetdoc.options[:mode].should == :text
53
it "should init format to to_rest" do
54
@puppetdoc.run_preinit
56
@puppetdoc.options[:format].should == :to_rest
60
describe "when handling options" do
61
[:all, :outputdir, :verbose, :debug].each do |option|
62
it "should declare handle_#{option} method" do
63
@puppetdoc.should respond_to("handle_#{option}".to_sym)
66
it "should store argument value when calling handle_#{option}" do
67
@puppetdoc.options.expects(:[]=).with(option, 'arg')
68
@puppetdoc.send("handle_#{option}".to_sym, 'arg')
72
it "should store the format if valid" do
73
Puppet::Util::Reference.stubs(:method_defined?).with('to_format').returns(true)
75
@puppetdoc.options.expects(:[]=).with(:format, 'to_format')
77
@puppetdoc.handle_format('format')
80
it "should raise an error if the format is not valid" do
81
Puppet::Util::Reference.stubs(:method_defined?).with('to_format').returns(false)
82
lambda { @puppetdoc.handle_format('format') }
85
it "should store the mode if valid" do
86
Puppet::Util::Reference.stubs(:modes).returns(stub('mode', :include? => true))
88
@puppetdoc.options.expects(:[]=).with(:mode, :mode)
90
@puppetdoc.handle_mode('mode')
93
it "should store the mode if :rdoc" do
94
Puppet::Util::Reference.modes.stubs(:include?).with('rdoc').returns(false)
96
@puppetdoc.options.expects(:[]=).with(:mode, :rdoc)
98
@puppetdoc.handle_mode('rdoc')
101
it "should raise an error if the mode is not valid" do
102
Puppet::Util::Reference.modes.stubs(:include?).with('unknown').returns(false)
103
lambda { @puppetdoc.handle_mode('unknown') }
106
it "should list all references on list and exit" do
107
reference = stubs 'reference'
109
Puppet::Util::Reference.stubs(:references).returns([reference])
111
Puppet::Util::Reference.expects(:reference).with(reference).returns(ref)
113
@puppetdoc.expects(:exit)
115
@puppetdoc.handle_list(nil)
118
it "should add reference to references list with --reference" do
119
@puppetdoc.options[:references] = [:ref1]
121
@puppetdoc.handle_reference('ref2')
123
@puppetdoc.options[:references].should == [:ref1,:ref2]
127
describe "during setup" do
130
Puppet::Log.stubs(:newdestination)
131
ARGV.stubs(:size).returns(0)
134
it "should default to rdoc mode if there are command line arguments" do
135
ARGV.stubs(:size).returns(1)
136
@puppetdoc.stubs(:setup_rdoc)
138
@puppetdoc.options.expects(:[]=).with(:mode,:rdoc)
143
it "should call setup_rdoc in rdoc mode" do
144
@puppetdoc.options.stubs(:[]).with(:mode).returns(:rdoc)
146
@puppetdoc.expects(:setup_rdoc)
151
it "should call setup_reference if not rdoc" do
152
@puppetdoc.options.stubs(:[]).with(:mode).returns(:test)
154
@puppetdoc.expects(:setup_reference)
159
describe "in non-rdoc mode" do
161
it "should get all non-dynamic reference if --all" do
162
@puppetdoc.options.stubs(:[]).with(:all).returns(true)
163
@puppetdoc.options.stubs(:[]).with(:references).returns([])
164
static = stub 'static', :dynamic? => false
165
dynamic = stub 'dynamic', :dynamic? => true
166
Reference.stubs(:reference).with(:static).returns(static)
167
Reference.stubs(:reference).with(:dynamic).returns(dynamic)
168
Reference.stubs(:references).returns([:static,:dynamic])
170
@puppetdoc.options.stubs(:[]=).with(:references, [:static])
172
@puppetdoc.setup_reference
175
it "should default to :type if no references" do
176
@puppetdoc.options.stubs(:[]).with(:all).returns(false)
177
array = stub 'array', :empty? => true
178
@puppetdoc.options.stubs(:[]).with(:references).returns(array)
180
array.expects(:<<).with(:type)
182
@puppetdoc.setup_reference
187
describe "in rdoc mode" do
190
@puppetdoc.options.stubs(:[]).returns(false)
191
Puppet.stubs(:[]=).with(:name, "puppetmasterd")
192
Puppet.stubs(:parse_config)
193
Puppet::Util::Log.stubs(:level=)
194
Puppet::Util::Log.stubs(:newdestination)
197
describe "when there are unknown args" do
199
it "should expand --modulepath if any" do
200
@puppetdoc.unknown_args = [ { :opt => "--modulepath", :arg => "path" } ]
201
Puppet.settings.stubs(:handlearg)
203
File.expects(:expand_path).with("path")
205
@puppetdoc.setup_rdoc
208
it "should expand --manifestdir if any" do
209
@puppetdoc.unknown_args = [ { :opt => "--manifestdir", :arg => "path" } ]
210
Puppet.settings.stubs(:handlearg)
212
File.expects(:expand_path).with("path")
214
@puppetdoc.setup_rdoc
217
it "should give them to Puppet.settings" do
218
@puppetdoc.unknown_args = [ { :opt => :option, :arg => :argument } ]
219
Puppet.settings.expects(:handlearg).with(:option,:argument)
221
@puppetdoc.setup_rdoc
225
it "should pretend to be puppetmasterd" do
226
Puppet.expects(:[]=).with(:name, "puppetmasterd")
228
@puppetdoc.setup_rdoc
231
it "should parse puppet configuration" do
232
Puppet.expects(:parse_config)
234
@puppetdoc.setup_rdoc
237
it "should set log level to debug if --debug" do
238
@puppetdoc.options.stubs(:[]).with(:debug).returns(true)
239
Puppet::Util::Log.expects(:level=).with(:debug)
241
@puppetdoc.setup_rdoc
244
it "should set log level to info if --verbose" do
245
@puppetdoc.options.stubs(:[]).with(:verbose).returns(true)
246
Puppet::Util::Log.expects(:level=).with(:info)
248
@puppetdoc.setup_rdoc
251
it "should set log destination to console if --verbose" do
252
@puppetdoc.options.stubs(:[]).with(:verbose).returns(true)
254
Puppet::Util::Log.expects(:newdestination).with(:console)
256
@puppetdoc.setup_rdoc
259
it "should set log destination to console if --debug" do
260
@puppetdoc.options.stubs(:[]).with(:debug).returns(true)
262
Puppet::Util::Log.expects(:newdestination).with(:console)
264
@puppetdoc.setup_rdoc
271
describe "when running" do
275
describe "in trac mode" do
276
it "should call trac for each reference" do
278
Puppet::Util::Reference.stubs(:reference).with(:ref).returns(ref)
279
@puppetdoc.options.stubs(:[]).with(:references).returns([:ref])
280
@puppetdoc.options.stubs(:[]).with(:mode).returns(:trac)
288
describe "in rdoc mode" do
290
@puppetdoc.manifest = false
292
Puppet.stubs(:[]).with(:trace).returns(false)
293
Puppet.stubs(:[]).with(:modulepath).returns('modules')
294
Puppet.stubs(:[]).with(:manifestdir).returns('manifests')
295
@puppetdoc.options.stubs(:[]).with(:all).returns(false)
296
@puppetdoc.options.stubs(:[]).with(:outputdir).returns('doc')
297
Puppet.settings.stubs(:[]=).with(:document_all, false)
298
Puppet.settings.stubs(:setdefaults)
299
Puppet::Util::RDoc.stubs(:rdoc)
300
@puppetdoc.stubs(:exit)
301
File.stubs(:expand_path).with('modules').returns('modules')
302
File.stubs(:expand_path).with('manifests').returns('manifests')
311
it "should set document_all on --all" do
312
@puppetdoc.options.expects(:[]).with(:all).returns(true)
313
Puppet.settings.expects(:[]=).with(:document_all, true)
318
it "should call Puppet::Util::RDoc.rdoc in full mode" do
319
Puppet::Util::RDoc.expects(:rdoc).with('doc', ['modules','manifests'])
323
it "should call Puppet::Util::RDoc.rdoc in full mode with outputdir set to doc if no --outputdir" do
324
@puppetdoc.options.expects(:[]).with(:outputdir).returns(false)
325
Puppet::Util::RDoc.expects(:rdoc).with('doc', ['modules','manifests'])
329
it "should call Puppet::Util::RDoc.manifestdoc in manifest mode" do
330
@puppetdoc.manifest = true
331
Puppet::Util::RDoc.expects(:manifestdoc)
336
describe "in the other modes" do
337
it "should get reference in given format" do
338
reference = stub 'reference'
339
@puppetdoc.options.stubs(:[]).with(:mode).returns(:none)
340
@puppetdoc.options.stubs(:[]).with(:references).returns([:ref])
341
Puppet::Util::Reference.expects(:reference).with(:ref).returns(reference)
342
@puppetdoc.options.stubs(:[]).with(:format).returns(:format)
343
@puppetdoc.stubs(:exit)
345
reference.expects(:send).with { |format,contents| format == :format }.returns('doc')