diff --git a/lib/vagrant/action/builder.rb b/lib/vagrant/action/builder.rb index 7873e9ecc..2da948214 100644 --- a/lib/vagrant/action/builder.rb +++ b/lib/vagrant/action/builder.rb @@ -47,6 +47,9 @@ module Vagrant # @param [Class] middleware The middleware class def use(middleware, *args, &block) if middleware.kind_of?(Builder) + # Prepend with a environment setter if args are given + self.use(Env::Set, *args, &block) if !args.empty? && args.first.is_a?(Hash) + # Merge in the other builder's stack into our own self.stack.concat(middleware.stack) else @@ -109,7 +112,7 @@ module Vagrant def to_app(env) # Prepend the error halt task so errneous environments are halted # before the chain even begins. - items = stack.dup.unshift([ErrorHalt, [], nil]) + items = stack.dup.unshift([Env::ErrorHalt, [], nil]) # Convert each middleware into a lambda which takes the next # middleware. diff --git a/lib/vagrant/action/env/error_halt.rb b/lib/vagrant/action/env/error_halt.rb new file mode 100644 index 000000000..570b693ff --- /dev/null +++ b/lib/vagrant/action/env/error_halt.rb @@ -0,0 +1,16 @@ +module Vagrant + class Action + module Env + # A middleware which simply halts if the environment is erroneous. + class ErrorHalt + def initialize(app,env) + @app = app + end + + def call(env) + @app.call(env) if !env.error? + end + end + end + end +end diff --git a/lib/vagrant/action/env/set.rb b/lib/vagrant/action/env/set.rb new file mode 100644 index 000000000..dd88fe5cf --- /dev/null +++ b/lib/vagrant/action/env/set.rb @@ -0,0 +1,18 @@ +module Vagrant + class Action + module Env + # A middleware which just sets up the environment with some + # options which are passed to it. + class Set + def initialize(app,env,options=nil) + @app = app + env.merge!(options || {}) + end + + def call(env) + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/error_halt.rb b/lib/vagrant/action/error_halt.rb deleted file mode 100644 index 2c4411659..000000000 --- a/lib/vagrant/action/error_halt.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Vagrant - class Action - # A middleware which simply halts if the environment is erroneous. - class ErrorHalt - def initialize(app,env) - @app = app - end - - def call(env) - @app.call(env) if !env.error? - end - end - end -end diff --git a/test/vagrant/action/builder_test.rb b/test/vagrant/action/builder_test.rb index e4c1dc17c..80624ca57 100644 --- a/test/vagrant/action/builder_test.rb +++ b/test/vagrant/action/builder_test.rb @@ -48,6 +48,17 @@ class ActionBuilderTest < Test::Unit::TestCase @instance.use other assert_equal 3, @instance.stack.length end + + should "prepend a set environment task for merging middlewares if given" do + other = @klass.new do + use 1 + end + + @instance.use 0 + @instance.use(other, :foo => :bar) + assert_equal 3, @instance.stack.length + assert_equal Vagrant::Action::Env::Set, @instance.stack[1].first + end end context "flatten" do @@ -156,7 +167,7 @@ class ActionBuilderTest < Test::Unit::TestCase middleware.expects(:new).with(anything, env).returns(result) @instance.use middleware result = @instance.to_app(env) - assert result.kind_of?(Vagrant::Action::ErrorHalt) + assert result.kind_of?(Vagrant::Action::Env::ErrorHalt) end should "make non-classes lambdas" do diff --git a/test/vagrant/action/error_halt_test.rb b/test/vagrant/action/env/error_halt_test.rb similarity index 68% rename from test/vagrant/action/error_halt_test.rb rename to test/vagrant/action/env/error_halt_test.rb index 4f3af8771..c34a010e0 100644 --- a/test/vagrant/action/error_halt_test.rb +++ b/test/vagrant/action/env/error_halt_test.rb @@ -1,8 +1,8 @@ -require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') -class ErrorHaltTest < Test::Unit::TestCase +class ErrorHaltEnvActionTest < Test::Unit::TestCase setup do - @klass = Vagrant::Action::ErrorHalt + @klass = Vagrant::Action::Env::ErrorHalt @app, @env = mock_action_data @instance = @klass.new(@app, @env) end diff --git a/test/vagrant/action/env/set_test.rb b/test/vagrant/action/env/set_test.rb new file mode 100644 index 000000000..15da49de2 --- /dev/null +++ b/test/vagrant/action/env/set_test.rb @@ -0,0 +1,23 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class SetEnvActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::Env::Set + @app, @env = mock_action_data + end + + should "merge in the given options" do + @klass.new(@app, @env, :foo => :bar) + assert_equal :bar, @env[:foo] + end + + should "not merge in anything if not given" do + @klass.new(@app, @env) + assert @env.empty? + end + + should "just continue the chain" do + @app.expects(:call).with(@env) + @klass.new(@app, @env).call(@env) + end +end