Tests that color and --no-color work properly

This commit is contained in:
Mitchell Hashimoto 2011-11-02 23:16:29 -07:00
parent d753b750e9
commit 974c180d8f
2 changed files with 62 additions and 9 deletions

View File

@ -1,4 +1,5 @@
require "fileutils"
require "pathname"
require "log4r"
require "posix-spawn"
@ -34,25 +35,25 @@ module Acceptance
@logger.info("Initialize isolated environment: #{@tempdir.path}")
# Setup the home and working directories
@homedir = File.join(@tempdir.path, "home")
@workdir = File.join(@tempdir.path, "work")
@homedir = Pathname.new(File.join(@tempdir.path, "home"))
@workdir = Pathname.new(File.join(@tempdir.path, "work"))
FileUtils.mkdir(@homedir)
FileUtils.mkdir(@workdir)
@homedir.mkdir
@workdir.mkdir
@env["HOME"] = @homedir
@env["HOME"] = @homedir.to_s
end
# Executes a command in the context of this isolated environment.
# Any command executed will therefore see our temporary directory
# as the home directory.
def execute(command, *argN)
command = @apps[command] if @apps.has_key?(command)
command = replace_command(command)
# Execute in a separate process, wait for it to complete, and
# return the IO streams.
@logger.info("Executing: #{command} #{argN.inspect}")
pid, stdin, stdout, stderr = popen4(@env, command, *argN, :chdir => @workdir)
pid, stdin, stdout, stderr = popen4(@env, command, *argN, :chdir => @workdir.to_s)
_pid, status = Process.waitpid2(pid)
@logger.info("Exit status: #{status.exitstatus}")
@ -64,6 +65,16 @@ module Acceptance
# Delete the temporary directory
FileUtils.rm_rf(@tempdir.path)
end
private
# This replaces a command with a replacement defined when this
# isolated environment was initialized. If nothing was defined,
# then the command itself is returned.
def replace_command(command)
return @apps[command] if @apps.has_key?(command)
return command
end
end
# This class represents a process which has run via the IsolatedEnvironment.

View File

@ -1,8 +1,50 @@
require File.expand_path("../base", __FILE__)
class VagrantTest < AcceptanceTest
# NOTE: Many tests in this test suite require the `expect`
# tool to be installed, because `expect` will launch with a
# PTY.
class VagrantTestColorOutput < AcceptanceTest
def has_expect?
`which expect`
$?.success?
end
should "output color if there is a TTY" do
skip("Test requires `expect`") if !has_expect?
@environment.workdir.join("color.exp").open("w+") do |f|
f.write(<<-SCRIPT)
catch {spawn vagrant status} reason
expect {
"\e[31m" { exit }
default { exit 1 }
}
SCRIPT
end
results = execute("expect", "color.exp")
assert_equal(0, results.exit_status)
end
should "not output color if there is a TTY but --no-color is present" do
skip("Test requires `expect`") if !has_expect?
@environment.workdir.join("color.exp").open("w+") do |f|
f.write(<<-SCRIPT)
catch {spawn vagrant status --no-color} reason
expect {
"\e[31m" { exit 1 }
default { exit }
}
SCRIPT
end
results = execute("expect", "color.exp")
assert_equal(0, results.exit_status)
end
should "not output color in the absense of a TTY" do
# This should always output an erorr, which on a TTY would
# This should always output an error, which on a TTY would
# output color. We check that this doesn't output color.
# If `vagrant status` itself is broken, another acceptance test
# should catch that. We just assume it works here.