Skip to content

Commit

Permalink
Update tests to work with sshkit master / no user
Browse files Browse the repository at this point in the history
This accounts for changes in sshkit's local username lookup introduced
by capistrano/sshkit#293 and also accounts for the conditional
formatting changes introduced by mattbrictson#66.

The latter did not previously cause failed tests, because the username
was stubbed in the tests, causing the relevant code to not be exercised.

Now the username is looked up through sshkit, which results in an empty
username on sshkit < 1.7.0, so the no-username display is triggered.

Unfortunately we need to keep the Etc stub for now to avoid crashing on
Windows with sshkit 1.7.0 and 1.7.1.
  • Loading branch information
felixbuenemann committed Nov 1, 2015
1 parent c7b73b5 commit a633f64
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions test/airbrussh/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ class Airbrussh::FormatterTest < Minitest::Test
def setup
@output = StringIO.new
@log_file = StringIO.new
@user = "test_user"

# Force a consistent username when SSHKit::Backend::Local is used.
# This is also necessary to work around a Windows-specific bug in SSHKit,
# where it relies on Etc.getpwuid to get the username, even though this
# doesn't work on Windows.
Etc.stubs(:getpwuid => stub(:name => @user))
# sshkit > 1.6.1 && <= 1.7.1 uses Etc.getpwuid.name for local user lookup
# which causes a NoMethodError on Windows, so we need to stub it here.
Etc.stubs(:getpwuid => stub(:name => "stubbed_user")) unless sshkit_after?("1.7.1")
@user = SSHKit::Host.new(:local).username # nil on sshkit < 1.7.0
@user_at_localhost = [@user, "localhost"].compact.join("@")
end

def teardown
Expand Down Expand Up @@ -52,7 +50,7 @@ def test_formats_execute_with_color
assert_output_lines(
" 01 \e[0;33;49mecho foo\e[0m\n",
" 01 foo\n",
/ \e\[0;32;49m✔ 01 #{@user}@localhost\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/
/ \e\[0;32;49m✔ 01 #{@user_at_localhost}\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/
)

assert_log_file_lines(
Expand All @@ -75,7 +73,7 @@ def test_formats_execute_without_color
assert_output_lines(
" 01 echo foo\n",
" 01 foo\n",
/ ✔ 01 #{@user}@localhost \d.\d+s\n/
/ ✔ 01 #{@user_at_localhost} \d.\d+s\n/
)

assert_log_file_lines(
Expand All @@ -94,7 +92,7 @@ def test_formats_without_command_output

assert_output_lines(
" 01 ls -l\n",
/ ✔ 01 #{@user}@localhost \d.\d+s\n/
/ ✔ 01 #{@user_at_localhost} \d.\d+s\n/
)
end

Expand All @@ -121,7 +119,7 @@ def test_formats_failing_execute_with_color
expected_output = [
" 01 \e[0;33;49mecho hi\e[0m\n",
" 01 hi\n",
/ \e\[0;32;49m✔ 01 #{@user}@localhost\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/,
/ \e\[0;32;49m✔ 01 #{@user_at_localhost}\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/,
" 02 \e[0;33;49mls _file_does_not_exist\e[0m\n"
]

Expand Down Expand Up @@ -163,7 +161,7 @@ def test_formats_capture_with_color
assert_output_lines(
" 01 \e[0;33;49mls -1 airbrussh.gemspec\e[0m\n",
" 01 airbrussh.gemspec\n",
/ \e\[0;32;49m✔ 01 #{@user}@localhost\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/
/ \e\[0;32;49m✔ 01 #{@user_at_localhost}\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/
)

assert_log_file_lines(
Expand All @@ -183,7 +181,7 @@ def test_formats_capture_without_color
assert_output_lines(
" 01 ls -1 airbrussh.gemspec\n",
" 01 airbrussh.gemspec\n",
/ ✔ 01 #{@user}@localhost \d.\d+s\n/
/ ✔ 01 #{@user_at_localhost} \d.\d+s\n/
)

assert_log_file_lines(
Expand Down Expand Up @@ -235,20 +233,20 @@ def test_handles_rake_tasks
"00:00 special_rake_task\n",
" 01 echo command 1\n",
" 01 command 1\n",
/ ✔ 01 #{@user}@localhost \d.\d+s\n/,
/ ✔ 01 #{@user_at_localhost} \d.\d+s\n/,
" Starting command 2\n",
" 02 echo command 2\n",
" 02 command 2\n",
/ ✔ 02 #{@user}@localhost \d.\d+s\n/,
/ ✔ 02 #{@user_at_localhost} \d.\d+s\n/,
"00:00 special_rake_task_2\n",
" New task starting\n",
"00:00 special_rake_task_3\n",
" 01 echo command 3\n",
" 01 command 3\n",
/ ✔ 01 #{@user}@localhost \d.\d+s\n/,
/ ✔ 01 #{@user_at_localhost} \d.\d+s\n/,
" 02 echo command 4\n",
" 02 command 4\n",
/ ✔ 02 #{@user}@localhost \d.\d+s\n/,
/ ✔ 02 #{@user_at_localhost} \d.\d+s\n/,
" All done\n"
)

Expand Down Expand Up @@ -314,14 +312,14 @@ def test_interleaved_debug_and_info_commands
"00:00 interleaving_test\n",
" 01 echo command 1\n",
" 01 command 1\n",
/ ✔ 01 #{@user}@localhost \d.\d+s\n/,
/ ✔ 01 #{@user_at_localhost} \d.\d+s\n/,
" Info line should be output\n",
" 02 echo command 2\n",
" 02 command 2\n",
/ ✔ 02 #{@user}@localhost \d.\d+s\n/,
/ ✔ 02 #{@user_at_localhost} \d.\d+s\n/,
" 03 echo command 4\n",
" 03 command 4\n",
/ ✔ 03 #{@user}@localhost \d.\d+s\n/
/ ✔ 03 #{@user_at_localhost} \d.\d+s\n/
)
end

Expand All @@ -330,11 +328,6 @@ def test_interleaved_debug_and_info_commands
def on_local(task_name=nil, &block)
define_and_execute_rake_task(task_name) do
local_backend = SSHKit::Backend::Local.new(&block)
# Note: The Local backend default log changed to include the user name around version 1.7.1
# Therefore we inject a user in order to make the logging consistent in old versions (i.e. 1.6.1)
unless sshkit_after?("1.6.1")
local_backend.instance_variable_get(:@host).user = @user
end
local_backend.run
end
end
Expand Down Expand Up @@ -367,7 +360,7 @@ def assert_log_file_lines(*command_lines)

def command_running(command, level="INFO")
level_tag_color = (level == "INFO") ? :blue : :black
/#{send(level_tag_color, level)} \[#{green('\w+')}\] Running #{bold_yellow("#{command}")} as #{blue(@user)}@#{blue('localhost')}\n/
/#{send(level_tag_color, level)} \[#{green('\w+')}\] Running #{bold_yellow("#{command}")} #{@user ? "as #{blue(@user)}@" : "on "}#{blue('localhost')}\n/
end

def command_started_debug(command)
Expand Down

0 comments on commit a633f64

Please sign in to comment.