summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2016-01-29 16:11:28 -0800
committerJohn Keiser <john@johnkeiser.com>2016-02-01 12:23:18 -0800
commit862a85015b841b15541eeb372a642ab50ca210a9 (patch)
tree52e63646efa60a9769e6cb5efb802e32b7ab525c
parentabc81c0eb35a73e15217371c37235d5f13f55df7 (diff)
downloadchef-862a85015b841b15541eeb372a642ab50ca210a9.tar.gz
Fix defaults on property_type
-rw-r--r--lib/chef/property.rb9
-rw-r--r--spec/unit/property_spec.rb15
2 files changed, 22 insertions, 2 deletions
diff --git a/lib/chef/property.rb b/lib/chef/property.rb
index 437ee10147..68d9b80103 100644
--- a/lib/chef/property.rb
+++ b/lib/chef/property.rb
@@ -129,7 +129,7 @@ class Chef
end
def to_s
- "#{name}#{declared_in ? " of resource #{declared_in.resource_name}" : ""}"
+ "#{name || "<property type>"}#{declared_in ? " of resource #{declared_in.resource_name}" : ""}"
end
#
@@ -463,7 +463,12 @@ class Chef
def validate(resource, value)
# If we have no default value, `nil` is never coerced or validated
unless value.nil? && !has_default?
- (resource || Chef::Mixin::ParamsValidate).validate({ name => value }, { name => validation_options })
+ if resource
+ resource.validate({ name => value }, { name => validation_options })
+ else
+ name = self.name || :property_type
+ (resource || Chef::Mixin::ParamsValidate).validate({ name => value }, { name => validation_options })
+ end
end
end
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index ffdbccf019..2ff266f0ca 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -1096,6 +1096,21 @@ describe "Chef::Resource.property" do
/Cannot specify both name_property and name_attribute together on property x of resource chef_resource_property_spec_(\d+)./
end
+ it "property_types validate their defaults" do
+ expect {
+ module PropertySpecPropertyTypes
+ include Chef::Mixin::Properties
+ property_type(is: [:a, :b], default: :c)
+ end
+ }.to raise_error(Chef::Exceptions::DeprecatedFeatureError, /Default value :c is invalid for property <property type>./)
+ expect {
+ module PropertySpecPropertyTypes
+ include Chef::Mixin::Properties
+ property_type(is: [:a, :b], default: :b)
+ end
+ }.not_to raise_error
+ end
+
context "with a custom property type" do
class CustomPropertyType < Chef::Property
end