diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-05-04 10:30:52 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-05-04 12:28:25 -0700 |
| commit | a4e735e133898a376747491d5bbc1ca5692ac122 (patch) | |
| tree | 1d479a95d91dbfb2cc38f80d8f76140ebbc53a99 | |
| parent | d203853bc8b40732c2ba88a4e5396f00a1e3a4ec (diff) | |
| download | puppet-a4e735e133898a376747491d5bbc1ca5692ac122.tar.gz puppet-a4e735e133898a376747491d5bbc1ca5692ac122.tar.xz puppet-a4e735e133898a376747491d5bbc1ca5692ac122.zip | |
(#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 <jacob@puppetlabs.com>
| -rw-r--r-- | lib/puppet/network/formats.rb | 36 | ||||
| -rwxr-xr-x | spec/unit/network/formats_spec.rb | 62 |
2 files changed, 98 insertions, 0 deletions
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index 4ca3240d4..082c83ee3 100644 --- a/lib/puppet/network/formats.rb +++ b/lib/puppet/network/formats.rb @@ -160,3 +160,39 @@ end # This is really only ever going to be used for Catalogs. Puppet::Network::FormatHandler.create_serialized_formats(:dot, :required_methods => [:render_method]) + + +Puppet::Network::FormatHandler.create(:console, + :mime => 'text/x-console-text', + :weight => 0) do + def json + @json ||= Puppet::Network::FormatHandler.format(:pson) + end + + def render(datum) + # String to String + return datum if datum.is_a? String + return datum if datum.is_a? Numeric + + # Simple hash to table + if datum.is_a? Hash and datum.keys.all? { |x| x.is_a? String or x.is_a? Numeric } + output = '' + column_a = datum.map do |k,v| k.to_s.length end.max + 2 + column_b = 79 - column_a + datum.sort_by { |k,v| k.to_s } .each do |key, value| + output << key.to_s.ljust(column_a) + output << json.render(value). + chomp.gsub(/\n */) { |x| x + (' ' * column_a) } + output << "\n" + end + return output + end + + # ...or pretty-print the inspect outcome. + return json.render(datum) + end + + def render_multiple(data) + data.collect(&:render).join("\n") + end +end 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 == <<EOT +5 5 +6.0 6 +four #{json.render(object).chomp} +one 1 +three {} +two [] +EOT + end + + it "should render a hash nicely with a multi-line value" do + pending "Moving to PSON rather than PP makes this unsupportable." + hash = { + "number" => { "1" => '1' * 40, "2" => '2' * 40, '3' => '3' * 40 }, + "text" => { "a" => 'a' * 40, 'b' => 'b' * 40, 'c' => 'c' * 40 } + } + subject.render(hash).should == <<EOT +number {"1"=>"1111111111111111111111111111111111111111", + "2"=>"2222222222222222222222222222222222222222", + "3"=>"3333333333333333333333333333333333333333"} +text {"a"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "b"=>"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "c"=>"cccccccccccccccccccccccccccccccccccccccc"} +EOT + end + end end |
