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-18 15:26:09 -0700
commitea47243253b7b093f635beaf81a01503e52f51bd (patch)
tree259e786179612e717dc9529e57eda38b582bd77d
parent19e3d2f39301bf543f472e35f111d51b2708ebc6 (diff)
downloadchef-ea47243253b7b093f635beaf81a01503e52f51bd.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 da53b829ec..1351dda24b 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 1922bea149..0ed86026bb 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