summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-10-08 20:24:55 -0700
committerJohn Keiser <john@johnkeiser.com>2015-10-08 20:25:47 -0700
commitc9473b61e7d5f6f48598acaef4af0cfdf821f579 (patch)
treec4ab7ac6ddc1748bfb049693fae1f234432d2d04
parent56b3c90bbd7c4657533b99d04dcdc3419dcd472b (diff)
downloadchef-c9473b61e7d5f6f48598acaef4af0cfdf821f579.tar.gz
Accept coercion as a way to accept nil values
-rw-r--r--lib/chef/property.rb5
-rw-r--r--spec/unit/property_spec.rb8
2 files changed, 12 insertions, 1 deletions
diff --git a/lib/chef/property.rb b/lib/chef/property.rb
index c1207d9132..e97d8f9607 100644
--- a/lib/chef/property.rb
+++ b/lib/chef/property.rb
@@ -479,6 +479,8 @@ class Chef
# A type accepts nil explicitly if "is" allows nil, it validates as nil, *and* is not simply
# an empty type.
#
+ # A type is presumed to accept nil if it does coercion (which must handle nil).
+ #
# These examples accept nil explicitly:
# ```ruby
# property :a, [ String, nil ]
@@ -510,7 +512,8 @@ class Chef
#
# @api private
def explicitly_accepts_nil?(resource)
- options.has_key?(:is) && resource.send(:_pv_is, { name => nil }, name, options[:is], raise_error: false)
+ options.has_key?(:coerce) ||
+ (options.has_key?(:is) && resource.send(:_pv_is, { name => nil }, name, options[:is], raise_error: false))
end
def get_value(resource)
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index 9ecf7a4cde..dc06cb3326 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -921,6 +921,9 @@ describe "Chef::Resource.property" do
expect(resource.x 10).to eq "101"
expect(Namer.current_index).to eq 1
end
+ it "does not emit a deprecation warning if set to nil" do
+ expect(resource.x nil).to eq "1"
+ end
it "coercion sets the value (and coercion does not run on get)" do
expect(resource.x 10).to eq "101"
expect(resource.x).to eq "101"
@@ -933,6 +936,11 @@ describe "Chef::Resource.property" do
expect(Namer.current_index).to eq 2
end
end
+ with_property ':x, coerce: proc { |x| x }' do
+ it "does not emit a deprecation warning if set to nil" do
+ expect(resource.x nil).to be_nil
+ end
+ end
with_property ':x, coerce: proc { |x| Namer.next_index; raise "hi" if x == 10; x }, is: proc { |x| Namer.next_index; x != 10 }' do
it "failed coercion fails to set the value" do
resource.x 20