Modify task DSL API to take env on initialization
This commit is contained in:
parent
356d778746
commit
1d6ae126ed
@ -3,8 +3,8 @@ module Producer
|
|||||||
class Task
|
class Task
|
||||||
class << self
|
class << self
|
||||||
def evaluate(name, env, *args, &block)
|
def evaluate(name, env, *args, &block)
|
||||||
dsl = DSL.new(&block)
|
dsl = DSL.new(env, &block)
|
||||||
dsl.evaluate(env, *args)
|
dsl.evaluate(*args)
|
||||||
Task.new(name, dsl.actions, dsl.condition)
|
Task.new(name, dsl.actions, dsl.condition)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -15,16 +15,16 @@ module Producer
|
|||||||
|
|
||||||
define_action :file_write, Actions::FileWriter
|
define_action :file_write, Actions::FileWriter
|
||||||
|
|
||||||
attr_accessor :actions
|
attr_accessor :env, :actions
|
||||||
|
|
||||||
def initialize(&block)
|
def initialize(env, &block)
|
||||||
|
@env = env
|
||||||
@block = block
|
@block = block
|
||||||
@actions = []
|
@actions = []
|
||||||
@condition = true
|
@condition = true
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluate(env, *args)
|
def evaluate(*args)
|
||||||
@env = env
|
|
||||||
instance_exec *args, &@block
|
instance_exec *args, &@block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ module Producer::Core
|
|||||||
describe Task::DSL do
|
describe Task::DSL do
|
||||||
let(:block) { proc { } }
|
let(:block) { proc { } }
|
||||||
let(:env) { double 'env' }
|
let(:env) { double 'env' }
|
||||||
subject(:dsl) { Task::DSL.new(&block) }
|
subject(:dsl) { Task::DSL.new(env, &block) }
|
||||||
|
|
||||||
%w[echo sh file_write].each do |action|
|
%w[echo sh file_write].each do |action|
|
||||||
it "has `#{action}' action defined" do
|
it "has `#{action}' action defined" do
|
||||||
@ -20,6 +20,10 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#initialize' do
|
describe '#initialize' do
|
||||||
|
it 'assigns the given env' do
|
||||||
|
expect(dsl.env).to be env
|
||||||
|
end
|
||||||
|
|
||||||
it 'assigns no action' do
|
it 'assigns no action' do
|
||||||
expect(dsl.actions).to be_empty
|
expect(dsl.actions).to be_empty
|
||||||
end
|
end
|
||||||
@ -49,7 +53,7 @@ module Producer::Core
|
|||||||
let(:block) { proc { throw :task_code } }
|
let(:block) { proc { throw :task_code } }
|
||||||
|
|
||||||
it 'evaluates its code' do
|
it 'evaluates its code' do
|
||||||
expect { dsl.evaluate env }
|
expect { dsl.evaluate }
|
||||||
.to throw_symbol :task_code
|
.to throw_symbol :task_code
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,7 +61,7 @@ module Producer::Core
|
|||||||
let(:block) { proc { |e| throw e } }
|
let(:block) { proc { |e| throw e } }
|
||||||
|
|
||||||
it 'passes arguments as block parameters' do
|
it 'passes arguments as block parameters' do
|
||||||
expect { dsl.evaluate env, :some_argument }
|
expect { dsl.evaluate :some_argument }
|
||||||
.to throw_symbol :some_argument
|
.to throw_symbol :some_argument
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -68,7 +72,7 @@ module Producer::Core
|
|||||||
|
|
||||||
before do
|
before do
|
||||||
Task::DSL.define_action(:some_action, some_action_class)
|
Task::DSL.define_action(:some_action, some_action_class)
|
||||||
dsl.evaluate env
|
dsl.evaluate
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'registers the action' do
|
it 'registers the action' do
|
||||||
@ -82,7 +86,7 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'DSL specific methods' do
|
context 'DSL specific methods' do
|
||||||
subject(:dsl) { Task::DSL.new(&block).evaluate(env) }
|
subject(:dsl) { Task::DSL.new(env, &block).evaluate }
|
||||||
|
|
||||||
describe '#condition' do
|
describe '#condition' do
|
||||||
context 'when a block is given' do
|
context 'when a block is given' do
|
||||||
@ -90,7 +94,7 @@ module Producer::Core
|
|||||||
|
|
||||||
it 'builds a new evaluated condition' do
|
it 'builds a new evaluated condition' do
|
||||||
expect(Condition)
|
expect(Condition)
|
||||||
.to receive(:evaluate).with(env) do |&b|
|
.to receive :evaluate do |&b|
|
||||||
expect(b.call).to eq :some_value
|
expect(b.call).to eq :some_value
|
||||||
end
|
end
|
||||||
dsl
|
dsl
|
||||||
|
@ -12,19 +12,19 @@ module Producer::Core
|
|||||||
let(:args) { [:some, :arguments] }
|
let(:args) { [:some, :arguments] }
|
||||||
let(:block) { proc { :some_task_code } }
|
let(:block) { proc { :some_task_code } }
|
||||||
|
|
||||||
it 'builds a new DSL sandbox with given code' do
|
it 'builds a new DSL sandbox with given env and code' do
|
||||||
dsl = double('dsl').as_null_object
|
dsl = double('dsl').as_null_object
|
||||||
expect(Task::DSL).to receive(:new).with(no_args) do |&b|
|
expect(Task::DSL).to receive(:new).with(env) do |&b|
|
||||||
expect(b).to be block
|
expect(b).to be block
|
||||||
dsl
|
dsl
|
||||||
end
|
end
|
||||||
Task.evaluate(name, env, *args, &block)
|
Task.evaluate(name, env, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'evaluates the DSL sandbox code with given environment' do
|
it 'evaluates the DSL sandbox code with given arguments' do
|
||||||
dsl = double('dsl').as_null_object
|
dsl = double('dsl').as_null_object
|
||||||
allow(Task::DSL).to receive(:new) { dsl }
|
allow(Task::DSL).to receive(:new) { dsl }
|
||||||
expect(dsl).to receive(:evaluate).with(env, *args)
|
expect(dsl).to receive(:evaluate).with(*args)
|
||||||
Task.evaluate(name, env, *args, &block)
|
Task.evaluate(name, env, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user