Skip to content

Commit

Permalink
Removed requirement for formatter outputs to be IO objects
Browse files Browse the repository at this point in the history
Any object which supports << can now be used for example Logger. Fixes compatibility with Airbrussh - see #246.
  • Loading branch information
robd committed May 9, 2015
1 parent 5124acf commit f37ef77
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
6 changes: 3 additions & 3 deletions lib/sshkit/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module SSHKit
class Color
def initialize(io, env=ENV)
@io, @env = io, env
def initialize(output, env=ENV)
@output, @env = output, env
end

def colorize(obj, color, mode=nil)
Expand All @@ -12,7 +12,7 @@ def colorize(obj, color, mode=nil)
end

def colorize?
@env['SSHKIT_COLOR'] || @io.tty?
@env['SSHKIT_COLOR'] || (@output.respond_to?(:tty?) && @output.tty?)
end
end
end
6 changes: 3 additions & 3 deletions lib/sshkit/formatters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Abstract
def_delegators :@original_output, :read, :rewind
def_delegators :@color, :colorize

def initialize(oio)
@original_output = oio
@color = SSHKit::Color.new(oio)
def initialize(output)
@original_output = output
@color = SSHKit::Color.new(output)
end

def log(messages)
Expand Down
6 changes: 3 additions & 3 deletions test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class TestNetssh < FunctionalTest

def setup
super
@out = StringIO.new
@output = String.new
SSHKit.config.output_verbosity = :debug
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@out)
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@output)
end

def a_host
Expand All @@ -32,7 +32,7 @@ def test_simple_netssh
end
end.run

command_lines = @out.string.lines.select { |line| line.start_with?('Command:') }
command_lines = @output.lines.select { |line| line.start_with?('Command:') }
assert_equal <<-EOEXPECTED.unindent, command_lines.join
Command: /usr/bin/env date
Command: /usr/bin/env ls -l
Expand Down
4 changes: 2 additions & 2 deletions test/unit/backends/test_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup
end

def output
@output ||= StringIO.new
@output ||= String.new
end

def printer
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_download
private

def assert_output_lines(*expected_lines)
assert_equal(expected_lines, output.string.split("\n"))
assert_equal(expected_lines, output.split("\n"))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/formatters/test_dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
end

def output
@output ||= StringIO.new
@output ||= String.new
end

def dot
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_unsupported_class
private

def assert_log_output(expected_output)
assert_equal expected_output, output.string
assert_equal expected_output, output
end
end
end
23 changes: 15 additions & 8 deletions test/unit/formatters/test_pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
end

def output
@output ||= StringIO.new
@output ||= String.new
end

def pretty
Expand All @@ -33,7 +33,7 @@ def pretty

def test_command_lifecycle_logging_with_color
output.stubs(:tty?).returns(true)
execute_command_lifecycle
simulate_command_lifecycle(pretty)

expected_log_lines = [
"\e[0;34;49mINFO\e[0m [\e[0;32;49maaaaaa\e[0m] Running \e[1;33;49m/usr/bin/env a_cmd some args\e[0m as \e[0;34;49muser\e[0m@\e[0;34;49mlocalhost\e[0m",
Expand All @@ -42,7 +42,7 @@ def test_command_lifecycle_logging_with_color
"\e[0;30;49mDEBUG\e[0m [\e[0;32;49maaaaaa\e[0m] \e[0;31;49m\tstderr message\e[0m",
"\e[0;34;49mINFO\e[0m [\e[0;32;49maaaaaa\e[0m] Finished in 1.000 seconds with exit status 0 (\e[1;32;49msuccessful\e[0m)."
]
assert_equal expected_log_lines, output.string.split("\n")
assert_equal expected_log_lines, output.split("\n")
end

{
Expand All @@ -55,12 +55,12 @@ def test_command_lifecycle_logging_with_color
}.each do |level, expected_output|
define_method("test_#{level}_output_without_color") do
pretty.send(level, "Test")
assert_equal expected_output, output.string
assert_equal expected_output, output
end
end

def test_command_lifecycle_logging_without_color
execute_command_lifecycle
simulate_command_lifecycle(pretty)

expected_log_lines = [
' INFO [aaaaaa] Running /usr/bin/env a_cmd some args as user@localhost',
Expand All @@ -70,7 +70,7 @@ def test_command_lifecycle_logging_without_color
' INFO [aaaaaa] Finished in 1.000 seconds with exit status 0 (successful).'
]

assert_equal expected_log_lines, output.string.split("\n")
assert_equal expected_log_lines, output.split("\n")
end

def test_unsupported_class
Expand All @@ -92,9 +92,16 @@ def test_does_not_log_when_verbosity_is_too_low
assert_log_output("\e[0;34;49mINFO\e[0m Some other info\n")
end

def test_can_write_to_output_which_just_supports_append
# Note output doesn't have to be an IO, it only needs to support <<
output = stub(:<<)
pretty = SSHKit::Formatter::Pretty.new(output)
simulate_command_lifecycle(pretty)
end

private

def execute_command_lifecycle
def simulate_command_lifecycle(pretty)
command = SSHKit::Command.new(:a_cmd, 'some args', host: Host.new('user@localhost'))
command.stubs(:uuid).returns('aaaaaa')
command.stubs(:runtime).returns(1)
Expand All @@ -110,7 +117,7 @@ def execute_command_lifecycle
end

def assert_log_output(expected_output)
assert_equal expected_output, output.string
assert_equal expected_output, output
end

end
Expand Down
8 changes: 4 additions & 4 deletions test/unit/formatters/test_simple_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
end

def output
@output ||= StringIO.new
@output ||= String.new
end

def simple
Expand All @@ -19,7 +19,7 @@ def simple
%w(fatal error warn info debug).each do |level|
define_method("test_#{level}_output") do
simple.send(level, 'Test')
assert_equal "Test\n", output.string
assert_equal "Test\n", output
end
end

Expand All @@ -45,7 +45,7 @@ def test_command_lifecycle_logging
"\tstderr message",
'Finished in 1.000 seconds with exit status 0 (successful).'
]
assert_equal expected_log_lines, output.string.split("\n")
assert_equal expected_log_lines, output.split("\n")
end

def test_unsupported_class
Expand All @@ -68,7 +68,7 @@ def test_does_not_log_when_verbosity_is_too_low
private

def assert_log_output(expected_output)
assert_equal expected_output, output.string
assert_equal expected_output, output
end
end
end
8 changes: 8 additions & 0 deletions test/unit/test_color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ def test_does_not_colorize_when_no_tty_and_SSHKIT_COLOR_not_present
color = SSHKit::Color.new(stub(tty?: false), {})
assert_equal 'hi', color.colorize('hi', :red)
end

# The output parameter may not define the tty method eg if it is a Logger.
# In this case we assume showing colors would not be supported
# https://github.com/capistrano/sshkit/pull/246#issuecomment-100358122
def test_does_not_colorize_when_tty_method_not_defined_and_SSHKIT_COLOR_not_present
color = SSHKit::Color.new(stub(), {})
assert_equal 'hi', color.colorize('hi', :red)
end
end
end
6 changes: 3 additions & 3 deletions test/unit/test_coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class TestCoordinator < UnitTest

def setup
super
@out = StringIO.new
@output = String.new
SSHKit.config.output_verbosity = :debug
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@out)
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@output)
SSHKit.config.backend = SSHKit::Backend::Printer
end

Expand Down Expand Up @@ -97,7 +97,7 @@ def actual_execution_times
end

def actual_output_commands
@out.string.lines.select { |line| line.start_with?('Command:') }
@output.lines.select { |line| line.start_with?('Command:') }
end

end
Expand Down

0 comments on commit f37ef77

Please sign in to comment.