Allow setting environment on builder

This commit is contained in:
Mitchell Hashimoto 2010-07-13 21:31:06 -07:00
parent 892a624756
commit 3d13a071c6
7 changed files with 76 additions and 19 deletions

View File

@ -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.

16
lib/vagrant/action/env/error_halt.rb vendored Normal file
View File

@ -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

18
lib/vagrant/action/env/set.rb vendored Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

23
test/vagrant/action/env/set_test.rb vendored Normal file
View File

@ -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