Environment UI class can now be set through the environment init

This commit is contained in:
Mitchell Hashimoto 2011-12-03 16:07:34 -08:00
parent 4d71b7bba3
commit 1355487a04
4 changed files with 68 additions and 19 deletions

View File

@ -7,25 +7,23 @@ require 'vagrant/cli'
logger = Log4r::Logger.new("vagrant::bin::vagrant")
logger.info("`vagrant` invoked: #{ARGV.inspect}")
# These will be the options that are passed to initialze the Vagrant
# environment.
opts = {}
# Disable color if the proper argument was passed
if !$stdout.tty? || ARGV.include?("--no-color")
opts[:ui_class] = Vagrant::UI::Basic
else
opts[:ui_class] = Vagrant::UI::Colored
end
# Create the environment, which is the cwd of wherever the
# `vagrant` command was invoked from
logger.debug("Creating Vagrant environment")
env = Vagrant::Environment.new
env = Vagrant::Environment.new(opts)
begin
# Disable color if the proper argument was passed
ui = nil
if !$stdout.tty? || ARGV.include?("--no-color")
ui = Vagrant::UI::Basic.new(env)
else
ui = Vagrant::UI::Colored.new(env)
end
# Set the UI early in case any errors are raised, and load
# the config immediately, so we gather any new commands from
# plugins
env.ui = ui
# Load the environment
logger.debug("Loading environment")
env.load!

View File

@ -63,7 +63,8 @@ module Vagrant
:vm => nil,
:cwd => nil,
:vagrantfile_name => nil,
:lock_path => nil
:lock_path => nil,
:ui_class => nil
}.merge(opts || {})
# Set the default working directory to look for the vagrantfile
@ -75,9 +76,13 @@ module Vagrant
opts[:vagrantfile_name] ||= ["Vagrantfile", "vagrantfile"]
opts[:vagrantfile_name] = [opts[:vagrantfile_name]] if !opts[:vagrantfile_name].is_a?(Array)
opts.each do |key, value|
instance_variable_set("@#{key}".to_sym, opts[key])
end
# Set instance variables for all the configuration parameters.
@parent = opts[:parent]
@vm = opts[:vm]
@cwd = opts[:cwd]
@vagrantfile_name = opts[:vagrantfile_name]
@lock_path = opts[:lock_path]
@ui_class = opts[:ui_class]
@loaded = false
@lock_acquired = false
@ -245,7 +250,8 @@ module Vagrant
result.env = self
result
else
UI::Silent.new(self)
ui_class = @ui_class || UI::Silent
ui_class.new(self)
end
end

15
test/unit/base.rb Normal file
View File

@ -0,0 +1,15 @@
require "rubygems"
require "rspec/autorun"
# Require Vagrant itself so we can reference the proper
# classes to test.
require "vagrant"
# Do not buffer output
$stdout.sync = true
$stderr.sync = true
# Configure RSpec
RSpec.configure do |c|
c.expect_with :rspec, :stdlib
end

View File

@ -0,0 +1,30 @@
require File.expand_path("../../base", __FILE__)
require "pathname"
describe Vagrant::Environment do
describe "current working directory" do
it "is the cwd by default" do
described_class.new.cwd.should == Pathname.new(Dir.pwd)
end
it "is set to the cwd given" do
instance = described_class.new(:cwd => "foobarbaz")
instance.cwd.should == Pathname.new("foobarbaz")
end
end
describe "ui" do
it "should be a silent UI by default" do
described_class.new.ui.should be_kind_of(Vagrant::UI::Silent)
end
it "should be a UI given in the constructor" do
# Create a custom UI for our test
class CustomUI < Vagrant::UI::Interface; end
instance = described_class.new(:ui_class => CustomUI)
instance.ui.should be_kind_of(CustomUI)
end
end
end