summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsersut <serdar@opscode.com>2014-03-12 15:33:40 -0700
committersersut <serdar@opscode.com>2014-03-12 15:33:40 -0700
commit5745ecbd55c49db714b4621f3e10872054accdb8 (patch)
tree8419b0d8989c276c0b547981bfbbccfce124363b
parent6dd87504a609da29c72d80f22dd00752d777c58b (diff)
parent68f99fc6c65cba739dad20f95df1c558ede66946 (diff)
downloadchef-5745ecbd55c49db714b4621f3e10872054accdb8.tar.gz
Merge branch 'CHEF-4799' of github.com:criteo/chef into contributions-cc-51
-rw-r--r--lib/chef/node/attribute_collections.rb9
-rw-r--r--lib/chef/node/immutable_collections.rb9
-rw-r--r--spec/unit/node/attribute_spec.rb7
-rw-r--r--spec/unit/node/immutable_collections_spec.rb6
4 files changed, 28 insertions, 3 deletions
diff --git a/lib/chef/node/attribute_collections.rb b/lib/chef/node/attribute_collections.rb
index d5d496fd60..f09b02b106 100644
--- a/lib/chef/node/attribute_collections.rb
+++ b/lib/chef/node/attribute_collections.rb
@@ -76,8 +76,15 @@ class Chef
super(data)
end
+ # For elements like Fixnums, true, nil...
+ def safe_dup(e)
+ e.dup
+ rescue TypeError
+ e
+ end
+
def dup
- Array.new(map {|e| e.dup})
+ Array.new(map {|e| safe_dup(e)})
end
end
diff --git a/lib/chef/node/immutable_collections.rb b/lib/chef/node/immutable_collections.rb
index f5b3a5121d..63ac2b4c6b 100644
--- a/lib/chef/node/immutable_collections.rb
+++ b/lib/chef/node/immutable_collections.rb
@@ -85,8 +85,15 @@ class Chef
METHOD_DEFN
end
+ # For elements like Fixnums, true, nil...
+ def safe_dup(e)
+ e.dup
+ rescue TypeError
+ e
+ end
+
def dup
- Array.new(map {|e| e.dup })
+ Array.new(map {|e| safe_dup(e)})
end
end
diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb
index ef3fc60cc6..bab2e33aa9 100644
--- a/spec/unit/node/attribute_spec.rb
+++ b/spec/unit/node/attribute_spec.rb
@@ -488,6 +488,13 @@ describe Chef::Node::Attribute do
end
end
+ describe "dup" do
+ it "array can be duped even if some elements can't" do
+ @attributes.default[:foo] = %w[foo bar baz] + Array(1..3) + [nil, true, false, [ "el", 0, nil ] ]
+ @attributes.default[:foo].dup
+ end
+ end
+
describe "has_key?" do
it "should return true if an attribute exists" do
@attributes.has_key?("music").should == true
diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb
index 0c2b878cd2..f30c1970f7 100644
--- a/spec/unit/node/immutable_collections_spec.rb
+++ b/spec/unit/node/immutable_collections_spec.rb
@@ -86,7 +86,7 @@ end
describe Chef::Node::ImmutableArray do
before do
- @immutable_array = Chef::Node::ImmutableArray.new(%w[foo bar baz])
+ @immutable_array = Chef::Node::ImmutableArray.new(%w[foo bar baz] + Array(1..3) + [nil, true, false, [ "el", 0, nil ] ])
end
##
@@ -130,6 +130,10 @@ describe Chef::Node::ImmutableArray do
end
end
+ it "can be duped even if some elements can't" do
+ @immutable_array.dup
+ end
+
it "returns a mutable version of itself when duped" do
mutable = @immutable_array.dup
mutable[0] = :value