Factor out the command environment initialization
This commit is contained in:
parent
64c3d46e1c
commit
e74bce8b10
|
@ -39,13 +39,9 @@ module Vagrant
|
||||||
/^([-_a-zA-Z0-9]+)(\s+(.+?))?$/.match(usage).to_a[1]
|
/^([-_a-zA-Z0-9]+)(\s+(.+?))?$/.match(usage).to_a[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(args=[], options={}, config={})
|
def initialize(*args)
|
||||||
super
|
super
|
||||||
|
initialize_environment(*args)
|
||||||
# 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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,13 +25,9 @@ module Vagrant
|
||||||
CLI.register(self, Base.extract_name_from_usage(usage), usage, description, opts)
|
CLI.register(self, Base.extract_name_from_usage(usage), usage, description, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(args=[], options={}, config={})
|
def initialize(*args)
|
||||||
super
|
super
|
||||||
|
initialize_environment(*args)
|
||||||
# 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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module Command
|
module Command
|
||||||
module Helpers
|
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
|
def require_environment
|
||||||
raise NoEnvironmentError.new("No Vagrant environment detected. Run `vagrant init` to set one up.") if !env.root_path
|
raise NoEnvironmentError.new("No Vagrant environment detected. Run `vagrant init` to set one up.") if !env.root_path
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,6 +6,13 @@ class CommandBaseTest < Test::Unit::TestCase
|
||||||
@env = mock_environment
|
@env = mock_environment
|
||||||
end
|
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
|
context "extracting a name from a usage string" do
|
||||||
should "extract properly" do
|
should "extract properly" do
|
||||||
assert_equal "init", @klass.extract_name_from_usage("init")
|
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")
|
assert_equal "ssh-config", @klass.extract_name_from_usage("ssh-config")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -6,37 +6,10 @@ class CommandGroupBaseTest < Test::Unit::TestCase
|
||||||
@env = mock_environment
|
@env = mock_environment
|
||||||
end
|
end
|
||||||
|
|
||||||
context "setting up a UI" do
|
context "initialization" do
|
||||||
setup do
|
should "require an environment" do
|
||||||
@env.ui = nil
|
assert_raises(Vagrant::CLIMissingEnvironment) { @klass.new([], {}, {}) }
|
||||||
end
|
assert_nothing_raised { @klass.new([], {}, { :env => @env }) }
|
||||||
|
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,18 @@ class CommandHelpersTest < Test::Unit::TestCase
|
||||||
@command.new(args, {}, { :env => env })
|
@command.new(args, {}, { :env => env })
|
||||||
end
|
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
|
context "requiring environment" do
|
||||||
setup do
|
setup do
|
||||||
@env = mock_environment
|
@env = mock_environment
|
||||||
|
|
Loading…
Reference in New Issue