summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2019-09-27 10:16:12 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2019-09-27 10:16:12 +0530
commit1a049aa3a42d8ce785afdbe8467d249991baf2c4 (patch)
tree4c60c42a2408b7c63bf7b1a76e6496bf30bed31d
parent49ff9da07c01255c36650ca55874ac067c0e68bd (diff)
downloadchef-1a049aa3a42d8ce785afdbe8467d249991baf2c4.tar.gz
Override the #to_yaml method for ImmutableMash & ImmutableArray  
 - Psych module to_yaml not working as expected for ImmutableMash & ImmutableArray.  - So parse first to Hash/Array then convert it to YAML formatted string. Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--lib/chef/node/immutable_collections.rb12
-rw-r--r--spec/unit/node/immutable_collections_spec.rb28
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/chef/node/immutable_collections.rb b/lib/chef/node/immutable_collections.rb
index b4a7a39ba0..96e589fdaa 100644
--- a/lib/chef/node/immutable_collections.rb
+++ b/lib/chef/node/immutable_collections.rb
@@ -96,6 +96,12 @@ class Chef
alias_method :to_array, :to_a
+ # As Psych module, not respecting Immutable ImmutableArray object
+ # So first convert it to Hash/Array then parse it to `.to_yaml`
+ def to_yaml(*opts)
+ to_a.to_yaml(*opts)
+ end
+
private
# needed for __path__
@@ -168,6 +174,12 @@ class Chef
alias_method :to_hash, :to_h
+ # As Psych module, not respecting ImmutableMash object
+ # So first convert it to Hash/Array then parse it to `.to_yaml`
+ def to_yaml(*opts)
+ to_h.to_yaml(*opts)
+ end
+
# For elements like Fixnums, true, nil...
def safe_dup(e)
e.dup
diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb
index f23e40dc4c..e9697c320f 100644
--- a/spec/unit/node/immutable_collections_spec.rb
+++ b/spec/unit/node/immutable_collections_spec.rb
@@ -145,6 +145,20 @@ describe Chef::Node::ImmutableMash do
end
end
+ describe "to_yaml" do
+ before do
+ @copy = @immutable_mash.to_yaml
+ end
+
+ it "converts an immutable mash to a new mutable YAML formatted string" do
+ expect(@copy).to be_instance_of(String)
+ end
+
+ it "should create a YAML formatted string with the content" do
+ expect(@copy).to eq("---\ntop:\n second_level: some value\ntop_level_2:\n- array\n- of\n- values\ntop_level_3:\n- hash_array: 1\n hash_array_b: 2\ntop_level_4:\n level2:\n key: value\n")
+ end
+ end
+
%i{
[]=
clear
@@ -318,6 +332,20 @@ describe Chef::Node::ImmutableArray do
end
end
+ describe "to_yaml" do
+ before do
+ @copy = @immutable_nested_array.to_yaml
+ end
+
+ it "converts an immutable array to a new YAML formatted mutable string" do
+ expect(@copy).to be_instance_of(String)
+ end
+
+ it "should create a YAML formatted string with content" do
+ expect(@copy).to eq("---\n- level1\n- - foo\n - bar\n - baz\n - 1\n - 2\n - 3\n - \n - true\n - false\n - - el\n - 0\n - \n- m: m\n")
+ end
+ end
+
describe "#[]" do
it "works with array slices" do
expect(@immutable_array[1, 2]).to eql(%w{bar baz})