summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/rdoc_spec.rb
blob: 13e22eec9a23972dfe387ce2c2f1db3c2106330a (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
#!/usr/bin/env ruby

require 'spec_helper'

require 'puppet/util/rdoc'
require 'rdoc/rdoc'

describe Puppet::Util::RDoc do

  describe "when generating RDoc HTML documentation" do
    before :each do
      @rdoc = stub_everything 'rdoc'
      RDoc::RDoc.stubs(:new).returns(@rdoc)
    end

    it "should tell the parser to ignore import" do
      Puppet.expects(:[]=).with(:ignoreimport, true)

      Puppet::Util::RDoc.rdoc("output", [])
    end

    it "should install the Puppet HTML Generator into RDoc generators" do
      Puppet::Util::RDoc.rdoc("output", [])

      RDoc::RDoc::GENERATORS["puppet"].file_name.should == "puppet/util/rdoc/generators/puppet_generator.rb"
    end

    it "should tell RDoc to generate documentation using the Puppet generator" do
      @rdoc.expects(:document).with { |args| args.include?("--fmt") and args.include?("puppet") }

      Puppet::Util::RDoc.rdoc("output", [])
    end

    it "should tell RDoc to be quiet" do
      @rdoc.expects(:document).with { |args| args.include?("--quiet") }

      Puppet::Util::RDoc.rdoc("output", [])
    end

    it "should pass charset to RDoc" do
      @rdoc.expects(:document).with { |args| args.include?("--charset") and args.include?("utf-8") }

      Puppet::Util::RDoc.rdoc("output", [], "utf-8")
    end

    it "should tell RDoc to force updates of indices when RDoc supports it" do
      Options::OptionList.stubs(:options).returns([["--force-update", "-U", 0 ]])
      @rdoc.expects(:document).with { |args| args.include?("--force-update") }

      Puppet::Util::RDoc.rdoc("output", [])
    end

    it "should not tell RDoc to force updates of indices when RDoc doesn't support it" do
      Options::OptionList.stubs(:options).returns([])
      @rdoc.expects(:document).never.with { |args| args.include?("--force-update") }

      Puppet::Util::RDoc.rdoc("output", [])
    end

    it "should tell RDoc to use the given outputdir" do
      @rdoc.expects(:document).with { |args| args.include?("--op") and args.include?("myoutputdir") }

      Puppet::Util::RDoc.rdoc("myoutputdir", [])
    end

    it "should tell RDoc to exclude .pp files under any modules/<mod>/files section" do
      @rdoc.expects(:document).with { |args| args.include?("--exclude") and args.include?("/modules/[^/]*/files/.*\.pp$") }

      Puppet::Util::RDoc.rdoc("myoutputdir", [])
    end

    it "should give all the source directories to RDoc" do
      @rdoc.expects(:document).with { |args| args.include?("sourcedir") }

      Puppet::Util::RDoc.rdoc("output", ["sourcedir"])
    end
  end

  describe "when running a manifest documentation" do
    it "should tell the parser to ignore import" do
      Puppet.expects(:[]=).with(:ignoreimport, true)

      Puppet::Util::RDoc.manifestdoc([])
    end

    it "should use a parser with the correct environment" do
      FileTest.stubs(:file?).returns(true)
      Puppet::Util::RDoc.stubs(:output)

      parser = stub_everything
      Puppet::Parser::Parser.stubs(:new).with{ |env| env.is_a?(Puppet::Node::Environment) }.returns(parser)

      parser.expects(:file=).with("file")
      parser.expects(:parse)

      Puppet::Util::RDoc.manifestdoc(["file"])
    end

    it "should puppet parse all given files" do
      FileTest.stubs(:file?).returns(true)
      Puppet::Util::RDoc.stubs(:output)

      parser = stub_everything
      Puppet::Parser::Parser.stubs(:new).returns(parser)

      parser.expects(:file=).with("file")
      parser.expects(:parse)

      Puppet::Util::RDoc.manifestdoc(["file"])
    end

    it "should call output for each parsed file" do
      FileTest.stubs(:file?).returns(true)

      ast = stub_everything
      parser = stub_everything
      Puppet::Parser::Parser.stubs(:new).returns(parser)
      parser.stubs(:parse).returns(ast)

      Puppet::Util::RDoc.expects(:output).with("file", ast)

      Puppet::Util::RDoc.manifestdoc(["file"])
    end

    describe "when outputing documentation" do
      it "should output doc for ast classes, nodes and definitions in order of increasing line number" do
        byline = sequence('documentation outputs in line order')
        Puppet::Util::RDoc.expects(:puts).with("im a class\n").in_sequence(byline)
        Puppet::Util::RDoc.expects(:puts).with("im a node\n").in_sequence(byline)
        Puppet::Util::RDoc.expects(:puts).with("im a define\n").in_sequence(byline)
        # any other output must fail
        Puppet::Util::RDoc.manifestdoc([my_fixture('basic.pp')])
      end

      it "should output resource documentation if needed" do
        pending "#6634 being fixed"
        Puppet.settings[:document_all] = true
        byline = sequence('documentation outputs in line order')
        Puppet::Util::RDoc.expects(:puts).with("im a class\n").in_sequence(byline)
        Puppet::Util::RDoc.expects(:puts).with("im a node\n").in_sequence(byline)
        Puppet::Util::RDoc.expects(:puts).with("im a define\n").in_sequence(byline)
        Puppet::Util::RDoc.expects(:puts).with("im a resource\n").in_sequence(byline)
        # any other output must fail
        Puppet::Util::RDoc.manifestdoc([my_fixture('basic.pp')])
      end
    end
  end
end