From f37ef77a3e9f3e0c4e513f5cb40ef8cfc05c81ef Mon Sep 17 00:00:00 2001 From: Rob Dupuis Date: Sat, 9 May 2015 01:44:38 +0100 Subject: [PATCH] Removed requirement for formatter outputs to be IO objects Any object which supports << can now be used for example Logger. Fixes compatibility with Airbrussh - see #246. --- lib/sshkit/color.rb | 6 +++--- lib/sshkit/formatters/abstract.rb | 6 +++--- test/functional/backends/test_netssh.rb | 6 +++--- test/unit/backends/test_printer.rb | 4 ++-- test/unit/formatters/test_dot.rb | 4 ++-- test/unit/formatters/test_pretty.rb | 23 +++++++++++++++-------- test/unit/formatters/test_simple_text.rb | 8 ++++---- test/unit/test_color.rb | 8 ++++++++ test/unit/test_coordinator.rb | 6 +++--- 9 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/sshkit/color.rb b/lib/sshkit/color.rb index 07707ac5..f66c4969 100644 --- a/lib/sshkit/color.rb +++ b/lib/sshkit/color.rb @@ -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) @@ -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 diff --git a/lib/sshkit/formatters/abstract.rb b/lib/sshkit/formatters/abstract.rb index 67705bfa..d339a5ea 100644 --- a/lib/sshkit/formatters/abstract.rb +++ b/lib/sshkit/formatters/abstract.rb @@ -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) diff --git a/test/functional/backends/test_netssh.rb b/test/functional/backends/test_netssh.rb index c556a295..02f52d19 100644 --- a/test/functional/backends/test_netssh.rb +++ b/test/functional/backends/test_netssh.rb @@ -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 @@ -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 diff --git a/test/unit/backends/test_printer.rb b/test/unit/backends/test_printer.rb index 165165ea..8b966a1c 100644 --- a/test/unit/backends/test_printer.rb +++ b/test/unit/backends/test_printer.rb @@ -12,7 +12,7 @@ def setup end def output - @output ||= StringIO.new + @output ||= String.new end def printer @@ -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 diff --git a/test/unit/formatters/test_dot.rb b/test/unit/formatters/test_dot.rb index 9b9f69f3..3c803a6f 100644 --- a/test/unit/formatters/test_dot.rb +++ b/test/unit/formatters/test_dot.rb @@ -9,7 +9,7 @@ def setup end def output - @output ||= StringIO.new + @output ||= String.new end def dot @@ -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 diff --git a/test/unit/formatters/test_pretty.rb b/test/unit/formatters/test_pretty.rb index fb88ae5c..8b3c4e1c 100644 --- a/test/unit/formatters/test_pretty.rb +++ b/test/unit/formatters/test_pretty.rb @@ -9,7 +9,7 @@ def setup end def output - @output ||= StringIO.new + @output ||= String.new end def pretty @@ -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", @@ -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 { @@ -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', @@ -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 @@ -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) @@ -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 diff --git a/test/unit/formatters/test_simple_text.rb b/test/unit/formatters/test_simple_text.rb index f7e31ab8..20bead26 100644 --- a/test/unit/formatters/test_simple_text.rb +++ b/test/unit/formatters/test_simple_text.rb @@ -9,7 +9,7 @@ def setup end def output - @output ||= StringIO.new + @output ||= String.new end def simple @@ -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 @@ -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 @@ -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 diff --git a/test/unit/test_color.rb b/test/unit/test_color.rb index 8f360b58..6f2d0cb6 100644 --- a/test/unit/test_color.rb +++ b/test/unit/test_color.rb @@ -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 diff --git a/test/unit/test_coordinator.rb b/test/unit/test_coordinator.rb index 93771b60..af1df4ca 100644 --- a/test/unit/test_coordinator.rb +++ b/test/unit/test_coordinator.rb @@ -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 @@ -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