4
# This is entirely an internal class for Interface, so we have to load it instead of our class.
5
require 'puppet/interface'
8
class ActionManagerTester
9
include Puppet::Interface::ActionManager
12
describe Puppet::Interface::ActionManager do
13
subject { ActionManagerTester.new }
15
describe "when included in a class" do
16
it "should be able to define an action" do
17
subject.action(:foo) do
18
when_invoked { |options| "something "}
22
it "should be able to define a 'script' style action" do
23
subject.script :bar do |options|
24
"a bar is where beer is found"
28
it "should be able to list defined actions" do
29
subject.action(:foo) do
30
when_invoked { |options| "something" }
32
subject.action(:bar) do
33
when_invoked { |options| "something" }
36
subject.actions.should =~ [:foo, :bar]
39
it "should list 'script' actions" do
40
subject.script :foo do |options| "foo" end
41
subject.actions.should =~ [:foo]
44
it "should list both script and normal actions" do
45
subject.action :foo do
46
when_invoked do |options| "foo" end
48
subject.script :bar do |options| "a bar is where beer is found" end
50
subject.actions.should =~ [:foo, :bar]
53
it "should be able to indicate when an action is defined" do
54
subject.action(:foo) do
55
when_invoked { |options| "something" }
58
subject.should be_action(:foo)
61
it "should indicate an action is defined for script actions" do
62
subject.script :foo do |options| "foo" end
63
subject.should be_action :foo
66
it "should correctly treat action names specified as strings" do
67
subject.action(:foo) do
68
when_invoked { |options| "something" }
71
subject.should be_action("foo")
75
describe "when used to extend a class" do
76
subject { Class.new.extend(Puppet::Interface::ActionManager) }
78
it "should be able to define an action" do
79
subject.action(:foo) do
80
when_invoked { |options| "something "}
84
it "should be able to list defined actions" do
85
subject.action(:foo) do
86
when_invoked { |options| "something" }
88
subject.action(:bar) do
89
when_invoked { |options| "something" }
92
subject.actions.should include(:bar)
93
subject.actions.should include(:foo)
96
it "should be able to indicate when an action is defined" do
97
subject.action(:foo) { when_invoked do |options| true end }
98
subject.should be_action(:foo)
102
describe "when used both at the class and instance level" do
104
@klass = Class.new do
105
include Puppet::Interface::ActionManager
106
extend Puppet::Interface::ActionManager
107
def __invoke_decorations(*args) true end
110
@instance = @klass.new
113
it "should be able to define an action at the class level" do
114
@klass.action(:foo) do
115
when_invoked { |options| "something "}
119
it "should create an instance method when an action is defined at the class level" do
120
@klass.action(:foo) do
121
when_invoked { |options| "something" }
123
@instance.foo.should == "something"
126
it "should be able to define an action at the instance level" do
127
@instance.action(:foo) do
128
when_invoked { |options| "something "}
132
it "should create an instance method when an action is defined at the instance level" do
133
@instance.action(:foo) do
134
when_invoked { |options| "something" }
136
@instance.foo.should == "something"
139
it "should be able to list actions defined at the class level" do
140
@klass.action(:foo) do
141
when_invoked { |options| "something" }
143
@klass.action(:bar) do
144
when_invoked { |options| "something" }
147
@klass.actions.should include(:bar)
148
@klass.actions.should include(:foo)
151
it "should be able to list actions defined at the instance level" do
152
@instance.action(:foo) do
153
when_invoked { |options| "something" }
155
@instance.action(:bar) do
156
when_invoked { |options| "something" }
159
@instance.actions.should include(:bar)
160
@instance.actions.should include(:foo)
163
it "should be able to list actions defined at both instance and class level" do
164
@klass.action(:foo) do
165
when_invoked { |options| "something" }
167
@instance.action(:bar) do
168
when_invoked { |options| "something" }
171
@instance.actions.should include(:bar)
172
@instance.actions.should include(:foo)
175
it "should be able to indicate when an action is defined at the class level" do
176
@klass.action(:foo) do
177
when_invoked { |options| "something" }
179
@instance.should be_action(:foo)
182
it "should be able to indicate when an action is defined at the instance level" do
183
@klass.action(:foo) do
184
when_invoked { |options| "something" }
186
@instance.should be_action(:foo)
189
context "with actions defined in superclass" do
191
@subclass = Class.new(@klass)
192
@instance = @subclass.new
194
@klass.action(:parent) do
195
when_invoked { |options| "a" }
197
@subclass.action(:sub) do
198
when_invoked { |options| "a" }
200
@instance.action(:instance) do
201
when_invoked { |options| "a" }
205
it "should list actions defined in superclasses" do
206
@instance.should be_action(:parent)
207
@instance.should be_action(:sub)
208
@instance.should be_action(:instance)
211
it "should list inherited actions" do
212
@instance.actions.should =~ [:instance, :parent, :sub]
215
it "should not duplicate instance actions after fetching them (#7699)" do
216
@instance.actions.should =~ [:instance, :parent, :sub]
217
@instance.get_action(:instance)
218
@instance.actions.should =~ [:instance, :parent, :sub]
221
it "should not duplicate subclass actions after fetching them (#7699)" do
222
@instance.actions.should =~ [:instance, :parent, :sub]
223
@instance.get_action(:sub)
224
@instance.actions.should =~ [:instance, :parent, :sub]
227
it "should not duplicate superclass actions after fetching them (#7699)" do
228
@instance.actions.should =~ [:instance, :parent, :sub]
229
@instance.get_action(:parent)
230
@instance.actions.should =~ [:instance, :parent, :sub]
234
it "should create an instance method when an action is defined in a superclass" do
235
@subclass = Class.new(@klass)
236
@instance = @subclass.new
238
@klass.action(:foo) do
239
when_invoked { |options| "something" }
241
@instance.foo.should == "something"
245
describe "#action" do
246
it 'should add an action' do
247
subject.action(:foo) { when_invoked do |options| true end }
248
subject.get_action(:foo).should be_a Puppet::Interface::Action
251
it 'should support default actions' do
252
subject.action(:foo) { when_invoked do |options| true end; default }
253
subject.get_default_action.should == subject.get_action(:foo)
256
it 'should not support more than one default action' do
257
subject.action(:foo) { when_invoked do |options| true end; default }
258
expect { subject.action(:bar) {
259
when_invoked do |options| true end
262
}.should raise_error /cannot both be default/
266
describe "#get_action" do
268
parent_class = Class.new(Puppet::Interface)
269
parent_class.action(:foo) { when_invoked do |options| true end }
273
it "should check that we can find inherited actions when we are a class" do
274
Class.new(parent_class).get_action(:foo).name.should == :foo
277
it "should check that we can find inherited actions when we are an instance" do
278
instance = parent_class.new(:foo, '0.0.0')
279
instance.get_action(:foo).name.should == :foo