summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/rdoc/parser_spec.rb
blob: d30c0c13b92c1eea955fb83e6fb6b9459f63a372 (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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env ruby

Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }

require 'puppet/resource/type_collection'
require 'puppet/util/rdoc/parser'
require 'puppet/util/rdoc/code_objects'
require 'rdoc/options'
require 'rdoc/rdoc'

describe RDoc::Parser do
    before :each do
        File.stubs(:stat).with("init.pp")
        @top_level = stub_everything 'toplevel', :file_relative_name => "init.pp"
        @parser = RDoc::Parser.new(@top_level, "module/manifests/init.pp", nil, Options.instance, RDoc::Stats.new)
    end

    describe "when scanning files" do
        it "should parse puppet files with the puppet parser" do
            @parser.stubs(:scan_top_level)
            parser = stub 'parser'
            Puppet::Parser::Parser.expects(:new).returns(parser)
            parser.expects(:parse)
            parser.expects(:file=).with("module/manifests/init.pp")

            @parser.scan
        end

        it "should scan the ast for Puppet files" do
            parser = stub_everything 'parser'
            Puppet::Parser::Parser.stubs(:new).returns(parser)

            @parser.expects(:scan_top_level)

            @parser.scan
        end

        it "should return a PuppetTopLevel to RDoc" do
            parser = stub_everything 'parser'
            Puppet::Parser::Parser.stubs(:new).returns(parser)

            @parser.expects(:scan_top_level)

            @parser.scan.should be_a(RDoc::PuppetTopLevel)
        end
    end

    describe "when scanning top level entities" do
        before :each do
            @resource_type_collection = stub_everything 'resource_type_collection'
            @parser.ast = @resource_type_collection
            @parser.stubs(:split_module).returns("module")

            @topcontainer = stub_everything 'topcontainer'
            @container = stub_everything 'container'
            @module = stub_everything 'module'
            @container.stubs(:add_module).returns(@module)
            @parser.stubs(:get_class_or_module).returns([@container, "module"])
        end

        it "should read any present README as module documentation" do
            FileTest.stubs(:readable?).returns(true)
            File.stubs(:open).returns("readme")
            @parser.stubs(:parse_elements)

            @module.expects(:comment=).with("readme")

            @parser.scan_top_level(@topcontainer)
        end

        it "should tell the container its module name" do
            @parser.stubs(:parse_elements)

            @topcontainer.expects(:module_name=).with("module")

            @parser.scan_top_level(@topcontainer)
        end

        it "should not document our toplevel if it isn't a valid module" do
            @parser.stubs(:split_module).returns(nil)

            @topcontainer.expects(:document_self=).with(false)
            @parser.expects(:parse_elements).never

            @parser.scan_top_level(@topcontainer)
        end

        it "should set the module as global if we parse the global manifests (ie <site> module)" do
            @parser.stubs(:split_module).returns("<site>")
            @parser.stubs(:parse_elements)

            @topcontainer.expects(:global=).with(true)

            @parser.scan_top_level(@topcontainer)
        end

        it "should attach this module container to the toplevel container" do
            @parser.stubs(:parse_elements)

            @container.expects(:add_module).with(RDoc::PuppetModule, "module").returns(@module)

            @parser.scan_top_level(@topcontainer)
        end

        it "should defer ast parsing to parse_elements for this module" do
            @parser.expects(:parse_elements).with(@module)

            @parser.scan_top_level(@topcontainer)
        end

        it "should defer plugins parsing to parse_plugins for this module" do
            @parser.input_file_name = "module/lib/puppet/parser/function.rb"

            @parser.expects(:parse_plugins).with(@module)

            @parser.scan_top_level(@topcontainer)
        end
    end

    describe "when finding modules from filepath" do
        before :each do
            Puppet::Module.stubs(:modulepath).returns("/path/to/modules")
        end

        it "should return the module name for modulized puppet manifests" do
            File.stubs(:expand_path).returns("/path/to/module/manifests/init.pp")
            File.stubs(:identical?).with("/path/to", "/path/to/modules").returns(true)
            @parser.split_module("/path/to/modules/mymodule/manifests/init.pp").should == "module"
        end

        it "should return <site> for manifests not under module path" do
            File.stubs(:expand_path).returns("/path/to/manifests/init.pp")
            File.stubs(:identical?).returns(false)
            @parser.split_module("/path/to/manifests/init.pp").should == "<site>"
        end
    end

    describe "when parsing AST elements" do
        before :each do
            @klass = stub_everything 'klass', :file => "module/manifests/init.pp", :name => "myclass", :type => :hostclass
            @definition = stub_everything 'definition', :file => "module/manifests/init.pp", :type => :definition, :name => "mydef"
            @node = stub_everything 'node', :file => "module/manifests/init.pp", :type => :node, :name => "mynode"

            @resource_type_collection = Puppet::Resource::TypeCollection.new("env")
            @parser.ast = @resource_type_collection

            @container = stub_everything 'container'
        end

        it "should document classes in the parsed file" do
            @resource_type_collection.add_hostclass(@klass)

            @parser.expects(:document_class).with("myclass", @klass, @container)

            @parser.parse_elements(@container)
        end

        it "should not document class parsed in an other file" do
            @klass.stubs(:file).returns("/not/same/path/file.pp")
            @resource_type_collection.add_hostclass(@klass)

            @parser.expects(:document_class).with("myclass", @klass, @container).never

            @parser.parse_elements(@container)
        end

        it "should document vardefs for the main class" do
            @klass.stubs(:name).returns :main
            @resource_type_collection.add_hostclass(@klass)

            code = stub 'code', :is_a? => false
            @klass.stubs(:name).returns("")
            @klass.stubs(:code).returns(code)

            @parser.expects(:scan_for_vardef).with(@container, code)

            @parser.parse_elements(@container)
        end

        it "should document definitions in the parsed file" do
            @resource_type_collection.add_definition(@definition)

            @parser.expects(:document_define).with("mydef", @definition, @container)

            @parser.parse_elements(@container)
        end

        it "should not document definitions parsed in an other file" do
            @definition.stubs(:file).returns("/not/same/path/file.pp")
            @resource_type_collection.add_definition(@definition)

            @parser.expects(:document_define).with("mydef", @definition, @container).never

            @parser.parse_elements(@container)
        end

        it "should document nodes in the parsed file" do
            @resource_type_collection.add_node(@node)

            @parser.expects(:document_node).with("mynode", @node, @container)

            @parser.parse_elements(@container)
        end

        it "should not document node parsed in an other file" do
            @node.stubs(:file).returns("/not/same/path/file.pp")
            @resource_type_collection.add_node(@node)

            @parser.expects(:document_node).with("mynode", @node, @container).never

            @parser.parse_elements(@container)
        end
    end

    describe "when documenting definition" do
        before(:each) do
            @define = stub_everything 'define', :arguments => [], :doc => "mydoc", :file => "file", :line => 42
            @class = stub_everything 'class'
            @parser.stubs(:get_class_or_module).returns([@class, "mydef"])
        end

        it "should register a RDoc method to the current container" do
            @class.expects(:add_method).with { |m| m.name == "mydef"}
            @parser.document_define("mydef", @define, @class)
        end

        it "should attach the documentation to this method" do
            @class.expects(:add_method).with { |m| m.comment = "mydoc" }

            @parser.document_define("mydef", @define, @class)
        end

        it "should produce a better error message on unhandled exception" do
            @class.expects(:add_method).raises(ArgumentError)

            lambda { @parser.document_define("mydef", @define, @class) }.should raise_error(Puppet::ParseError, /in file at line 42/)
        end

        it "should convert all definition parameter to string" do
            arg = stub 'arg'
            val = stub 'val'

            @define.stubs(:arguments).returns({arg => val})

            arg.expects(:to_s).returns("arg")
            val.expects(:to_s).returns("val")

            @parser.document_define("mydef", @define, @class)
        end
    end

    describe "when documenting nodes" do
        before :each do
            @code = stub_everything 'code'
            @node = stub_everything 'node', :doc => "mydoc", :parent => "parent", :code => @code, :file => "file", :line => 42
            @rdoc_node = stub_everything 'rdocnode'

            @class = stub_everything 'class'
            @class.stubs(:add_node).returns(@rdoc_node)
        end

        it "should add a node to the current container" do
            @class.expects(:add_node).with("mynode", "parent").returns(@rdoc_node)
            @parser.document_node("mynode", @node, @class)
        end

        it "should associate the node documentation to the rdoc node" do
            @rdoc_node.expects(:comment=).with("mydoc")
            @parser.document_node("mynode", @node, @class)
        end

        it "should scan for include and require" do
            @parser.expects(:scan_for_include_or_require).with(@rdoc_node, @code)
            @parser.document_node("mynode", @node, @class)
        end

        it "should scan for variable definition" do
            @parser.expects(:scan_for_vardef).with(@rdoc_node, @code)
            @parser.document_node("mynode", @node, @class)
        end

        it "should scan for resources if needed" do
            Puppet.settings.stubs(:[]).with(:document_all).returns(true)
            @parser.expects(:scan_for_resource).with(@rdoc_node, @code)
            @parser.document_node("mynode", @node, @class)
        end

        it "should produce a better error message on unhandled exception" do
            @class.stubs(:add_node).raises(ArgumentError)

            lambda { @parser.document_node("mynode", @node, @class) }.should raise_error(Puppet::ParseError, /in file at line 42/)
        end
    end

    describe "when documenting classes" do
        before :each do
            @code = stub_everything 'code'
            @class = stub_everything 'class', :doc => "mydoc", :parent => "parent", :code => @code, :file => "file", :line => 42
            @rdoc_class = stub_everything 'rdoc-class'

            @module = stub_everything 'class'
            @module.stubs(:add_class).returns(@rdoc_class)
            @parser.stubs(:get_class_or_module).returns([@module, "myclass"])
        end

        it "should add a class to the current container" do
            @module.expects(:add_class).with(RDoc::PuppetClass, "myclass", "parent").returns(@rdoc_class)
            @parser.document_class("mynode", @class, @module)
        end

        it "should set the superclass" do
            @rdoc_class.expects(:superclass=).with("parent")
            @parser.document_class("mynode", @class, @module)
        end

        it "should associate the node documentation to the rdoc class" do
            @rdoc_class.expects(:comment=).with("mydoc")
            @parser.document_class("mynode", @class, @module)
        end

        it "should scan for include and require" do
            @parser.expects(:scan_for_include_or_require).with(@rdoc_class, @code)
            @parser.document_class("mynode", @class, @module)
        end

        it "should scan for resources if needed" do
            Puppet.settings.stubs(:[]).with(:document_all).returns(true)
            @parser.expects(:scan_for_resource).with(@rdoc_class, @code)
            @parser.document_class("mynode", @class, @module)
        end

        it "should produce a better error message on unhandled exception" do
            @module.stubs(:add_class).raises(ArgumentError)

            lambda { @parser.document_class("mynode", @class, @module) }.should raise_error(Puppet::ParseError, /in file at line 42/)
        end
    end

    describe "when scanning for includes and requires" do

        def create_stmt(name)
            stmt_value = stub "#{name}_value", :value => "myclass"
            stmt = stub_everything 'stmt', :name => name, :arguments => [stmt_value], :doc => "mydoc"
            stmt.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(false)
            stmt.stubs(:is_a?).with(Puppet::Parser::AST::Function).returns(true)
            stmt
        end

        before(:each) do
            @class = stub_everything 'class'
            @code = stub_everything 'code'
            @code.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(true)
        end

        it "should also scan mono-instruction code" do
            @class.expects(:add_include).with { |i| i.is_a?(RDoc::Include) and i.name == "myclass" and i.comment == "mydoc" }

            @parser.scan_for_include_or_require(@class,create_stmt("include"))
        end

        it "should register recursively includes to the current container" do
            @code.stubs(:children).returns([ create_stmt("include") ])

            @class.expects(:add_include).with { |i| i.is_a?(RDoc::Include) and i.name == "myclass" and i.comment == "mydoc" }
            @parser.scan_for_include_or_require(@class, [@code])
        end

        it "should register requires to the current container" do
            @code.stubs(:children).returns([ create_stmt("require") ])

            @class.expects(:add_require).with { |i| i.is_a?(RDoc::Include) and i.name == "myclass" and i.comment == "mydoc" }
            @parser.scan_for_include_or_require(@class, [@code])
        end
    end

    describe "when scanning for realized virtual resources" do

        def create_stmt
            stmt_value = stub "resource_ref", :to_s => "File[\"/tmp/a\"]"
            stmt = stub_everything 'stmt', :name => "realize", :arguments => [stmt_value], :doc => "mydoc"
            stmt.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(false)
            stmt.stubs(:is_a?).with(Puppet::Parser::AST::Function).returns(true)
            stmt
        end

        before(:each) do
            @class = stub_everything 'class'
            @code = stub_everything 'code'
            @code.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(true)
        end

        it "should also scan mono-instruction code" do
            @class.expects(:add_realize).with { |i| i.is_a?(RDoc::Include) and i.name == "File[\"/tmp/a\"]" and i.comment == "mydoc" }

            @parser.scan_for_realize(@class,create_stmt())
        end

        it "should register recursively includes to the current container" do
            @code.stubs(:children).returns([ create_stmt() ])

            @class.expects(:add_realize).with { |i| i.is_a?(RDoc::Include) and i.name == "File[\"/tmp/a\"]" and i.comment == "mydoc" }
            @parser.scan_for_realize(@class, [@code])
        end
    end

    describe "when scanning for variable definition" do
        before :each do
            @class = stub_everything 'class'

            @stmt = stub_everything 'stmt', :name => "myvar", :value => "myvalue", :doc => "mydoc"
            @stmt.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(false)
            @stmt.stubs(:is_a?).with(Puppet::Parser::AST::VarDef).returns(true)

            @code = stub_everything 'code'
            @code.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(true)
        end

        it "should recursively register variables to the current container" do
            @code.stubs(:children).returns([ @stmt ])

            @class.expects(:add_constant).with { |i| i.is_a?(RDoc::Constant) and i.name == "myvar" and i.comment == "mydoc" }
            @parser.scan_for_vardef(@class, [ @code ])
        end

        it "should also scan mono-instruction code" do
            @class.expects(:add_constant).with { |i| i.is_a?(RDoc::Constant) and i.name == "myvar" and i.comment == "mydoc" }

            @parser.scan_for_vardef(@class, @stmt)
        end
    end

    describe "when scanning for resources" do
        before :each do
            @class = stub_everything 'class'

            param = stub 'params', :children => []
            @stmt = stub_everything 'stmt', :type => "File", :title => "myfile", :doc => "mydoc", :params => param
            @stmt.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(false)
            @stmt.stubs(:is_a?).with(Puppet::Parser::AST::Resource).returns(true)

            @code = stub_everything 'code'
            @code.stubs(:is_a?).with(Puppet::Parser::AST::ASTArray).returns(true)
        end

        it "should register a PuppetResource to the current container" do
            @code.stubs(:children).returns([ @stmt ])

            @class.expects(:add_resource).with { |i| i.is_a?(RDoc::PuppetResource) and i.title == "myfile" and i.comment == "mydoc" }
            @parser.scan_for_resource(@class, [ @code ])
        end

        it "should also scan mono-instruction code" do
            @class.expects(:add_resource).with { |i| i.is_a?(RDoc::PuppetResource) and i.title == "myfile" and i.comment == "mydoc" }

            @parser.scan_for_resource(@class, @stmt)
        end
    end

    describe "when parsing plugins" do
        before :each do
            @container = stub 'container'
        end

        it "should delegate parsing custom facts to parse_facts" do
            @parser = RDoc::Parser.new(@top_level, "module/manifests/lib/puppet/facter/test.rb", nil, Options.instance, RDoc::Stats.new)

            @parser.expects(:parse_fact).with(@container)
            @parser.parse_plugins(@container)
        end

        it "should delegate parsing plugins to parse_plugins" do
            @parser = RDoc::Parser.new(@top_level, "module/manifests/lib/puppet/functions/test.rb", nil, Options.instance, RDoc::Stats.new)

            @parser.expects(:parse_puppet_plugin).with(@container)
            @parser.parse_plugins(@container)
        end
    end

    describe "when parsing plugins" do
        before :each do
            @container = stub_everything 'container'
        end

        it "should add custom functions to the container" do
            File.stubs(:open).yields("# documentation
            module Puppet::Parser::Functions
            	newfunction(:myfunc, :type => :rvalue) do |args|
            		File.dirname(args[0])
            	end
            end".split("\n"))

            @container.expects(:add_plugin).with do |plugin|
                plugin.comment == "documentation\n" #and
                plugin.name == "myfunc"
            end

            @parser.parse_puppet_plugin(@container)
        end

        it "should add custom types to the container" do
            File.stubs(:open).yields("# documentation
            Puppet::Type.newtype(:mytype) do
            end".split("\n"))

            @container.expects(:add_plugin).with do |plugin|
                plugin.comment == "documentation\n" #and
                plugin.name == "mytype"
            end

            @parser.parse_puppet_plugin(@container)
        end
    end

    describe "when parsing facts" do
        before :each do
            @container = stub_everything 'container'
            File.stubs(:open).yields(["# documentation", "Facter.add('myfact') do", "confine :kernel => :linux", "end"])
        end

        it "should add facts to the container" do
            @container.expects(:add_fact).with do |fact|
                fact.comment == "documentation\n" and
                fact.name == "myfact"
            end

            @parser.parse_fact(@container)
        end

        it "should add confine to the parsed facts" do
            ourfact = nil
            @container.expects(:add_fact).with do |fact|
                ourfact = fact
                true
            end

            @parser.parse_fact(@container)
            ourfact.confine.should == { :type => "kernel", :value => ":linux" }
        end
    end
end