Factor out the command environment initialization

This commit is contained in:
Mitchell Hashimoto 2010-08-25 21:49:19 -07:00
parent 64c3d46e1c
commit e74bce8b10
6 changed files with 35 additions and 77 deletions

View File

@ -39,13 +39,9 @@ module Vagrant
/^([-_a-zA-Z0-9]+)(\s+(.+?))?$/.match(usage).to_a[1]
end
def initialize(args=[], options={}, config={})
def initialize(*args)
super
# 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]
@env = config[:env]
@env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell)
initialize_environment(*args)
end
end
end

View File

@ -25,13 +25,9 @@ module Vagrant
CLI.register(self, Base.extract_name_from_usage(usage), usage, description, opts)
end
def initialize(args=[], options={}, config={})
def initialize(*args)
super
# 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]
@env = config[:env]
@env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell)
initialize_environment(*args)
end
end
end

View File

@ -1,6 +1,14 @@
module Vagrant
module Command
module Helpers
# Initializes the environment by pulling the environment out of
# the configuration hash and sets up the UI if necessary.
def initialize_environment(args, options, config)
raise CLIMissingEnvironment.new("This command requires that a Vagrant environment be properly passed in as the last parameter.") if !config[:env]
@env = config[:env]
@env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell)
end
def require_environment
raise NoEnvironmentError.new("No Vagrant environment detected. Run `vagrant init` to set one up.") if !env.root_path
end

View File

@ -6,6 +6,13 @@ class CommandBaseTest < Test::Unit::TestCase
@env = mock_environment
end
context "initialization" do
should "require an environment" do
assert_raises(Vagrant::CLIMissingEnvironment) { @klass.new([], {}, {}) }
assert_nothing_raised { @klass.new([], {}, { :env => @env }) }
end
end
context "extracting a name from a usage string" do
should "extract properly" do
assert_equal "init", @klass.extract_name_from_usage("init")
@ -13,38 +20,4 @@ class CommandBaseTest < Test::Unit::TestCase
assert_equal "ssh-config", @klass.extract_name_from_usage("ssh-config")
end
end
context "setting up a UI" do
setup do
@env.ui = nil
end
should "setup a shell UI" do
silence_stream(STDOUT) { @klass.start([], :env => @env) }
assert @env.ui.is_a?(Vagrant::UI::Shell)
end
should "setup a shell UI only once" do
silence_stream(STDOUT) { @klass.start([], :env => @env) }
ui = @env.ui
silence_stream(STDOUT) { @klass.start([], :env => @env) }
assert @env.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

View File

@ -6,37 +6,10 @@ class CommandGroupBaseTest < Test::Unit::TestCase
@env = mock_environment
end
context "setting up a UI" do
setup do
@env.ui = nil
end
should "setup a shell UI" do
silence_stream(STDOUT) { @klass.start([], :env => @env) }
assert @env.ui.is_a?(Vagrant::UI::Shell)
end
should "setup a shell UI only once" do
silence_stream(STDOUT) { @klass.start([], :env => @env) }
ui = @env.ui
silence_stream(STDOUT) { @klass.start([], :env => @env) }
assert @env.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
context "initialization" do
should "require an environment" do
assert_raises(Vagrant::CLIMissingEnvironment) { @klass.new([], {}, {}) }
assert_nothing_raised { @klass.new([], {}, { :env => @env }) }
end
end
end

View File

@ -12,6 +12,18 @@ class CommandHelpersTest < Test::Unit::TestCase
@command.new(args, {}, { :env => env })
end
context "initializing environment" do
should "raise an exception if no environment is given" do
assert_raises(Vagrant::CLIMissingEnvironment) { command([], nil) }
end
should "not raise an exception if environment is given and setup UI" do
env = mock_environment
assert_nothing_raised { command([], env) }
assert env.ui.is_a?(Vagrant::UI::Shell)
end
end
context "requiring environment" do
setup do
@env = mock_environment