summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-07-31 17:24:07 -0600
committerJohn Keiser <john@johnkeiser.com>2015-08-03 13:42:10 -0600
commitfe6676bab51390429674b0ecd4670924ffb09cc5 (patch)
tree87a3f38d63663164ba3fab00365de873c9cd2a1b
parent0e3fda07d99269a35009673eafec573d0db52000 (diff)
downloadchef-fe6676bab51390429674b0ecd4670924ffb09cc5.tar.gz
Remove freeze of defaults, add warning for array/hash constant defaults
-rw-r--r--lib/chef/property.rb5
-rw-r--r--lib/chef/resource.rb4
-rw-r--r--spec/unit/property_spec.rb12
3 files changed, 11 insertions, 10 deletions
diff --git a/lib/chef/property.rb b/lib/chef/property.rb
index 6579eb06a8..1a3b8ec72c 100644
--- a/lib/chef/property.rb
+++ b/lib/chef/property.rb
@@ -89,9 +89,6 @@ class Chef
options[:name_property] = options.delete(:name_attribute) if options.has_key?(:name_attribute) && !options.has_key?(:name_property)
@options = options
- if options.has_key?(:default)
- options[:default] = options[:default].freeze
- end
options[:name] = options[:name].to_sym if options[:name]
options[:instance_variable_name] = options[:instance_variable_name].to_sym if options[:instance_variable_name]
end
@@ -285,7 +282,7 @@ class Chef
# If the value is mutable (non-frozen), we set it on the instance
# so that people can mutate it. (All constant default values are
# frozen.)
- if !value.frozen?
+ if !value.frozen? && !value.nil?
set_value(resource, value)
end
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index b9c074117f..60ec6bde93 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -808,6 +808,10 @@ class Chef
property = property_type(**options)
end
+ if !options[:default].frozen? && (options[:default].is_a?(Array) || options[:default].is_a?(Hash))
+ Chef::Log.warn("Property #{self}.#{name} has an array or hash default (#{options[:default]}). This means that if one resource modifies or appends to it, all other resources of the same type will also see the changes. Either freeze the constant with `.freeze` to prevent appending, or use lazy { #{options[:default].inspect} }.")
+ end
+
local_properties = properties(false)
local_properties[name] = property
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index 09f7e52329..55eaead9ba 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -474,12 +474,12 @@ describe "Chef::Resource.property" do
it "Multiple instances of x receive the exact same value" do
expect(resource.x.object_id).to eq(resource_class.new('blah2').x.object_id)
end
- it "The default value is frozen" do
- expect(resource.x).to be_frozen
- end
- it "The default value cannot be written to" do
- expect { resource.x[:a] = 1 }.to raise_error RuntimeError, /frozen/
- end
+ end
+
+ it "when a property is declared with default: {}, a warning is issued" do
+ expect(Chef::Log).to receive(:warn).with(match(/^Property .+\.x has an array or hash default \(\{\}\)\. This means that if one resource modifies or appends to it, all other resources of the same type will also see the changes\. Either freeze the constant with \`\.freeze\` to prevent appending, or use lazy \{ \{\} \}\.$/))
+ resource_class.class_eval("property :x, default: {}", __FILE__, __LINE__)
+ expect(resource.x).to eq({})
end
with_property ':x, default: lazy { {} }' do