diff options
author | Seth Vargo <sethvargo@gmail.com> | 2014-06-25 12:42:32 -0700 |
---|---|---|
committer | Seth Vargo <sethvargo@gmail.com> | 2014-07-11 14:50:35 -0400 |
commit | 8ebbcb0f41c731fa60170ad48727d4941fd39545 (patch) | |
tree | 02306b6af8a0b562a216fa7d73a8c02dfc503821 /spec/unit/lwrp_spec.rb | |
parent | fbbc45178baf6c933380a35dd44278dbc3e4b02f (diff) | |
download | chef-8ebbcb0f41c731fa60170ad48727d4941fd39545.tar.gz |
Delegate DSL method values to their superclass
When inheriting from +Resource::LWRPBase+, the +default_action+ and +actions+
attributes are set as class instance variables. In Ruby's object model, class
instance variables are **not** shared with children. So subclassing a resource
that extended from +Resource::LWRPBase+ would result in some unexplainable
side-effects:
class Chef::Resource::Parent < Chef::Resource::LWRPBase
actions :execute
default_action :execute
end
class Child::Resource::Child < Chef::Resource::Parent
# In here, +actions+ and +default_action+ are **not** inherited so they
# are +nil+!
end
This commit tells resources that inherit from LWRPBase to check their
superclasses for values if they do not have one on their own.
This commit also deprecate +valid_actions+ in favor of just using +actions+ in
the LWRP DSL.
Diffstat (limited to 'spec/unit/lwrp_spec.rb')
-rw-r--r-- | spec/unit/lwrp_spec.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index eaa58bf48c..bffec9cace 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -180,6 +180,67 @@ describe "LWRP" do end end + describe "when inheriting from LWRPBase" do + let(:parent) do + Class.new(Chef::Resource::LWRPBase) do + actions :eat, :sleep + default_action :eat + end + end + + context "when the child does not defined the methods" do + let(:child) do + Class.new(parent) + end + + it "delegates #actions to the parent" do + expect(child.actions).to eq([:eat, :sleep]) + end + + it "delegates #default_action to the parent" do + expect(child.default_action).to eq(:eat) + end + end + + context "when the child does define the methods" do + let(:child) do + Class.new(parent) do + actions :dont_eat, :dont_sleep + default_action :dont_eat + end + end + + it "does not delegate #actions to the parent" do + expect(child.actions).to eq([:dont_eat, :dont_sleep]) + end + + it "does not delegate #default_action to the parent" do + expect(child.default_action).to eq(:dont_eat) + end + end + + context "when actions are already defined" do + let(:child) do + Class.new(parent) do + actions :eat + actions :sleep + actions :drink + end + end + + def raise_if_deprecated! + if Chef::VERSION.split('.').first.to_i > 11 + raise "This test should be removed and the associated code should be removed!" + end + end + + it "ammends actions when they are already defined" do + raise_if_deprecated! + expect(child.actions).to eq([:eat, :sleep, :drink]) + end + end + end + end describe "Lightweight Chef::Provider" do |