summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@chef.io>2019-09-30 10:31:26 -0700
committerGitHub <noreply@github.com>2019-09-30 10:31:26 -0700
commit50b4847e21a8a0173c19f1f100c2728f74c84e9a (patch)
treeeda6444e04d8e128c9d15842682b539543b67fa3
parentf8dcd18acc4924af77b65699c9ee4abdd83dadcf (diff)
parent6abaf050fa3385e59df5a4dc53305885178e0971 (diff)
downloadchef-50b4847e21a8a0173c19f1f100c2728f74c84e9a.tar.gz
Merge pull request #8927 from MsysTechnologiesllc/VSingh/MSYS-1113_to_yaml_support_for_node_attribute
Add #to_yaml method for ImmutableMash & ImmutableArray
-rw-r--r--lib/chef/node/attribute.rb2
-rw-r--r--lib/chef/node/attribute_collections.rb8
-rw-r--r--lib/chef/node/immutable_collections.rb12
-rw-r--r--lib/chef/node/mixin/immutablize_array.rb1
-rw-r--r--lib/chef/node/mixin/immutablize_hash.rb1
-rw-r--r--spec/unit/node/attribute_spec.rb22
-rw-r--r--spec/unit/node/immutable_collections_spec.rb216
7 files changed, 118 insertions, 144 deletions
diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb
index 0beddf25f5..8bccc9a05d 100644
--- a/lib/chef/node/attribute.rb
+++ b/lib/chef/node/attribute.rb
@@ -148,7 +148,9 @@ class Chef
to_a
to_h
to_hash
+ to_json
to_set
+ to_yaml
value?
values
values_at
diff --git a/lib/chef/node/attribute_collections.rb b/lib/chef/node/attribute_collections.rb
index dadc209ae9..bfa814934d 100644
--- a/lib/chef/node/attribute_collections.rb
+++ b/lib/chef/node/attribute_collections.rb
@@ -65,6 +65,10 @@ class Chef
Array.new(map { |e| safe_dup(e) })
end
+ def to_yaml(*opts)
+ to_a.to_yaml(*opts)
+ end
+
private
def convert_value(value)
@@ -172,6 +176,10 @@ class Chef
Mash.new(self)
end
+ def to_yaml(*opts)
+ to_h.to_yaml(*opts)
+ end
+
prepend Chef::Node::Mixin::StateTracking
end
end
diff --git a/lib/chef/node/immutable_collections.rb b/lib/chef/node/immutable_collections.rb
index b4a7a39ba0..06bd3b2a3f 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 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/lib/chef/node/mixin/immutablize_array.rb b/lib/chef/node/mixin/immutablize_array.rb
index e9cbb2142a..36b8c33528 100644
--- a/lib/chef/node/mixin/immutablize_array.rb
+++ b/lib/chef/node/mixin/immutablize_array.rb
@@ -119,6 +119,7 @@ class Chef
to_h
to_plist
to_set
+ to_yaml
transpose
union
uniq
diff --git a/lib/chef/node/mixin/immutablize_hash.rb b/lib/chef/node/mixin/immutablize_hash.rb
index 8c0d1c9509..6ffa42a4f3 100644
--- a/lib/chef/node/mixin/immutablize_hash.rb
+++ b/lib/chef/node/mixin/immutablize_hash.rb
@@ -116,6 +116,7 @@ class Chef
to_proc
to_set
to_xml_attributes
+ to_yaml
transform_keys
transform_values
uniq
diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb
index 5055941d06..7d6d264405 100644
--- a/spec/unit/node/attribute_spec.rb
+++ b/spec/unit/node/attribute_spec.rb
@@ -1297,4 +1297,26 @@ describe Chef::Node::Attribute do
expect(@attributes["foo"]).to be nil
end
end
+
+ describe "to_json" do
+ it "should convert to a valid json string" do
+ json = @attributes["hot"].to_json
+ expect { JSON.parse(json) }.not_to raise_error
+ end
+
+ it "should convert to a json based on current state" do
+ expect(@attributes["hot"].to_json).to eq("{\"day\":\"sunday\"}")
+ end
+ end
+
+ describe "to_yaml" do
+ it "should convert to a valid yaml format" do
+ json = @attributes["hot"].to_yaml
+ expect { YAML.parse(json) }.not_to raise_error
+ end
+
+ it "should convert to a yaml based on current state" do
+ expect(@attributes["hot"].to_yaml).to eq("---\nday: sunday\n")
+ end
+ end
end
diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb
index f23e40dc4c..8f05182b78 100644
--- a/spec/unit/node/immutable_collections_spec.rb
+++ b/spec/unit/node/immutable_collections_spec.rb
@@ -19,6 +19,64 @@
require "spec_helper"
require "chef/node/immutable_collections"
+shared_examples_for "ImmutableMash module" do |param|
+ let(:copy) { @immutable_mash.send(param) }
+
+ it "converts an immutable mash to a new mutable hash" do
+ expect(copy).to be_is_a(Hash)
+ end
+
+ it "converts an immutable nested mash to a new mutable hash" do
+ expect(copy["top_level_4"]["level2"]).to be_is_a(Hash)
+ end
+
+ it "converts an immutable nested array to a new mutable array" do
+ expect(copy["top_level_2"]).to be_instance_of(Array)
+ end
+
+ it "should create a mash with the same content" do
+ expect(copy).to eq(@immutable_mash)
+ end
+
+ it "should allow mutation" do
+ expect { copy["m"] = "m" }.not_to raise_error
+ end
+end
+
+shared_examples_for "ImmutableArray module" do |param|
+ let(:copy) { @immutable_nested_array.send(param) }
+
+ it "converts an immutable array to a new mutable array" do
+ expect(copy).to be_instance_of(Array)
+ end
+
+ it "converts an immutable nested array to a new mutable array" do
+ expect(copy[1]).to be_instance_of(Array)
+ end
+
+ it "converts an immutable nested mash to a new mutable hash" do
+ expect(copy[2]).to be_is_a(Hash)
+ end
+
+ it "should create an array with the same content" do
+ expect(copy).to eq(@immutable_nested_array)
+ end
+
+ it "should allow mutation" do
+ expect { copy << "m" }.not_to raise_error
+ end
+end
+
+shared_examples_for "Immutable#to_yaml" do
+ it "converts an immutable array to a new valid YAML mutable string" do
+ expect { YAML.parse(copy) }.not_to raise_error
+ end
+
+ it "should create a YAML string with content" do
+ expect(copy).to eq(parsed_yaml)
+ end
+end
+
describe Chef::Node::ImmutableMash do
before do
@data_in = { "top" => { "second_level" => "some value" },
@@ -67,82 +125,17 @@ describe Chef::Node::ImmutableMash do
expect(@mash["test2"]).to eql("bar")
end
- describe "to_hash" do
- before do
- @copy = @immutable_mash.to_hash
- end
-
- it "converts an immutable mash to a new mutable hash" do
- expect(@copy).to be_instance_of(Hash)
- end
-
- it "converts an immutable nested mash to a new mutable hash" do
- expect(@copy["top_level_4"]["level2"]).to be_instance_of(Hash)
- end
-
- it "converts an immutable nested array to a new mutable array" do
- expect(@copy["top_level_2"]).to be_instance_of(Array)
- end
-
- it "should create a mash with the same content" do
- expect(@copy).to eq(@immutable_mash)
- end
-
- it "should allow mutation" do
- expect { @copy["m"] = "m" }.not_to raise_error
+ %w{to_h to_hash dup}.each do |immutable_meth|
+ describe "#{immutable_meth}" do
+ include_examples "ImmutableMash module", description
end
end
- describe "dup" do
- before do
- @copy = @immutable_mash.dup
- end
-
- it "converts an immutable mash to a new mutable hash" do
- expect(@copy).to be_instance_of(Mash)
- end
-
- it "converts an immutable nested mash to a new mutable hash" do
- expect(@copy["top_level_4"]["level2"]).to be_instance_of(Mash)
- end
-
- it "converts an immutable nested array to a new mutable array" do
- expect(@copy["top_level_2"]).to be_instance_of(Array)
- end
-
- it "should create a mash with the same content" do
- expect(@copy).to eq(@immutable_mash)
- end
+ describe "to_yaml" do
+ let(:copy) { @immutable_mash.to_yaml }
+ let(:parsed_yaml) { "---\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" }
- it "should allow mutation" do
- expect { @copy["m"] = "m" }.not_to raise_error
- end
- end
-
- describe "to_h" do
- before do
- @copy = @immutable_mash.to_h
- end
-
- it "converts an immutable mash to a new mutable hash" do
- expect(@copy).to be_instance_of(Hash)
- end
-
- it "converts an immutable nested mash to a new mutable hash" do
- expect(@copy["top_level_4"]["level2"]).to be_instance_of(Hash)
- end
-
- it "converts an immutable nested array to a new mutable array" do
- expect(@copy["top_level_2"]).to be_instance_of(Array)
- end
-
- it "should create a mash with the same content" do
- expect(@copy).to eq(@immutable_mash)
- end
-
- it "should allow mutation" do
- expect { @copy["m"] = "m" }.not_to raise_error
- end
+ include_examples "Immutable#to_yaml"
end
%i{
@@ -240,82 +233,17 @@ describe Chef::Node::ImmutableArray do
expect(mutable[0]).to eq(:value)
end
- describe "to_a" do
- before do
- @copy = @immutable_nested_array.to_a
- end
-
- it "converts an immutable array to a new mutable array" do
- expect(@copy).to be_instance_of(Array)
- end
-
- it "converts an immutable nested array to a new mutable array" do
- expect(@copy[1]).to be_instance_of(Array)
- end
-
- it "converts an immutable nested mash to a new mutable hash" do
- expect(@copy[2]).to be_instance_of(Hash)
- end
-
- it "should create an array with the same content" do
- expect(@copy).to eq(@immutable_nested_array)
- end
-
- it "should allow mutation" do
- expect { @copy << "m" }.not_to raise_error
+ %w{to_a to_array dup}.each do |immutable_meth|
+ describe "#{immutable_meth}" do
+ include_examples "ImmutableArray module", description
end
end
- describe "dup" do
- before do
- @copy = @immutable_nested_array.dup
- end
-
- it "converts an immutable array to a new mutable array" do
- expect(@copy).to be_instance_of(Array)
- end
-
- it "converts an immutable nested array to a new mutable array" do
- expect(@copy[1]).to be_instance_of(Array)
- end
-
- it "converts an immutable nested mash to a new mutable hash" do
- expect(@copy[2]).to be_instance_of(Mash)
- end
-
- it "should create an array with the same content" do
- expect(@copy).to eq(@immutable_nested_array)
- end
-
- it "should allow mutation" do
- expect { @copy << "m" }.not_to raise_error
- end
- end
+ describe "to_yaml" do
+ let(:copy) { @immutable_nested_array.to_yaml }
+ let(:parsed_yaml) { "---\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" }
- describe "to_array" do
- before do
- @copy = @immutable_nested_array.to_array
- end
-
- it "converts an immutable array to a new mutable array" do
- expect(@copy).to be_instance_of(Array)
- end
-
- it "converts an immutable nested array to a new mutable array" do
- expect(@copy[1]).to be_instance_of(Array)
- end
-
- it "converts an immutable nested mash to a new mutable hash" do
- expect(@copy[2]).to be_instance_of(Hash)
- end
-
- it "should create an array with the same content" do
- expect(@copy).to eq(@immutable_nested_array)
- end
-
- it "should allow mutation" do
- expect { @copy << "m" }.not_to raise_error
- end
+ include_examples "Immutable#to_yaml"
end
describe "#[]" do