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 | |
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.
-rw-r--r-- | lib/chef/resource/lwrp_base.rb | 38 | ||||
-rw-r--r-- | spec/unit/lwrp_spec.rb | 61 |
2 files changed, 92 insertions, 7 deletions
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb index b7bb7d08fd..5e5b05e3ce 100644 --- a/lib/chef/resource/lwrp_base.rb +++ b/lib/chef/resource/lwrp_base.rb @@ -92,19 +92,32 @@ class Chef # Sets the default action def self.default_action(action_name=NULL_ARG) unless action_name.equal?(NULL_ARG) - valid_actions.push(action_name) - @default_action = action_name + action = action_name.to_sym + actions.push(action) unless actions.include?(action) + @default_action = action end - @default_action + + @default_action ||= from_superclass(:default_action) end # Adds +action_names+ to the list of valid actions for this resource. def self.actions(*action_names) - valid_actions.push(*action_names) + if action_names.empty? + @actions || from_superclass(:actions, []).dup + else + # BC-compat way for checking if actions have already been defined + if @actions + @actions.push(*action_names) + else + @actions = action_names + end + end end - def self.valid_actions - @valid_actions ||= [] + # @deprecated + def self.valid_actions(*args) + Chef::Log.warn("`valid_actions' is deprecated, please use actions `instead'!") + actions(*args) end # Set the run context on the class. Used to provide access to the node @@ -125,6 +138,17 @@ class Chef DelayedEvaluator.new(&block) end + private + + # Get the value from the superclass, if it responds, otherwise return + # +nil+. Since class instance variables are **not** inherited upon + # subclassing, this is a required check to ensure Chef pulls the + # +default_action+ and other DSL-y methods when extending LWRP::Base. + def self.from_superclass(m, default = nil) + return default if superclass == Chef::Resource::LWRPBase + superclass.respond_to?(m) ? superclass.send(m) : default + end + # Default initializer. Sets the default action and allowed actions. def initialize(name, run_context=nil) super(name, run_context) @@ -137,7 +161,7 @@ class Chef @resource_name = self.class.resource_name.to_sym @action = self.class.default_action - allowed_actions.push(self.class.valid_actions).flatten! + allowed_actions.push(self.class.actions).flatten! end end 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 |