summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-09 12:11:44 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-23 16:29:44 -0700
commit3d55871cd82093d8f837738c90b3093dbed45e06 (patch)
treee73a84a06e1bb725909f5213811b185bb65471e2
parentee7b601cc3be01e87d59752cd8191c737f8f44bf (diff)
downloadchef-3d55871cd82093d8f837738c90b3093dbed45e06.tar.gz
Allow property specialization from superclass
-rw-r--r--lib/chef/resource.rb9
-rw-r--r--spec/unit/property_spec.rb34
2 files changed, 35 insertions, 8 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 978e9d2635..94f50d5df5 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -197,6 +197,7 @@ class Chef
def self.property(name, type=NOT_PASSED, **options)
name = name.to_sym
+ # Combine the type with "is"
if type != NOT_PASSED
if options[:is]
options[:is] = ([ type ] + [ options[:is] ]).flatten(1)
@@ -206,7 +207,13 @@ class Chef
end
local_properties = properties(false)
- local_properties[name] = Property.new(name: name, declared_in: self, **options)
+
+ # Inherit from the current / parent property if type is not passed
+ if type == NOT_PASSED && properties[name]
+ local_properties[name] = properties[name].specialize(declared_in: self, **options)
+ else
+ local_properties[name] = Property.new(name: name, declared_in: self, **options)
+ end
begin
class_eval <<-EOM, __FILE__, __LINE__+1
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index a35c223ddf..f24e44e4b1 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -175,13 +175,33 @@ describe "Chef::Resource.property" do
expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x]
end
- it "x's validation is overwritten" do
- expect(subresource.x 'ohno').to eq 'ohno'
- expect(subresource.x).to eq 'ohno'
+ it "x's validation is inherited" do
+ expect { subresource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
end
+ end
- it "the superclass's validation for x is still there" do
- expect { resource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
+ context "with property :x, default: 80 on the subclass" do
+ before do
+ subresource_class.class_eval do
+ property :x, default: 80
+ end
+ end
+
+ it "x is still there" do
+ expect(subresource.x 10).to eq 10
+ expect(subresource.x).to eq 10
+ expect(subresource.x = 20).to eq 20
+ expect(subresource.x).to eq 20
+ expect(subresource_class.properties[:x]).not_to be_nil
+ expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x]
+ end
+
+ it "x defaults to 80" do
+ expect(subresource.x).to eq 80
+ end
+
+ it "x's validation is inherited" do
+ expect { subresource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
end
end
@@ -241,9 +261,9 @@ describe "Chef::Resource.property" do
resource.x lazy { 10 }
expect(resource.property_is_set?(:x)).to be_truthy
end
- it "when x is retrieved, property_is_set?(:x) is false" do
+ it "when x is retrieved, property_is_set?(:x) is true" do
resource.x
- expect(resource.property_is_set?(:x)).to be_falsey
+ expect(resource.property_is_set?(:x)).to be_truthy
end
end