Allow setting environment on builder
This commit is contained in:
parent
892a624756
commit
3d13a071c6
|
@ -47,6 +47,9 @@ module Vagrant
|
||||||
# @param [Class] middleware The middleware class
|
# @param [Class] middleware The middleware class
|
||||||
def use(middleware, *args, &block)
|
def use(middleware, *args, &block)
|
||||||
if middleware.kind_of?(Builder)
|
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
|
# Merge in the other builder's stack into our own
|
||||||
self.stack.concat(middleware.stack)
|
self.stack.concat(middleware.stack)
|
||||||
else
|
else
|
||||||
|
@ -109,7 +112,7 @@ module Vagrant
|
||||||
def to_app(env)
|
def to_app(env)
|
||||||
# Prepend the error halt task so errneous environments are halted
|
# Prepend the error halt task so errneous environments are halted
|
||||||
# before the chain even begins.
|
# 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
|
# Convert each middleware into a lambda which takes the next
|
||||||
# middleware.
|
# middleware.
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
|
@ -48,6 +48,17 @@ class ActionBuilderTest < Test::Unit::TestCase
|
||||||
@instance.use other
|
@instance.use other
|
||||||
assert_equal 3, @instance.stack.length
|
assert_equal 3, @instance.stack.length
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "flatten" do
|
context "flatten" do
|
||||||
|
@ -156,7 +167,7 @@ class ActionBuilderTest < Test::Unit::TestCase
|
||||||
middleware.expects(:new).with(anything, env).returns(result)
|
middleware.expects(:new).with(anything, env).returns(result)
|
||||||
@instance.use middleware
|
@instance.use middleware
|
||||||
result = @instance.to_app(env)
|
result = @instance.to_app(env)
|
||||||
assert result.kind_of?(Vagrant::Action::ErrorHalt)
|
assert result.kind_of?(Vagrant::Action::Env::ErrorHalt)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "make non-classes lambdas" do
|
should "make non-classes lambdas" do
|
||||||
|
|
|
@ -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
|
setup do
|
||||||
@klass = Vagrant::Action::ErrorHalt
|
@klass = Vagrant::Action::Env::ErrorHalt
|
||||||
@app, @env = mock_action_data
|
@app, @env = mock_action_data
|
||||||
@instance = @klass.new(@app, @env)
|
@instance = @klass.new(@app, @env)
|
||||||
end
|
end
|
|
@ -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
|
Loading…
Reference in New Issue