diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-09-23 16:49:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-23 16:49:33 -0700 |
commit | 6b38babb0769ffd8820b460ebc60943a69dd100f (patch) | |
tree | 8b93687430518b62268b1824063c1af75ab6b219 | |
parent | 9076a6e666d10df35c400681d77d350acd69ca1e (diff) | |
parent | dabb318ccb01ea15b9d577bf4f6eb0a05789aee0 (diff) | |
download | chef-6b38babb0769ffd8820b460ebc60943a69dd100f.tar.gz |
Merge pull request #5360 from gbagnoli/array_literals_attributes
Chef 12 Attribute Regression
-rw-r--r-- | lib/chef/node/attribute.rb | 4 | ||||
-rw-r--r-- | lib/chef/node/attribute_collections.rb | 20 | ||||
-rw-r--r-- | spec/unit/node/attribute_spec.rb | 24 | ||||
-rw-r--r-- | spec/unit/node/vivid_mash_spec.rb | 27 |
4 files changed, 71 insertions, 4 deletions
diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index 95b3b09f7e..a4a07275c0 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -452,9 +452,7 @@ class Chef # def merged_attributes(*path) - # immutablize( - merge_all(path) - # ) + immutablize(merge_all(path)) end def combined_override(*path) diff --git a/lib/chef/node/attribute_collections.rb b/lib/chef/node/attribute_collections.rb index b739ea8490..1bd31bceb0 100644 --- a/lib/chef/node/attribute_collections.rb +++ b/lib/chef/node/attribute_collections.rb @@ -73,6 +73,7 @@ class Chef def initialize(root, data) @root = root super(data) + map! { |e| convert_value(e) } end # For elements like Fixnums, true, nil... @@ -86,6 +87,23 @@ class Chef Array.new(map { |e| safe_dup(e) }) end + private + + def convert_value(value) + case value + when VividMash + value + when AttrArray + value + when Hash + VividMash.new(root, value) + when Array + AttrArray.new(root, value) + else + value + end + end + end # == VividMash @@ -184,6 +202,8 @@ class Chef case value when VividMash value + when AttrArray + value when Hash VividMash.new(root, value) when Array diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index e40f454c0b..00081a9fd9 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -1171,7 +1171,29 @@ describe Chef::Node::Attribute do Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { @attributes.new_key = "new value" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end - end + describe "deeply converting values" do + it "converts values through an array" do + @attributes.default[:foo] = [ { bar: true } ] + expect(@attributes["foo"].class).to eql(Chef::Node::ImmutableArray) + expect(@attributes["foo"][0].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"][0]["bar"]).to be true + end + + it "converts values through nested arrays" do + @attributes.default[:foo] = [ [ { bar: true } ] ] + expect(@attributes["foo"].class).to eql(Chef::Node::ImmutableArray) + expect(@attributes["foo"][0].class).to eql(Chef::Node::ImmutableArray) + expect(@attributes["foo"][0][0].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"][0][0]["bar"]).to be true + end + + it "converts values through nested hashes" do + @attributes.default[:foo] = { baz: { bar: true } } + expect(@attributes["foo"].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"]["baz"].class).to eql(Chef::Node::ImmutableMash) + expect(@attributes["foo"]["baz"]["bar"]).to be true + end + end end diff --git a/spec/unit/node/vivid_mash_spec.rb b/spec/unit/node/vivid_mash_spec.rb index 5319ba4a35..206b15ef6c 100644 --- a/spec/unit/node/vivid_mash_spec.rb +++ b/spec/unit/node/vivid_mash_spec.rb @@ -37,6 +37,33 @@ describe Chef::Node::VividMash do expect(root).to receive(:top_level_breadcrumb=).with(key).at_least(:once).and_call_original end + context "#[]=" do + it "deep converts values through arrays" do + allow(root).to receive(:reset_cache) + vivid[:foo] = [ { :bar => true } ] + expect(vivid["foo"].class).to eql(Chef::Node::AttrArray) + expect(vivid["foo"][0].class).to eql(Chef::Node::VividMash) + expect(vivid["foo"][0]["bar"]).to be true + end + + it "deep converts values through nested arrays" do + allow(root).to receive(:reset_cache) + vivid[:foo] = [ [ { :bar => true } ] ] + expect(vivid["foo"].class).to eql(Chef::Node::AttrArray) + expect(vivid["foo"][0].class).to eql(Chef::Node::AttrArray) + expect(vivid["foo"][0][0].class).to eql(Chef::Node::VividMash) + expect(vivid["foo"][0][0]["bar"]).to be true + end + + it "deep converts values through hashes" do + allow(root).to receive(:reset_cache) + vivid[:foo] = { baz: { :bar => true } } + expect(vivid["foo"]).to be_an_instance_of(Chef::Node::VividMash) + expect(vivid["foo"]["baz"]).to be_an_instance_of(Chef::Node::VividMash) + expect(vivid["foo"]["baz"]["bar"]).to be true + end + end + context "#read" do before do # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards |