diff options
author | Noah Kantrowitz <noah@coderanger.net> | 2015-06-26 15:01:23 -0700 |
---|---|---|
committer | Noah Kantrowitz <noah@coderanger.net> | 2015-06-29 10:51:58 -0700 |
commit | 116ca7f695fd5ec20bf24cb4114e9b6984a2b5b7 (patch) | |
tree | f8feb5ecc289e89acd5081ff29dc928743ba1613 | |
parent | 1289161dd9b53a5e3c9f6c601f6552c452a3e5ad (diff) | |
download | chef-116ca7f695fd5ec20bf24cb4114e9b6984a2b5b7.tar.gz |
Rework Resource#action to match the 12.3 API.
This means it always coerces to an Array. Also ensures that
Resource#action= goes through the same validation.
Fixes #3604.
-rw-r--r-- | lib/chef/resource.rb | 14 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 38 |
2 files changed, 44 insertions, 8 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 8d2532dac4..353eaddfa2 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -169,26 +169,24 @@ class Chef # @param arg [Array[Symbol], Symbol] A list of actions (e.g. `:create`) # @return [Array[Symbol]] the list of actions. # - attr_accessor :action def action(arg=nil) if arg - if arg.is_a?(Array) - arg = arg.map { |a| a.to_sym } - else - arg = arg.to_sym - end - Array(arg).each do |action| + arg = Array(arg).map(&:to_sym) + arg.each do |action| validate( { action: action }, { action: { kind_of: Symbol, equal_to: allowed_actions } } ) end - self.action = arg + @action = arg else @action end end + # Alias for normal assigment syntax. + alias_method :action=, :action + # # Sets up a notification that will run a particular action on another resource # if and when *this* resource is updated by an action. diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 8b0baff921..44fe1e90a3 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -977,4 +977,42 @@ describe Chef::Resource do end end + + describe "#action" do + let(:resource_class) do + Class.new(described_class) do + allowed_actions(%i{one two}) + end + end + let(:resource) { resource_class.new('test', nil) } + subject { resource.action } + + context "with a symbol action" do + before { resource.action(:one) } + it { is_expected.to eq [:one] } + end + + context "with a string action" do + before { resource.action('two') } + it { is_expected.to eq [:two] } + end + + context "with an array action" do + before { resource.action([:two, :one]) } + it { is_expected.to eq [:two, :one] } + end + + context "with an assignment" do + before { resource.action = :one } + it { is_expected.to eq [:one] } + end + + context "with an invalid action" do + it { expect { resource.action(:three) }.to raise_error Chef::Exceptions::ValidationFailed } + end + + context "with an invalid assignment action" do + it { expect { resource.action = :three }.to raise_error Chef::Exceptions::ValidationFailed } + end + end end |