Implement `macro' recipe keyword

This commit is contained in:
Thibault Jouan 2013-12-20 09:51:15 +00:00
parent 43a781dc78
commit e8be44d1f0
7 changed files with 93 additions and 13 deletions

View File

@ -0,0 +1,40 @@
Feature: `macro' recipe keyword
Scenario: declares new keyword accepting task code
Given a recipe with:
"""
macro :hello do
echo 'hello macro'
end
hello
"""
When I successfully execute the recipe
Then the output must contain "hello macro"
Scenario: supports arguments
Given a recipe with:
"""
macro :my_echo do |kind, message|
echo "#{kind}: #{message}"
end
my_echo 'my', 'hello'
"""
When I successfully execute the recipe
Then the output must contain "my: hello"
Scenario: supports arguments in conditions
Given a recipe with:
"""
macro :my_echo do |message|
condition { message =~ /bar/ }
echo message
end
%w[foo bar].each { |e| my_echo e }
"""
When I successfully execute the recipe
Then the output must not contain "foo"
And the output must contain "bar"

View File

@ -34,8 +34,14 @@ module Producer
env.target = hostname
end
def task(name, &block)
@tasks << Task.evaluate(name, env, &block)
def task(name, *args, &block)
@tasks << Task.evaluate(name, env, *args, &block)
end
def macro(name, &block)
define_singleton_method(name) do |*args|
task("#{name}", *args, &block)
end
end
end
end

View File

@ -2,9 +2,9 @@ module Producer
module Core
class Task
class << self
def evaluate(name, env, &block)
def evaluate(name, env, *args, &block)
dsl = DSL.new(&block)
dsl.evaluate(env)
dsl.evaluate(env, *args)
Task.new(name, dsl.actions, dsl.condition)
end
end

View File

@ -23,9 +23,9 @@ module Producer
@condition = true
end
def evaluate(env)
def evaluate(env, *args)
@env = env
instance_eval &@block
instance_exec *args, &@block
end
def condition(&block)

View File

@ -81,11 +81,11 @@ module Producer::Core
end
describe '#task' do
let(:code) { proc { task(:some_task) { :some_value } } }
let(:code) { proc { task(:some_task, :some, :arg) { :some_value } } }
it 'builds a new evaluated task' do
expect(Task)
.to receive(:evaluate).with(:some_task, env) do |&b|
.to receive(:evaluate).with(:some_task, env, :some, :arg) do |&b|
expect(b.call).to eq :some_value
end
dsl
@ -97,6 +97,30 @@ module Producer::Core
expect(dsl.tasks).to include(task)
end
end
describe '#macro' do
let(:code) { proc { macro(:hello) { echo 'hello' } } }
it 'defines the new recipe keyword' do
expect(dsl).to respond_to(:hello)
end
context 'when the new keyword is called' do
let(:code) { proc { macro(:hello) { echo 'hello' }; hello } }
it 'registers the new task' do
expect(dsl.tasks.first.actions.first).to be_an Actions::Echo
end
end
context 'when macro takes arguments' do
let(:code) { proc { macro(:hello) { |e| echo e }; hello :arg } }
it 'evaluates task code with arguments' do
expect(dsl.tasks.first.actions.first.arguments.first).to be :arg
end
end
end
end
end
end

View File

@ -53,6 +53,15 @@ module Producer::Core
.to throw_symbol :task_code
end
context 'when arguments are given' do
let(:block) { proc { |e| throw e } }
it 'passes arguments as block parameters' do
expect { dsl.evaluate env, :some_argument }
.to throw_symbol :some_argument
end
end
context 'when a defined keyword action is called' do
let(:some_action_class) { Class.new(Action) }
let(:block) { proc { some_action } }

View File

@ -9,6 +9,7 @@ module Producer::Core
describe '.evaluate' do
let(:env) { double 'env' }
let(:args) { [:some, :arguments] }
let(:block) { proc { :some_task_code } }
it 'builds a new DSL sandbox with given code' do
@ -17,14 +18,14 @@ module Producer::Core
expect(b).to be block
dsl
end
Task.evaluate(name, env, &block)
Task.evaluate(name, env, *args, &block)
end
it 'evaluates the DSL sandbox code with given environment' do
dsl = double('dsl').as_null_object
allow(Task::DSL).to receive(:new) { dsl }
expect(dsl).to receive(:evaluate).with(env)
Task.evaluate(name, env, &block)
expect(dsl).to receive(:evaluate).with(env, *args)
Task.evaluate(name, env, *args, &block)
end
it 'builds the task with its name, actions and condition' do
@ -34,13 +35,13 @@ module Producer::Core
allow(Task::DSL).to receive(:new) { dsl }
expect(Task)
.to receive(:new).with(:some_task, [:some_action], :some_condition)
Task.evaluate(name, env, &block)
Task.evaluate(name, env, *args, &block)
end
it 'returns the task' do
task = double 'task'
allow(Task).to receive(:new) { task }
expect(Task.evaluate(name, env, &block)).to be task
expect(Task.evaluate(name, env, *args, &block)).to be task
end
end