Skip to content

Commit

Permalink
Fix empty hash console rendering bug
Browse files Browse the repository at this point in the history
Previously, if a face action returned an empty hash Puppet would print
out an error something like the following:

    err: undefined method `+' for nil:NilClass
    err: Try 'puppet help stack build' for usage

The cause of the error was a method in network/formats.rb that did not
account for the possibilty of an empty hash. This commit resolves the
issue by adding logic to account for the empty hash scenario, rendering
an empty hash as an empty string.
  • Loading branch information
reidmv authored and aboe76 committed Feb 9, 2013
1 parent d2e3b89 commit 62fd91b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/puppet/network/formats.rb
Expand Up @@ -148,7 +148,7 @@ def render(datum)
# 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_a = datum.empty? ? 2 : datum.map{ |k,v| k.to_s.length }.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)
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/network/formats_spec.rb
Expand Up @@ -311,6 +311,10 @@ def to_pson(*args)
end
end

it "should render empty hashes as empty strings" do
subject.render({}).should == ''
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
Expand Down

0 comments on commit 62fd91b

Please sign in to comment.