From a5780a9a67efb8ccd82b5007fdb2c2e7c582d295 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Tue, 29 Sep 2015 14:09:03 -0700 Subject: Make sure name_attribute works on derived properties --- lib/chef/property.rb | 8 +++++++- spec/unit/property_spec.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/chef/property.rb b/lib/chef/property.rb index 2b151b350a..b8d8005655 100644 --- a/lib/chef/property.rb +++ b/lib/chef/property.rb @@ -422,7 +422,13 @@ class Chef # @return [Property] The new property type. # def derive(**modified_options) - Property.new(**options.merge(**modified_options)) + # Since name_property and name_attribute are aliases, if you specify either + # one in modified_options it overrides anything in original options. + options = self.options + if modified_options.has_key?(:name_property) || modified_options.has_key?(:name_attribute) + options = options.reject { |k,v| k == :name_attribute || k == :name_property } + end + Property.new(options.merge(modified_options)) end # diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb index 64638d9be9..456da89dcf 100644 --- a/spec/unit/property_spec.rb +++ b/spec/unit/property_spec.rb @@ -107,6 +107,44 @@ describe "Chef::Resource.property" do end end + with_property ":x, name_property: true" do + context "and subclass" do + let(:subresource_class) do + new_resource_name = self.class.new_resource_name + Class.new(resource_class) do + resource_name new_resource_name + end + end + let(:subresource) do + subresource_class.new('blah') + end + + context "with property :x on the subclass" do + before do + subresource_class.class_eval do + property :x + end + end + + it "x is still name_property" do + expect(subresource.x).to eq 'blah' + end + end + + context "with property :x, name_attribute: false on the subclass" do + before do + subresource_class.class_eval do + property :x, name_attribute: false + end + end + + it "x is no longer name_property" do + expect(subresource.x).to be_nil + end + end + end + end + with_property ":x, Integer" do context "and subclass" do let(:subresource_class) do @@ -1031,8 +1069,14 @@ describe "Chef::Resource.property" do end end - it "raises an error if both name_property and name_attribute are specified (even if they are false or nil)" do - expect { resource_class.property :x, :name_property => false, :name_attribute => true }.to raise_error ArgumentError, + it "raises an error if both name_property and name_attribute are specified" do + expect { resource_class.property :x, :name_property => false, :name_attribute => 1 }.to raise_error ArgumentError, + /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./ + expect { resource_class.property :x, :name_property => false, :name_attribute => nil }.to raise_error ArgumentError, + /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./ + expect { resource_class.property :x, :name_property => false, :name_attribute => false }.to raise_error ArgumentError, + /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./ + expect { resource_class.property :x, :name_property => true, :name_attribute => true }.to raise_error ArgumentError, /Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./ end end -- cgit v1.2.1