Tests for the CLI class

This commit is contained in:
Mitchell Hashimoto 2010-08-23 23:53:27 -07:00
parent 5e42f8bbb2
commit 4be0063d12
3 changed files with 59 additions and 2 deletions

View File

@ -4,12 +4,12 @@ module Vagrant
class CLI < Thor
attr_reader :env
def initialize(args, options, config)
def initialize(args=[], options={}, config={})
super
# Set the UI to a shell based UI using the shell object which
# Thor sets up.
Vagrant.ui = UI::Shell.new(shell)
Vagrant.ui = UI::Shell.new(shell) if !Vagrant.ui.is_a?(UI::Shell)
# The last argument must _always_ be a Vagrant Environment class.
raise CLIMissingEnvironment.new("This command requires that a Vagrant environment be properly passed in as the last parameter.") if !config[:env]

View File

@ -124,5 +124,20 @@ class Test::Unit::TestCase
_, env = mock_action_data
[downloader_klass.new(env), tempfile]
end
# Silences one or more streams for the duration of a block.
# This was taken from the facets library.
def silence_stream(*streams) #:yeild:
on_hold = streams.collect{ |stream| stream.dup }
streams.each do |stream|
stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
stream.sync = true
end
yield
ensure
streams.each_with_index do |stream, i|
stream.reopen(on_hold[i])
end
end
end

42
test/vagrant/cli_test.rb Normal file
View File

@ -0,0 +1,42 @@
require "test_helper"
class CLITest < Test::Unit::TestCase
setup do
@klass = Vagrant::CLI
@env = mock_environment
end
context "setting up a UI" do
setup do
Vagrant.ui = nil
end
should "setup a shell UI" do
silence_stream(STDOUT) { @klass.start([], :env => @env) }
assert Vagrant.ui.is_a?(Vagrant::UI::Shell)
end
should "setup a shell UI only once" do
silence_stream(STDOUT) { @klass.start([], :env => @env) }
ui = Vagrant.ui
silence_stream(STDOUT) { @klass.start([], :env => @env) }
assert Vagrant.ui.equal?(ui)
end
end
context "requiring an environment" do
should "raise an exception if the environment is not sent in" do
assert_raises(Vagrant::CLIMissingEnvironment) {
@klass.start([])
}
end
should "not raise an exception if the environment is properly sent in" do
silence_stream(STDOUT) do
assert_nothing_raised {
@klass.start([], :env => @env)
}
end
end
end
end