Skip to content

Commit

Permalink
Raise NameError if a bad formatter name is given
Browse files Browse the repository at this point in the history
This makes the error more explicit and helpful. Previously, setting
`output.format = :bad_value` would not raise immediately, but would fail later
with a confusing error once a logging operation is attempted.
  • Loading branch information
mattbrictson committed Nov 1, 2015
1 parent d1b757d commit 411c0e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/sshkit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ def logger(verbosity)
end

def formatter(format)
SSHKit::Formatter.constants.each do |const|
return SSHKit::Formatter.const_get(const).new($stdout) if const.downcase.eql?(format.downcase)
formatter_class(format).new($stdout)
end

def formatter_class(symbol)
name = symbol.to_s.downcase
found = SSHKit::Formatter.constants.find do |const|
const.to_s.downcase == name
end
fail NameError, 'Unrecognized SSHKit::Formatter "#{symbol}"' if found.nil?
SSHKit::Formatter.const_get(found)
end

end
Expand Down
5 changes: 5 additions & 0 deletions test/unit/test_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def test_setting_formatter_types
end
end

def test_prohibits_unknown_formatter_type_with_exception
assert_raises(NameError) do
SSHKit.config.format = :doesnotexist
end
end
end

end

0 comments on commit 411c0e0

Please sign in to comment.