1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
# encoding: UTF-8
require "minitest_helper"
require "stringio"
require "airbrussh/configuration"
require "airbrussh/console"
class Airbrussh::ConsoleTest < Minitest::Test
def setup
@output = StringIO.new
end
def test_color_is_allowed_for_tty
console = configured_console(:tty => true) do |config|
config.color = :auto
end
console.print_line("The \e[0;32;49mgreen\e[0m text")
assert_equal("The \e[0;32;49mgreen\e[0m text\n", output)
end
def test_color_is_can_be_forced
console = configured_console(:tty => false) do |config|
config.color = true
end
console.print_line("The \e[0;32;49mgreen\e[0m text")
assert_equal("The \e[0;32;49mgreen\e[0m text\n", output)
end
def test_color_is_can_be_forced_via_env
console = configured_console(:tty => false) do |config|
config.color = :auto
end
ENV.stubs(:[]).with("SSHKIT_COLOR").returns("1")
console.print_line("The \e[0;32;49mgreen\e[0m text")
assert_equal("The \e[0;32;49mgreen\e[0m text\n", output)
end
def test_color_is_stripped_for_non_tty
console = configured_console(:tty => false) do |config|
config.color = :auto
end
console.print_line("The \e[0;32;49mgreen\e[0m text")
assert_equal("The green text\n", output)
end
def test_color_can_be_disabled_for_tty
console = configured_console(:tty => true) do |config|
config.color = false
end
console.print_line("The \e[0;32;49mgreen\e[0m text")
assert_equal("The green text\n", output)
end
def test_truncates_to_winsize
console = configured_console(:tty => true) do |config|
config.color = false
config.truncate = :auto
end
IO.stubs(:console => stub(:winsize => [100, 20]))
console.print_line("The quick brown fox jumps over the lazy dog.")
assert_equal("The quick brown fox…\n", output)
end
def test_ignores_ascii_color_codes_when_determing_truncation_amount
console = configured_console(:tty => true) do |config|
config.color = true
config.truncate = :auto
end
IO.stubs(:console => stub(:winsize => [100, 20]))
twenty_chars_plus_color = "\e[0;31;49m#{'a' * 20}\e[0m"
console.print_line(twenty_chars_plus_color)
assert_equal("#{twenty_chars_plus_color}\n", output)
end
def test_truncates_to_explicit_width
console = configured_console(:tty => true) do |config|
config.color = false
config.truncate = 25
end
console.print_line("The quick brown fox jumps over the lazy dog.")
assert_equal("The quick brown fox jump…\n", output)
end
def test_truncation_can_be_disabled
console = configured_console(:tty => true) do |config|
config.truncate = false
end
IO.expects(:console).never
console.print_line("The quick brown fox jumps over the lazy dog.")
assert_equal("The quick brown fox jumps over the lazy dog.\n", output)
end
# SSHKit sometimes returns raw ASCII-8BIT data that cannot be converted to
# UTF-8, which could frustrate the truncation logic. Make sure that Console
# recovers gracefully in this scenario.
def test_truncates_improperly_encoded_ascii_string
console = configured_console(:tty => true) do |config|
config.color = false
config.truncate = 10
end
console.print_line(ascii_8bit("The ‘quick’ brown fox"))
# Note that the left-apostrophe character is actually 3 bytes as raw
# ASCII-8BIT, which accounts for the short truncated value.
assert_equal(ascii_8bit("The ‘...\n"), ascii_8bit(output))
end
def test_print_line_handles_invalid_utf8
console = configured_console(:tty => false)
invalid_utf8 = "The ‘quick’ brown fox"
.encode("Windows-1255")
.force_encoding("UTF-8")
console.print_line(invalid_utf8)
assert_equal("The �quick� brown fox\n", output)
end
def test_doesnt_truncates_to_zero_width
console = configured_console(:tty => true) do |config|
config.color = false
config.truncate = 0
end
console.print_line("The quick brown fox jumps over the lazy dog.")
assert_equal("The quick brown fox jumps over the lazy dog.\n", output)
end
private
def ascii_8bit(string)
string.force_encoding("ASCII-8BIT")
end
def output
@output.string
end
def configured_console(opts={})
config = Airbrussh::Configuration.new
yield(config) if block_given?
@output.stubs(:tty? => opts.fetch(:tty, false))
Airbrussh::Console.new(@output, config)
end
end
|