~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to spec/unit/interface/action_builder_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env rspec
 
2
require 'spec_helper'
 
3
require 'puppet/interface/action_builder'
 
4
require 'puppet/network/format_handler'
 
5
 
 
6
describe Puppet::Interface::ActionBuilder do
 
7
  let :face do Puppet::Interface.new(:puppet_interface_actionbuilder, '0.0.1') end
 
8
 
 
9
  it "should build an action" do
 
10
    action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
11
      when_invoked do |options| true end
 
12
    end
 
13
    action.should be_a(Puppet::Interface::Action)
 
14
    action.name.should == :foo
 
15
  end
 
16
 
 
17
  it "should define a method on the face which invokes the action" do
 
18
    face = Puppet::Interface.new(:action_builder_test_interface, '0.0.1') do
 
19
      action(:foo) { when_invoked { |options| "invoked the method" } }
 
20
    end
 
21
 
 
22
    face.foo.should == "invoked the method"
 
23
  end
 
24
 
 
25
  it "should require a block" do
 
26
    expect { Puppet::Interface::ActionBuilder.build(nil, :foo) }.
 
27
      should raise_error("Action :foo must specify a block")
 
28
  end
 
29
 
 
30
  it "should require an invocation block" do
 
31
    expect {
 
32
      Puppet::Interface::ActionBuilder.build(face, :foo) {}
 
33
    }.to raise_error(/actions need to know what to do when_invoked; please add the block/)
 
34
  end
 
35
 
 
36
  describe "when handling options" do
 
37
    it "should have a #option DSL function" do
 
38
      method = nil
 
39
      Puppet::Interface::ActionBuilder.build(face, :foo) do
 
40
        when_invoked do |options| true end
 
41
        method = self.method(:option)
 
42
      end
 
43
      method.should be_an_instance_of Method
 
44
    end
 
45
 
 
46
    it "should define an option without a block" do
 
47
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
48
        when_invoked do |options| true end
 
49
        option "--bar"
 
50
      end
 
51
      action.should be_option :bar
 
52
    end
 
53
 
 
54
    it "should accept an empty block" do
 
55
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
56
        when_invoked do |options| true end
 
57
        option "--bar" do
 
58
          # This space left deliberately blank.
 
59
        end
 
60
      end
 
61
      action.should be_option :bar
 
62
    end
 
63
  end
 
64
 
 
65
  context "inline documentation" do
 
66
    it "should set the summary" do
 
67
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
68
        when_invoked do |options| true end
 
69
        summary "this is some text"
 
70
      end
 
71
      action.summary.should == "this is some text"
 
72
    end
 
73
  end
 
74
 
 
75
  context "action defaulting" do
 
76
    it "should set the default to true" do
 
77
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
78
        when_invoked do |options| true end
 
79
        default
 
80
      end
 
81
      action.default.should be_true
 
82
    end
 
83
 
 
84
    it "should not be default by, er, default. *cough*" do
 
85
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
86
        when_invoked do |options| true end
 
87
      end
 
88
      action.default.should be_false
 
89
    end
 
90
  end
 
91
 
 
92
  context "#when_rendering" do
 
93
    it "should fail if no rendering format is given" do
 
94
      expect {
 
95
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
96
          when_invoked do |options| true end
 
97
          when_rendering do true end
 
98
        end
 
99
      }.to raise_error ArgumentError, /must give a rendering format to when_rendering/
 
100
    end
 
101
 
 
102
    it "should fail if no block is given" do
 
103
      expect {
 
104
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
105
          when_invoked do |options| true end
 
106
          when_rendering :json
 
107
        end
 
108
      }.to raise_error ArgumentError, /must give a block to when_rendering/
 
109
    end
 
110
 
 
111
    it "should fail if the block takes no arguments" do
 
112
      expect {
 
113
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
114
          when_invoked do |options| true end
 
115
          when_rendering :json do true end
 
116
        end
 
117
      }.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/
 
118
    end
 
119
 
 
120
    it "should fail if the block takes more than one argument" do
 
121
      expect {
 
122
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
123
          when_invoked do |options| true end
 
124
          when_rendering :json do |a, b, c| true end
 
125
        end
 
126
      }.to raise_error ArgumentError, /when_rendering methods take one argument, the result, not/
 
127
    end
 
128
 
 
129
    it "should fail if the block takes a variable number of arguments" do
 
130
      expect {
 
131
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
132
          when_invoked do |options| true end
 
133
          when_rendering :json do |*args| true end
 
134
        end
 
135
      }.to raise_error(ArgumentError,
 
136
                       /when_rendering methods take one argument, the result, not/)
 
137
    end
 
138
 
 
139
    it "should stash a rendering block" do
 
140
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
141
        when_invoked do |options| true end
 
142
        when_rendering :json do |a| true end
 
143
      end
 
144
      action.when_rendering(:json).should be_an_instance_of Method
 
145
    end
 
146
 
 
147
    it "should fail if you try to set the same rendering twice" do
 
148
      expect {
 
149
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
150
          when_invoked do |options| true end
 
151
          when_rendering :json do |a| true end
 
152
          when_rendering :json do |a| true end
 
153
        end
 
154
      }.to raise_error ArgumentError, /You can't define a rendering method for json twice/
 
155
    end
 
156
 
 
157
    it "should work if you set two different renderings" do
 
158
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
159
        when_invoked do |options| true end
 
160
        when_rendering :json do |a| true end
 
161
        when_rendering :yaml do |a| true end
 
162
      end
 
163
      action.when_rendering(:json).should be_an_instance_of Method
 
164
      action.when_rendering(:yaml).should be_an_instance_of Method
 
165
    end
 
166
 
 
167
    it "should be bound to the face when called" do
 
168
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
169
        when_invoked do |options| true end
 
170
        when_rendering :json do |a| self end
 
171
      end
 
172
      action.when_rendering(:json).call(true).should == face
 
173
    end
 
174
  end
 
175
 
 
176
  context "#render_as" do
 
177
    it "should default to nil (eg: based on context)" do
 
178
      action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
179
        when_invoked do |options| true end
 
180
      end
 
181
      action.render_as.should be_nil
 
182
    end
 
183
 
 
184
    it "should fail if not rendering format is given" do
 
185
      expect {
 
186
        Puppet::Interface::ActionBuilder.build(face, :foo) do
 
187
          when_invoked do |options| true end
 
188
          render_as
 
189
        end
 
190
      }.to raise_error ArgumentError, /must give a rendering format to render_as/
 
191
    end
 
192
 
 
193
    Puppet::Network::FormatHandler.formats.each do |name|
 
194
      it "should accept #{name.inspect} format" do
 
195
        action = Puppet::Interface::ActionBuilder.build(face, :foo) do
 
196
          when_invoked do |options| true end
 
197
          render_as name
 
198
        end
 
199
        action.render_as.should == name
 
200
      end
 
201
    end
 
202
 
 
203
    [:if_you_define_this_format_you_frighten_me, "json", 12].each do |input|
 
204
      it "should fail if given #{input.inspect}" do
 
205
        expect {
 
206
          Puppet::Interface::ActionBuilder.build(face, :foo) do
 
207
            when_invoked do |options| true end
 
208
            render_as input
 
209
          end
 
210
        }.to raise_error ArgumentError, /#{input.inspect} is not a valid rendering format/
 
211
      end
 
212
    end
 
213
  end
 
214
end