summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-04 10:43:38 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-23 15:23:02 -0700
commitf47252a75191d96e169abdc7c9d84aa6e50df708 (patch)
tree45dcb92ca96ff5b1881bc227236e81a86a3e946a
parent2d7f1282c63ca6480f363603608e9d3cdf38116e (diff)
downloadchef-f47252a75191d96e169abdc7c9d84aa6e50df708.tar.gz
Add property_is_set?
-rw-r--r--lib/chef/mixin/params_validate.rb27
-rw-r--r--lib/chef/resource.rb9
-rw-r--r--spec/unit/property/state_spec.rb56
-rw-r--r--spec/unit/property_spec.rb188
4 files changed, 145 insertions, 135 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb
index 142c582cdd..ebab964b74 100644
--- a/lib/chef/mixin/params_validate.rb
+++ b/lib/chef/mixin/params_validate.rb
@@ -84,7 +84,8 @@ class Chef
end
def set_or_return(symbol, value, validation)
- iv_symbol = "@#{symbol.to_s}".to_sym
+ symbol = symbol.to_sym
+ iv_symbol = :"@#{symbol}"
# If the user passed NOT_PASSED, or passed nil, then this is a get.
if value == NOT_PASSED || (value.nil? && !explicitly_allows_nil?(symbol, validation))
@@ -98,16 +99,22 @@ class Chef
# Get the default value
else
- value = validate({}, { symbol => validation })[symbol]
- # Handle the case where the "default" was a DelayedEvaluator. In
- # this case, the block yields an optional parameter of +self+,
- # which is the equivalent of "new_resource"
- if value.is_a?(DelayedEvaluator)
- value = value.call(self)
- end
+ validated = validate({}, { symbol => validation })
+ if validated.has_key?(symbol)
+ value = validated[symbol]
- # Defaults are presently "stickily" set on the instance
- self.instance_variable_set(iv_symbol, value)
+ # Handle the case where the "default" was a DelayedEvaluator. In
+ # this case, the block yields an optional parameter of +self+,
+ # which is the equivalent of "new_resource"
+ if value.is_a?(DelayedEvaluator)
+ value = value.call(self)
+ end
+
+ # Defaults are presently "stickily" set on the instance
+ self.instance_variable_set(iv_symbol, value)
+ else
+ value = nil
+ end
end
# Set the value
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 9496656705..8d2532dac4 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -791,6 +791,15 @@ class Chef
end
#
+ # Whether this property has been set (or whether it has a default that has
+ # been retrieved).
+ #
+ def property_is_set?(name)
+ name = name.to_sym
+ instance_variable_defined?("@#{name}")
+ end
+
+ #
# Create a lazy value for assignment to a default value.
#
# @param block The block to run when the value is retrieved.
diff --git a/spec/unit/property/state_spec.rb b/spec/unit/property/state_spec.rb
index d5c0893cfd..80ebe01a41 100644
--- a/spec/unit/property/state_spec.rb
+++ b/spec/unit/property/state_spec.rb
@@ -194,45 +194,39 @@ describe "Chef::Resource#identity and #state" do
before do
resource_class.class_eval do
def custom_property
- @blarghle*3
+ @blarghle ? @blarghle*3 : nil
end
def custom_property=(x)
@blarghle = x*2
end
end
+ end
- context "And identity_attr :custom_property" do
- before do
- resource_class.class_eval do
- identity_attr :custom_property
- end
+ context "And identity_attr :custom_property" do
+ before do
+ resource_class.class_eval do
+ identity_attr :custom_property
end
+ end
- it "identity_attr comes back as :custom_property" do
- # expect(resource_class.properties[:custom_property].identity?).to be_truthy
- expect(resource_class.identity_attr).to eq :custom_property
- end
- it "custom_property becomes part of desired_state" do
- # expect(resource_class.properties[:custom_property].desired_state?).to be_truthy
- expect(resource_class.state_attrs).to eq [ :custom_property ]
- end
- it "identity_attr does not change custom_property's getter or setter" do
- expect(resource.custom_property = 1).to eq 2
- expect(resource.custom_property).to eq 6
- end
- it "custom_property is returned as the identity" do
- expect(resource_class.identity_attr).to
- expect(resource.identity).to be_nil
- resource.custom_property = 1
- expect(resource.identity).to eq 6
- end
- it "custom_property is part of desired state" do
- resource.custom_property = 1
- expect(resource.state).to eq({ custom_property: 6 })
- end
- it "property_is_set?(:custom_property) returns true even if it hasn't been set" do
- expect(resource.property_is_set?(:custom_property)).to be_truthy
- end
+ it "identity_attr comes back as :custom_property" do
+ # expect(resource_class.properties[:custom_property].identity?).to be_truthy
+ expect(resource_class.identity_attr).to eq :custom_property
+ end
+ # it "custom_property becomes part of desired_state" do
+ # resource.custom_property = 1
+ # expect(resource.state).to eq({ custom_property: 6 })
+ # expect(resource_class.properties[:custom_property].desired_state?).to be_truthy
+ # expect(resource_class.state_attrs).to eq [ :custom_property ]
+ # end
+ it "identity_attr does not change custom_property's getter or setter" do
+ resource.custom_property = 1
+ expect(resource.custom_property).to eq 6
+ end
+ it "custom_property is returned as the identity" do
+ expect(resource.identity).to be_nil
+ resource.custom_property = 1
+ expect(resource.identity).to eq 6
end
end
end
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index 152bad9bc0..83ce3762c7 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -212,105 +212,105 @@ describe "Chef::Resource.property" do
end
end
- # context "Chef::Resource::PropertyType#property_is_set?" do
- # it "when a resource is newly created, property_is_set?(:name) is true" do
- # expect(resource.property_is_set?(:name)).to be_truthy
- # end
- #
- # it "when referencing an undefined property, property_is_set?(:x) raises an error" do
- # expect { resource.property_is_set?(:x) }.to raise_error(ArgumentError)
- # end
- #
- # with_property ':x' do
- # it "when the resource is newly created, property_is_set?(:x) is false" do
- # expect(resource.property_is_set?(:x)).to be_falsey
- # end
- # it "when x is set, property_is_set?(:x) is true" do
- # resource.x 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set with =, property_is_set?(:x) is true" do
- # resource.x = 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set to a lazy value, property_is_set?(:x) is true" 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
- # resource.x
- # expect(resource.property_is_set?(:x)).to be_falsey
- # end
- # end
- #
- # with_property ':x, default: 10' do
- # it "when the resource is newly created, property_is_set?(:x) is false" do
- # expect(resource.property_is_set?(:x)).to be_falsey
- # end
- # it "when x is set, property_is_set?(:x) is true" do
- # resource.x 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set with =, property_is_set?(:x) is true" do
- # resource.x = 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set to a lazy value, property_is_set?(:x) is true" 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 true" do
- # resource.x
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # end
- #
- # with_property ':x, default: nil' do
- # it "when the resource is newly created, property_is_set?(:x) is false" do
- # expect(resource.property_is_set?(:x)).to be_falsey
- # end
- # it "when x is set, property_is_set?(:x) is true" do
- # resource.x 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set with =, property_is_set?(:x) is true" do
- # resource.x = 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set to a lazy value, property_is_set?(:x) is true" 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 true" do
- # resource.x
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # end
- #
- # with_property ':x, default: lazy { 10 }' do
- # it "when the resource is newly created, property_is_set?(:x) is false" do
- # expect(resource.property_is_set?(:x)).to be_falsey
- # end
- # it "when x is set, property_is_set?(:x) is true" do
- # resource.x 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is set with =, property_is_set?(:x) is true" do
- # resource.x = 10
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # it "when x is retrieved, property_is_set?(:x) is true" do
- # resource.x
- # expect(resource.property_is_set?(:x)).to be_truthy
- # end
- # end
- # end
+ context "Chef::Resource::PropertyType#property_is_set?" do
+ it "when a resource is newly created, property_is_set?(:name) is true" do
+ expect(resource.property_is_set?(:name)).to be_truthy
+ end
+
+ # it "when referencing an undefined property, property_is_set?(:x) raises an error" do
+ # expect { resource.property_is_set?(:x) }.to raise_error(ArgumentError)
+ # end
+
+ with_property ':x' do
+ it "when the resource is newly created, property_is_set?(:x) is false" do
+ expect(resource.property_is_set?(:x)).to be_falsey
+ end
+ it "when x is set, property_is_set?(:x) is true" do
+ resource.x 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set with =, property_is_set?(:x) is true" do
+ resource.x = 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set to a lazy value, property_is_set?(:x) is true" 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
+ resource.x
+ expect(resource.property_is_set?(:x)).to be_falsey
+ end
+ end
+
+ with_property ':x, default: 10' do
+ it "when the resource is newly created, property_is_set?(:x) is false" do
+ expect(resource.property_is_set?(:x)).to be_falsey
+ end
+ it "when x is set, property_is_set?(:x) is true" do
+ resource.x 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set with =, property_is_set?(:x) is true" do
+ resource.x = 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set to a lazy value, property_is_set?(:x) is true" 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 true" do
+ resource.x
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ end
+
+ with_property ':x, default: nil' do
+ it "when the resource is newly created, property_is_set?(:x) is false" do
+ expect(resource.property_is_set?(:x)).to be_falsey
+ end
+ it "when x is set, property_is_set?(:x) is true" do
+ resource.x 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set with =, property_is_set?(:x) is true" do
+ resource.x = 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set to a lazy value, property_is_set?(:x) is true" 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 true" do
+ resource.x
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ end
+
+ with_property ':x, default: lazy { 10 }' do
+ it "when the resource is newly created, property_is_set?(:x) is false" do
+ expect(resource.property_is_set?(:x)).to be_falsey
+ end
+ it "when x is set, property_is_set?(:x) is true" do
+ resource.x 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is set with =, property_is_set?(:x) is true" do
+ resource.x = 10
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ it "when x is retrieved, property_is_set?(:x) is true" do
+ resource.x
+ expect(resource.property_is_set?(:x)).to be_truthy
+ end
+ end
+ end
context "Chef::Resource::PropertyType#default" do
with_property ':x, default: 10' do
it "when x is set, it returns its value" do
expect(resource.x 20).to eq 20
- # expect(resource.property_is_set?(:x)).to be_truthy
+ expect(resource.property_is_set?(:x)).to be_truthy
expect(resource.x).to eq 20
end
it "when x is not set, it returns 10" do