Converted more tests to new vagrant env helpers and removed unused code in ResourceLogger

This commit is contained in:
Mitchell Hashimoto 2010-09-03 20:39:44 -07:00
parent 260f1dcec4
commit f24094bba8
7 changed files with 45 additions and 167 deletions

View File

@ -1,5 +1,4 @@
require 'thread'
require 'mario'
module Vagrant
module Util
@ -12,14 +11,8 @@ module Vagrant
#
# This class is thread safe. The backing class which actually does
# all the logging IO is protected.
#
# This class also handles progress meters of multiple resources and
# handles all the proper interleaving and console updating to
# display the progress meters in a way which doesn't conflict with
# other incoming log messages.
class ResourceLogger
@@singleton_logger = nil
@@progress_reporters = nil
@@writer_lock = Mutex.new
# The resource which this logger represents.
@ -49,12 +42,6 @@ module Vagrant
def reset_singleton_logger!
@@singleton_logger = nil
end
# Returns the progress parts array which contains the various
# progress reporters.
def progress_reporters
@@progress_reporters ||= {}
end
end
def initialize(resource, env)
@ -66,64 +53,10 @@ module Vagrant
[:debug, :info, :error, :fatal].each do |method|
define_method(method) do |message|
@@writer_lock.synchronize do
# We clear the line in case progress reports have been going
# out.
print(cl_reset)
logger.send(method, "[#{resource}] #{message}")
end
# Once again flush the progress reporters since we probably
# cleared any existing ones.
flush_progress
end
end
# Sets a progress report for the resource that this logger
# represents. This progress report is interleaved within the output.
def report_progress(progress, total, show_parts=true)
# Simply add the progress reporter to the list of progress
# reporters
self.class.progress_reporters[resource] = {
:progress => progress,
:total => total,
:show_parts => show_parts
}
# And force an update to occur
flush_progress
end
# Clears the progress report for this resource
def clear_progress
self.class.progress_reporters.delete(resource)
end
def flush_progress
# Don't do anything if there are no progress reporters
return if self.class.progress_reporters.length <= 0
@@writer_lock.synchronize do
reports = []
# First generate all the report percentages and output
self.class.progress_reporters.each do |name, data|
percent = (data[:progress].to_f / data[:total].to_f) * 100
line = "#{name}: #{percent.to_i}%"
line << " (#{data[:progress]} / #{data[:total]})" if data[:show_parts]
reports << line
end
# Output it to stdout
print "#{cl_reset}[progress] #{reports.join(" ")}"
$stdout.flush
end
end
def cl_reset
reset = "\r"
reset += "\e[0K" unless Mario::Platform.windows?
reset
end
end
end
end

View File

@ -20,6 +20,8 @@ module VagrantTestHelpers
str = args.shift || ""
File.open(path.to_s, "w") do |f|
f.puts "Vagrant::Config.run do |config|"
f.puts "config.vagrant.log_output = nil"
f.puts "config.vagrant.home = '#{home_path}'"
f.puts str
f.puts "end"
end

View File

@ -1,8 +1,21 @@
require 'fileutils'
module VagrantTestHelpers
module Path
# Path to the tmp directory for the tests
def tmp_path
Vagrant.source_root.join("test", "tmp")
end
# Path to the "home" directory for the tests
def home_path
tmp_path.join("home")
end
# Cleans all the test temp paths
def clean_paths
FileUtils.rm_rf(tmp_path)
FileUtils.mkdir_p(tmp_path)
end
end
end

View File

@ -2,11 +2,15 @@
$:.unshift(File.dirname(__FILE__))
require 'vagrant'
require 'mario'
require 'contest'
require 'mocha'
require 'support/path'
require 'support/environment'
# Silence Mario by sending log output to black hole
Mario::Platform.logger(nil)
# Add the I18n locale for tests
I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)

View File

View File

@ -373,7 +373,7 @@ class EnvironmentTest < Test::Unit::TestCase
context "loading home directory" do
setup do
@env = mock_environment
@env = vagrant_env
@home_dir = File.expand_path(@env.config.vagrant.home)
File.stubs(:directory?).returns(true)
@ -398,33 +398,27 @@ class EnvironmentTest < Test::Unit::TestCase
end
context "loading box" do
setup do
@box = mock("box")
@box.stubs(:env=)
@env = mock_environment
@env.stubs(:root_path).returns("foo")
end
should "do nothing if the root path is nil" do
env = @klass.new(:cwd => "/")
Vagrant::Box.expects(:find).never
@env.stubs(:root_path).returns(nil)
@env.load_box!
env.load_box!
end
should "not load the box if its not set" do
@env = mock_environment do |config|
config.vm.box = nil
end
env = vagrant_env
assert env.config.vm.box.nil?
Vagrant::Box.expects(:find).never
@env.load_box!
env.load_box!
end
should "set the box to what is found by the Box class" do
Vagrant::Box.expects(:find).with(@env, @env.config.vm.box).once.returns(@box)
@env.load_box!
assert @box.equal?(@env.box)
env = vagrant_env(vagrantfile("config.vm.box = 'foo'"))
@box = mock("box")
@box.stubs(:env=)
Vagrant::Box.expects(:find).with(env, env.config.vm.box).once.returns(@box)
env.load_box!
assert @box.equal?(env.box)
end
end
@ -475,35 +469,29 @@ class EnvironmentTest < Test::Unit::TestCase
end
context "loading blank VMs" do
setup do
@env = mock_environment
end
should "blank the VMs" do
@env = mock_environment do |config|
config.vm.define :foo do |foo_config|
end
env = vagrant_env(vagrantfile(<<-vf))
config.vm.define :foo
config.vm.define :bar
vf
config.vm.define :bar do |bar_config|
end
end
env.load_blank_vms!
@env.load_blank_vms!
assert_equal 2, env.vms.length
assert(env.vms.all? { |name, vm| !vm.created? })
assert_equal 2, @env.vms.length
assert(@env.vms.all? { |name, vm| !vm.created? })
sorted_vms = @env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
sorted_vms = env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
assert_equal [:bar, :foo], sorted_vms
end
should "load the default VM blank if no multi-VMs are specified" do
assert @env.config.vm.defined_vms.empty? # sanity
env = vagrant_env
assert env.config.vm.defined_vms.empty? # sanity
@env.load_blank_vms!
env.load_blank_vms!
assert_equal 1, @env.vms.length
assert !@env.vms.values.first.created?
assert_equal 1, env.vms.length
assert !env.vms.values.first.created?
end
end
end

View File

@ -71,11 +71,6 @@ class ResourceLoggerUtilTest < Test::Unit::TestCase
end
context "logging methods" do
setup do
@instance.stubs(:flush_progress)
@instance.stubs(:cl_reset).returns("")
end
[:debug, :info, :error, :fatal].each do |method|
should "log with the proper format on #{method}" do
message = "bar"
@ -84,62 +79,5 @@ class ResourceLoggerUtilTest < Test::Unit::TestCase
end
end
end
context "reporting progress" do
setup do
@instance.stubs(:flush_progress)
end
should "flush progress" do
@instance.expects(:flush_progress).once
@instance.report_progress(72, 100)
end
should "add the reporter to the progress reporters" do
@instance.report_progress(72, 100)
assert @klass.progress_reporters.has_key?(@instance.resource)
end
end
context "clearing progress" do
setup do
@instance.stubs(:flush_progress)
@klass.progress_reporters.clear
@instance.report_progress(72, 100)
end
should "remove the key from the reporters" do
assert @klass.progress_reporters.has_key?(@instance.resource)
@instance.clear_progress
assert !@klass.progress_reporters.has_key?(@instance.resource)
end
end
context "command line reset" do
setup do
Mario::Platform.logger(nil)
end
context "on windows" do
setup do
Mario::Platform.forced = Mario::Platform::Windows7
end
should "just return \\r for the clear screen" do
assert_equal "\r", @instance.cl_reset
end
end
context "on other platforms" do
setup do
Mario::Platform.forced = Mario::Platform::Linux
end
should "return the full clear screen" do
assert_equal "\r\e[0K", @instance.cl_reset
end
end
end
end
end