Tests for the CLI class
This commit is contained in:
parent
5e42f8bbb2
commit
4be0063d12
|
@ -4,12 +4,12 @@ module Vagrant
|
||||||
class CLI < Thor
|
class CLI < Thor
|
||||||
attr_reader :env
|
attr_reader :env
|
||||||
|
|
||||||
def initialize(args, options, config)
|
def initialize(args=[], options={}, config={})
|
||||||
super
|
super
|
||||||
|
|
||||||
# Set the UI to a shell based UI using the shell object which
|
# Set the UI to a shell based UI using the shell object which
|
||||||
# Thor sets up.
|
# 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.
|
# 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]
|
raise CLIMissingEnvironment.new("This command requires that a Vagrant environment be properly passed in as the last parameter.") if !config[:env]
|
||||||
|
|
|
@ -124,5 +124,20 @@ class Test::Unit::TestCase
|
||||||
_, env = mock_action_data
|
_, env = mock_action_data
|
||||||
[downloader_klass.new(env), tempfile]
|
[downloader_klass.new(env), tempfile]
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue