File: option_builder_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 (86 lines) | stat: -rw-r--r-- 2,805 bytes parent folder | download | duplicates (6)
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
require 'spec_helper'
require 'puppet/interface'

describe Puppet::Interface::OptionBuilder do
  let :face do Puppet::Interface.new(:option_builder_testing, '0.0.1') end

  it "should be able to construct an option without a block" do
    expect(Puppet::Interface::OptionBuilder.build(face, "--foo")).
      to be_an_instance_of Puppet::Interface::Option
  end

  Puppet.settings.each do |name, value|
    it "should fail when option #{name.inspect} already exists in puppet core" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--#{name}")
      end.to raise_error ArgumentError, /already defined/
    end
  end

  it "should work with an empty block" do
    option = Puppet::Interface::OptionBuilder.build(face, "--foo") do
      # This block deliberately left blank.
    end

    expect(option).to be_an_instance_of Puppet::Interface::Option
  end

  [:description, :summary].each do |doc|
    it "should support #{doc} declarations" do
      text = "this is the #{doc}"
      option = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        self.send doc, text
      end
      expect(option).to be_an_instance_of Puppet::Interface::Option
      expect(option.send(doc)).to eq(text)
    end
  end

  context "before_action hook" do
    it "should support a before_action hook" do
      option = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        before_action do |a,b,c| :whatever end
      end
      expect(option.before_action).to be_an_instance_of UnboundMethod
    end

    it "should fail if the hook block takes too few arguments" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--foo") do
          before_action do |one, two| true end
        end
      end.to raise_error ArgumentError, /takes three arguments/
    end

    it "should fail if the hook block takes too many arguments" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--foo") do
          before_action do |one, two, three, four| true end
        end
      end.to raise_error ArgumentError, /takes three arguments/
    end

    it "should fail if the hook block takes a variable number of arguments" do
      expect do
        Puppet::Interface::OptionBuilder.build(face, "--foo") do
          before_action do |*blah| true end
        end
      end.to raise_error ArgumentError, /takes three arguments/
    end

    it "should support simple required declarations" do
      opt = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        required
      end
      expect(opt).to be_required
    end

    it "should support arguments to the required property" do
      opt = Puppet::Interface::OptionBuilder.build(face, "--foo") do
        required(false)
      end
      expect(opt).not_to be_required
    end
    
  end
end