diff options
-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 |