File: doc_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (333 lines) | stat: -rw-r--r-- 10,651 bytes parent folder | download
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
require 'spec_helper'

require 'puppet/application/doc'
require 'puppet/util/reference'
require 'puppet/util/rdoc'

describe Puppet::Application::Doc do
  before :each do
    @doc = Puppet::Application[:doc]
    allow(@doc).to receive(:puts)
    @doc.preinit
    allow(Puppet::Util::Log).to receive(:newdestination)
  end

  it "should declare an other command" do
    expect(@doc).to respond_to(:other)
  end

  it "should declare a rdoc command" do
    expect(@doc).to respond_to(:rdoc)
  end

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

  it "should declare a preinit block" do
    expect(@doc).to respond_to(:preinit)
  end

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

      expect(@doc.options[:references]).to eq([])
    end

    it "should init mode to text" do
      @doc.preinit

      expect(@doc.options[:mode]).to eq(:text)
    end

    it "should init format to to_markdown" do
      @doc.preinit

      expect(@doc.options[:format]).to eq(:to_markdown)
    end
  end

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

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

    it "should store the format if valid" do
      allow(Puppet::Util::Reference).to receive(:method_defined?).with('to_format').and_return(true)

      @doc.handle_format('format')
      expect(@doc.options[:format]).to eq('to_format')
    end

    it "should raise an error if the format is not valid" do
      allow(Puppet::Util::Reference).to receive(:method_defined?).with('to_format').and_return(false)
      expect { @doc.handle_format('format') }.to raise_error(RuntimeError, /Invalid output format/)
    end

    it "should store the mode if valid" do
      allow(Puppet::Util::Reference).to receive(:modes).and_return(double('mode', :include? => true))

      @doc.handle_mode('mode')
      expect(@doc.options[:mode]).to eq(:mode)
    end

    it "should store the mode if :rdoc" do
      allow(Puppet::Util::Reference.modes).to receive(:include?).with('rdoc').and_return(false)

      @doc.handle_mode('rdoc')
      expect(@doc.options[:mode]).to eq(:rdoc)
    end

    it "should raise an error if the mode is not valid" do
      allow(Puppet::Util::Reference.modes).to receive(:include?).with('unknown').and_return(false)
      expect { @doc.handle_mode('unknown') }.to raise_error(RuntimeError, /Invalid output mode/)
    end

    it "should list all references on list and exit" do
      reference = double('reference')
      ref = double('ref')
      allow(Puppet::Util::Reference).to receive(:references).and_return([reference])

      expect(Puppet::Util::Reference).to receive(:reference).with(reference).and_return(ref)
      expect(ref).to receive(:doc)

      expect { @doc.handle_list(nil) }.to exit_with 0
    end

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

      @doc.handle_reference('ref2')

      expect(@doc.options[:references]).to eq([:ref1,:ref2])
    end
  end

  describe "during setup" do
    before :each do
      allow(Puppet::Log).to receive(:newdestination)
      allow(@doc.command_line).to receive(:args).and_return([])
    end

    it "should default to rdoc mode if there are command line arguments" do
      allow(@doc.command_line).to receive(:args).and_return(["1"])
      allow(@doc).to receive(:setup_rdoc)

      @doc.setup
      expect(@doc.options[:mode]).to eq(:rdoc)
    end

    it "should call setup_rdoc in rdoc mode" do
      @doc.options[:mode] = :rdoc

      expect(@doc).to receive(:setup_rdoc)

      @doc.setup
    end

    it "should call setup_reference if not rdoc" do
      @doc.options[:mode] = :test

      expect(@doc).to receive(:setup_reference)

      @doc.setup
    end

    describe "configuring logging" do
      before :each do
        allow(Puppet::Util::Log).to receive(:newdestination)
      end

      describe "with --debug" do
        before do
          @doc.options[:debug] = true
        end

        it "should set log level to debug" do
          @doc.setup
          expect(Puppet::Util::Log.level).to eq(:debug)
        end

        it "should set log destination to console" do
          expect(Puppet::Util::Log).to receive(:newdestination).with(:console)
          @doc.setup
        end
      end

      describe "with --verbose" do
        before do
          @doc.options[:verbose] = true
        end

        it "should set log level to info" do
          @doc.setup
          expect(Puppet::Util::Log.level).to eq(:info)
        end

        it "should set log destination to console" do
          expect(Puppet::Util::Log).to receive(:newdestination).with(:console)
          @doc.setup
        end
      end

      describe "without --debug or --verbose" do
        before do
          @doc.options[:debug] = false
          @doc.options[:verbose] = false
        end

        it "should set log level to warning" do
          @doc.setup
          expect(Puppet::Util::Log.level).to eq(:warning)
        end

        it "should set log destination to console" do
          expect(Puppet::Util::Log).to receive(:newdestination).with(:console)
          @doc.setup
        end
      end
    end

    describe "in non-rdoc mode" do
      it "should get all non-dynamic reference if --all" do
        @doc.options[:all] = true
        static = double('static', :dynamic? => false)
        dynamic = double('dynamic', :dynamic? => true)
        allow(Puppet::Util::Reference).to receive(:reference).with(:static).and_return(static)
        allow(Puppet::Util::Reference).to receive(:reference).with(:dynamic).and_return(dynamic)
        allow(Puppet::Util::Reference).to receive(:references).and_return([:static,:dynamic])

        @doc.setup_reference
        expect(@doc.options[:references]).to eq([:static])
      end

      it "should default to :type if no references" do
        @doc.setup_reference
        expect(@doc.options[:references]).to eq([:type])
      end
    end

    describe "in rdoc mode" do
      describe "when there are unknown args" do
        it "should expand --modulepath if any" do
          @doc.unknown_args = [ { :opt => "--modulepath", :arg => "path" } ]
          allow(Puppet.settings).to receive(:handlearg)

          @doc.setup_rdoc

          expect(@doc.unknown_args[0][:arg]).to eq(File.expand_path('path'))
        end

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

          @doc.setup_rdoc
        end
      end

      it "should operate in master run_mode" do
        expect(@doc.class.run_mode.name).to eq(:master)

        @doc.setup_rdoc
      end
    end
  end

  describe "when running" do
    describe "in rdoc mode" do
      include PuppetSpec::Files

      let(:envdir) { tmpdir('env') }
      let(:modules) { File.join(envdir, "modules") }
      let(:modules2) { File.join(envdir, "modules2") }
      let(:manifests) { File.join(envdir, "manifests") }

      before :each do
        @doc.manifest = false
        allow(Puppet).to receive(:info)
        Puppet[:trace] = false
        Puppet[:modulepath] = modules
        Puppet[:manifest] = manifests
        @doc.options[:all] = false
        @doc.options[:outputdir] = 'doc'
        @doc.options[:charset] = nil
        allow(Puppet.settings).to receive(:define_settings)
        allow(Puppet::Util::RDoc).to receive(:rdoc)
        allow(@doc.command_line).to receive(:args).and_return([])
      end

      around(:each) do |example|
        FileUtils.mkdir_p(modules)
        env = Puppet::Node::Environment.create(Puppet[:environment].to_sym, [modules], "#{manifests}/site.pp")
        Puppet.override({:environments => Puppet::Environments::Static.new(env), :current_environment => env}) do
          example.run
        end
      end

      it "should set document_all on --all" do
        @doc.options[:all] = true
        expect(Puppet.settings).to receive(:[]=).with(:document_all, true)

        expect { @doc.rdoc }.to exit_with(0)
      end

      it "should call Puppet::Util::RDoc.rdoc in full mode" do
        expect(Puppet::Util::RDoc).to receive(:rdoc).with('doc', [modules, manifests], nil)
        expect { @doc.rdoc }.to exit_with(0)
      end

      it "should call Puppet::Util::RDoc.rdoc with a charset if --charset has been provided" do
        @doc.options[:charset] = 'utf-8'
        expect(Puppet::Util::RDoc).to receive(:rdoc).with('doc', [modules, manifests], "utf-8")
        expect { @doc.rdoc }.to exit_with(0)
      end

      it "should call Puppet::Util::RDoc.rdoc in full mode with outputdir set to doc if no --outputdir" do
        @doc.options[:outputdir] = false
        expect(Puppet::Util::RDoc).to receive(:rdoc).with('doc', [modules, manifests], nil)
        expect { @doc.rdoc }.to exit_with(0)
      end

      it "should call Puppet::Util::RDoc.manifestdoc in manifest mode" do
        @doc.manifest = true
        expect(Puppet::Util::RDoc).to receive(:manifestdoc)
        expect { @doc.rdoc }.to exit_with(0)
      end

      it "should get modulepath and manifest values from the environment" do
        FileUtils.mkdir_p(modules)
        FileUtils.mkdir_p(modules2)
        env = Puppet::Node::Environment.create(Puppet[:environment].to_sym,
          [modules, modules2],
          "envmanifests/site.pp")
        Puppet.override({:environments => Puppet::Environments::Static.new(env), :current_environment => env}) do
           allow(Puppet::Util::RDoc).to receive(:rdoc).with('doc', [modules.to_s, modules2.to_s, env.manifest.to_s], nil)
          expect { @doc.rdoc }.to exit_with(0)
        end
      end
    end

    describe "in the other modes" do
      it "should get reference in given format" do
        reference = double('reference')
        @doc.options[:mode] = :none
        @doc.options[:references] = [:ref]
        expect(Puppet::Util::Reference).to receive(:reference).with(:ref).and_return(reference)
        @doc.options[:format] = :format
        allow(@doc).to receive(:exit)

        expect(reference).to receive(:send).with(:format, anything).and_return('doc')
        @doc.other
      end
    end
  end
end