From: Daniel Pittman <[email protected]>

This data structure generates YAML with an extra newline that violates the
syntax rules and all:

  list = [1]
  { :a => list, :b => list }.to_yaml

This breaks real client use of the YAML catalogs, not to mention our own use
of cached catalogs...
---
 lib/puppet/util/zaml.rb     |    7 +++----
 spec/unit/util/zaml_spec.rb |   21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/lib/puppet/util/zaml.rb b/lib/puppet/util/zaml.rb
index 9fda5ae..6ac9565 100644
--- a/lib/puppet/util/zaml.rb
+++ b/lib/puppet/util/zaml.rb
@@ -59,13 +59,12 @@ class ZAML
       @@previously_emitted_object = {}
       @@next_free_label_number = 0
     end
-    def initialize(obj,indent)
-      @indent = indent
+    def initialize(obj)
       @this_label_number = nil
       @@previously_emitted_object[obj.object_id] = self
     end
     def to_s
-      @this_label_number ? ('&id%03d%s' % [@this_label_number, @indent]) : ''
+      @this_label_number ? ('&id%03d ' % @this_label_number) : ''
     end
     def reference
       @this_label_number ||= (@@next_free_label_number += 1)
@@ -76,7 +75,7 @@ class ZAML
     end
   end
   def new_label_for(obj)
-    Label.new(obj,(Hash === obj || Array === obj) ? "#{@indent || "\n"}  " : ' 
')
+    Label.new(obj)
   end
   def first_time_only(obj)
     if label = Label.for(obj)
diff --git a/spec/unit/util/zaml_spec.rb b/spec/unit/util/zaml_spec.rb
index f2bcefe..59590c5 100755
--- a/spec/unit/util/zaml_spec.rb
+++ b/spec/unit/util/zaml_spec.rb
@@ -35,5 +35,26 @@ describe "Pure ruby yaml implementation" do
       lambda { YAML.load(o.to_yaml) }.should_not raise_error
     end
   }
+
+  it "should handle references to Array in Hash values correctly" do
+    list = [1]
+    data = { "one" => list, "two" => list }
+    data.to_yaml.should == "--- \n  two: &id001 \n    - 1\n  one: *id001"
+    expect { YAML.load(data.to_yaml).should == data }.should_not raise_error
+  end
+
+  it "should handle references to Hash in Hash values correctly" do
+    hash = { 1 => 1 }
+    data = { "one" => hash, "two" => hash }
+    data.to_yaml.should == "--- \n  two: &id001 \n    1: 1\n  one: *id001"
+    expect { YAML.load(data.to_yaml).should == data }.should_not raise_error
+  end
+
+  it "should handle references to Scalar in Hash" do
+    str = "hello"
+    data = { "one" => str, "two" => str }
+    data.to_yaml.should == "--- \n  two: &id001 hello\n  one: *id001"
+    expect { YAML.load(data.to_yaml).should == data }.should_not raise_error
+  end
 end
 
-- 
1.7.3.5

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to