From a4e735e133898a376747491d5bbc1ca5692ac122 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:30:52 -0700 Subject: (#7353) Add 'console' format to FormatHandler This adds a console rendering format to the Network FormatHandler subsystem; it provides the same human-friendly textual rendering as the Faces application did, except it uses JSON rather than PP as the fall-back rendering mode. This paves the path for unification of all formatting into the same subsystem, rather than the half-measures we used to have. Reviewed-By: Jacob Helwig --- spec/unit/network/formats_spec.rb | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'spec/unit') diff --git a/spec/unit/network/formats_spec.rb b/spec/unit/network/formats_spec.rb index 72d355192..62c2dbb9d 100755 --- a/spec/unit/network/formats_spec.rb +++ b/spec/unit/network/formats_spec.rb @@ -330,4 +330,66 @@ describe "Puppet Network Format" do end end end + + describe ":console format" do + subject { Puppet::Network::FormatHandler.format(:console) } + it { should be_an_instance_of Puppet::Network::Format } + let :json do Puppet::Network::FormatHandler.format(:pson) end + + [:intern, :intern_multiple].each do |method| + it "should not implement #{method}" do + expect { subject.send(method, String, 'blah') }.to raise_error NotImplementedError + end + end + + ["hello", 1, 1.0].each do |input| + it "should just return a #{input.inspect}" do + subject.render(input).should == input + end + end + + [[1, 2], ["one"], [{ 1 => 1 }]].each do |input| + it "should render #{input.inspect} as JSON" do + subject.render(input).should == json.render(input).chomp + end + end + + it "should render a non-trivially-keyed Hash as JSON" do + hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 } + subject.render(hash).should == json.render(hash).chomp + end + + it "should render a {String,Numeric}-keyed Hash into a table" do + object = Object.new + hash = { "one" => 1, "two" => [], "three" => {}, "four" => object, + 5 => 5, 6.0 => 6 } + + # Gotta love ASCII-betical sort order. Hope your objects are better + # structured for display than my test one is. --daniel 2011-04-18 + subject.render(hash).should == < { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 }, + "text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 } + } + subject.render(hash).should == <"1111111111111111111111111111111111111111", + "2"=>"2222222222222222222222222222222222222222", + "3"=>"3333333333333333333333333333333333333333"} +text {"a"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "b"=>"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "c"=>"cccccccccccccccccccccccccccccccccccccccc"} +EOT + end + end end -- cgit From 5986e8a3747ebb0fe48169f88fecb481be76d16c Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:36:50 -0700 Subject: (#7353) Unify rendering in the face_bace application. We now move over to using only network FormatHandler rendering code for output, ditching all the support we had in the application. We include a compatibility shim to ensure that the :for_humans format that was supported for a while is now an alias for the :console format we are using moving forward. Reviewed-By: Jacob Helwig --- spec/unit/application/face_base_spec.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'spec/unit') diff --git a/spec/unit/application/face_base_spec.rb b/spec/unit/application/face_base_spec.rb index 133e6b2e9..25797ea3e 100755 --- a/spec/unit/application/face_base_spec.rb +++ b/spec/unit/application/face_base_spec.rb @@ -246,14 +246,14 @@ describe Puppet::Application::FaceBase do end [[1, 2], ["one"], [{ 1 => 1 }]].each do |input| - it "should render #{input.class} using the 'pp' library" do - app.render(input).should == input.pretty_inspect + it "should render #{input.class} using JSON" do + app.render(input).should == input.to_pson.chomp end end - it "should render a non-trivially-keyed Hash with the 'pp' library" do + it "should render a non-trivially-keyed Hash with using JSON" do hash = { [1,2] => 3, [2,3] => 5, [3,4] => 7 } - app.render(hash).should == hash.pretty_inspect + app.render(hash).should == hash.to_pson.chomp end it "should render a {String,Numeric}-keyed Hash into a table" do @@ -266,7 +266,7 @@ describe Puppet::Application::FaceBase do app.render(hash).should == < { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 }, "text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 } @@ -289,7 +290,7 @@ EOT end it "should invoke the action rendering hook while rendering" do - app.action.set_rendering_method_for(:for_humans, proc { |value| "bi-winning!" }) + app.action.set_rendering_method_for(:console, proc { |value| "bi-winning!" }) app.render("bi-polar?").should == "bi-winning!" end @@ -328,4 +329,15 @@ EOT }.to have_printed(/you invoked the 's' rendering hook/) end end + + describe "#render_as=" do + # This is for compatibility with the format name in 2.7.0rc1, but isn't a + # long term desirable name. We can't just randomly drop it since it was + # released, though, so it will live for a couple of major versions. + # --daniel 2011-05-04 + it "should treat :for_humans as a synonym for :console" do + app.render_as = :for_humans + app.render_as.name.should == :console + end + end end -- cgit From dda32647b4c11ecb352e469088f2438835ff99d7 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 4 May 2011 10:40:55 -0700 Subject: (#7353) Note the :for_humans compatibility issue. Where we need special support for :for_humans as an alias for :console, call it out in comments. This makes it clear to someone who wonders why what the actual underlying purpose of the whole thing is. Reviewed-By: Jacob Helwig --- spec/unit/interface/action_builder_spec.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'spec/unit') diff --git a/spec/unit/interface/action_builder_spec.rb b/spec/unit/interface/action_builder_spec.rb index e0d0ebe72..89aef26d0 100755 --- a/spec/unit/interface/action_builder_spec.rb +++ b/spec/unit/interface/action_builder_spec.rb @@ -200,6 +200,8 @@ describe Puppet::Interface::ActionBuilder do end end + # :for_humans is an alias for :console, and deprecated, but since we + # shipped it in 2.7.0rc1 we need to support it. --daniel 2011-05-04 it "should accept :for_humans format" do action = Puppet::Interface::ActionBuilder.build(face, :foo) do when_invoked do true end -- cgit