Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to allow rake test to work on Windows; set up Windows-based CI #55

Merged
merged 7 commits into from
Jul 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Gem Version](https://badge.fury.io/rb/airbrussh.svg)](http://badge.fury.io/rb/airbrussh)
[![Build Status](https://travis-ci.org/mattbrictson/airbrussh.svg?branch=master)](https://travis-ci.org/mattbrictson/airbrussh)
[![Build status](https://ci.appveyor.com/api/projects/status/h052rlq54sne3md6/branch/master?svg=true)](https://ci.appveyor.com/project/mattbrictson/airbrussh/branch/master)
[![Code Climate](https://codeclimate.com/github/mattbrictson/airbrussh/badges/gpa.svg)](https://codeclimate.com/github/mattbrictson/airbrussh)
[![Coverage Status](https://coveralls.io/repos/mattbrictson/airbrussh/badge.svg?branch=master)](https://coveralls.io/r/mattbrictson/airbrussh?branch=master)

Expand Down
18 changes: 18 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '{build}'

skip_tags: true

environment:
matrix:
- ruby_version: "21"
- ruby_version: "21-x64"

install:
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
- gem install bundler --no-document --conservative --version 1.10.5
- bundle install --retry=3

test_script:
- bundle exec rake

build: off
110 changes: 63 additions & 47 deletions test/airbrussh/formatter_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8
require "minitest_helper"
require "bundler"
require "etc"

# rubocop:disable Metrics/LineLength

Expand All @@ -11,6 +12,12 @@ 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))
end

def teardown
Expand All @@ -19,10 +26,16 @@ def teardown
end

def configure
config = Airbrussh::Configuration.new
config.log_file = @log_file
yield(config, SSHKit.config)
SSHKit.config.output = formatter_class.new(@output, config)
airbrussh_config = Airbrussh::Configuration.new
airbrussh_config.log_file = @log_file

# Replace command map so it doesn't prefix every cmd with /usr/bin/env
sshkit_config = SSHKit.config
sshkit_config.command_map = Hash.new { |h, cmd| h[cmd] = cmd.to_s }

yield(airbrussh_config, sshkit_config)

sshkit_config.output = formatter_class.new(@output, airbrussh_config)
end

def test_formats_execute_with_color
Expand All @@ -39,12 +52,12 @@ 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;49m0.\d+s\e\[0m\n/
/ \e\[0;32;49m✔ 01 #{@user}@localhost\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/
)

assert_log_file_lines(
command_running("echo foo"),
command_started_debug("/usr/bin/env echo foo"),
command_started_debug("echo foo"),
command_std_stream(:stdout, "foo"),
command_success
)
Expand All @@ -62,7 +75,7 @@ def test_formats_execute_without_color
assert_output_lines(
" 01 echo foo\n",
" 01 foo\n",
/ ✔ 01 #{@user}@localhost 0.\d+s\n/
/ ✔ 01 #{@user}@localhost \d.\d+s\n/
)

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

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

Expand All @@ -96,7 +109,7 @@ def test_formats_failing_execute_with_color
on_local do
begin
execute(:echo, "hi")
execute(:nogo, "mr")
execute(:ls, "_file_does_not_exist")
# rubocop:disable Lint/HandleExceptions
rescue SSHKit::Command::Failed => error
# rubocop:enable Lint/HandleExceptions
Expand All @@ -108,12 +121,11 @@ 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 test_user@localhost\e\[0m \e\[0;90;49m0.\d+s\e\[0m\n/,
" 02 \e[0;33;49mnogo mr\e[0m\n"
/ \e\[0;32;49m✔ 01 #{@user}@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"
]

# /usr/bin/ prefix seems to be present on linux but not on OS X
error_message = "(/usr/bin/env|env): nogo: No such file or directory"
error_message = "ls: (cannot access )?_file_does_not_exist: No such file or directory"

# Don't know why this log line doesn't show up in SSHKit 1.6.1
expected_output << / 02 #{error_message}\n/ if sshkit_after?("1.6.1")
Expand All @@ -122,17 +134,17 @@ def test_formats_failing_execute_with_color

expected_log_output = [
command_running("echo hi"),
command_started_debug("/usr/bin/env echo hi"),
command_started_debug("echo hi"),
command_std_stream(:stdout, "hi"),
command_success,

command_running("nogo mr"),
command_started_debug("/usr/bin/env nogo mr")
command_running("ls _file_does_not_exist"),
command_started_debug("ls _file_does_not_exist")
]

if sshkit_after?("1.6.1")
expected_log_output << command_std_stream(:stderr, error_message)
expected_log_output << "\e[0m" unless sshkit_master?
expected_log_output << "\e[0m" if color_output?
end

assert_log_file_lines(*expected_log_output)
Expand All @@ -151,7 +163,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;49m0.\d+s\e\[0m\n/
/ \e\[0;32;49m✔ 01 #{@user}@localhost\e\[0m \e\[0;90;49m\d.\d+s\e\[0m\n/
)

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

assert_log_file_lines(
Expand All @@ -186,15 +198,16 @@ def test_does_not_output_test_commands
end

on_local do
test("[ -f ~ ]")
test("echo hi")
end

assert_output_lines

assert_log_file_lines(
command_running("\\[ -f ~ \\]", "DEBUG"),
command_started_debug("\\[ -f ~ \\]"),
command_failed(256)
command_running("echo hi", "DEBUG"),
command_started_debug("echo hi"),
command_std_stream(:stdout, "hi"),
command_success("DEBUG")
)
end

Expand Down Expand Up @@ -222,20 +235,20 @@ def test_handles_rake_tasks
"00:00 special_rake_task\n",
" 01 echo command 1\n",
" 01 command 1\n",
/ ✔ 01 #{@user}@localhost 0.\d+s\n/,
/ ✔ 01 #{@user}@localhost \d.\d+s\n/,
" Starting command 2\n",
" 02 echo command 2\n",
" 02 command 2\n",
/ ✔ 02 #{@user}@localhost 0.\d+s\n/,
/ ✔ 02 #{@user}@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 0.\d+s\n/,
/ ✔ 01 #{@user}@localhost \d.\d+s\n/,
" 02 echo command 4\n",
" 02 command 4\n",
/ ✔ 02 #{@user}@localhost 0.\d+s\n/,
/ ✔ 02 #{@user}@localhost \d.\d+s\n/,
" All done\n"
)

Expand Down Expand Up @@ -286,10 +299,10 @@ def test_interleaved_debug_and_info_commands
end

on_local("interleaving_test") do
test("[ -f ~ ]")
test("echo hi")
# test methods are logged at debug level by default
execute(:echo, "command 1")
test("[ -f . ]")
test("echo hello")
debug("Debug line should not be output")
info("Info line should be output")
execute(:echo, "command 2")
Expand All @@ -301,14 +314,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 0.\d+s\n/,
/ ✔ 01 #{@user}@localhost \d.\d+s\n/,
" Info line should be output\n",
" 02 echo command 2\n",
" 02 command 2\n",
/ ✔ 02 #{@user}@localhost 0.\d+s\n/,
/ ✔ 02 #{@user}@localhost \d.\d+s\n/,
" 03 echo command 4\n",
" 03 command 4\n",
/ ✔ 03 #{@user}@localhost 0.\d+s\n/
/ ✔ 03 #{@user}@localhost \d.\d+s\n/
)
end

Expand All @@ -319,7 +332,9 @@ def on_local(task_name=nil, &block)
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)
local_backend.instance_variable_get(:@host).user = @user
unless sshkit_after?("1.6.1")
local_backend.instance_variable_get(:@host).user = @user
end
local_backend.run
end
end
Expand Down Expand Up @@ -352,7 +367,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("/usr/bin/env #{command}")} as #{blue(@user)}@#{blue('localhost')}\n/
/#{send(level_tag_color, level)} \[#{green('\w+')}\] Running #{bold_yellow("#{command}")} as #{blue(@user)}@#{blue('localhost')}\n/
end

def command_started_debug(command)
Expand All @@ -367,12 +382,13 @@ def command_std_stream(stream, output)
/#{black('DEBUG')} \[#{green('\w+')}\] #{formatted_output}/
end

def command_success
/#{blue('INFO')} \[#{green('\w+')}\] Finished in 0.\d+ seconds with exit status 0 \(#{bold_green("successful")}\).\n/
def command_success(level="INFO")
level_tag_color = (level == "INFO") ? :blue : :black
/#{send(level_tag_color, level)} \[#{green('\w+')}\] Finished in \d.\d+ seconds with exit status 0 \(#{bold_green("successful")}\).\n/
end

def command_failed(exit_status)
/#{black('DEBUG')} \[#{green('\w+')}\] Finished in 0.\d+ seconds with exit status #{exit_status} \(#{bold_red("failed")}\)/
/#{black('DEBUG')} \[#{green('\w+')}\] Finished in \d.\d+ seconds with exit status #{exit_status} \(#{bold_red("failed")}\)/
end

{
Expand All @@ -386,19 +402,19 @@ def command_failed(exit_status)
:bold_yellow => "1;33;49"
}.each do |color, code|
define_method(color) do |string|
color_if_legacy(string, code)
if color_output?
"\\e\\[#{code}m#{string}\\e\\[0m"
else
string
end
end
end

def color_if_legacy(text, color)
# SSHKit versions up to 1.7.1 added colors to the log file even though it did not have a tty.
# Versions after this don't, so we must match output both with, and without colors
# depending on the SSHKit version.
if sshkit_after?("1.7.1") || sshkit_master?
text
else
"\\e\\[#{color}m#{text}\\e\\[0m"
end
# Whether or not SSHKit emits color depends on the test environment. SSHKit
# versions up to 1.7.1 added colors to the log file, but only if
# `$stdout.tty?` is true. Later versions never output color to the log file.
def color_output?
$stdout.tty? && !(sshkit_after?("1.7.1") || sshkit_master?)
end

def sshkit_after?(version)
Expand Down
10 changes: 9 additions & 1 deletion test/airbrussh/log_file_formatter_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "minitest_helper"
require "airbrussh/log_file_formatter"
require "fileutils"
require "tempfile"

class Airbrussh::LogFileFormatterTest < Minitest::Test
Expand Down Expand Up @@ -30,7 +31,7 @@ def test_writes_through_via_pretty_formatter
end

def test_creates_log_directory_and_file
Dir.mktmpdir("airbrussh-test-") do |dir|
with_tempdir do |dir|
log_file = File.join(dir, "log", "capistrano.log")
Airbrussh::LogFileFormatter.new(log_file)
assert(File.exist?(log_file))
Expand All @@ -42,4 +43,11 @@ def test_creates_log_directory_and_file
def output
@output ||= IO.read(@file.path)
end

def with_tempdir
dir = Dir.mktmpdir("airbrussh-test-")
yield(dir)
ensure
FileUtils.rm_rf(dir)
end
end