diff --git a/lib/vagrant/command/base.rb b/lib/vagrant/command/base.rb index 150b6ec67..55044a7bf 100644 --- a/lib/vagrant/command/base.rb +++ b/lib/vagrant/command/base.rb @@ -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 diff --git a/lib/vagrant/command/group_base.rb b/lib/vagrant/command/group_base.rb index cf31f3386..d0c842a6f 100644 --- a/lib/vagrant/command/group_base.rb +++ b/lib/vagrant/command/group_base.rb @@ -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 diff --git a/lib/vagrant/command/helpers.rb b/lib/vagrant/command/helpers.rb index bbf39a3d2..a14a83658 100644 --- a/lib/vagrant/command/helpers.rb +++ b/lib/vagrant/command/helpers.rb @@ -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 diff --git a/test/vagrant/command/base_test.rb b/test/vagrant/command/base_test.rb index 6f088ae17..2df09f9fa 100644 --- a/test/vagrant/command/base_test.rb +++ b/test/vagrant/command/base_test.rb @@ -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 diff --git a/test/vagrant/command/group_base_test.rb b/test/vagrant/command/group_base_test.rb index 8b9be9a48..1d753fa0f 100644 --- a/test/vagrant/command/group_base_test.rb +++ b/test/vagrant/command/group_base_test.rb @@ -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 diff --git a/test/vagrant/command/helpers_test.rb b/test/vagrant/command/helpers_test.rb index 57229f849..020752513 100644 --- a/test/vagrant/command/helpers_test.rb +++ b/test/vagrant/command/helpers_test.rb @@ -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