summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-07-15 12:59:17 -0600
committerJohn Keiser <john@johnkeiser.com>2015-07-15 12:59:17 -0600
commit9a3e6e04f3bb39c2b2f5749719f0c21dd3f3f2ec (patch)
tree10d13791a8744ab02891ee1160e5dd9f72b1b12b
parent2c98a7b90507b2479ffda8ccd31bd464b1f5bee0 (diff)
downloadchef-jk/state_attrs.tar.gz
Fix issue where DSL is not emitted if state_properties happens before propertyjk/state_attrs
If you call state_properties :x, and there is no property :x yet, it assumes you are working with a custom getter/setter like "def x". This is fine, but when you say "property :x", it won't override that (and will treat the property as if the getter/setter were already defined instead of creating a new one).
-rw-r--r--lib/chef/resource.rb5
-rw-r--r--spec/unit/property/state_spec.rb15
2 files changed, 18 insertions, 2 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index ac6f5e8923..dca0033409 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -779,6 +779,7 @@ class Chef
def self.property(name, type=NOT_PASSED, **options)
name = name.to_sym
+ options[:instance_variable_name] = :"@#{name}" if !options.has_key?(:instance_variable_name)
options.merge!(name: name, declared_in: self)
if type == NOT_PASSED
@@ -1002,8 +1003,8 @@ class Chef
end
end
- # If state_attrs *excludes* something which is currently part of the
- # identity, mark it as identity: false.
+ # If identity_properties *excludes* something which is currently part of
+ # the identity, mark it as identity: false.
properties.each do |name,property|
if property.identity? && !names.include?(name)
self.property name, identity: false
diff --git a/spec/unit/property/state_spec.rb b/spec/unit/property/state_spec.rb
index bf2de178c9..e7fee0387f 100644
--- a/spec/unit/property/state_spec.rb
+++ b/spec/unit/property/state_spec.rb
@@ -369,6 +369,21 @@ describe "Chef::Resource#identity and #state" do
end
end
+ context "When state_properties happens before properties are declared" do
+ before do
+ resource_class.class_eval do
+ state_properties :x
+ property :x
+ end
+ end
+ it "the property works and is in state_properties" do
+ expect(resource_class.state_properties).to include(resource_class.properties[:x])
+ resource.x = 1
+ expect(resource.x).to eq 1
+ expect(resource.state_for_resource_reporter).to eq(x: 1)
+ end
+ end
+
with_property ":x, Integer, identity: true" do
it "state_properties(:x) leaves the property in desired_state" do
resource_class.state_properties(:x)