Skip to content

Commit 516f41b

Browse files
adamhjkdanielsdeleo
authored andcommitted
Only reading from the set_type on write, fixes CHEF-1304
1 parent 7ec8272 commit 516f41b

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

‎chef/lib/chef/node/attribute.rb‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def initialize(normal, default, override, automatic, state=[])
5151
@state = state
5252
@auto_vivifiy_on_read = false
5353
@set_unless_value_present = false
54-
@set_type = :normal
54+
@set_type = nil
5555
@has_been_read = false
5656
end
5757

@@ -93,12 +93,23 @@ def [](key)
9393
# See the comments in []= for more details.
9494
@has_been_read = true
9595

96-
a_value = value_or_descend(current_automatic, key, auto_vivifiy_on_read && @set_type == :automatic)
97-
o_value = value_or_descend(current_override, key, auto_vivifiy_on_read && @set_type == :override)
98-
n_value = value_or_descend(current_normal, key, auto_vivifiy_on_read && @set_type == :normal)
99-
d_value = value_or_descend(current_default, key, auto_vivifiy_on_read && @set_type == :default)
96+
# If we have a set type, our destiny is to write
97+
if @set_type
98+
a_value = @set_type == :automatic ? value_or_descend(current_automatic, key, auto_vivifiy_on_read) : nil
99+
o_value = @set_type == :override ? value_or_descend(current_override, key, auto_vivifiy_on_read) : nil
100+
n_value = @set_type == :normal ? value_or_descend(current_normal, key, auto_vivifiy_on_read) : nil
101+
d_value = @set_type == :default ? value_or_descend(current_default, key, auto_vivifiy_on_read) : nil
100102

101-
determine_value(a_value, o_value, n_value, d_value)
103+
determine_value(a_value, o_value, n_value, d_value)
104+
# Our destiny is only to read, so we get the full list.
105+
else
106+
a_value = value_or_descend(current_automatic, key)
107+
o_value = value_or_descend(current_override, key)
108+
n_value = value_or_descend(current_normal, key)
109+
d_value = value_or_descend(current_default, key)
110+
111+
determine_value(a_value, o_value, n_value, d_value)
112+
end
102113
end
103114

104115
def attribute?(key)
@@ -309,6 +320,9 @@ def determine_value(a_value, o_value, n_value, d_value)
309320
end
310321

311322
def []=(key, value)
323+
# If we don't have one, then we'll pretend we're normal
324+
@set_type ||= :normal
325+
312326
if set_unless_value_present
313327
if get_value(set_type_hash, key) != nil
314328
Chef::Log.debug("Not setting #{state.join("/")}/#{key} to #{value.inspect} because it has a #{@set_type} value already")

‎chef/spec/unit/node/attribute_spec.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,14 @@
381381

382382
it "should get a value based on the state of the object" do
383383
@attributes.auto_vivifiy_on_read = true
384+
@attributes.set_type = :normal
384385
@attributes[:foo][:bar][:baz] = "snack"
385386
@attributes.get_value(@attribute_hash, :baz).should == "snack"
386387
end
387388

388389
it "should return nil based on the state of the object if the key does not exist" do
389390
@attributes.auto_vivifiy_on_read = true
391+
@attributes.set_type = :normal
390392
@attributes[:foo][:bar][:baz] = "snack"
391393
@attributes.get_value(@attribute_hash, :baznatch).should == nil
392394
end

0 commit comments

Comments
 (0)