summaryrefslogtreecommitdiffstats
path: root/spec/unit/application/puppetdoc.rb
blob: 2bb74a7fa73ee3b99f30ddb399b2c1ebc6ac52c5 (plain)
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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/application/puppetdoc'

describe "puppetdoc" do
    before :each do
        @puppetdoc = Puppet::Application[:puppetdoc]
        @puppetdoc.stubs(:puts)
        @puppetdoc.run_preinit
        Puppet::Util::Log.stubs(:newdestination)
        Puppet::Util::Log.stubs(:level=)
    end

    it "should ask Puppet::Application to not parse Puppet configuration file" do
        @puppetdoc.should_parse_config?.should be_false
    end

    it "should declare a other command" do
        @puppetdoc.should respond_to(:other)
    end

    it "should declare a rdoc command" do
        @puppetdoc.should respond_to(:rdoc)
    end

    it "should declare a trac command" do
        @puppetdoc.should respond_to(:trac)
    end

    it "should declare a fallback for unknown options" do
        @puppetdoc.should respond_to(:handle_unknown)
    end

    it "should declare a preinit block" do
        @puppetdoc.should respond_to(:run_preinit)
    end

    describe "in preinit" do
        it "should set references to []" do
            @puppetdoc.run_preinit

            @puppetdoc.options[:references].should == []
        end

        it "should init mode to text" do
            @puppetdoc.run_preinit

            @puppetdoc.options[:mode].should == :text
        end

        it "should init format to to_rest" do
            @puppetdoc.run_preinit

            @puppetdoc.options[:format].should == :to_rest
        end
    end

    describe "when handling options" do
        [:all, :outputdir, :verbose, :debug].each do |option|
            it "should declare handle_#{option} method" do
                @puppetdoc.should respond_to("handle_#{option}".to_sym)
            end

            it "should store argument value when calling handle_#{option}" do
                @puppetdoc.options.expects(:[]=).with(option, 'arg')
                @puppetdoc.send("handle_#{option}".to_sym, 'arg')
            end
        end

        it "should store the format if valid" do
            Puppet::Util::Reference.stubs(:method_defined?).with('to_format').returns(true)

            @puppetdoc.options.expects(:[]=).with(:format, 'to_format')

            @puppetdoc.handle_format('format')
        end

        it "should raise an error if the format is not valid" do
            Puppet::Util::Reference.stubs(:method_defined?).with('to_format').returns(false)
            lambda { @puppetdoc.handle_format('format') }
        end

        it "should store the mode if valid" do
            Puppet::Util::Reference.stubs(:modes).returns(stub('mode', :include? => true))

            @puppetdoc.options.expects(:[]=).with(:mode, :mode)

            @puppetdoc.handle_mode('mode')
        end

        it "should store the mode if :rdoc" do
            Puppet::Util::Reference.modes.stubs(:include?).with('rdoc').returns(false)

            @puppetdoc.options.expects(:[]=).with(:mode, :rdoc)

            @puppetdoc.handle_mode('rdoc')
        end

        it "should raise an error if the mode is not valid" do
            Puppet::Util::Reference.modes.stubs(:include?).with('unknown').returns(false)
            lambda { @puppetdoc.handle_mode('unknown') }
        end

        it "should list all references on list and exit" do
            reference = stubs 'reference'
            ref = stubs 'ref'
            Puppet::Util::Reference.stubs(:references).returns([reference])

            Puppet::Util::Reference.expects(:reference).with(reference).returns(ref)
            ref.expects(:doc)
            @puppetdoc.expects(:exit)

            @puppetdoc.handle_list(nil)
        end

        it "should add reference to references list with --reference" do
            @puppetdoc.options[:references] = [:ref1]

            @puppetdoc.handle_reference('ref2')

            @puppetdoc.options[:references].should == [:ref1,:ref2]
        end
    end

    describe "during setup" do

        before :each do
            Puppet::Log.stubs(:newdestination)
            ARGV.stubs(:size).returns(0)
        end

        it "should default to rdoc mode if there are command line arguments" do
            ARGV.stubs(:size).returns(1)
            @puppetdoc.stubs(:setup_rdoc)

            @puppetdoc.options.expects(:[]=).with(:mode,:rdoc)

            @puppetdoc.run_setup
        end

        it "should call setup_rdoc in rdoc mode" do
            @puppetdoc.options.stubs(:[]).with(:mode).returns(:rdoc)

            @puppetdoc.expects(:setup_rdoc)

            @puppetdoc.run_setup
        end

        it "should call setup_reference if not rdoc" do
            @puppetdoc.options.stubs(:[]).with(:mode).returns(:test)

            @puppetdoc.expects(:setup_reference)

            @puppetdoc.run_setup
        end

        describe "in non-rdoc mode" do

            it "should get all non-dynamic reference if --all" do
                @puppetdoc.options.stubs(:[]).with(:all).returns(true)
                @puppetdoc.options.stubs(:[]).with(:references).returns([])
                static = stub 'static', :dynamic? => false
                dynamic = stub 'dynamic', :dynamic? => true
                Reference.stubs(:reference).with(:static).returns(static)
                Reference.stubs(:reference).with(:dynamic).returns(dynamic)
                Reference.stubs(:references).returns([:static,:dynamic])

                @puppetdoc.options.stubs(:[]=).with(:references, [:static])

                @puppetdoc.setup_reference
            end

            it "should default to :type if no references" do
                @puppetdoc.options.stubs(:[]).with(:all).returns(false)
                array = stub 'array', :empty? => true
                @puppetdoc.options.stubs(:[]).with(:references).returns(array)

                array.expects(:<<).with(:type)

                @puppetdoc.setup_reference
            end

        end

        describe "in rdoc mode" do

            before :each do
                @puppetdoc.options.stubs(:[]).returns(false)
                Puppet.stubs(:[]=).with(:name, "puppetmasterd")
                Puppet.stubs(:parse_config)
                Puppet::Util::Log.stubs(:level=)
                Puppet::Util::Log.stubs(:newdestination)
            end

            describe "when there are unknown args" do

                it "should expand --modulepath if any" do
                    @puppetdoc.unknown_args = [ { :opt => "--modulepath", :arg => "path" } ]
                    Puppet.settings.stubs(:handlearg)

                    File.expects(:expand_path).with("path")

                    @puppetdoc.setup_rdoc
                end

                it "should expand --manifestdir if any" do
                    @puppetdoc.unknown_args = [ { :opt => "--manifestdir", :arg => "path" } ]
                    Puppet.settings.stubs(:handlearg)

                    File.expects(:expand_path).with("path")

                    @puppetdoc.setup_rdoc
                end

                it "should give them to Puppet.settings" do
                    @puppetdoc.unknown_args = [ { :opt => :option, :arg => :argument } ]
                    Puppet.settings.expects(:handlearg).with(:option,:argument)

                    @puppetdoc.setup_rdoc
                end
            end

            it "should pretend to be puppetmasterd" do
                Puppet.expects(:[]=).with(:name, "puppetmasterd")

                @puppetdoc.setup_rdoc
            end

            it "should parse puppet configuration" do
                Puppet.expects(:parse_config)

                @puppetdoc.setup_rdoc
            end

            it "should set log level to debug if --debug" do
                @puppetdoc.options.stubs(:[]).with(:debug).returns(true)
                Puppet::Util::Log.expects(:level=).with(:debug)

                @puppetdoc.setup_rdoc
            end

            it "should set log level to info if --verbose" do
                @puppetdoc.options.stubs(:[]).with(:verbose).returns(true)
                Puppet::Util::Log.expects(:level=).with(:info)

                @puppetdoc.setup_rdoc
            end

            it "should set log destination to console if --verbose" do
                @puppetdoc.options.stubs(:[]).with(:verbose).returns(true)

                Puppet::Util::Log.expects(:newdestination).with(:console)

                @puppetdoc.setup_rdoc
            end

            it "should set log destination to console if --debug" do
                @puppetdoc.options.stubs(:[]).with(:debug).returns(true)

                Puppet::Util::Log.expects(:newdestination).with(:console)

                @puppetdoc.setup_rdoc
            end

        end

    end

    describe "when running" do
        before :each do
        end

        describe "in trac mode" do
            it "should call trac for each reference" do
                ref = stub 'ref'
                Puppet::Util::Reference.stubs(:reference).with(:ref).returns(ref)
                @puppetdoc.options.stubs(:[]).with(:references).returns([:ref])
                @puppetdoc.options.stubs(:[]).with(:mode).returns(:trac)

                ref.expects(:trac)

                @puppetdoc.trac
            end
        end

        describe "in rdoc mode" do
            before :each do
                @puppetdoc.manifest = false
                Puppet.stubs(:info)
                Puppet.stubs(:[]).with(:trace).returns(false)
                Puppet.stubs(:[]).with(:modulepath).returns('modules')
                Puppet.stubs(:[]).with(:manifestdir).returns('manifests')
                @puppetdoc.options.stubs(:[]).with(:all).returns(false)
                @puppetdoc.options.stubs(:[]).with(:outputdir).returns('doc')
                Puppet.settings.stubs(:[]=).with(:document_all, false)
                Puppet.settings.stubs(:setdefaults)
                Puppet::Util::RDoc.stubs(:rdoc)
                @puppetdoc.stubs(:exit)
                File.stubs(:expand_path).with('modules').returns('modules')
                File.stubs(:expand_path).with('manifests').returns('manifests')
                @old = ARGV.dup
                ARGV.clear
            end

            after :each do
                ARGV << @old
            end

            it "should set document_all on --all" do
                @puppetdoc.options.expects(:[]).with(:all).returns(true)
                Puppet.settings.expects(:[]=).with(:document_all, true)

                @puppetdoc.rdoc
            end

            it "should call Puppet::Util::RDoc.rdoc in full mode" do
                Puppet::Util::RDoc.expects(:rdoc).with('doc', ['modules','manifests'])
                @puppetdoc.rdoc
            end

            it "should call Puppet::Util::RDoc.manifestdoc in manifest mode" do
                @puppetdoc.manifest = true
                Puppet::Util::RDoc.expects(:manifestdoc)
                @puppetdoc.rdoc
            end
        end

        describe "in the other modes" do
            it "should get reference in given format" do
                reference = stub 'reference'
                @puppetdoc.options.stubs(:[]).with(:mode).returns(:none)
                @puppetdoc.options.stubs(:[]).with(:references).returns([:ref])
                Puppet::Util::Reference.expects(:reference).with(:ref).returns(reference)
                @puppetdoc.options.stubs(:[]).with(:format).returns(:format)
                @puppetdoc.stubs(:exit)

                reference.expects(:send).with { |format,contents| format == :format }.returns('doc')
                @puppetdoc.other
            end
        end

    end
end